3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-24 11:42:35 +01:00

Plugins/Connect4: use event queue for game loop

This commit is contained in:
Pragmatic Software 2020-03-06 20:24:31 -08:00
parent 857021f25f
commit c17c885600

View File

@ -21,8 +21,6 @@ sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->connect4_cmd(@_) }, 'connect4', 0); $self->{pbot}->{commands}->register(sub { $self->connect4_cmd(@_) }, 'connect4', 0);
$self->{pbot}->{timer}->register(sub { $self->connect4_timer }, 1, 'connect4 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(@_) });
@ -35,10 +33,10 @@ sub initialize {
sub unload { sub unload {
my $self = shift; my $self = shift;
$self->{pbot}->{commands}->unregister('connect4'); $self->{pbot}->{commands}->unregister('connect4');
$self->{pbot}->{timer}->unregister('connect4 timer');
$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');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.kick'); $self->{pbot}->{event_dispatcher}->remove_handler('irc.kick');
$self->{pbot}->{timer}->dequeue_event('connect4 loop');
} }
sub on_kick { sub on_kick {
@ -164,6 +162,12 @@ sub connect4_cmd {
$self->{current_state} = 'accept'; $self->{current_state} = 'accept';
$self->{state_data} = {players => [], counter => 0}; $self->{state_data} = {players => [], counter => 0};
$self->{pbot}->{timer}->enqueue_event(
sub {
$self->run_one_state;
}, 1, 'connect4 loop', 1
);
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account($nick, $user, $host); my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account($nick, $user, $host);
my $player = {id => $id, name => $nick, missedinputs => 0}; my $player = {id => $id, name => $nick, missedinputs => 0};
push @{$self->{state_data}->{players}}, $player; push @{$self->{state_data}->{players}}, $player;
@ -180,13 +184,19 @@ sub connect4_cmd {
if (not $challengee) { return "That nick is not present in this channel. Invite them to $self->{channel} and try again!"; } if (not $challengee) { return "That nick is not present in this channel. Invite them to $self->{channel} and try again!"; }
$self->{current_state} = 'accept';
$self->{state_data} = {players => [], counter => 0};
if (length $options) { if (length $options) {
if ($err = $self->parse_challenge($options)) { return $err; } if ($err = $self->parse_challenge($options)) { return $err; }
} }
$self->{current_state} = 'accept';
$self->{state_data} = {players => [], counter => 0};
$self->{pbot}->{timer}->enqueue_event(
sub {
$self->run_one_state;
}, 1, 'connect4 loop', 1
);
my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account($nick, $user, $host); my $id = $self->{pbot}->{messagehistory}->{database}->get_message_account($nick, $user, $host);
my $player = {id => $id, name => $nick, missedinputs => 0}; my $player = {id => $id, name => $nick, missedinputs => 0};
push @{$self->{state_data}->{players}}, $player; push @{$self->{state_data}->{players}}, $player;
@ -330,11 +340,6 @@ sub connect4_cmd {
return $result; return $result;
} }
sub connect4_timer {
my $self = shift;
$self->run_one_state;
}
sub player_left { sub player_left {
my ($self, $nick, $user, $host) = @_; my ($self, $nick, $user, $host) = @_;
@ -684,6 +689,7 @@ sub show_board {
sub nogame { sub nogame {
my ($self, $state) = @_; my ($self, $state) = @_;
$state->{result} = 'nogame'; $state->{result} = 'nogame';
$self->{pbot}->{timer}->update_repeating('connect4 loop', 0);
return $state; return $state;
} }