]> bicyclesonthemoon.info Git - ott/bsta/blob - ong.1.pl
Done comment system
[ott/bsta] / ong.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # ong.pl is generated from ong.1.pl.
4 #
5 # The ONG bot
6 #
7 # Copyright (C) 2016, 2017, 2023, 2024  Balthasar SzczepaƄski
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU Affero General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 use strict;
23 use utf8;
24 # use Encode::Locale ('decode_argv');
25 use Encode ('encode', 'decode');
26
27 ###PERL_LIB: use lib /botm/lib/bsta
28 use botm_common (
29         'read_data_file', 'write_data_file',
30         'open_encoded'
31 );
32 use bsta_lib (
33         'STATE',
34         'ong'
35 );
36
37 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
38 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
39
40 binmode STDIN,  ':encoding(UTF-8)';
41 binmode STDOUT, ':encoding(UTF-8)';
42 binmode STDERR, ':encoding(UTF-8)';
43 # decode_argv();
44
45 my $time = time();
46 srand ($time-$$);
47
48 my %settings;
49 my %state;
50 my %goto_list;
51
52 my $fh;
53 my $ongstate;
54 my $frame;
55 my $next_ong;
56 my $ongtime;
57 my $static_timer;
58 my $timer;
59 my $last;
60 my $r = 0;
61
62 $ongtime = int($time / 3600) * 3600; # check time as if it was last full hour!
63
64 print $time.' - '.$ongtime."\n";
65
66 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
67 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
68
69 unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
70         print "NO STATEFILE\n";
71 }
72 else {
73         unless (flock($fh, 2)) {
74                 print "NO STATELOCK\n";
75         }
76         else {
77                 %state = read_data_file($fh);
78                 $ongstate = int($state{'state'});
79                 print 'state: '.$ongstate."\n";
80                 unless ($ongstate > STATE->{'inactive'}) {
81                         print "INACTIVE\n";
82                 }
83                 else {
84                         $next_ong = int($state{'nextong'});
85                         print 'ongtime: '.$next_ong."\n";
86                         
87                         unless ($ongtime >= $next_ong) {
88                                 print "WAIT\n";
89                         }
90                         else {
91                                 %settings = read_data_file(DATA_SETTINGS_PATH());
92                                 $static_timer = int($settings{'ongtime'});
93                                 $timer        = int($settings{'dynamicongtime'});
94                                 $last         = int($settings{'last'});
95                                 $frame        = int($state   {'last'})+1;
96                                 
97                                 if (($timer > 0) && ($frame < $last)) {
98                                         $timer = int($timer / ($last - $frame));
99                                 }
100                                 else {
101                                         $timer=0;
102                                 }
103                                 
104                                 if ($static_timer > $timer) {
105                                         $timer = $static_timer;
106                                 }
107                                 $next_ong = $ongtime + ($timer*3600);
108                                 $state{'nextong'} = $next_ong;
109                                 print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
110                                 $state{'ongtime'}=$timer;
111                                 
112                                 if ($ongstate == STATE->{'ready'}) {
113                                         print 'next frame: '.$frame."\n";
114                                         
115                                         $r = ong (
116                                                 $frame, # frame ID
117                                                 $time,  # ONG time
118                                                 $timer, # timer to next ONG
119                                                 0,      # update
120                                                 1,      # print
121                                                 \%settings,
122                                                 '',     # %default
123                                                 '',     # %frame_data
124                                                 ''      # %goto_list
125                                         );
126                                         
127                                         if ($r) {
128                                                 $state{'last'}   = $frame;
129                                                 $state{'state'}  = 1;
130                                                 $state{'ip1'}    = '';
131                                                 $state{'ip2'}    = '';
132                                                 $state{'ip3'}    = '';
133                                                 $state{'ongtime'}= $timer;
134                                                 print "ONG\n";
135                                         }
136                                         else {
137                                                 print "NO ONG\n";
138                                         }
139                                 }
140                                 write_data_file($fh, \%state);
141                         }
142                 }
143         }
144         close ($fh);
145 }
146
147 print "\n";