]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - ong.1.pl
update config; drop unneeded oldlogs.1.pl
[ott/bsta] / ong.1.pl
index ef9063233a05a2d17276663ba7c8233bfd8f76c6..df5eab07d05965a158c421c77b0e7ede8a435921 100644 (file)
--- a/ong.1.pl
+++ b/ong.1.pl
@@ -4,7 +4,7 @@
 #
 # The ONG bot
 #
-# Copyright (C) 2016, 2017, 2023  Balthasar Szczepański
+# Copyright (C) 2016, 2017, 2023, 2024  Balthasar Szczepański
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -26,22 +26,35 @@ use Encode ('encode', 'decode');
 
 ###PERL_LIB: use lib /botm/lib/bsta
 use botm_common (
-       'read_data_file', 'write_data_file'
+       'open_encoded',
 );
 use bsta_lib (
        'STATE',
-       'ong'
+       'ong',
+       'write_static_viewer_page', 'write_index', 'write_static_goto',
+       'read_default', 'read_frame_data', 'read_settings', 'read_goto',
+       'read_state', 'write_state'
 );
 
-###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
 
+binmode STDIN,  ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
+
 my $time = time();
 srand ($time-$$);
 
 my %settings;
 my %state;
-my %goto_list;
+my %new_state;
+# my %goto_list;
+my %default;
+my %frame_data;
+my %frame_1_data;
+my %frame_2_data;
+my %frame_3_data;
 
 my $fh;
 my $ongstate;
@@ -60,82 +73,141 @@ print $time.' - '.$ongtime."\n";
 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
 
