| 1 | package Game::Dominoes::Error; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Object::Proto::Sugar -types; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | |||
| 10 | our (@FLAGS, %MESSAGE); | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | @FLAGS = qw/ | ||
| 14 | not_your_turn | ||
| 15 | tile_not_held | ||
| 16 | end_mismatch | ||
| 17 | no_such_arm | ||
| 18 | arm_closed | ||
| 19 | game_over | ||
| 20 | bad_move | ||
| 21 | /; | ||
| 22 | |||
| 23 | %MESSAGE = ( | ||
| 24 | not_your_turn => 'it is not your turn', | ||
| 25 | tile_not_held => 'you do not hold that tile', | ||
| 26 | end_mismatch => 'that tile does not match that end', | ||
| 27 | no_such_arm => 'there is no such arm', | ||
| 28 | arm_closed => 'that arm is not open yet', | ||
| 29 | game_over => 'the game is over', | ||
| 30 | bad_move => 'that is not a move', | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | has [@FLAGS] => ( | ||
| 35 | is => 'ro' | ||
| 36 | ); | ||
| 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 "'$flag' is not an error flag" unless $MESSAGE{$flag}; | ||
| 57 | return $class->new( | ||
| 58 | $flag => 1, | ||
| 59 | message => $MESSAGE{$flag}, | ||
| 60 | %extra | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub code { | ||
| 65 | my ($self) = @_; | ||
| 66 | for my $flag (@FLAGS) { | ||
| 67 | return $flag if $self->$flag; | ||
| 68 | } | ||
| 69 | return undef; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub flags { | ||
| 73 | return [@FLAGS]; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub stringify { | ||
| 77 | my ($self) = @_; | ||
| 78 | return ($self->code // 'error') . ': ' . ($self->message // ''); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ | ||