summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2014-12-04 20:10:57 +0100
committerJulien Dessaux2014-12-04 20:11:22 +0100
commit535336c7179b4d2374e733c14e2c97f8f296c34c (patch)
tree9b2a956ca96e236c1ad11c28d6f2af268b82b45b
parentImplemented REGISTER (diff)
downloadavalon-arthur-535336c7179b4d2374e733c14e2c97f8f296c34c.tar.gz
avalon-arthur-535336c7179b4d2374e733c14e2c97f8f296c34c.tar.bz2
avalon-arthur-535336c7179b4d2374e733c14e2c97f8f296c34c.zip
Implemented GAMESTART
Diffstat (limited to '')
-rw-r--r--Avalon.pm56
-rw-r--r--INSTALL.markdown1
2 files changed, 55 insertions, 2 deletions
diff --git a/Avalon.pm b/Avalon.pm
index a5708ec..1465f37 100644
--- a/Avalon.pm
+++ b/Avalon.pm
@@ -1,6 +1,6 @@
package Bot::BasicBot::Pluggable::Module::Avalon;
{
- $Avalon::Arthur::VERSION = '0.03';
+ $Avalon::Arthur::VERSION = '0.04';
};
use strict;
@@ -9,6 +9,7 @@ use v5.12;
use experimental qw(autoderef switch);
use Bot::BasicBot::Pluggable::Store::DBI;
use List::Util qw(shuffle);
+use Math::Random::Secure qw(rand);
use POE;
use Time::HiRes qw(time);
@@ -27,6 +28,11 @@ my %gamerules = (
10 => [ 4, 3, 4, 4, 5, 5, 1],
);
+sub game_ready {
+ my $av = shift->{avalon};
+ return scalar keys $av->{registered} >= 5;
+}
+
sub load_avalon_db {
my $self = shift;
# This database stores registrations and bans
@@ -54,14 +60,59 @@ sub reset_game {
$av->{quests} = { pass => 0, fail => 0, votes => 0 };
}
+sub rules {
+ my $av = shift->{avalon};
+ my $players = scalar keys $av->{players};
+ return ($players, $gamerules{$players});
+}
+
sub set_timeout {
my ( $self, $value) = @_;
$poe_kernel->alarm( avalon_timeout => time() + $value );
}
+sub start_game {
+ my $self = shift;
+ my $av = $self->{avalon};
+ my @players = shuffle keys $av->{registered};
+ $av->{players} = \@players;
+ $self->say( channel => $av->{config}->{'game.channel'}, body => "GAMESTART " . join(' ', @players) );
+ $self->set_timeout(10);
+}
+
sub timeout_occurred {
my $self = shift;
- $self->say( channel => $self->{avalon}->{config}->{'game.channel'}, body => "timeout" );
+ my $av = $self->{avalon};
+ given ($av->{gamephase}) {
+ when (GAMESTART) {
+ return unless $self->game_ready;
+ # First we prepare the characters pool
+ my ($players, $rules) = $self->rules;
+ my $evils = $rules->[NUMBER_OF_EVIL_PLAYERS];
+ my @characters = ( 'MERLIN', 'ASSASSIN');
+ for (my $i = 1; $i < $evils; $i++) { push @characters, ( 'EVIL' ); }
+ for (my $i = $evils + 1; $i < $players; $i++) { push @characters, ( 'GOOD' ); }
+ my @shuffled = shuffle @characters;
+ # Then we assign roles
+ for (my $i = 0; $i < $players; $i++) {
+ my $role = pop @shuffled;
+ push $av->{roles}->{$role}, $av->{players}->[$i];
+ $self->say( channel => 'msg', who => $av->{players}->[$i], body => "ROLE $role");
+ }
+ # Now we give special information to special characters
+ my $evil_msg = "EVIL $av->{roles}->{ASSASSIN}->[0] " . join(' ', @{$av->{roles}->{EVIL}});
+ $self->say( channel => 'msg', who => $av->{roles}->{MERLIN}->[0], body => $evil_msg );
+ $self->say( channel => 'msg', who => $av->{roles}->{ASSASSIN}->[0], body => $evil_msg );
+ $self->say( channel => 'msg', who => $_, body => $evil_msg ) foreach (@{$av->{roles}->{EVIL}});
+ # Finally we designate the first king
+ $av->{king} = rand($players);
+ $self->say( channel => $self->{avalon}->{config}->{'game.channel'}, body => "KING $av->{players}->[$av->{king}]" );
+ $self->{gamephase} = TEAM;
+ }
+ default {
+ $self->say( channel => $self->{avalon}->{config}->{'game.channel'}, body => "timeout" );
+ }
+ }
}
### IRC methods override ######################################################
@@ -105,6 +156,7 @@ sub told {
}
$av->{registered}->{$who} = { owner => $owner, version => $bot_version };
$self->say( channel => $av->{config}->{'game.channel'}, body => "REGISTERED $who" );
+ $self->start_game if $self->game_ready;
}
when ("REGISTERED") {}
when ("UNREGISTER") {}
diff --git a/INSTALL.markdown b/INSTALL.markdown
index 18d9919..bb90fc9 100644
--- a/INSTALL.markdown
+++ b/INSTALL.markdown
@@ -7,4 +7,5 @@ Required Perl modules
Bot::BasicBot::Pluggable
Config::Simple
enum
+Math::Random::Secure
POE::Component::SSLify