-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoGame.pm
318 lines (225 loc) · 6.67 KB
/
GoGame.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package GoGame;
use strict;
use locale;
use GoatLib;
use FFGPlayer;
=head1 DESCRIPTION
GoGame contains all the information about a Go Game played in a tournament:
players, handicaps, date of the game, result... and so on. Obviously not
everything might be defined (e.g. result might be undefined if the game hasn't
be played yet).
=cut
=head2 The state machine
Each game is considered like a state machine. The following
states apply:
=over 4
=item undef
Nothing is known about the game: the players haven't been
notified.
=item challenged
The players have been notified of the game. Parameter: date
of the challenge (to allow for reminders).
=item scheduled
The players have replied to schedule the game, and specified
a location and date. Parameter: date of the game (to not
bother them anymore and ask them for the results after the
game)
=item finished
The game has been played and the result is known. Parameter:
who won? (black|white)
=item canceled
The game has been canceled for some reason. (? Is it useful
to keep track of it then?)
=back
=head2 Attributes of the object
=over 4
=item black
FFGPlayer object of the black player
=item white
FFGPlayer object of the black player
=item state
(one of the values specified above)
=item handicap
Handicap at which the game is to be player
=item challenge_date
=item scheduled_date
=item reminder_date
The dates are GMT, seconds from 1970. reminder_date corresponds to the date of
the last reminder sent to the players about that game: before the game, when
was the last reminder about organising the game sent; after the game, when was
the last reminder about sending in the results sent.
=item location
Location for a scheduled game.
=item result
Who won the game (black or white).
=item winner
FFGPlayer object that won the game
=item loser
FFGPlayer object that lost the game
=item rated
True if the game is to be submitted to a rating system. (Typically, we don't
want games that haven't been played to count towards the rating system).
=item submitted
True if the game was submitted to the rating system. This is to keep track of
what has already be submitted and ease partial submissions.
=item sgf
Path to the SGF file of the finished game, if any.
=cut
BEGIN {
my $subs;
foreach my $data ( qw/challenge_date scheduled_date reminder_date location result rated submitted sgf/ ) {
$subs .= qq{
sub $data {
defined \$_[1] ? \$_[0]->{$data} = \$_[1] : \$_[0]->{$data};
}
}
}
eval $subs;
}
################################################################################
# CONSTANTS
# Constant for the various status-es (these aren't parameters,
# don't change them)
use constant UNDEF => 'undef';
use constant CHALLENGED => 'challenged';
use constant SCHEDULED => 'scheduled';
use constant FINISHED => 'finished';
use constant CANCELED => 'canceled';
################################################################################
# Special accessors
sub status {
my ($obj, $s) = @_;
$obj->{status} = UNDEF if not defined $obj->{status};
$s ? $obj->{status} = $s : $obj->{status};
}
sub handicap {
my ($obj, $s) = @_;
$obj->{handicap} = 0 if not defined $obj->{handicap};
defined $s ? $obj->{handicap} = $s : $obj->{handicap};
}
sub black {
my ($obj, $r) = @_;
$obj->{black}->{FFGPlayer} = $r if $r;
$obj->{black}->{FFGPlayer};
}
sub white {
my ($obj, $r) = @_;
$obj->{white}->{FFGPlayer} = $r if $r;
$obj->{white}->{FFGPlayer};
}
# Returns object for winner player
sub winner {
my ($obj) = @_;
return undef unless $obj->finished;
return ($obj->result eq 'white') ? $obj->white : $obj->black;
}
# Returns object for loser player
sub loser {
my ($obj) = @_;
return undef unless $obj->finished;
return ($obj->result eq 'white') ? $obj->black : $obj->white;
}
# True if game has both specified players
sub has_players {
my ($g, $p1, $p2) = @_;
return ((($p1 == $g->white) and ($p2 == $g->black))
or (($p1 == $g->black) and ($p2 == $g->white)));
}
#################################################################################
# Change status to 'challenged': challenge mail has been sent
# in: Date the challenge was sent
sub challenged {
my ($obj, $date) = @_;
$obj->status(CHALLENGED);
$obj->challenge_date($date);
}
# Change status to 'scheduled': a player has scheduled the game
sub scheduled {
my ($obj, $date, $location) = @_;
$obj->status(SCHEDULED);
$obj->scheduled_date($date);
$obj->location($location);
$obj->reminder_date($date); # Don't pester until after the game
}
# Change status to finished and set result
sub finished {
my ($obj, $res) = @_;
$obj->status(FINISHED);
$obj->result($res);
}
# Unfinish a game (e.g. when fixing someone's erroneous result report).
sub unfinish {
my ($obj) = @_;
$obj->status(UNDEF);
$obj->{result} = undef; # Accessor doesn't let us undef...
}
sub cancel {
my ($obj, $res) = @_;
$obj->status(CANCELED);
}
sub is_challenge_sent {
$_[0]->status eq CHALLENGED;
}
sub is_scheduled {
$_[0]->status eq SCHEDULED;
}
sub is_finished {
$_[0]->status eq FINISHED;
}
sub is_canceled {
$_[0]->status eq CANCELED;
}
sub new {
my ($class, $black, $white, $handi) = @_;
my (%h, $obj);
$obj = \%h;
bless $obj, $class;
$obj->handicap($handi);
$obj->black($black);
$obj->white($white);
$obj->rated(1);
$obj->reminder_date(0);
return $obj;
}
=item text_status
Returns a multi-line description of the game's status.
Options:
summary: Only print player names and handicap
=cut
sub text_status {
my ($game, %opts) = @_;
my ($black, $white, $c_date, $s_date, $location, $result,
$state, $handicap);
$black = $game->black->fulladdress;
$white = $game->white->fulladdress;
$result = "\t";
if ($game->result) {
$result = $game->result;
$result = "($result)" unless $game->rated;
$result = "winner: $result";
}
$state = $game->status;
$handicap = $game->handicap;
$location = $game->location ? $game->location : "\t";
if (defined $game->challenge_date) {
$c_date = "Challenge sent ".gmtime $game->challenge_date;
} else {
$c_date = "Challenge not sent";
}
if (defined $game->scheduled_date) {
$s_date = "Scheduled for ".(gmtime $game->scheduled_date)." at $location";
} else {
$s_date = "Not scheduled";
}
if ($opts{summary}) {
return <<"EOF";
$black\t$white\t($handicap)\t$result
EOF
}
return <<"EOF";
$state: $black $white ($handicap stones) $result
$c_date
$s_date
EOF
}
1;