-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpair
executable file
·312 lines (243 loc) · 8.6 KB
/
pair
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
#! /usr/bin/perl -w
# Goat: Gentil Organisateur et Administrateur de Tournois
# Copyright (C) 2006-2018 Yves Rutschle
#
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more
# details.
#
# The full text for the General Public License is here:
# http://www.gnu.org/licenses/gpl.html
=head1 NAME
pair -- compute a new round in a tournament
=head1 SYNOPSIS
pair [--bye [<bye_player>|'random']] [--no-getech] [--date <date>] [--verbose <1|2>] [--file <toperm>]
=head1 DESCRIPTION
I<pair> computes a new round based on t(he specified
tournament file. If the number of players in the file is
odd, it'll complain and you need to specify the e-mail
address of a 'bye' player, who won't take part in this
round. Alternatively you can specify 'random', in which case
the bye player will be randomly picked among those that
haven't played their game in the previous round (or among
all players, if all have played their game).
If the B<date> is specified, it will be displayed in
all reminders. If not specified, one month from now is
picked.
B<--no-getech> prevents downloading the echelle file and
updating player information. This is useful to force-change
player's registration levels, for example.
=cut
use strict;
use Algorithm::Pair::Best2;
use Pod::Usage;
use open ':locale';
use GoatLib;
use GoatConfig;
use Tournament;
use MailOut;
use Getopt::Long;
# Interpret command line
my ($param_bye, $param_final_date, $param_round,
$param_no_getech, $help);
my $verbose = 0;
GetOptions(
'help' => \$help,
'date=s' => \$param_final_date,
'bye=s' => \$param_bye,
'round=i' => \$param_round,
'verbose=i' => \$verbose,
'no-getech' => \$param_no_getech,
) or die pod2usage();
die pod2usage(-verbose=>2) if defined $help;
my $Tournament = Tournament->load($TOURNAMENT_FILE);
$Tournament->update_ech($Tournament) unless $param_no_getech;
# Applies function to all games in a tournament. Game is
# passed as first parameter to function, e.g. remove all
# handicaps:
# map_game { $_[0]->handi(0) } $tournament
sub map_game (&@) {
my ($cref, $tournament) = @_;
foreach my $round ($Tournament->rounds) {
foreach my $game ($round->games) {
&$cref($game);
}
}
}
=head2 Pairing criteria
Pairing is based on the "best fit" algorithm (see
Pair::Algorithm, module on which this program is based).
Lower is better. The following criteria are used:
=over 4
=item Level difference
Each point of difference in level decreases the desirability
of the pairing (remembering that there is 100 points per
stone). E.g. pairing a -875 with a -1450, this criterion
will produce a score of 575.
=item Already played
Players who have already played together during this
tournament get a +10000 to their pairing score.
=item Different club
Players who belong to the same club get points to encourage
variety
=back
=cut
my %criteria = (
level_diff => sub {
my ($p1, $p2, $explain) = @_;
my $diff = abs($p1->level - $p2->level);
my $score = $diff;
print "level_diff:\t".$p1->fullname." vs ".$p2->fullname.": $score\n" if $explain;
return $score;
},
already_played => sub {
my ($p1, $p2, $explain) = @_;
my $played = 0;
map_game {
my $g = $_[0];
$played++ if
$g->white->id eq $p1->id and $g->black->id eq $p2->id or
$g->white->id eq $p2->id and $g->black->id eq $p1->id;
} $Tournament;
my $score = 10000 * $played;
print "already_played:\t".$p1->fullname." vs ".$p2->fullname.": $score\n" if $explain;
return $score;
},
same_club => sub {
my ($p1, $p2, $explain) = @_;
my $score = 0;
$score = 500 if $p1->club eq $p2->club;
print "same_club:\t".$p1->fullname." vs ".$p2->fullname.": $score\n" if $explain;
return $score;
},
);
sub score {
my ($a, $b) = @_;
my $explain = ($verbose >= 2);
my $sum = 0;
map { $sum += &$_($a, $b, $explain) } @criteria{ @PAIRING_CRITERIA };
return $sum;
}
my $matchmaker;
$matchmaker = Algorithm::Pair::Best2->new(
scoreSub => \&score,
window => 6,
progress => sub {
my ($p1, $p2, $i1, $i2) = @_;
if ($verbose >= 1) {
my $score = $matchmaker->get_score($i1, $i2);
print "Paired ".$p1->fullname." - ".$p2->fullname." ($score)\n";
}
}
);
# Validate that configured pairing criteria are correct
{
die "No `pairing_criteria` defined in configuration file\n" unless @PAIRING_CRITERIA;
my @errors = grep { not exists $criteria{$_} } @PAIRING_CRITERIA;
if (@errors) {
warn "pairing_criteria: " . (join " ", @errors ) . " not known\n";
die "Valid entries: " . (join " ", keys %criteria) .
"\n";
}
}
# Returns a random array element; if testing, return first
# element
sub random_pick {
my (@l) = @_;
if ($CFG->{testing}) {
return $l[0];
} else {
return $l[ rand @l ];
}
}
# Returns the list of players that are still registered
sub keep_registered {
my ($tournament, @in) = @_;
my @out;
foreach my $p (@in) {
my $found = grep { $_->id eq $p->id } @{$tournament->ffgplayers};
push @out, $p if $found;
}
return @out;
}
# Test for odd number of players -- request to remove one
my @players = $Tournament->players;
my $bye;
if (1 & @players) {
if (defined $param_bye and $param_bye eq 'random') {
# Find players that haven't played their last game
my @bye_candidates;
if (defined $Tournament->curr_round) {
@bye_candidates = $Tournament->unplayed;
# Keep those that are registered (people from
# last round may have both not played and left)
@bye_candidates = keep_registered $Tournament, @bye_candidates;
}
# If all have played or tournament not started, select from all players
@bye_candidates = @players unless @bye_candidates;
# Remove players that have already been bye
my @old_bye = grep { defined $_ } map { $_->bye } $Tournament->rounds;
foreach my $old (@old_bye) {
@bye_candidates = grep { $_->id ne $old->id } @bye_candidates;
}
$bye = random_pick @bye_candidates;
warn "Randomly picking ".$bye->fullname." as bye player\n";;
die "Undef bye player?\n" if not defined $bye; # May happen e.g. if 2 players haven't played but were also bye before
@players = grep { $_->id ne $bye->id } @players;
} else {
die "Odd number of players -- specify a bye with:\n".
"\tpair --bye <email>\n" unless defined $param_bye;
@players = grep { $_->email ne $param_bye } @players;
die "$param_bye not found\n" if (1 & @players);
$bye = $Tournament->find_player_by_email($param_bye);
}
}
# Add all players to the pairing object.
# (Sorted so they're already sort of close to each other wrt
# to the criteria)
foreach my $p (reverse sort {$a->level <=> $b->level} @players) {
$matchmaker->add($p);
}
my $round_num = $param_round // (($Tournament->round_number || 0) + 1);
my $round = Round->new($round_num);
my $final_date;
if (defined $param_final_date) {
$final_date = parse_datestr($param_final_date);
die "Wrong date format $param_final_date." unless defined $final_date;
} else {
$final_date = my_time() + 30 * 24 * 3600; # one month in the future
}
$round->start_date(&main::my_time);
$round->final_date($final_date);
$round->bye($bye) if defined $bye;
$Tournament->add_round($round_num, $round);
my @res = $matchmaker->pick();
while (@res) {
my $p1 = shift @res;
my $p2 = shift @res;
# Weaker player is black
my ($black, $white) = ($p1->level > $p2->level) ? ($p2, $p1) : ($p1, $p2);
my $handi = int(($white->level - $black->level)/100);
$handi -= $CFG->{handicap} if $handi > $CFG->{handicap} - 1;
$handi = 9 if $handi > 9;
my $g = new GoGame $black, $white, $handi;
$Tournament->curr_round->add_game($g);
}
print $Tournament->curr_round->pairings_as_text;
$Tournament->save($TOURNAMENT_FILE);
my $mail_out = new MailOut(
no_send => (defined $CFG->{testing}),
testing => (defined $CFG->{testing}),
);
$mail_out->deadline(
round => $round_num,
time => $final_date,
);