From 2f998f0d1f9d7e56f8a73660a5873764b0537b26 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 1 Jan 2015 19:08:07 +0100 Subject: Implemented TEAMVOTE game phase --- Avalon.pm | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/Avalon.pm b/Avalon.pm index 438257a..403593f 100644 --- a/Avalon.pm +++ b/Avalon.pm @@ -28,6 +28,20 @@ my %gamerules = ( 10 => [ 4, 3, 4, 4, 5, 5, 1], ); +sub check_endgame_and_proceed { + my $self = shift; + my $av = $self->{avalon}; + + if ($av->{round}->{failed_votes} >= 5) { + # TODO $self->evil_wins + } + given ($av->{gamephase}) { + when (TEAMVOTE) { + $self->new_king; + } + } +} + sub game_ready { my $av = shift->{avalon}; return ( $av->{gamephase} == GAMESTART and scalar keys $av->{registered} >= 5 ); @@ -60,6 +74,7 @@ sub new_king { my $av = $self->{avalon}; my ($players, $rules) = $self->rules; $av->{king} = ($av->{king} +1) % $players; + $av->{team} = []; $self->say( channel => $av->{config}->{'game.channel'}, body => "KING $av->{players}->[$av->{king}] $rules->[$av->{round}->{id}] $av->{round}->{failed_votes}" ); $av->{gamephase} = TEAM; $av->{lastcall} = 0; @@ -144,6 +159,20 @@ sub timeout_occurred { $av->{lastcall} = 1; } } + when (TEAMVOTE) { + foreach (@{$av->{players}}) { + next if (exists $av->{votes}->{$_}); + if ($av->{lastcall}) { + $self->kick($_); + } else { + $self->say( channel => 'msg', who => $_, body => "VOTENOW" ); + } + } + unless ($av->{lastcall}) { + $self->set_timeout(2); + $av->{lastcall} = 1; + } + } default { $self->say( channel => $av->{config}->{'game.channel'}, body => "timeout" ); } @@ -227,7 +256,35 @@ sub told { $av->{lastcall} = 0; $self->set_timeout(58); } - when ("VOTE") {} + when ("VOTE") { + return 'ERR_BAD_ARGUMENTS' unless scalar @args == 1 and $args[0] ~~ [ "yes", "no" ]; + my ($players, $rules) = $self->rules; + given ($av->{gamephase}) { + when (TEAMVOTE) { + $self->kick($who) unless $who ~~ $av->{players}; + $av->{votes}->{$who} = $args[0] unless exists $av->{votes}->{$who}; + if (scalar keys $av->{votes} == $players) { + my $score = 0; + foreach (keys $av->{votes}) { + $score++ if $av->{votes}->{$_} eq "yes"; + } + $av->{votes} = {}; + if ($score > $players / 2) { + $av->{round}->{failed_votes} = 0; + $av->{votes} = {}; + $av->{gamephase} = QUESTVOTE; + $av->{lastcall} = 0; + $self->set_timeout(58); + $self->say( channel => $av->{config}->{'game.channel'}, body => "VOTERESULT PASS $score" ); + } else { + $self->say( channel => $av->{config}->{'game.channel'}, body => "VOTERESULT FAIL $score" ); + $av->{round}->{failed_votes}++; + $self->check_endgame_and_proceed; + } + } + } + } + } when ("VOTENOW") {} when ("VOTERESULT") {} when ("QUESTRESULT") {} -- cgit v1.2.3