-unless (open ($fh, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STATE_PATH()))) {
-       print "NO STATEFILE\n";
+unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
+       print "NO STATEFILE\n\n";
+       exit;
+}
+unless (flock($fh, 2)) {
+       print "NO STATELOCK\n\n";
+       close ($fh);
+       exit;
+}
+%state = read_state($fh);
+$ongstate = int($state{'state'});
+print 'state: '.$ongstate."\n";
+unless ($ongstate > STATE->{'inactive'}) {
+       print "INACTIVE\n\n";
+       close ($fh);
+       exit;
+}
+$next_ong = int($state{'nextong'});
+print 'ongtime: '.$next_ong."\n";
+
+unless ($ongtime >= $next_ong) {
+       print "WAIT\n\n";
+       close ($fh);
+       exit;
+}
+
+%settings = read_settings();
+$static_timer = int($settings{'ongtime'});
+$timer        = int($settings{'dynamicongtime'});
+$last         = int($settings{'last'});
+$frame        = int($state   {'last'})+1;
+
+if (($timer > 0) && ($frame < $last)) {
+       $timer = int($timer / ($last - $frame));
 }
 else {
-       unless (flock($fh, 2)) {
-               print "NO STATELOCK\n";
+       $timer=0;
+}
+
+if ($static_timer > $timer) {
+       $timer = $static_timer;
+}
+$next_ong = $ongtime + ($timer*3600);
+$state{'nextong'} = $next_ong;
+print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
+$state{'ongtime'}=$timer;
+
+if ($ongstate == STATE->{'ready'}) {
+       print 'next frame: '.$frame."\n";
+       
+       %default = read_default();
+       %frame_data = read_frame_data($frame);
+       # %goto_list = read_goto());
+       
+       %new_state = %state;
+       $new_state{'last'}   = $frame;
+       $new_state{'state'}  = 1;
+       $new_state{'ip1'}    = '';
+       $new_state{'ip2'}    = '';
+       $new_state{'ip3'}    = '';
+       $new_state{'ongtime'}= $timer;
+       
+       if ($frame >= 1) {
+               %frame_1_data = read_frame_data($frame-1);
+       }
+       if ($frame >= 2) {
+               %frame_2_data = read_frame_data($frame-2);
+       }
+       if ($frame >= 3) {
+               %frame_3_data = read_frame_data($frame-3);
+       }
+       
+       $r = ong (
+               $frame, # frame ID
+               $time,  # ONG time
+               $timer, # timer to next ONG
+               0,      # update
+               1,      # print
+               \%settings,
+               \%default,
+               \%frame_data,
+               '' # \%goto_list
+       );
+       if ($r && ($frame >= 2)) {
+               print 'static page '.($frame-1);
+               $r = write_static_viewer_page(
+                       $frame-1, # frame ID
+                       \%new_state,
+                       \%settings,
+                       \%default,
+                       \%frame_1_data, # frame data
+                       \%frame_2_data, # prev frame data
+                       \%frame_data,   # next frame data
+                       '' # words data
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+       if ($r && ($frame >= 3)) {
+               print 'static page '.($frame-2);
+               $r = write_static_viewer_page(
+                       $frame-2, # frame ID
+                       \%new_state,
+                       \%settings,
+                       \%default,
+                       \%frame_2_data, # frame data
+                       \%frame_3_data, # prev frame data
+                       \%frame_1_data, # next frame data
+                       '' # words data
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+       if ($r && ($frame <= 2)) {
+               print 'index';
+               $r = write_index(
+                       \%new_state,
+                       \%settings
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+       if ($r) {
+               print 'static GOTO';
+               $r = write_static_goto(
+                       \%new_state,
+                       \%settings,
+                       '' # \%goto_list
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+       if ($r) {
+               %state = %new_state;
+               print "ONG\n\n";
        }
        else {
-               %state = read_data_file($fh);
-               $ongstate = int($state{'state'});
-               print 'state: '.$ongstate."\n";
-               unless ($ongstate > STATE->{'inactive'}) {
-                       print "INACTIVE\n";
-               }
-               else {
-                       $next_ong = int($state{'nextong'});
-                       print 'ongtime: '.$next_ong."\n";
-                       
-                       unless ($ongtime >= $next_ong) {
-                               print "WAIT\n";
-                       }
-                       else {
-                               %settings = read_data_file(DATA_SETTINGS_PATH());
-                               $static_timer = int($settings{'ongtime'});
-                               $timer        = int($settings{'dynamicongtime'});
-                               $last         = int($settings{'last'});
-                               $frame        = int($state   {'last'})+1;
-                               
-                               if (($timer > 0) && ($frame < $last)) {
-                                       $timer = int($timer / ($last - $frame));
-                               }
-                               else {
-                                       $timer=0;
-                               }
-                               
-                               if ($static_timer > $timer) {
-                                       $timer = $static_timer;
-                               }
-                               $next_ong = $ongtime + ($timer*3600);
-                               $state{'nextong'} = $next_ong;
-                               print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
-                               $state{'ongtime'}=$timer;
-                               
-                               if ($ongstate == STATE->{'ready'}) {
-                                       print 'next frame: '.$frame."\n";
-                                       
-                                       $r = ong (
-                                               $frame, # frame ID
-                                               $time,  # ONG time
-                                               $timer, # timer to next ONG
-                                               0,      # update
-                                               1,      # print
-                                               \%settings,
-                                               '',     # %default
-                                               '',     # %frame_data
-                                               ''      # %goto_list
-                                       );
-                                       
-                                       if ($r) {
-                                               $state{'last'}   = $frame;
-                                               $state{'state'}  = 1;
-                                               $state{'ip1'}    = '';
-                                               $state{'ip2'}    = '';
-                                               $state{'ip3'}    = '';
-                                               $state{'ongtime'}= $timer;
-                                               print "ONG\n";
-                                       }
-                                       else {
-                                               print "NO ONG\n";
-                                       }
-                               }
-                               write_data_file($fh, '', '', \%state);
-                       }
-               }
+               print "NO ONG\n\n";
        }
-       close ($fh);
 }
-
-print "\n";
\ No newline at end of file
+write_state($fh, \%state);
+close ($fh);