From 7868ee96e89c9c75fc6badc3cc80e4e537bd3112 Mon Sep 17 00:00:00 2001 From: b Date: Tue, 2 Jan 2024 11:35:16 +0000 Subject: [PATCH] ONG will use generic function --- 2words.1.pl | 8 ++- attach.1.pl | 2 +- bsta_lib.1.pm | 8 ++- ong.1.pl | 167 +++++++++++++++++++++++--------------------------- viewer.1.pl | 4 +- 5 files changed, 92 insertions(+), 97 deletions(-) diff --git a/2words.1.pl b/2words.1.pl index cb5fd79..e679c62 100644 --- a/2words.1.pl +++ b/2words.1.pl @@ -5,7 +5,7 @@ # # The wordgame interface # -# Copyright (C) 2016 - 2017, 2023 - 2024 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 @@ -46,7 +46,6 @@ use bsta_lib ( 'merge_settings', 'ong' ); -use File::Copy; ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = / @@ -140,7 +139,7 @@ $cmd_clear = $settings{'password'}.' clear'; $cmd_clear_all = $settings{'password'}.' clearall'; $story_lock=0; -if (open ($story_file,"+<:encoding(UTF-8)",DATA_STORY_PATH())){ +if (open ($story_file, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STORY_PATH()))){ $story_lock=1; if (flock($story_file,2)) { $story_lock=2; @@ -295,6 +294,7 @@ if (open ($story_file,"+<:encoding(UTF-8)",DATA_STORY_PATH())){ $time, # ONG time; not relevant 0, # timer; not relevant 0, # update; not relevant + 0, # print \%settings, # not relevant '', # %default; not relevant '', # %frame_data; not relevant @@ -307,6 +307,7 @@ if (open ($story_file,"+<:encoding(UTF-8)",DATA_STORY_PATH())){ $time, # ONG time; not relevant 0, # timer; not relevant 0, # update; not relevant + 0, # print \%settings, '', # %default '', # %frame_data @@ -320,6 +321,7 @@ if (open ($story_file,"+<:encoding(UTF-8)",DATA_STORY_PATH())){ $time, # ONG time; might get overwritten later 0, # timer 0, # update + 0, # print \%settings, '', # %default '', # %frame_data diff --git a/attach.1.pl b/attach.1.pl index 26ed413..d680f76 100644 --- a/attach.1.pl +++ b/attach.1.pl @@ -161,7 +161,7 @@ else { exit fail_attachment($method, $ID); } } - unless(binmode($fh)) { + unless (binmode($fh)) { close($fh); exit fail_500("Can't switch file to binary mode."); } diff --git a/bsta_lib.1.pm b/bsta_lib.1.pm index fe55c3b..6d90ed8 100644 --- a/bsta_lib.1.pm +++ b/bsta_lib.1.pm @@ -1578,7 +1578,7 @@ sub write_index { # ONG the frame + attachment & stiff. NOT update state file. sub ong { ( - my $ID, my $ongtime, my $timer, my $update, + my $ID, my $ongtime, my $timer, my $update, my $print, my $settings_ref, my $default_ref, my $data_ref, my $goto_ref ) = @_; my @files; @@ -1707,7 +1707,13 @@ sub ong { foreach my $file (@files) { $in_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $file); $out_path = join_path(PATH_SEPARATOR(), WWW_PATH() , $file); + if ($print) { + print $in_path.' -> '.$out_path; + } $r = copy($in_path, $out_path); + if ($print) { + print (($r) ? " OK\n" : " FAIL\n"); + } unless ($r) { return $r } diff --git a/ong.1.pl b/ong.1.pl index 1eeb352..ef90632 100644 --- a/ong.1.pl +++ b/ong.1.pl @@ -20,135 +20,122 @@ # along with this program. If not, see . use strict; -#use warnings; +use utf8; +# use Encode::Locale ('decode_argv'); +use Encode ('encode', 'decode'); + ###PERL_LIB: use lib /botm/lib/bsta -use bsta_lib qw(entityencode readdatafile writedatafile urlencode); -use File::Copy; +use botm_common ( + 'read_data_file', 'write_data_file' +); +use bsta_lib ( + 'STATE', + 'ong' +); -###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta -###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state -###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/ -###PERL_DATA_LIST_PATH: DATA_LIST_PATH = /botm/data/bsta/list -my %framedata; -my %nextframedata; -my %default; +my $time = time(); +srand ($time-$$); + my %settings; my %state; -my %gotolist; +my %goto_list; -my $statefile; +my $fh; my $ongstate; my $frame; -my $nextong; -my $inpath; -my $outpath; +my $next_ong; my $ongtime; -my $static; -my $dynamic; +my $static_timer; +my $timer; my $last; +my $r = 0; -my $time = time(); -srand ($time-$$); -$ongtime = int($time / 3600) * 3600; +$ongtime = int($time / 3600) * 3600; # check time as if it was last full hour! print $time.' - '.$ongtime."\n"; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin; - -if (open ($statefile,"+<",DATA_STATE_PATH)){ - if (flock($statefile,2)) { - %state=readdatafile($statefile); - $ongstate=int($state{'state'}); +unless (open ($fh, "+<:encoding(UTF-8)", encode('locale_fs', DATA_STATE_PATH()))) { + print "NO STATEFILE\n"; +} +else { + unless (flock($fh, 2)) { + print "NO STATELOCK\n"; + } + else { + %state = read_data_file($fh); + $ongstate = int($state{'state'}); print 'state: '.$ongstate."\n"; - if($ongstate > 0) { - $nextong = int($state{'nextong'}); - print 'ongtime: '.$nextong."\n"; + unless ($ongstate > STATE->{'inactive'}) { + print "INACTIVE\n"; + } + else { + $next_ong = int($state{'nextong'}); + print 'ongtime: '.$next_ong."\n"; - if($ongtime >= $nextong) { - %settings=readdatafile(DATA_SETTINGS_PATH); - $static=int($settings{'ongtime'}); - $dynamic=int($settings{'dynamicongtime'}); - $last=int($settings{'last'}); - $frame=int($state{'last'})+1; + 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($dynamic > 0 && $frame < $last) { - $dynamic = int($dynamic/($last-$frame)); + if (($timer > 0) && ($frame < $last)) { + $timer = int($timer / ($last - $frame)); } else { - $dynamic=0; + $timer=0; } - if($static>$dynamic){ - $dynamic=$static; + if ($static_timer > $timer) { + $timer = $static_timer; } - $nextong=$ongtime+($dynamic*3600); - $state{'nextong'}=$nextong; - print 'next ongtime: '.$nextong.' (+'.$dynamic.")\n"; - $state{'ongtime'}=$dynamic; + $next_ong = $ongtime + ($timer*3600); + $state{'nextong'} = $next_ong; + print 'next ongtime: '.$next_ong.' (+'.$timer.")\n"; + $state{'ongtime'}=$timer; - if($ongstate == 2) { + if ($ongstate == STATE->{'ready'}) { print 'next frame: '.$frame."\n"; - %framedata=readdatafile(DATA_PATH.$frame); - %default=readdatafile(DATA_DEFAULT_PATH); - %gotolist=readdatafile(DATA_LIST_PATH); - $framedata{'ongtime'}=$time; - $framedata{'timer'}=$dynamic; - $gotolist{'title-'.$frame}=$framedata{'title'}; - $gotolist{'ongtime-'.$frame}=$framedata{'ongtime'}; - writedatafile(DATA_PATH.$frame,%framedata); - writedatafile(DATA_LIST_PATH,%gotolist); + $r = ong ( + $frame, # frame ID + $time, # ONG time + $timer, # timer to next ONG + 0, # update + 1, # print + \%settings, + '', # %default + '', # %frame_data + '' # %goto_list + ); - foreach my $ind (keys %default) { - unless(defined($framedata{$ind})){ - $framedata{$ind}=$default{$ind}; - } - unless(defined($nextframedata{$ind})){ - $nextframedata{$ind}=$default{$ind}; - } - } - - $inpath = DATA_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'}); - $outpath = WWW_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'}); - - print $inpath.' -> '.$outpath."\n"; - - # ALSO-ATTACHMENTS!!! (here) - - if(copy ($inpath, $outpath)) { - $state{'last'}=$frame; - $state{'state'}=1; - $state{'ip1'}=''; - $state{'ip2'}=''; - $state{'ip3'}=''; - $state{'ongtime'}=$dynamic; + 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"; } } - writedatafile($statefile,%state); + write_data_file($fh, '', '', \%state); } - else { - print "WAIT\n"; - } - } - else { - print "INACTIVE\n"; } } - else { - print "NO STATELOCK\n"; - } - close ($statefile); -} -else { - print "NO STATEFILE\n"; + close ($fh); } + print "\n"; \ No newline at end of file diff --git a/viewer.1.pl b/viewer.1.pl index 27ef3fa..263e502 100644 --- a/viewer.1.pl +++ b/viewer.1.pl @@ -5,7 +5,7 @@ # # The viewer interface # -# Copyright (C) 2016-2017, 2019-2020, 2023 - 2024 Balthasar Szczepański +# Copyright (C) 2016, 2017, 2019, 2020, 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 @@ -40,7 +40,6 @@ use bsta_lib ( 'print_viewer_page', 'write_index', 'ong' ); -use File::Copy; ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = / @@ -196,6 +195,7 @@ if (open ($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) { $time, # ONG time, $ong_time, # timer 0, # update + 0, # print \%settings, \%default, \%frame_data, -- 2.30.2