summaryrefslogtreecommitdiffstats
path: root/lib/Bot/Games/Game/Chess.pm
blob: fb32c9dce1d2cb91862f3372b0cfd165db817494 (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
64
65
66
67
68
69
70
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 "The game has already begun between " . join ' and ', $self->players
        unless $self->has_player($player);
    my $player_index = $self->game->to_move ? 0 : 1;
    return "It's not your turn"
        if $player ne ($self->players->[$player_index] || '');

    my $status = eval { $self->game->go_move($move) };
    return $@ if $@;
    my $desc = $self->format_turn($status);
    $self->inc_turn 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};
}

__PACKAGE__->meta->make_immutable;
no Bot::Games::OO::Game;

1;