-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.php
More file actions
218 lines (178 loc) · 5.88 KB
/
validation.php
File metadata and controls
218 lines (178 loc) · 5.88 KB
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
<?php
$fixtures = get_fixtures();
// All teams must get 4 games.
check__game_count($fixtures, 4);
check__possible_matches($fixtures);
check__consecutive_games($fixtures, 1);
check__a_vs_b($fixtures);
check__duplicates($fixtures);
// TODO Check teams games are all in the same group?
/**
* Read the fixtures from the CSV file.
* Format: each row is a round.
* each group of 4 columns is a pitch. The 4 columns are:
* Grouping, Team 1, (vs) Team 2, [blank].
*
* @return array
*/
function get_fixtures() {
$handle = fopen(__DIR__ . '/fixtures.csv', 'r');
$fixtures = array();
$round = 1;
while ($row = fgetcsv($handle, NULL, "\t")) {
$games = array_chunk($row, 4);
foreach ($games as $i => $game) {
$pitch_no = $i + 1;
if (!array_filter($game)) {
error("Pitch $pitch_no is empty in round $round");
continue;
}
$fixtures[$round][$pitch_no] = array(
'grouping' => $game[0],
't1' => $game[1],
't2' => $game[2],
);
}
$round++;
}
return $fixtures;
}
/**
* Check that each team get's a specified number of games.
* @todo warn on byes.
*/
function check__game_count($fixtures, $required_games) {
$checker = array();
foreach ($fixtures as $round_no => $round) {
foreach ($round as $pitch_no => $game) {
if (empty($checker[$game['grouping']][$game['t1']])) {
$checker[$game['grouping']][$game['t1']] = 0;
}
$checker[$game['grouping']][$game['t1']]++;
if (empty($checker[$game['grouping']][$game['t2']])) {
$checker[$game['grouping']][$game['t2']] = 0;
}
$checker[$game['grouping']][$game['t2']]++;
}
}
foreach ($checker as $group => $teams) {
foreach ($teams as $team_name => $game_count) {
if ($game_count != $required_games) {
error("$group - " . $team_name . ' has ' . $game_count . ' games');
}
}
}
}
/**
* Check that no team plays itself.
*/
function check__possible_matches($fixtures) {
foreach ($fixtures as $round_no => $round) {
foreach ($round as $pitch_no => $game) {
if ($game['t1'] == $game['t2']) {
error($game['t1'] . ' plays themselves in round ' . $round_no . ' on pitch ' . $pitch_no);
}
}
}
}
/**
* Check that every team has a break of at least n matches between each game.
*
* min_gap == 0 will check that no team is playing on two pitches at the same time
* min_gap == 1 will check that every team has at least one games break between each match.
* min_gap == 2 will check that every team has at least two games break between each match etc.
*
*/
function check__consecutive_games($fixtures, $min_gap = 0) {
// Look ahead one row. No team should play twice in a row.
// For each team, we could calculate the spread of their games.
//
// We gather the group/team name. And the gap between each of their games.
// No team can play two games at the same time.
foreach ($fixtures as $round_no => $round) {
foreach ($round as $pitch_no => $game) {
foreach (_get_teams($game) as $t => $team) {
// _find_team_in_round will find the current game being checked if we
// don't exclude it.
$ignore = array(
'grouping' => $game['grouping'],
'pitch' => $pitch_no,
);
for ($i = 0; $i <= $min_gap; $i++) {
$test_round_no = $round_no + $i;
if (isset($fixtures[$test_round_no])) {
$test_round = $fixtures[$test_round_no];
$dupe = _find_team_in_round($test_round, $game['grouping'], $team);
if ($dupe && ($test_round_no != $round_no || $dupe != $ignore)) {
error("{$game['grouping']} - $team is playing in round $round_no on pitch $pitch_no and round $test_round_no on pitch {$dupe['pitch']}");
}
}
}
}
}
}
}
function check__a_vs_b($fixtures) {
foreach ($fixtures as $round_no => $round) {
foreach ($round as $pitch_no => $game) {
if ($game['t1'] !== $game['t2'] && _get_team_base_name($game['t1']) == _get_team_base_name($game['t2'])) {
error("{$game['grouping']} - {$game['t1']} plays {$game['t2']} in round $round_no");
}
}
}
}
function check__duplicates($fixtures) {
foreach ($fixtures as $round_no1 => $round1) {
foreach ($round1 as $pitch_no1 => $game1) {
foreach ($fixtures as $round_no2 => $round2) {
foreach ($round2 as $pitch_no2 => $game2) {
// Don't check the same fixture against itself.
if ($round_no1 === $round_no2 && $pitch_no1 === $pitch_no2) {
continue;
}
// Don't repeat the same checks.
if ($round_no1 >= $round_no2) {
continue;
}
// Don't check matches across groups.
if ($game1['grouping'] !== $game2['grouping']) {
continue;
}
$match =
($game1['t1'] == $game2['t1'] || $game1['t1'] == $game2['t2'])
&& ($game1['t2'] == $game2['t1'] || $game1['t2'] == $game2['t2']);
if ($match) {
error("{$game1['grouping']} - {$game1['t1']} plays {$game1['t2']} in round $round_no1 and again in $round_no2");
}
}
}
}
}
}
function _find_team_in_round($round, $grouping, $search_team) {
foreach ($round as $pitch_no => $game) {
foreach (_get_teams($game) as $t => $team) {
if ($search_team == $team && $game['grouping'] == $grouping) {
return array(
'grouping' => $grouping,
'pitch' => $pitch_no,
);
}
}
}
}
function _get_teams($game) {
return array('t1' => $game['t1'], 't2' => $game['t2']);
}
function _get_team_base_name($name) {
$name = trim($name);
// Match the entire team name, if it ends up with a space and a capital
// letter.
if (preg_match('/^(.*)\s+([A-Za-z])$/', $name, $matches)) {
return $matches[1];
}
return $name;
}
function error($message) {
echo $message . "\n";
}