3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

Plugins/Spinach: use event queue for game loop

This commit is contained in:
Pragmatic Software 2020-03-07 19:38:29 -08:00
parent 470d463686
commit 766c8ef877

View File

@ -45,8 +45,6 @@ sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->spinach_cmd(@_) }, 'spinach', 0); $self->{pbot}->{commands}->register(sub { $self->spinach_cmd(@_) }, 'spinach', 0);
$self->{pbot}->{timer}->register(sub { $self->spinach_timer }, 1, 'spinach timer');
$self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_departure(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.part', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_departure(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.quit', sub { $self->on_departure(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.kick', sub { $self->on_kick(@_) });
@ -78,7 +76,7 @@ sub initialize {
sub unload { sub unload {
my $self = shift; my $self = shift;
$self->{pbot}->{commands}->unregister('spinach'); $self->{pbot}->{commands}->unregister('spinach');
$self->{pbot}->{timer}->unregister('spinach timer'); $self->{pbot}->{timer}->dequeue_event('spinach loop');
$self->{stats}->end if $self->{stats_running}; $self->{stats}->end if $self->{stats_running};
$self->{pbot}->{event_dispatcher}->remove_handler('irc.part'); $self->{pbot}->{event_dispatcher}->remove_handler('irc.part');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.quit'); $self->{pbot}->{event_dispatcher}->remove_handler('irc.quit');
@ -343,8 +341,13 @@ sub spinach_cmd {
when ('join') { when ('join') {
if ($self->{current_state} eq 'nogame') { if ($self->{current_state} eq 'nogame') {
$self->{state_data} = {players => [], counter => 0}; $self->{state_data} = { players => [], counter => 0 };
$self->{current_state} = 'getplayers'; $self->{current_state} = 'getplayers';
$self->{pbot}->{timer}->enqueue_event(sub {
$self->run_one_state;
}, 1, 'spinach loop', 1
);
} }
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($nick, $user, $host); my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account_ancestor($nick, $user, $host);
@ -414,6 +417,7 @@ sub spinach_cmd {
if (not @{$self->{state_data}->{players}}) { if (not @{$self->{state_data}->{players}}) {
$self->{current_state} = 'nogame'; $self->{current_state} = 'nogame';
$self->{pbot}->{timer}->update_repeating('spinach loop', 0);
return "/msg $self->{channel} $nick has left the game! All players have left. The game has been stopped."; return "/msg $self->{channel} $nick has left the game! All players have left. The game has been stopped.";
} else { } else {
return "/msg $self->{channel} $nick has left the game!"; return "/msg $self->{channel} $nick has left the game!";
@ -851,11 +855,6 @@ sub spinach_cmd {
return $result; return $result;
} }
sub spinach_timer {
my $self = shift;
$self->run_one_state();
}
sub player_left { sub player_left {
my ($self, $nick, $user, $host) = @_; my ($self, $nick, $user, $host) = @_;
@ -939,6 +938,7 @@ sub run_one_state {
if (not @{$self->{state_data}->{players}}) { if (not @{$self->{state_data}->{players}}) {
$self->send_message($self->{channel}, "All players have left the game!"); $self->send_message($self->{channel}, "All players have left the game!");
$self->{current_state} = 'nogame'; $self->{current_state} = 'nogame';
$self->{pbot}->{timer}->update_repeating('spinach loop', 0);
} }
} }
@ -948,6 +948,7 @@ sub run_one_state {
if (not defined $self->{current_state}) { if (not defined $self->{current_state}) {
$self->{pbot}->{logger}->log("Spinach state broke.\n"); $self->{pbot}->{logger}->log("Spinach state broke.\n");
$self->{current_state} = 'nogame'; $self->{current_state} = 'nogame';
$self->{pbot}->{timer}->update_repeating('spinach loop', 0);
return; return;
} }
@ -2154,6 +2155,7 @@ sub nogame {
delete $self->{stats_running}; delete $self->{stats_running};
} }
$state->{result} = 'nogame'; $state->{result} = 'nogame';
$self->{pbot}->{timer}->update_repeating('spinach loop', 0);
return $state; return $state;
} }
@ -2206,6 +2208,7 @@ sub getplayers {
$self->send_message($self->{channel}, "All players have left the queue. The game has been stopped."); $self->send_message($self->{channel}, "All players have left the queue. The game has been stopped.");
$self->{current_state} = 'nogame'; $self->{current_state} = 'nogame';
$self->{result} = 'nogame'; $self->{result} = 'nogame';
$self->{pbot}->{timer}->update_repeating('spinach loop', 0);
return $state; return $state;
} }
} }