Skip to content

Commit

Permalink
Merge some fixes from 'fix_compatibility' which don't broke our tests.
Browse files Browse the repository at this point in the history
Next task is ai development. Ai development require refactoring of existing code. Before refactoring we should create big tests set. Before creationg of big test suite we should fix compatibility and rework testing framework to
quick test development and reworking.
  • Loading branch information
kevroletin committed Jan 28, 2012
2 parents 7cb3402 + 488462d commit 0dcda60
Show file tree
Hide file tree
Showing 33 changed files with 472 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Game.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ sub parse_request {
my $json = request()->raw_body();

use Data::Dumper;
# print Dumper($json);
print Dumper($json);

my $data = '';
eval {
Expand All @@ -67,7 +67,7 @@ sub parse_request {
}

use Data::Dumper;
# print Dumper(response()->body());
print Dumper(response()->body());

response()->finalize();
};
Expand Down
12 changes: 10 additions & 2 deletions Game/Actions/Debug.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ sub resetServer {
if (defined $data->{randSeed}) {
srand($data->{randSeed})
}
unlink 'tmp/test.db';
`rm -rf db`;
if (1) {
use DBI;
my $dbh = eval{ DBI->connect('dbi:SQLite:dbname=tmp/test.db') };
$dbh->do('delete from entries');
$dbh->do('delete from gin_index;');
die $DBI::errstr if $DBI::errstr;
} else {
unlink 'tmp/test.db';
`rm -rf db`;
}
response_json({result => 'ok'});
}

Expand Down
7 changes: 3 additions & 4 deletions Game/Actions/Game.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ sub getGameInfo {
sub getGameState {
my ($data) = @_;
my $game;

if (defined $data->{gameId}) {
$game = _get_game_by_id($data->{gameId})
} elsif (defined $data->{sid}) {
Expand Down Expand Up @@ -199,16 +200,14 @@ sub setReadinessStatus {
my $game = global_game();
assert($game->state() eq 'notStarted', 'badGameState');

printf STDERR "user: %s; [%s]", global_user()->{id},
join ', ', map { $_->{readinessStatus} } @{global_game()->players()};
global_user()->readinessStatus($data->{isReady});
if ($game->ready()) {
$game->state('conquer');
}

if (compability() && defined $data->{visibleRaces}) {
$game->racesPack([map { lc($_) } @{$data->{visibleRaces}}]);
$game->powersPack([map { lc($_) } @{$data->{visibleSpecialPowers}}]);
$game->racesPack([map { ($_) } @{$data->{visibleRaces}}]);
$game->powersPack([map { ($_) } @{$data->{visibleSpecialPowers}}]);
}

db()->update(global_user(), $game);
Expand Down
58 changes: 26 additions & 32 deletions Game/Actions/Gameplay.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use warnings;
use strict;

use Game::Actions;
use Game::Environment qw( assert response_json early_response_json
db global_game global_user );
use Game::Environment qw( assert compability response_json early_response_json
db global_game global_user is_debug );
use Moose::Util qw( apply_all_roles );

#TODO: move to separate module or use smth. like Module::Find
Expand Down Expand Up @@ -58,9 +58,7 @@ sub _control_state {
my $a = $data->{action};
my $game = global_game();
my $ok = sub {
unless ($_[0]) {
early_response_json({result => 'badStage'})
}
assert(shift, 'badStage', stage => $game->state(), @_);
};
my $state = sub {
$ok->($game->state() ~~ [@_])
Expand All @@ -78,7 +76,7 @@ sub _control_state {
};
my $start_moving = sub {
$state->('conquer');
$ok->(!@{$game->history()})
$ok->(!@{$game->history()} && !global_user()->raceSelected());
};
my $have_race = sub {
$ok->($game->activePlayer()->activeRace())
Expand All @@ -88,6 +86,7 @@ sub _control_state {
$curr_usr->();
$state->('conquer');
$ok->(global_user()->activeRace());
$ok->(!defined global_game()->lastDiceValue(), descr => 'diceUsed');
} elsif ($a eq 'decline' ) {
$curr_usr->();
$have_race->();
Expand Down Expand Up @@ -129,7 +128,8 @@ sub _control_state {
} elsif ($a eq 'throwDice' ) {
$curr_usr->();
$state->('conquer');
$power->('berserk')
$power->('berserk');
$ok->(!defined global_game()->lastDiceValue(), descr => 'diceUsed');
}
}

Expand All @@ -141,16 +141,18 @@ sub conquer {
my $reg = global_game()->map()->region_by_id($data->{regionId});
my $race = global_user()->activeRace();
$race->check_is_move_possible($reg);
my $defender = $race->conquer($reg);
my ($defender, $dice) = $race->conquer($reg, is_debug() ? $data->{dice} : 0);

if ($defender && $defender->have_owned_regions()) {
global_game()->state('defend');
} else {
global_game()->state('conquer');
}
db()->update(grep { defined $_ } global_user(), global_game(),
$reg, $defender);
response_json({result => 'ok'});
$reg, $defender);
my $res = {result => 'ok'};
$res->{dice} = $dice if defined $dice;
response_json($res);
}

sub decline {
Expand All @@ -171,17 +173,10 @@ sub __moves_pairs_from_array {
my $sum = 0;
for (@{$arr}) {
my $reg = global_game()->map()->region_by_id($_->{regionId});
unless ($reg->owner() &&
$reg->owner() eq global_user())
{
early_response_json({result => 'badRegion'})
}
unless ($_->{$field} =~ /^\d+$/) {
early_response_json(result => $error_msg)
}
if ($proc_reg{$reg}) {
early_response_json({result => 'badRegion'})
}
assert($reg->owner() && $reg->owner() eq global_user(), 'badRegion');
assert(defined $_->{$field} &&
$_->{$field} =~ /^\d+$/ && $_->{$field} > 0, $error_msg);
assert(!$proc_reg{$reg}, 'badRegion');
$proc_reg{$reg} = 1;
push @res, [$reg, $_->{$field}];
$sum += $_->{$field}
Expand Down Expand Up @@ -218,11 +213,9 @@ sub __heroes_regs_from_date {

sub _moves_from_data {
my ($data) = @_;
unless (defined $data->{regions} &&
ref($data->{regions}) eq 'ARRAY')
{
early_response_json({result => 'badJson'})
}
assert(defined $data->{regions} && ref($data->{regions}) eq 'ARRAY',
'badJson');

my ($units, $units_sum) =
__moves_pairs_from_array($data->{regions},
'tokensNum',
Expand Down Expand Up @@ -365,7 +358,7 @@ sub selectRace {

my $p = $data->{position};
assert($p =~ /^\d+$/ && 0 <= $p && $p <= 5, 'badPosition');
for (0 .. $p) {
for (0 .. $p - 1) {
$game->bonusMoney()->[$_] += 1;
}
my $coins = $game->bonusMoney()->[$p] - $p;
Expand All @@ -374,26 +367,27 @@ sub selectRace {
}
global_user()->coins(global_user()->coins + $coins);

my ($race, $power) = $game->pick_tokens($p);
my ($race, $power, $id) = $game->pick_tokens($p);

my $pair = ("Game::Race::" . ucfirst($race))->new();
my $pair = ("Game::Race::" . ucfirst($race))->new(tokenBadgeId => $id);
apply_all_roles($pair, ("Game::Power::" . ucfirst($power)));
global_user()->activeRace($pair);
global_user()->tokensInHand($pair->tokens_cnt());
global_user()->raceSelected(1);

$game->state('conquer');
db()->store_nonroot($pair);
db()->update($game, global_user());

response_json({result => 'ok'})
response_json({tokenBadgeId => $id, result => 'ok'})
}

sub throwDice {
my ($data) = @_;
proto($data, );
_control_state($data);

global_user()->activeRace()->throwDice();
my $dice = is_debug() ? $data->{dice} : 0;
global_user()->activeRace()->throwDice($dice);
}

1;
Expand Down
3 changes: 2 additions & 1 deletion Game/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use Exporter::Easy (
OK => [ qw(races
races_with_debug
powers
powers_with_debug) ] );
powers_with_debug) ]);


our @races = qw(
amazons
Expand Down
Loading

0 comments on commit 0dcda60

Please sign in to comment.