forked from Thorium/webchess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.php
438 lines (385 loc) · 11.7 KB
/
gui.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
// $Id: gui.php,v 1.14 2013/12/07 20:00:00 gitjake Exp $
/*
This file is part of WebChess. http://webchess.sourceforge.net
Copyright 2010 Jonathan Evraire, Rodrigo Flores
WebChess 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.
WebChess 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 WebChess. If not, see <http://www.gnu.org/licenses/>.
*/
/* functions for outputting to html and javascript */
require('chess.inc');
function drawboard()
{
global $board, $playersColor, $numMoves;
global $CFG_BOARDSQUARESIZE;
/* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */
if (!minimum_version("4.1.0"))
global $_POST, $_GET, $_SESSION;
/* find out if it's the current player's turn */
if (( (($numMoves == -1) || (($numMoves % 2) == 1)) && ($playersColor == "white"))
|| ((($numMoves % 2) == 0) && ($playersColor == "black")) )
$isPlayersTurn = true;
else
$isPlayersTurn = false;
/* determine who's perspective of the board to show */
if ($_SESSION['isSharedPC'] && !$isPlayersTurn)
{
if ($playersColor == "white")
$perspective = "black";
else
$perspective = "white";
}
else
{
$perspective = $playersColor;
}
/* NOTE: if both players are using the same PC, in a sense it's always the players turn */
if ($_SESSION['isSharedPC'])
$isPlayersTurn = true;
/* determine if board is disabled */
$isDisabled = isBoardDisabled();
echo ("var isBoardDisabled = '" . $isDisabled . "';\n");
echo ("var isPlayersTurn = '" . $isPlayersTurn . "';\n");
echo ("var perspective = '" . $perspective . "';\n");
echo ("var squareSize = " . $CFG_BOARDSQUARESIZE . ";\n");
}
function writeJSboard()
{
global $board, $numMoves;
/* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */
if (!minimum_version("4.1.0"))
global $_POST, $_GET, $_SESSION;
/* write out constants */
echo ("var DEBUG = ".DEBUG.";\n");
echo ("var CURRENTTHEME = '".$_SESSION['pref_theme']."';\n");
echo ("var PAWN = ".PAWN.";\n");
echo ("var KNIGHT = ".KNIGHT.";\n");
echo ("var BISHOP = ".BISHOP.";\n");
echo ("var ROOK = ".ROOK.";\n");
echo ("var QUEEN = ".QUEEN.";\n");
echo ("var KING = ".KING.";\n");
echo ("var BLACK = ".BLACK.";\n");
echo ("var WHITE = ".WHITE.";\n");
echo ("var COLOR_MASK = ".COLOR_MASK.";\n");
/* write code for array */
echo ("var board = [");
for ($i = 0; $i < 8; $i++)
{
echo ("\n[");
for ($j = 0; $j < 8; $j++)
{
echo ($board[$i][$j]);
if($j < 7)
echo (', ');
}
echo ("]");
if($i < 7)
{
echo (",");
}
}
echo "];\n";
echo("var numMoves = $numMoves;\n");
echo("var errMsg = '';\n"); /* global var used for error messages */
}
/* provide history data to javascript function */
/* NOTE: currently, only pawn validation script uses history */
function writeJSHistory()
{
global $history, $numMoves;
/* write out constants */
echo ("var CURPIECE = 0;\n");
echo ("var CURCOLOR = 1;\n");
echo ("var FROMROW = 2;\n");
echo ("var FROMCOL = 3;\n");
echo ("var TOROW = 4;\n");
echo ("var TOCOL = 5;\n");
echo ("var PROMOTEDTO = 6;\n");
/* write code for array */
echo ("var chessHistory = [");
for ($i = 0; $i <= $numMoves; $i++)
{
if($i % 4 == 0) // Four moves on each line
echo("\n");
echo ("['".$history[$i]['curPiece']."', ");
echo ("'".$history[$i]['curColor']."', ");
echo ($history[$i]['fromRow'].", ");
echo ($history[$i]['fromCol'].", ");
echo ($history[$i]['toRow'].", ");
echo ($history[$i]['toCol']);
if($history[$i]['promotedTo'] != '')
{
echo (", '".$history[$i]['promotedTo']."'");
}
echo ("]");
if($i < $numMoves)
{
echo (",");
}
}
echo "];\n";
}
function writeVerbousHistory()
{
global $history, $numMoves;
for ($i = 0; $i <= $numMoves; $i++)
{
$tmpReplaced = "";
if (!is_null($history[$i]['replaced']))
$tmpReplaced = $history[$i]['replaced'];
$tmpPromotedTo = "";
if (!is_null($history[$i]['promotedTo']))
$tmpPromotedTo = $history[$i]['promotedTo'];
$tmpCheck = ($history[$i]['isInCheck'] == 1);
$moves[$i/2][$i & 1] = moveToVerbousString($history[$i]['curColor'], $history[$i]['curPiece'], $history[$i]['fromRow'], $history[$i]['fromCol'], $history[$i]['toRow'], $history[$i]['toCol'], $tmpReplaced, $tmpPromotedTo, $tmpCheck);
}
return $moves;
}
function writeHistoryPGN()
{
global $history, $numMoves;
$moves = array();
for ($i = 0; $i <= $numMoves; $i++)
{
$tmpReplaced = "";
if (!is_null($history[$i]['replaced']))
$tmpReplaced = $history[$i]['replaced'];
$tmpPromotedTo = "";
if (!is_null($history[$i]['promotedTo']))
$tmpPromotedTo = $history[$i]['promotedTo'];
$tmpCheck = ($history[$i]['isInCheck'] == 1);
$moves[$i/2][$i & 1] = moveToPGNString($history[$i]['curColor'], $history[$i]['curPiece'], $history[$i]['fromRow'], $history[$i]['fromCol'], $history[$i]['toRow'], $history[$i]['toCol'], $tmpReplaced, $tmpPromotedTo, $tmpCheck);
}
return $moves;
}
function writeHistory()
{
global $numMoves;
/* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */
if (!minimum_version("4.1.0"))
global $_POST, $_GET, $_SESSION;
/* based on player's preferences, display the history */
$moves = array(); // Make sure that $moves is defined
switch($_SESSION['pref_history'])
{
case 'verbous':
$moves = writeVerbousHistory();
break;
case 'pgn':
$moves = writeHistoryPGN();
break;
}
$comma = '';
echo("var moves = [");
for ($i = 0; $i < count($moves); $i++)
{
echo($comma);
if($i % 4 == 0) // Four moves on each line
echo("\n");
$moves2 = '';
if (isset($moves[$i][1])) {
$moves2 = $moves[$i][1];
}
echo ("['" . $moves[$i][0]."', '".$moves2."']");
$comma = ', ';
}
echo("];\n");
}
function writeStatus()
{
global $numMoves, $history, $isCheckMate, $statusMessage, $isPlayersTurn;
if (($numMoves == -1) || ($numMoves % 2 == 1))
$curColor = "White";
else
$curColor = "Black";
if ($_SESSION['isSharedPC'])
echo ("var whosMove = '$curColor\'s Turn';\n");
elseif ($isPlayersTurn)
echo ("var whosMove = 'Your Turn';\n");
else
echo("var whosMove = 'Opponent\'s Turn';\n");
echo("var checkMsg = '");
if (!$isCheckMate && ($history[$numMoves]['isInCheck'] == 1))
echo($curColor." is currently in check!");
echo("';\n");
echo("var statusMessage = '".$statusMessage."';\n");
}
function writePromotion()
{
?>
<p>
<table width="435" border="1">
<tr><td>
Promote pawn to:
<br>
<input type="radio" name="promotion" value="<?php echo (QUEEN); ?>" checked="checked"> Queen
<input type="radio" name="promotion" value="<?php echo (ROOK); ?>"> Rook
<input type="radio" name="promotion" value="<?php echo (KNIGHT); ?>"> Knight
<input type="radio" name="promotion" value="<?php echo (BISHOP); ?>"> Bishop
<input type="button" name="btnPromote" value="Promote" onClick="promotepawn()" />
</td></tr>
</table>
</p>
<?php
}
function writeUndoRequest()
{
?>
<p>
<table width="435" border="1">
<tr><td>
Your opponent would like to undo their latest move. Will you allow it?
<br>
<input type="radio" name="undoResponse" value="yes"> Yes
<input type="radio" name="undoResponse" value="no" checked="checked"> No
<input type="hidden" name="isUndoResponseDone" value="no">
<input type="button" value="Reply" onClick="this.form.isUndoResponseDone.value = 'yes'; this.form.submit()">
</td></tr>
</table>
</p>
<?php
}
function writeDrawRequest()
{
?>
<p>
<table width="435" border="1">
<tr><td>
Your opponent is proposing a draw. Do you agree?
<br>
<input type="radio" name="drawResponse" value="yes"> Yes
<input type="radio" name="drawResponse" value="no" checked="checked"> No
<input type="hidden" name="isDrawResponseDone" value="no">
<input type="button" value="Reply" onClick="this.form.isDrawResponseDone.value = 'yes'; this.form.submit()">
</td></tr>
</table>
</p>
<?php
}
function writePGN()
{
require 'connectdb.php';
global $_SESSION, $history, $numMoves,$pWhite,$pWhiteF,$pWhiteL,$pBlack,$pBlackF,$pBlackL,$gStart;
global $simplemove,$isDraw,$move_turn,$move_PlyNumber;
$FEN="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
set_FEN($FEN);
#get_current_Position();
#return;
$xheader ='[Event "' . APP_NAME . " Board #{$_SESSION['gameID']}\"]\n";
$xheader .='[Site "' . APP_NAME . "\"]\n";
$xheader .="[Date \"$gStart\"]\n";
$xheader .="[Round \"-\"]\n";
$xheader .="[White \"$pWhiteL, $pWhiteF\"]\n";
$xheader .="[Black \"$pBlackL, $pBlackF\"]\n";
$body = "";
$bodyLine = "";
for ($i = 0; $i <= $numMoves; $i+=2)
{
// White's move
$token = "".(($i/2)+1).".";
if(strlen($bodyLine) + strlen($token) > 79)
{
$body .= $bodyLine . "\n";
$bodyLine = "";
}
elseif(strlen($bodyLine) > 0)
{
$bodyLine .= " ";
}
$bodyLine .= $token;
$tmpPromotedTo = "";
if (!is_null($history[$i]['promotedTo']))
$tmpPromotedTo = $history[$i]['promotedTo'];
$tmpCheck = ($history[$i]['isInCheck'] == 1);
$fr=$history[$i]['fromRow'];
$fc=$history[$i]['fromCol'];
$tr=$history[$i]['toRow'];
$tc=$history[$i]['toCol'];
$fs=coordsToSquare($fr,$fc);
$ts=coordsToSquare($tr,$tc);
#echo "<hr>Adding Move:$fs-$ts$tmpPromotedTo<hr>";
add_Move("$fs-$ts".getPGNCode($tmpPromotedTo));
#echo get_current_Position();
$token = $simplemove;
if(strlen($bodyLine) + strlen($token) > 79)
{
$body .= $bodyLine . "\n";
$bodyLine = "";
}
elseif(strlen($bodyLine) > 0)
{
$bodyLine .= " ";
}
$bodyLine .= $token;
if ($i != $numMoves)
{ // Black's move
$tmpPromotedTo = "";
if (!is_null($history[$i+1]['promotedTo']))
$tmpPromotedTo = $history[$i+1]['promotedTo'];
$tmpCheck = ($history[$i+1]['isInCheck'] == 1);
$fr=$history[$i+1]['fromRow'];
$fc=$history[$i+1]['fromCol'];
$tr=$history[$i+1]['toRow'];
$tc=$history[$i+1]['toCol'];
$fs=coordsToSquare($fr,$fc);
$ts=coordsToSquare($tr,$tc);
#echo "<hr>Adding Move:$fs-$ts$tmpPromotedTo<hr>";
add_Move("$fs-$ts".getPGNCode($tmpPromotedTo));
#echo get_current_Position();
$token = $simplemove;
if(strlen($bodyLine) + strlen($token) > 79)
{
$body .= $bodyLine . "\n";
$bodyLine = "";
}
elseif(strlen($bodyLine) > 0)
{
$bodyLine .= " ";
}
$bodyLine .= $token;
}
}
/* getResult() returns only definitive wins and remis.
* If a player resigns, this is not reflected in it, so we need to check
*/
$result=getResult();
if($isDraw){
$result="1/2-1/2";
};
$tmpQuery = "SELECT gameMessage, messageFrom FROM " . $CFG_TABLE[games] . " WHERE gameID = ".$_SESSION['gameID'];
$tmpMessages = mysql_query($tmpQuery);
$tmpMessage = mysql_fetch_array($tmpMessages, MYSQL_ASSOC);
if ($tmpMessage['gameMessage'] == "playerResigned")
{
if ( $tmpMessage['messageFrom'] == "white" )
{
$result = "0-1";
}
else
{
$result = "1-0";
}
}
$body .= $bodyLine;
if(strlen($bodyLine) + strlen($result) > 79)
{
$body .= "\n";
}
elseif(strlen($bodyLine) > 0)
{
$body .= " ";
}
$body.= $result . "\n\n";
$xheader .="[Result \"$result\"]\n\n";
echo ($xheader);
echo ($body);
}
?>