| 1 | package Game::Mahjong::Error; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Object::Proto::Sugar -types; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | our $VERSION = '0.04'; | ||
| 10 | |||
| 11 | our (@FLAGS, %MESSAGE); | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | @FLAGS = qw/ | ||
| 15 | not_your_turn wrong_phase tile_not_held cannot_claim not_a_meld | ||
| 16 | no_kong not_a_win too_few_points no_window already_answered | ||
| 17 | game_over bad_move | ||
| 18 | /; | ||
| 19 | |||
| 20 | %MESSAGE = ( | ||
| 21 | not_your_turn => 'it is not your turn', | ||
| 22 | wrong_phase => 'that is not what the game is waiting for', | ||
| 23 | tile_not_held => 'you do not hold that tile', | ||
| 24 | cannot_claim => 'you cannot claim that tile', | ||
| 25 | not_a_meld => 'those tiles do not make a set with it', | ||
| 26 | no_kong => 'you cannot declare a kong now', | ||
| 27 | not_a_win => 'that hand is not complete', | ||
| 28 | too_few_points => 'that hand is worth fewer than eight points', | ||
| 29 | no_window => 'there is nothing to answer', | ||
| 30 | already_answered => 'you have already answered', | ||
| 31 | game_over => 'this game has finished', | ||
| 32 | bad_move => 'that is not a move', | ||
| 33 | ); | ||
| 34 | } | ||
| 35 | |||
| 36 | has [@FLAGS] => (is => 'ro'); | ||
| 37 | |||
| 38 | has error => ( | ||
| 39 | is => 'ro', | ||
| 40 | default => 1 | ||
| 41 | ); | ||
| 42 | |||
| 43 | has message => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => Str | ||
| 46 | ); | ||
| 47 | |||
| 48 | has legal => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => ArrayRef, | ||
| 51 | default => [] | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub throw { | ||
| 55 | my ($class, $flag, %extra) = @_; | ||
| 56 | die "'" . (defined $flag ? $flag : 'undef') . "' is not an error flag" | ||
| 57 | unless defined $flag && $MESSAGE{$flag}; | ||
| 58 | return $class->new($flag => 1, message => $MESSAGE{$flag}, %extra); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub code { | ||
| 62 | my ($self) = @_; | ||
| 63 | for my $flag (@FLAGS) { | ||
| 64 | return $flag if $self->$flag; | ||
| 65 | } | ||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub flags { return [@FLAGS] } | ||
| 70 | |||
| 71 | sub messages { return { %MESSAGE } } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ | ||