From 23e181f35153f6a5bbe14601ab311b37a9a2e3cc Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 4 Dec 2014 16:58:18 +0100 Subject: Implemented the game state --- Avalon.pm | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'Avalon.pm') diff --git a/Avalon.pm b/Avalon.pm index cd6d874..ba73ae3 100644 --- a/Avalon.pm +++ b/Avalon.pm @@ -6,13 +6,54 @@ package Bot::BasicBot::Pluggable::Module::Avalon; use strict; use warnings; use v5.12; -use experimental qw(switch); +use experimental qw(autoderef switch); +use Bot::BasicBot::Pluggable::Store::DBI; +use List::Util qw(shuffle); use POE; use Time::HiRes qw(time); use base qw(Bot::BasicBot::Pluggable::Module); ### Game logic ############################################################### +use enum qw(GAMESTART TEAM TEAMVOTE QUESTVOTE ASSASSINGUESS); +use enum qw(NUMBER_OF_EVIL_PLAYERS FIRST_QUEST SECOND_QUEST THIRD_QUEST FOURTH_QUEST FIFTH_QUEST SPECIAL_QUEST_RULE); +my %gamerules = ( + # number of players => ( number of evil, players on first quest, second, third, fourth, fifth, number of fail for fourth quest success ) + 5 => [ 2, 2, 3, 2, 3, 3, 0], + 6 => [ 2, 2, 3, 4, 3, 4, 0], + 7 => [ 3, 2, 3, 3, 4, 4, 1], + 8 => [ 3, 3, 4, 4, 5, 5, 1], + 9 => [ 3, 3, 4, 4, 5, 5, 1], + 10 => [ 4, 3, 4, 4, 5, 5, 1], +); + +sub load_avalon_db { + my $self = shift; + # This database stores registrations and bans + $self->{avdb} = Bot::BasicBot::Pluggable::Store::DBI->new( + dsn => "dbi:SQLite:dbname=arthur.sqlite", + table => "arthur", + ); +} + +sub reset_game { + my $av = shift->{avalon}; + $av->{gamephase} = GAMESTART; + $av->{gamesplayed} = 0; + $av->{timeout} = 0; + $av->{registered} = {}; + $av->{players} = (); + $av->{roles} = { + 'MERLIN' => [], + 'ASSASSIN' => [], + 'GOOD' => [], + 'EVIL' => [], + }; + $av->{king} = 0; + $av->{votes} = { pass => 0, fail => 0 }; + $av->{quests} = { pass => 0, fail => 0, votes => 0 }; +} + sub set_timeout { my ( $self, $value) = @_; $poe_kernel->alarm( avalon_timeout => time() + $value ); @@ -33,6 +74,8 @@ sub init { my $self = shift; $self->{avalon} = {}; $self->{avalon}->{config} = $self->bot->{store_object}->{store}->{cfg}->{cfg}; + $self->load_avalon_db; + $self->reset_game; } sub help { @@ -44,6 +87,8 @@ sub told { my $who = $mess->{who}; my $body = $mess->{body}; my $ispriv = defined $mess->{address}; + my $av = $self->{avalon}; + my $avdb = $self->{avdb}; my ( $command, @args ) = split /\s+/, $mess->{body}; given ($command) { -- cgit v1.2.3