projects
/
svn42.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
added undo in perl framework
[svn42.git]
/
dart
/
Dart.pm
diff --git
a/dart/Dart.pm
b/dart/Dart.pm
index
9f5fd3c
..
2f38988
100644
(file)
--- a/
dart/Dart.pm
+++ b/
dart/Dart.pm
@@
-2,6
+2,7
@@
package Dart;
use strict;
use base 'Exporter';
package Dart;
use strict;
use base 'Exporter';
+use Clone;
# new Dart(player_names=>[ 'lala', 'popo' ]);
## Player, Rank, Active,
# new Dart(player_names=>[ 'lala', 'popo' ]);
## Player, Rank, Active,
@@
-12,7
+13,7
@@
sub new
my (%params) =@_;
my $self = bless {}, $class;
die "Missing player_names" if not ref $params{player_names} eq 'ARRAY';
my (%params) =@_;
my $self = bless {}, $class;
die "Missing player_names" if not ref $params{player_names} eq 'ARRAY';
- die "Missing player_names" if @{$params{player_names}};
+ die "Missing player_names" if not @{$params{player_names}};
$self->{player}=[];
$self->{current_player}=0;
my $player_counter=0;
$self->{player}=[];
$self->{current_player}=0;
my $player_counter=0;
@@
-26,7
+27,7
@@
sub new
$self->{round}=1;
$self->{max_shoots_per_player}=3;
$self->{current_shoot_count}=0;
$self->{round}=1;
$self->{max_shoots_per_player}=3;
$self->{current_shoot_count}=0;
-
+ $self->{callbacks}=$params{callbacks};
$self->callback('init');
return $self;
}
$self->callback('init');
return $self;
}
@@
-52,11
+53,15
@@
sub run
{
my $self=shift;
my ($data_in_fh,$sound_out_fh)=@_;
{
my $self=shift;
my ($data_in_fh,$sound_out_fh)=@_;
+ my @history;
# $data_in_fh ||= STDIN;
# $sound_out_fh ||= STDOUT;
$self->{sound_out_fh}=$sound_out_fh;
# $data_in_fh ||= STDIN;
# $sound_out_fh ||= STDOUT;
$self->{sound_out_fh}=$sound_out_fh;
- while ( my $shoot_data = <$data_in_fh>)
+ push @history, Clone::clone($self);
+ $self->callback('before_shoot');
+ #while ( my $shoot_data = <$data_in_fh>)
+ while ( my $shoot_data = <STDIN>)
{
#print STDERR $schuss;
my ($mult,$number) = split /\s+/, $shoot_data;
{
#print STDERR $schuss;
my ($mult,$number) = split /\s+/, $shoot_data;
@@
-67,7
+72,13
@@
sub run
$self->shoot($mult,$number);
} elsif ($mult eq 'btn') {
$self->next_player();
$self->shoot($mult,$number);
} elsif ($mult eq 'btn') {
$self->next_player();
+ } elsif ($mult eq 'undo' and $#history) {
+ pop @history;
+ $self= pop @history;
+ $self->callback('undo');
}
}
+ push @history, Clone::clone($self);
+ $self->callback('before_shoot');
}
}
}
}
@@
-88,7
+99,10
@@
sub shoot
sub shout_last_shoot
{
my $self=shift;
sub shout_last_shoot
{
my $self=shift;
- if ($self->{current_shoot}{mult} == 2) {
+ if ($self->{current_shoot}{mult} == 2 && $self->{current_shoot}{number} == 25) {
+ $self->shout("bullseye");
+ return;
+ } elsif ($self->{current_shoot}{mult} == 2 ) {
$self->shout('double');
} elsif ($self->{current_shoot}{mult} == 3) {
$self->shout('triple');
$self->shout('double');
} elsif ($self->{current_shoot}{mult} == 3) {
$self->shout('triple');
@@
-102,7
+116,27
@@
sub shout
my $self=shift;
my ($what)=@_;
my $fh = $self->{sound_out_fh};
my $self=shift;
my ($what)=@_;
my $fh = $self->{sound_out_fh};
- print $fh "$what\n";
+ if ($what eq 25)
+ {
+ print "bull\n";
+ } else {
+ print "$what\n";
+ }
+#print $fh "$what\n";
+}
+
+sub get_current_player
+{
+ my $self=shift;
+ return $self->get_player($self->{current_player});
+}
+
+sub get_player
+{
+ my $self=shift;
+ my ($player_idx)=@_;
+ die "Illegal Player Index $player_idx" if $player_idx < 0 or $player_idx >= $self->{player_count};
+ return $self->{player}[$player_idx];
}
sub next_player
}
sub next_player
@@
-110,9
+144,9
@@
sub next_player
my $self=shift;
$self->callback('before_next_player');
$self->{current_shoot_count}=0;
my $self=shift;
$self->callback('before_next_player');
$self->{current_shoot_count}=0;
- ($self->{current_player},my $new_round)=get_next_active_player($self->{current_player});
+ ($self->{current_player},my $new_round)=get_next_active_player($self->{player},$self->{current_player});
$self->shout("player");
$self->shout("player");
- $self->shout($self->{player}[$self->{current_player}]{name});
+ $self->shout($self->get_current_player()->{name});
$self->next_round() if $new_round;
return $self->callback('next_player');
}
$self->next_round() if $new_round;
return $self->callback('next_player');
}
@@
-174,3
+208,5
@@
sub create_player
#my $player = {%player_attributes};
#return $player;
}
#my $player = {%player_attributes};
#return $player;
}
+
+1;