blob: 28a7183b1a3ca9b9b57c2887072a9552bda3da18 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package Bot::Games::Game::Chess;
use Bot::Games::OO::Game;
extends 'Bot::Games::Game';
use Chess::Rep;
use App::Nopaste;
has '+help' => (
default => 'chess help',
);
has game => (
is => 'ro',
isa => 'Chess::Rep',
default => sub { Chess::Rep->new },
);
has turn_count => (
traits => ['Counter'],
is => 'ro',
isa => 'Int',
default => 1,
provides => {
inc => 'inc_turn',
}
);
augment turn => sub {
my $self = shift;
my ($player, $move) = @_;
$self->add_player($player) unless $self->has_player($player);
return unless $self->has_player($player);
my $status = eval { $self->game->go_move($move) };
return $@ if $@;
my $desc = $self->format_turn($status);
$self->game->inc_counter if $self->game->to_move;
$self->is_active(0) if $self->game->status->{mate}
|| $self->game->status->{stalemate};
return "$desc: " . App::Nopaste::nopaste(text => $self->game->dump_pos,
desc => $desc,
nick => $player);
};
around allow_new_player => sub {
my $orig = shift;
my $self = shift;
return if $self->num_players >= 2;
return $self->$orig(@_);
};
sub format_turn {
my $self = shift;
my ($turn) = @_;
my $ret = $self->turn_count . ". ";
if ($self->game->to_move) {
$ret .= "... ";
}
$ret .= $turn->{san};
}
1;
|