| 1 | package Telegram::BotKit::Screens; | ||||||||||||||
| 2 | 1 | $Telegram::BotKit::Screens::VERSION = '0.03'; | |||||||||||||
| 3 | # ABSTRACT: Implements navigation by screens JSON file. Used by Telegram::BotKit::Wizard | ||||||||||||||
| 4 | |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use common::sense; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 10 | 1 | my $class = shift; | |||||||||||||
| 11 | 1 | my $s = {}; | |||||||||||||
| 12 | 1 | $s->{scrns} = shift; # Array with all screens | |||||||||||||
| 13 | 1 | bless $s, $class; | |||||||||||||
| 14 | 1 | return $s; | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | 32 | ▶ | sub _get_all_screens { | ||||||||||||
|
Subroutine
_get_all_screens: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 32 | my $self = shift; | |||||||||||||
| 20 | 32 | return $self->{scrns}; | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | 17 | ▶ | sub get_screen_by_name { | ||||||||||||
|
Subroutine
get_screen_by_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 25 | 17 | my ($self, $screen_name) = @_; | |||||||||||||
| 26 | 17 | for (@{$self->_get_all_screens}) { | |||||||||||||
| 27 | 42 | ▶ | if ($_->{name} eq $screen_name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 12 | return $_; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | 5 | return undef; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | |||||||||||||||
| 35 | 5 | ▶ | sub get_next_screen_by_name { | ||||||||||||
|
Subroutine
get_next_screen_by_name: called CC=7 SCAR=28.9
Pod
documented
| |||||||||||||||
| 36 | 5 | my ($self, $screen_name, $text) = @_; | |||||||||||||
| 37 | 5 | my @candidates_to_return = grep ($_->{parent} eq $screen_name, @{$self->_get_all_screens}); | |||||||||||||
| 38 | 5 | ▶ | if (scalar @candidates_to_return == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 39 | 2 | return $candidates_to_return[0]; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | elsif (scalar @candidates_to_return > 1) { | ||||||||||||||
| 42 | 0 | ▶ | if ($text) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 0 | for (@candidates_to_return) { | |||||||||||||
| 44 | 0 | ▶ | if ($_->{callback_msg} eq $text) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 0 | return $_; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | } else { | ||||||||||||||
| 49 | 0 | ▶ | if ($candidates_to_return[0]->{callback_msg}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | # patch for is_last screen() function, it has only one argument ($screen name) and must "screen" object | ||||||||||||||
| 51 | 0 | return $candidates_to_return[0]; | |||||||||||||
| 52 | } else { | ||||||||||||||
| 53 | # two or more child screens with same parent and without callback_msg specific | ||||||||||||||
| 54 | 0 | die "wrong json file"; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | } else { | ||||||||||||||
| 58 | 3 | return undef; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | |||||||||||||||
| 63 | 8 | ▶ | sub get_prev_screen_by_name { | ||||||||||||
|
Subroutine
get_prev_screen_by_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 64 | 8 | my ($self, $screen_name) = @_; | |||||||||||||
| 65 | 8 | for (@{$self->_get_all_screens}) { | |||||||||||||
| 66 | 12 | ▶ | if ($_->{name} eq $screen_name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 8 | return $self->get_screen_by_name($_->{parent}); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | 0 | return undef; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | 2 | ▶ | sub level { | ||||||||||||
|
Subroutine
level: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 76 | 2 | my ($self, $screen_name) = @_; | |||||||||||||
| 77 | 2 | my $i = 0; | |||||||||||||
| 78 | 2 | my $prev_screen = $self->get_prev_screen_by_name($screen_name); | |||||||||||||
| 79 | |||||||||||||||
| 80 | 2 | ▶ | while (defined $prev_screen ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 1 | $i++; | |||||||||||||
| 82 | 1 | $prev_screen = $self->get_prev_screen_by_name($prev_screen->{name}); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | 2 | return $i; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | |||||||||||||||
| 88 | |||||||||||||||
| 89 | |||||||||||||||
| 90 | 2 | ▶ | sub get_screen_by_start_cmd { | ||||||||||||
|
Subroutine
get_screen_by_start_cmd: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 91 | 2 | my ($self, $cmd) = @_; | |||||||||||||
| 92 | 2 | for my $s (@{$self->_get_all_screens}) { | |||||||||||||
| 93 | 2 | ▶ | if ($s->{start_command} eq $cmd) { # don't use eq here | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 2 | return $s; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | 0 | return undef; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | 2 | ▶ | sub is_last_screen { | ||||||||||||
|
Subroutine
is_last_screen: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 102 | 2 | my ($self, $screen_name) = @_; | |||||||||||||
| 103 | 2 | ▶ | if (defined $self->get_next_screen_by_name($screen_name)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 1 | return 0; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | 1 | return 1; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | |||||||||||||||
| 110 | |||||||||||||||
| 111 | 2 | ▶ | sub is_first_screen { | ||||||||||||
|
Subroutine
is_first_screen: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 112 | 2 | my ($self, $screen_name) = @_; | |||||||||||||
| 113 | 2 | ▶ | if (defined $self->get_prev_screen_by_name($screen_name)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 1 | return 0; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | 1 | return 1; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | |||||||||||||||
| 120 | 2 | ▶ | sub is_static { | ||||||||||||
|
Subroutine
is_static: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 121 | 2 | my ($self, $screen) = @_; | |||||||||||||
| 122 | 2 | ▶ | if ($screen->{keyboard}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 1 | return 1; | |||||||||||||
| 124 | } else { | ||||||||||||||
| 125 | 1 | return 0; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | 2 | ▶ | sub get_keys_arrayref { | ||||||||||||
|
Subroutine
get_keys_arrayref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 132 | 2 | my ($self, $screen_name) = @_; | |||||||||||||
| 133 | 2 | my @a; | |||||||||||||
| 134 | 2 | for (@{$self->get_screen_by_name($screen_name)->{keyboard}}) { | |||||||||||||
| 135 | 6 | push @a, $_->{key}; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | 2 | return \@a; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | |||||||||||||||
| 141 | |||||||||||||||
| 142 | 2 | ▶ | sub get_answers_arrayref { | ||||||||||||
|
Subroutine
get_answers_arrayref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 143 | 2 | my ($self, $screen_name) = @_; | |||||||||||||
| 144 | 2 | my @a; | |||||||||||||
| 145 | 2 | for (@{$self->get_screen_by_name($screen_name)->{keyboard}}) { | |||||||||||||
| 146 | 6 | push @a, $_->{answ}; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | 2 | return \@a; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | |||||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | ▶ | sub get_answ_by_key { | ||||||||||||
|
Subroutine
get_answ_by_key: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 154 | 1 | my ($self, $screen_name, $msg) = @_; | |||||||||||||
| 155 | 1 | my $s = $self->get_screen_by_name($screen_name)->{keyboard}; | |||||||||||||
| 156 | 1 | for (@$s) { | |||||||||||||
| 157 | 1 | ▶ | if ($msg eq $_->{key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 1 | return $_->{answ}; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | } | ||||||||||||||
| 161 | 0 | return undef; | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 1 | 1; | |||||||||||||
| 165 | |||||||||||||||
| 166 | __END__ | ||||||||||||||