From 977e51932f84d4ed18a0a351cd575a78f3c2df40 Mon Sep 17 00:00:00 2001 From: ElTata <gennuso2015@perso.iiens.net> Date: Fri, 4 Oct 2019 11:25:02 +0200 Subject: [PATCH] classes wip: - bugfix with rps reference on reloaddb : created a 2nd object, corrected - loaddb() takes a reference to rps (as writedb) and write inside --- Irpg/Action.pm | 14 +++--- Irpg/Admin.pm | 4 +- Irpg/Main.pm | 14 +++--- Irpg/Utils.pm | 118 +++++++++++++++++++++++++------------------------ irpg_bot.pl | 4 +- 5 files changed, 79 insertions(+), 75 deletions(-) diff --git a/Irpg/Action.pm b/Irpg/Action.pm index 63085d1..be23546 100644 --- a/Irpg/Action.pm +++ b/Irpg/Action.pm @@ -63,7 +63,7 @@ sub choose_action { my $stl_score = $rps->{$user}{class}->cha() + $rps->{$user}{class}->dex(); my $score = int(rand($phy_score+$mys_score+$stl_score)); return 'fight' if ($score < $phy_score); - return 'mystic' if ($score < $mys_score); + return 'mystic' if ($score < $phy_score+$mys_score); return 'steal'; } @@ -100,7 +100,7 @@ sub fight_result { $gain = $rps->{$p1}{class}->real_gain($gain); $rps->{$p1}{next} += $gain; push(@queue, - duration($gain)." is removed to $p1\'s clock."); + duration($gain)." is removed from $p1\'s clock."); } $queue[0] .= " $p1 reaches next level in ".duration($rps->{$p1}{next})."."; return \@queue; @@ -120,7 +120,7 @@ sub mystic_result { $rps->{$p2}{next} += $gain; push(@queue, "$p1 transfers ".duration($gain)." from ". - pronoun(2, $rps->{$p1}{gender}). "clock to $p2\'s. ". + pronoun(2, $rps->{$p1}{gender}). " clock to $p2\'s. ". "$p1 reaches next level in ".duration($rps->{$p1}{next}).". ". "$p2 reaches next level in ".duration($rps->{$p2}{next})."."); if ($win == 2) { @@ -174,8 +174,9 @@ sub steal_result { Irpg::Irc::notice( "You (rightfully) acquired a new level ". int($rps->{$p1}{item}{$type})." $type, which looks far better ". - "than you old level ".int($rps->{$p2}{item}{$type})." $type! ". - "You left your old one to whom you took the new from."); + "than your old level ".int($rps->{$p2}{item}{$type})." $type! ". + "You left your old one to whom you took the new from.", + $rps->{$p1}{nick}); } else { push(@queue, @@ -205,7 +206,8 @@ sub steal_result { "You (rightfully) acquired a new level ". int($rps->{$p1}{item}{$type})." $type, which looks far better ". "than you old level ".int($rps->{$p2}{item}{$type})." $type! ". - "You left your old one to whom you took the new from."); + "You left your old one to whom you took the new from.", + $rps->{$p1}{nick}); } else { $queue[$#queue] .= "Alas, despite ".pronoun(2, $rps->{$p1}{gender}). diff --git a/Irpg/Admin.pm b/Irpg/Admin.pm index c8abe21..cd95fae 100644 --- a/Irpg/Admin.pm +++ b/Irpg/Admin.pm @@ -188,13 +188,13 @@ sub push { sub bot_die { my ($userhost, $usernick, $username, $source, @arg) = @_; $opts->{reconnect} = 0; - writedb($opts, $rps); + writedb($rps); Irpg::Irc::sts("QUIT :DIE from $usernick",1); } sub restart { my ($userhost, $usernick, $username, $source, @arg) = @_; - writedb($opts, $rps); + writedb($rps); Irpg::Irc::sts("QUIT :RESTART from $usernick",1); #close($Irpg::Irc::sock); #Irpg::Irc::irc_clean($opts); diff --git a/Irpg/Main.pm b/Irpg/Main.pm index 67f0d22..5fd1ba7 100644 --- a/Irpg/Main.pm +++ b/Irpg/Main.pm @@ -83,7 +83,7 @@ sub reloaddb { $usernick, 1); } else { - loaddb(); + $prev_online = loaddb($rps); Irpg::Irc::privmsg("Reread player database file; ".scalar(keys(%$rps)). " accounts loaded.",$usernick,1); } @@ -302,8 +302,8 @@ sub rpcheck { # check levels, update database if ($rps->{$k}{online} && exists $rps->{$k}{nick} && $rps->{$k}{nick} && exists $onchan{$rps->{$k}{nick}}) { $rps->{$k}{next} -= ($curtime - $$lasttime_ref); - $rps->{$k}{next_f} -= ($curtime - $$lasttime_ref); - $rps->{$k}{next_f} = 0 if ($rps->{$k}{next_f} < 0); + $rps->{$k}{next_a} -= ($curtime - $$lasttime_ref); + $rps->{$k}{next_a} = 0 if ($rps->{$k}{next_a} < 0); $rps->{$k}{idled} += ($curtime - $$lasttime_ref); if ($rps->{$k}{next} < 1) { $rps->{$k}{level}++; @@ -324,17 +324,17 @@ sub rpcheck { # check levels, update database find_item($k); challenge_opp($k); } - if ($rps->{$k}{next_f} < 1 + if ($rps->{$k}{next_a} < 1 && $rps->{$k}{actions} < int($rps->{$k}{level}/10)) { $rps->{$k}{actions}++; - $rps->{$k}{next_f} = 3600; # 1 hour + $rps->{$k}{next_a} = 3600; # 1 hour Irpg::Irc::notice("You feel ready to perform a new deed !", $rps->{$k}{nick}); } } # attempt to make sure this is an actual user, and not just an # artifact of a bad PEVAL } - if (!$pausemode && $rpreport%60==0) { writedb($opts, $rps); } + if (!$pausemode && $rpreport%60==0) { writedb($rps); } $rpreport += $opts->{self_clock}; $$lasttime_ref = $curtime; } @@ -364,7 +364,7 @@ sub parse { # flags to 1, rewrite db, and attempt to reconnect (if that's wanted of # us) $rps->{$_}{online}=1 for keys(%$auto_login); - writedb($opts, $rps); + writedb($rps); return; } $arg[1] = lc($arg[1]); # original case no longer matters diff --git a/Irpg/Utils.pm b/Irpg/Utils.pm index 500c718..34a5de1 100644 --- a/Irpg/Utils.pm +++ b/Irpg/Utils.pm @@ -38,7 +38,6 @@ foreach (<Irpg/Classes/*.pm>) { my $_configfile = '.irpg.conf.dev'; my $opts; -my $rps; =head1 FUNCTION ts This function returns a random salt for passwds @@ -227,14 +226,16 @@ sub backup { This function loads the content of the database into a hash and returns a reference to this hash =over -=item SCALAR - socket (from irc connection) +=item SCALAR (ref) - reference to the rps hash +=item SCALAR - is it the startup loading ? =back =cut sub loaddb { # load the players database - my $sock = shift; + my $rps = shift; + return unless ($rps); + my $startup = shift; backup($opts); my $l; - my %rps = (); my %prev_online = (); if (!open(RPS,$opts->{dbfile}) && -e $opts->{dbfile}) { Irpg::Irc::sts("QUIT :loaddb() failed: $!"); @@ -251,61 +252,60 @@ sub loaddb { # load the players database debug("Anomaly in loaddb(); line $. of $opts->{dbfile} has wrong ". "fields (".scalar(@i).")",1); } - if (!$sock) { # if not RELOADDB + if ($startup) { # if not RELOADDB if ($i[8]) { $prev_online{$i[7]}=$i[0]; } # log back in } - ($rps{$i[0]}{pass}, - $rps{$i[0]}{isadmin}, - $rps{$i[0]}{level}, - $rps{$i[0]}{title}, - $rps{$i[0]}{next}, - $rps{$i[0]}{nick}, - $rps{$i[0]}{userhost}, - $rps{$i[0]}{online}, - $rps{$i[0]}{idled}, - $rps{$i[0]}{x}, - $rps{$i[0]}{y}, - $rps{$i[0]}{pen_mesg}, - $rps{$i[0]}{pen_nick}, - $rps{$i[0]}{pen_part}, - $rps{$i[0]}{pen_kick}, - $rps{$i[0]}{pen_quit}, - $rps{$i[0]}{pen_quest}, - $rps{$i[0]}{pen_logout}, - $rps{$i[0]}{pen_title}, - $rps{$i[0]}{created}, - $rps{$i[0]}{lastlogin}, - $rps{$i[0]}{item}{amulet}, - $rps{$i[0]}{item}{charm}, - $rps{$i[0]}{item}{helm}, - $rps{$i[0]}{item}{"pair of boots"}, - $rps{$i[0]}{item}{"pair of gloves"}, - $rps{$i[0]}{item}{ring}, - $rps{$i[0]}{item}{"set of leggings"}, - $rps{$i[0]}{item}{shield}, - $rps{$i[0]}{item}{tunic}, - $rps{$i[0]}{item}{weapon}, - $rps{$i[0]}{stats}{str}, - $rps{$i[0]}{stats}{con}, - $rps{$i[0]}{stats}{wis}, - $rps{$i[0]}{stats}{int}, - $rps{$i[0]}{stats}{cha}, - $rps{$i[0]}{stats}{dex}, + ($rps->{$i[0]}{pass}, + $rps->{$i[0]}{isadmin}, + $rps->{$i[0]}{level}, + $rps->{$i[0]}{title}, + $rps->{$i[0]}{next}, + $rps->{$i[0]}{nick}, + $rps->{$i[0]}{userhost}, + $rps->{$i[0]}{online}, + $rps->{$i[0]}{idled}, + $rps->{$i[0]}{x}, + $rps->{$i[0]}{y}, + $rps->{$i[0]}{pen_mesg}, + $rps->{$i[0]}{pen_nick}, + $rps->{$i[0]}{pen_part}, + $rps->{$i[0]}{pen_kick}, + $rps->{$i[0]}{pen_quit}, + $rps->{$i[0]}{pen_quest}, + $rps->{$i[0]}{pen_logout}, + $rps->{$i[0]}{pen_title}, + $rps->{$i[0]}{created}, + $rps->{$i[0]}{lastlogin}, + $rps->{$i[0]}{item}{amulet}, + $rps->{$i[0]}{item}{charm}, + $rps->{$i[0]}{item}{helm}, + $rps->{$i[0]}{item}{"pair of boots"}, + $rps->{$i[0]}{item}{"pair of gloves"}, + $rps->{$i[0]}{item}{ring}, + $rps->{$i[0]}{item}{"set of leggings"}, + $rps->{$i[0]}{item}{shield}, + $rps->{$i[0]}{item}{tunic}, + $rps->{$i[0]}{item}{weapon}, + $rps->{$i[0]}{stats}{str}, + $rps->{$i[0]}{stats}{con}, + $rps->{$i[0]}{stats}{wis}, + $rps->{$i[0]}{stats}{int}, + $rps->{$i[0]}{stats}{cha}, + $rps->{$i[0]}{stats}{dex}, $classname, - $rps{$i[0]}{points}, - $rps{$i[0]}{actions}, - $rps{$i[0]}{next_f}, - $rps{$i[0]}{alignment}, - $rps{$i[0]}{gender}) = (@i[1..7],($sock?$i[8]:0),@i[9..$#i]); + $rps->{$i[0]}{points}, + $rps->{$i[0]}{actions}, + $rps->{$i[0]}{next_a}, + $rps->{$i[0]}{alignment}, + $rps->{$i[0]}{gender}) = (@i[1..7],($startup?0:$i[8]),@i[9..$#i]); - $rps{$i[0]}{class} = eval 'Irpg::Classes::'.$classname. - '->new($rps{$i[0]}{stats})'; + $rps->{$i[0]}{class} = eval 'Irpg::Classes::'.$classname. + '->new($rps->{$i[0]}{stats})'; } close(RPS); - debug("loaddb(): loaded ".scalar(keys(%rps))." accounts, ". + debug("loaddb(): loaded ".scalar(keys(%$rps))." accounts, ". scalar(keys(%prev_online))." previously online."); - $rps = \%rps; - return ($rps, \%prev_online); + return \%prev_online; } @@ -313,6 +313,8 @@ sub loaddb { # load the players database This function write the content of the hash of players into the database =cut sub writedb { + my $rps = shift; + return unless $rps; open(RPS,">$opts->{dbfile}") or do { Irpg::Irc::chanmsg("ERROR: Cannot write $opts->{dbfile}: $!"); return 0; @@ -358,7 +360,7 @@ sub writedb { "class", "points", "actions", - "next_f", + "next_a", "alignment", "gender")."\n"; my $k; @@ -406,7 +408,7 @@ sub writedb { $rps->{$k}{class}->{NAME}, $rps->{$k}{points}, $rps->{$k}{actions}, - $rps->{$k}{next_f}, + $rps->{$k}{next_a}, $rps->{$k}{alignment}, $rps->{$k}{gender})."\n"; } @@ -454,7 +456,7 @@ sub createdb { $rps{$uname}{class} = Irpg::Classes::Farmer->new($rps{$uname}{stats}); $rps{$uname}{points} = 1; $rps{$uname}{actions} = 0; - $rps{$uname}{next_f} = 0; + $rps{$uname}{next_a} = 0; $rps{$uname}{online} = 0; $rps{$uname}{idled} = 0; $rps{$uname}{created} = time(); @@ -474,10 +476,9 @@ sub createdb { "pen_logout","pen_logout","pen_title") { $rps{$uname}{$pen} = 0; } - $rps = \%rps; - writedb(); + writedb(\%rps); print "OK, wrote you into $opts->{dbfile}.\n"; - return $rps; + return \%rps; } =head1 FUNCTION checksplits @@ -489,6 +490,7 @@ sub createdb { =back =cut sub checksplits { # removed expired split hosts from the hash + my $rps = shift; my $split = shift; my $host; while ($host = each(%$split)) { diff --git a/irpg_bot.pl b/irpg_bot.pl index 3e82cdd..a8fb665 100644 --- a/irpg_bot.pl +++ b/irpg_bot.pl @@ -147,7 +147,7 @@ $SIG{HUP} = "readconfig"; # sighup = reread config file CONNECT: -($rps, $prev_online) = loaddb($sock); +$prev_online = loaddb($rps, 1); irc_connect($opts); @@ -167,7 +167,7 @@ while (1) { # users' online flags to 1, rewrite db, and attempt to reconnect # (if that's wanted of us) $rps->{$_}{online}=1 for keys(%$auto_login); - writedb(); + writedb($rps); if ($opts->{reconnect}) { irc_clean($opts); goto CONNECT; -- GitLab