-
Notifications
You must be signed in to change notification settings - Fork 1
/
updmatch2.php
271 lines (234 loc) · 6.82 KB
/
updmatch2.php
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
<?php
// Copyright 2011-2021 John Collins
// *********************************************************************
// Please do not edit the live file directly as it will break the "Git"
// mechanism to update the live files automatically when a new version
// is pushed. Thanks!
// *********************************************************************
// 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 3 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.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
include 'php/html_blocks.php';
include 'php/error_handling.php';
include 'php/connection.php';
include 'php/opendatabase.php';
include 'php/club.php';
include 'php/rank.php';
include 'php/player.php';
include 'php/team.php';
include 'php/teammemb.php';
include 'php/match.php';
include 'php/matchdate.php';
include 'php/game.php';
include 'php/sortrank.php';
include 'php/params.php';
include 'php/hcp_message.php';
include 'php/mailalloc.php';
$Connection = opendatabase(true);
$pars = new Params();
$pars->fetchvalues();
$mtch = new Match();
try {
$mtch->frompost();
$mtch->fetchdets();
$mtch->fetchteams();
$mtch->fetchgames();
}
catch (MatchException $e) {
wrongentry($e->getMessage());
}
// Set if handicaps apply to this division
$hcapable = $mtch->Division >= $pars->Hdiv;
$hred = $pars->Hreduct;
// If colours are set up don't change anything
$colset = false;
$hadnig = false;
$hadw = false;
$hadb = false;
$forcing = isset($_POST["forceass"]);
try {
for ($b = 0; $b < 3; $b++) {
$h = new Player();
$a = new Player();
$hn = $_POST["htm$b"];
$an = $_POST["atm$b"];
if (strlen($hn) == 0)
throw new PlayerException("Home team $b is null");
if (strlen($an) == 0)
throw new PlayerException("Away team $b is null");
$h->fromsel($hn);
$a->fromsel($an);
$h->fetchdets();
$a->fetchdets();
$cols[$b] = $_POST["colours$b"];
switch ($cols[$b]) {
default:
$hadnig = true;
break;
case 1: // Home player white
$hadw = true;
$colset = true;
break;
case 2: // Home player black
$hadb = true;
$colset = true;
break;
}
$hplayer[$b] = $h;
$aplayer[$b] = $a;
}
}
catch (PlayerException $e) {
wrongentry($e->getMessage());
}
if ($forcing && (!$colset || $hadnig))
wrongentry("Colours not set up");
// If colours not set, sort players into rank order and assign
// otherwise set nigiri randomly
if ($colset) {
// Colours set - if had nigiri try to make at least one different
if ($hadnig) {
if ($hadw && $hadb) {
// Had both white and black - if middle one nigiri choose at random
// Otherwise make nigiri one opposite of middle one
if ($cols[1] <= 1)
$cols[1] = rand(1,2);
elseif ($cols[0] <= 1)
$cols[0] = 3 - $cols[1];
else
$cols[2] = 3 - $cols[1];
}
elseif ($hadw) {
// White set set nigiri ones to black
for ($b = 0; $b < 3; $b++)
if ($cols[$b] <= 1)
$cols[$b] = 2;
}
else {
// Black set set nigiri ones to white
for ($b = 0; $b < 3; $b++)
if ($cols[$b] <= 1)
$cols[$b] = 1;
}
}
}
else {
// So now we sort each team into order
sortrank($hplayer, $pars->Rankfuzz);
sortrank($aplayer, $pars->Rankfuzz);
// Now assign colours either WBW or BWB
$cols[0] = rand(1,2);
$cols[1] = 3 - $cols[0];
$cols[2] = $cols[0];
}
// If matchdate has changed, fix it
$newdate = new Matchdate();
$newdate->frompost();
$newslack = $_POST["slackd"];
// Fix changed date
if ($newdate->unequal($mtch->Date) || $newslack != $mtch->Slackdays) {
$mtch->Date = $newdate;
$mtch->Slackdays = $newslack;
$mtch->dateupdate();
}
// Set up game details according to who is white/black
// col=1 means "home" player is white otherwise black
function setgteams($g, $col, $hteam, $ateam, $hplay, $aplay) {
global $hcapable, $hred;
// Force colour to white for stronger player if handicaps apply and rank
// difference is greater than handicap reduction
if ($hcapable) {
if ($hplay->Rank->Rankvalue - $aplay->Rank->Rankvalue > $hred)
$col = 1;
elseif ($aplay->Rank->Rankvalue - $hplay->Rank->Rankvalue > $hred)
$col = 0;
}
if ($col == 1) {
$g->Wteam = $hteam;
$g->Bteam = $ateam;
$g->Wplayer = $hplay;
$g->Bplayer = $aplay;
}
else {
$g->Wteam = $ateam;
$g->Bteam = $hteam;
$g->Wplayer = $aplay;
$g->Bplayer = $hplay;
}
}
// Get and create or update each game
$gnum = $mtch->ngames();
for ($gm = 0; $gm < 3; $gm++) {
if ($gm < $gnum) {
// Game already allocated
$g = $mtch->Games[$gm];
// Don't try to change any played games
if ($g->Result == 'N') {
setgteams($g, $cols[$gm], $mtch->Hteam, $mtch->Ateam, $hplayer[$gm], $aplayer[$gm]);
$g->update_players();
}
}
else {
$g = $mtch->newgame();
setgteams($g, $cols[$gm], $mtch->Hteam, $mtch->Ateam, $hplayer[$gm], $aplayer[$gm]);
$g->create_game();
}
}
lg_html_header("Match edit result");
lg_html_nav();
print <<<EOT
<h1>Match Edit Result</h1>
<p>Match is set up between {$mtch->Hteam->display_name()} ({$mtch->Hteam->display_description()})
and {$mtch->Ateam->display_name()} ({$mtch->Ateam->display_description()})
on {$mtch->Date->display()} with {$mtch->Slackdays} days to play the games.</p>
<p>Team captains are {$mtch->Hteam->display_captain()} for {$mtch->Hteam->display_name()}
and {$mtch->Ateam->display_captain()} for {$mtch->Ateam->display_name()}.</p>
<p>Player assignments are as follows:</p>
<table>
<tr><th colspan="4" align="center">White</th><th colspan="4" align="center">Black</th><th>Result</th></tr>
<tr><th>Player</th><th>Rank</th><th>Online</th><th>Team</th><th>Player</th><th>Rank</th><th>Online</th><th>Team</th></tr>
EOT;
foreach ($mtch->Games as $g) {
switch ($g->Result) {
default:
$res = ' ';
break;
case 'W':
$res = "White Win";
break;
case 'J':
$res = "Jigo";
break;
case 'B':
$res = "Black Win";
break;
}
print <<<EOT
<tr>
<td>{$g->Wplayer->display_name()}</td>
<td>{$g->Wplayer->display_rank()}</td>
<td>{$g->Wplayer->display_online()}</td>
<td>{$g->Wteam->display_name()}</td>
<td>{$g->Bplayer->display_name()}</td>
<td>{$g->Bplayer->display_rank()}</td>
<td>{$g->Bplayer->display_online()}</td>
<td>{$g->Bteam->display_name()}</td>
<td>$res</td>
</tr>
EOT;
}
mail_allocated($mtch, $pars, true);
print <<<EOT
</table>
<p>Click <a href="updmatch.php?{$mtch->urlof()}">here</a> to change any details of the match.</p>
<p>Click <a href="matchupd.php">here</a> to edit some other match.</p>
EOT;
lg_html_footer();
?>