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