| 1 | #=============================================================================== | ||
| 2 | # | ||
| 3 | # FILE: Games::Go::AGA::DataObjects::Tournament.pm | ||
| 4 | # | ||
| 5 | # USAGE: use Games::Go::AGA::DataObjects::Tournament; | ||
| 6 | # | ||
| 7 | # PODNAME: Games::Go::AGA::DataObjects::Tournament | ||
| 8 | # ABSTRACT: models AGA register.tde file information | ||
| 9 | # | ||
| 10 | # AUTHOR: Reid Augustin (REID), <reid@lucidport.com> | ||
| 11 | # COMPANY: LucidPort Technology, Inc. | ||
| 12 | # CREATED: 11/19/2010 03:13:05 PM PST | ||
| 13 | #=============================================================================== | ||
| 14 | |||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | package Games::Go::AGA::DataObjects::Tournament; | ||
| 20 | |||
| 21 | use Mouse; | ||
| 22 | use parent 'Games::Go::AGA::DataObjects::Register'; | ||
| 23 | use Games::Go::AGA::DataObjects::Round; | ||
| 24 | |||
| 25 | use Carp; | ||
| 26 | use IO::File; | ||
| 27 | use IO::String; | ||
| 28 | |||
| 29 | our $VERSION = '0.107'; # VERSION | ||
| 30 | |||
| 31 | # public attributes | ||
| 32 | # has 'rounds' => ( | ||
| 33 | # isa => 'ArrayRef[Games::Go::AGA::DataObjects::Round]', | ||
| 34 | # is => 'ro', | ||
| 35 | # default => sub { [] } | ||
| 36 | # ); | ||
| 37 | |||
| 38 | # Note: the change callback shouldn't really be necessary since | ||
| 39 | # Directives are changed by the Directives object and player data is | ||
| 40 | # changed by the Player object. But it might be convenient... | ||
| 41 | has 'change_callback' => ( | ||
| 42 | isa => 'Maybe[CodeRef]', | ||
| 43 | is => 'rw', | ||
| 44 | default => sub { sub { } } | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub BUILD { | ||
| 48 | my ($self) = @_; | ||
| 49 | $self->{rounds} = [ undef ]; # rounds start at 1 | ||
| 50 | } | ||
| 51 | |||
| 52 | sub changed { | ||
| 53 | my ($self) = @_; | ||
| 54 | |||
| 55 | &{$self->change_callback}($self) if ($self->{change_callback}); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub add_round { | ||
| 59 | my ($self, $round_num, $round, $replace) = @_; | ||
| 60 | |||
| 61 | if ($round_num <= 0) { | ||
| 62 | croak "Round number must be >= 1\n"; | ||
| 63 | } | ||
| 64 | elsif ($round_num > 1 and # allow round 1 | ||
| 65 | not defined $self->{rounds}[$round_num - 1]) { | ||
| 66 | croak "Can't add round $round_num, previous round doesn't exist yet\n"; | ||
| 67 | } | ||
| 68 | my $rm_round = $self->{rounds}[$round_num]; | ||
| 69 | if (defined $rm_round) { | ||
| 70 | if ($replace) { | ||
| 71 | # remove games from this stale round | ||
| 72 | foreach my $game (@{$rm_round->games}) { | ||
| 73 | $game->white->delete_game($game); | ||
| 74 | $game->black->delete_game($game); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | croak "$round_num already exists\n"; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | $self->{rounds}[$round_num] = $round; | ||
| 82 | # $self->changed; # rounds are recorded in N.tde files, not in register.tde | ||
| 83 | } | ||
| 84 | |||
| 85 | sub games { | ||
| 86 | my ($self, $round_num) = @_; | ||
| 87 | |||
| 88 | $round_num = @{$self->{rounds}} if (not defined $round_num); | ||
| 89 | my @games; | ||
| 90 | foreach my $r_num (0 .. $round_num) { | ||
| 91 | my $round = $self->{rounds}[$r_num]; | ||
| 92 | next if (not $round); | ||
| 93 | push @games, $round->games; | ||
| 94 | } | ||
| 95 | return wantarray | ||
| 96 | ? @games | ||
| 97 | : \@games; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub rounds { | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | return $#{$self->{rounds}}; # we don't count 0 | ||
| 104 | } | ||
| 105 | |||
| 106 | sub round { | ||
| 107 | my ($self, $round_num) = @_; | ||
| 108 | |||
| 109 | if (not defined $self->{rounds}[$round_num]) { | ||
| 110 | croak "Round $round_num doesn't exist\n"; | ||
| 111 | } | ||
| 112 | return $self->{rounds}[$round_num]; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub send_to_AGA { | ||
| 116 | my ($self, $fd) = @_; | ||
| 117 | |||
| 118 | if (not $fd) { | ||
| 119 | $fd = IO::String->new() or die "Failed to create IO::String\n"; | ||
| 120 | } | ||
| 121 | |||
| 122 | $fd->printf("TOURNEY %s\n", | ||
| 123 | $self->get_directive_value('TOURNEY')); | ||
| 124 | |||
| 125 | my $date = $self->get_directive_value('DATE'); | ||
| 126 | my ($start, $finish) = $date =~ m/^(\S+)[\-\s]+(\S+)$/; | ||
| 127 | $start ||= $date; | ||
| 128 | $finish ||= $start; | ||
| 129 | $start =~ s/\D/\//g; # use slash date notation | ||
| 130 | $finish =~ s/\D/\//g; | ||
| 131 | $fd->print(" start=$start\n"), | ||
| 132 | $fd->print(" finish=$finish\n"), | ||
| 133 | |||
| 134 | $fd->printf(" rules=%s\n", | ||
| 135 | $self->get_directive_value('RULES')); | ||
| 136 | $fd->print("\nPLAYERS\n"); | ||
| 137 | |||
| 138 | # print player info | ||
| 139 | my $name_width = 5; | ||
| 140 | for my $player ($self->players) { | ||
| 141 | $name_width = length($player->full_name) | ||
| 142 | if (length($player->full_name) > $name_width); | ||
| 143 | } | ||
| 144 | |||
| 145 | for my $player ($self->players) { | ||
| 146 | $fd->printf("%9.9s %*.*s %s\n", | ||
| 147 | $player->id, | ||
| 148 | $name_width, | ||
| 149 | $name_width, | ||
| 150 | $player->full_name, | ||
| 151 | $player->rating, | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # print games with results | ||
| 156 | $fd->print("\nGAMES\n"); | ||
| 157 | foreach my $game ($self->games) { | ||
| 158 | if ($game->winner) { | ||
| 159 | my $result = ($game->winner->id eq $game->white->id) ? 'W' : 'B'; | ||
| 160 | $fd->printf("%9.9s %9.9s $result %s %s\n", | ||
| 161 | $game->white->id, | ||
| 162 | $game->black->id, | ||
| 163 | $game->handi, | ||
| 164 | $game->komi, | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | $fd->print("\n"); | ||
| 169 | |||
| 170 | return $fd | ||
| 171 | } | ||
| 172 | |||
| 173 | # this really shouldn't be necessary. Register and Directives will | ||
| 174 | # fprint the register.tde files, and Round will fprint the N.tde files. | ||
| 175 | sub fprint { | ||
| 176 | my ($self, $fh) = @_; | ||
| 177 | |||
| 178 | $self->SUPER::fprint($fh); # print the register.tde file | ||
| 179 | } | ||
| 180 | |||
| 181 | no Mouse; | ||
| 182 | __PACKAGE__->meta->make_immutable; | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |