Player.pm (952B) [raw]
1 # Player.pm -- a Player class for each websocket client 2 use strict; 3 use warnings; 4 5 package Euchre::Player; 6 7 use Euchre::Errors; 8 9 use Class::Tiny qw(id ws), { 10 seat => -1, # spectator 11 name => 'Anon', 12 start_time => sub { time }, 13 }; 14 15 sub BUILD { 16 my ($self) = @_; 17 # de lazy the attributes 18 $self->$_ for qw(seat name start_time); 19 } 20 21 sub error { 22 my ($self, $errno) = @_; 23 my $json = { 24 msg_type => 'error', 25 errno => $errno, 26 msg => err_msg($errno), 27 }; 28 $self->send($json); 29 } 30 31 # Send a JSON message 32 sub send { 33 my ($self, $json) = @_; 34 $self->ws->send({ json => $json }); 35 } 36 37 sub is_spectator { 38 my ($self) = @_; 39 return $self->seat == -1; 40 } 41 42 sub stand_up { 43 my ($self) = @_; 44 if ($self->is_spectator) { 45 return ALREADY_STANDING; 46 } else { 47 $self->seat(-1); 48 } 49 return SUCCESS; 50 } 51 52 sub is_active { 53 my ($self) = @_; 54 return !$self->ws->is_finished; 55 } 56 57 1;