-
Notifications
You must be signed in to change notification settings - Fork 8
/
chess.php
526 lines (450 loc) · 16.7 KB
/
chess.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
// set the session cookie parameters so the cookie is only valid for this game
$parts = pathinfo($_SERVER['REQUEST_URI']);
$path = $parts['dirname'];
if (empty($parts['extension'])) {
$path .= '/'.$parts['basename'];
}
$path = str_replace('\\', '/', $path).'/';
session_set_cookie_params(0, $path);
@session_start( );
// load 'always needed' settings
require_once './includes/config.inc.php';
require_once './includes/html.inc.php';
// include outside functions
require_once './includes/chessconstants.inc.php';
require_once './includes/chessutils.inc.php';
require_once './includes/gui.inc.php';
require_once './includes/chessdb.inc.php';
//******************************************************************************
// load basic information
//******************************************************************************
// check if loading game
if (isset($_POST['game_id']))
{
$_SESSION['game_id'] = (int) $_POST['game_id'];
}
// make sure we have game id data
if (empty($_SESSION['game_id'])) {
header('Location: index.php');
exit;
}
if (isset($_SESSION['game_id']) || ! isset($_SESSION['white']))
{
// get White's data
$query = "
SELECT p_id
, p_username
, p_email
FROM ".T_PLAYER."
, ".T_GAME."
WHERE ".T_PLAYER.".p_id = ".T_GAME.".g_white_player_id
AND ".T_GAME.".g_id = '{$_SESSION['game_id']}'
";
$_SESSION['white'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);
// get Black's data
$query = "
SELECT p_id
, p_username
, p_email
FROM ".T_PLAYER."
, ".T_GAME."
WHERE ".T_PLAYER.".p_id = ".T_GAME.".g_black_player_id
AND ".T_GAME.".g_id = '{$_SESSION['game_id']}'
";
$_SESSION['black'] = $mysql->fetch_assoc($query, __LINE__, __FILE__);
// get players' color
if ($_SESSION['white']['p_username'] == $_SESSION['username'])
{
$_SESSION['player'] = &$_SESSION['white'];
$_SESSION['player']['p_color'] = 'white';
$_SESSION['opponent'] = &$_SESSION['black'];
$_SESSION['opponent']['p_color'] = 'black';
}
else
{
$_SESSION['player'] = &$_SESSION['black'];
$_SESSION['player']['p_color'] = 'black';
$_SESSION['opponent'] = &$_SESSION['white'];
$_SESSION['opponent']['p_color'] = 'white';
}
// get id960 and position
$query = "
SELECT g_id960
FROM ".T_GAME."
WHERE g_id = '{$_SESSION['game_id']}'
";
$_SESSION['id960'] = $mysql->fetch_value($query, __LINE__, __FILE__);
}
$initpos = id960_to_pos($_SESSION['id960']);
$promoting = false; // init the promotion flag
$undoing = false; // init the undo flag
// get FEN array (this should probably be in an include somewhere)
$i = 0;
$query = "
SELECT h_fen
FROM ".T_HISTORY."
WHERE h_game_id = '{$_SESSION['game_id']}'
ORDER BY h_time
";
$FENarray = $mysql->fetch_value_array($query, __LINE__, __FILE__);
$num_moves = count($FENarray) - 1; // remove one for initpos
loadGame( ); // sets up board using last entry in ".T_HISTORY." table (chessdb.inc.php)
FENtomoves( ); // creates movesArray from FENarray (chessutils.inc.php)
// find out if it's the current player's turn
$FENitems = explode(' ',$FENarray[$num_moves]);
$curTurn = $colorArray[$FENitems[1]]; // convert w -> white, b -> black
$isPlayersTurn = ($curTurn == $_SESSION['player']['p_color']) ? true : false;
//*/
//******************************************************************************
// save incoming information
//******************************************************************************
checkDatabase( ); // check the database data against the current FEN to make sure the game is ended properly (chessdb.inc.php)
processMessages( ); // processes the messages (undo, resign, etc) (chessdb.inc.php)
// are we undoing ?
if ($undoing && 0 < $num_moves)
{
call("UNDO REQUEST");
// just remove the last FEN entered into the history table
$query = "
SELECT MAX(h_time)
FROM ".T_HISTORY."
WHERE h_game_id = '{$_SESSION['game_id']}'
";
$max_time = $mysql->fetch_value($query, __LINE__, __FILE__);
$query = "
DELETE FROM ".T_HISTORY."
WHERE h_game_id = '{$_SESSION['game_id']}'
AND h_time = '{$max_time}'
LIMIT 1
";
$mysql->query($query, __LINE__, __FILE__);
if (!DEBUG) header("Location: ./chess.php");
}
// or saving the promotion
elseif ( isset($_POST['promotion']) && '' != $_POST['promotion'] && false != $_POST['promoting'] )
{
call("SAVING PROMOTION");
savePromotion( ); // inserts promoted piece and saves to database (chessdb.inc.php)
if (!DEBUG) header("Location: ./chess.php");
}
// or making a move
elseif ( ( isset($_POST['fromRow']) && '' != $_POST['fromRow'] && '' != $_POST['fromCol'] && '' != $_POST['toRow'] && '' != $_POST['toCol'] ) || ( isset($_POST['castleMove']) && 'false' != $_POST['castleMove'] ) )
{
call("MAKING A MOVE");
call($_POST);
call($_POST['fromRow']);
/* ensure it's the current player moving */
/* NOTE: if not, this will currently ignore the command... */
/* perhaps the status should be instead? */
/* (Could be confusing to player if they double-click or something */
$is_valid = true;
if ('white' == $curTurn) // white's move
{
call("WHITE");
call($board[$_POST['fromRow']][$_POST['fromCol']]);
// ensure that piece being moved isn't black (and is a piece)
if (('black' == $pieceColor[$board[$_POST['fromRow']][$_POST['fromCol']]]) || ('0' == $board[$_POST['fromRow']][$_POST['fromCol']]))
$is_valid = false; // if test passes, piece was black
}
else // black' move
{
call("BLACK");
call($pieceColor[$board[$_POST['fromRow']][$_POST['fromCol']]]);
// ensure that piece being moved isn't white (and is a piece)
if (("white" == $pieceColor[$board[$_POST['fromRow']][$_POST['fromCol']]]) || ('0' == $board[$_POST['fromRow']][$_POST['fromCol']]))
$is_valid = false; // if test passes, piece was white
}
if ($is_valid)
{
call("IS VALID");
saveGame( ); // (chessdb.inc.php)
// reload a fresh page to avoid errors
// and to display the new database data
if (!DEBUG) header("Location: ./chess.php");
}
}
// or we need to select the promoting piece
elseif ('P' == strtoupper($movesArray[$num_moves]['piece']) && ( ! isset($movesArray[$num_moves]['promo']) || null == $movesArray[$num_moves]['promo']))
{
if($movesArray[$num_moves]['toRow'] == 7 || $movesArray[$num_moves]['toRow'] == 0)
{
$promoting = true;
}
}
//*/
//******************************************************************************
// submit chat message
//******************************************************************************
if (isset($_POST['txtChatbox']) && ('' != $_POST['txtChatbox']))
{
$_POST = sani($_POST);
$private = (isset($_POST['private']) && 'on' == $_POST['private']) ? 'Yes' : 'No';
// select the last post entered and make sure it is not a IE error duplicate message
// (same message within 1 second)
$query = "
SELECT COUNT(*)
FROM ".T_CHAT."
WHERE c_message = '{$_POST['txtChatbox']}'
AND c_time BETWEEN
DATE_SUB(NOW( ), INTERVAL 1 SECOND)
AND DATE_ADD(NOW( ), INTERVAL 1 SECOND)
";
$count = $mysql->fetch_value($query, __LINE__, __FILE__);
if (0 == $count)
{
$query = "
INSERT INTO ".T_CHAT."
(c_game_id, c_player_id, c_time, c_message, c_private)
VALUES
('{$_SESSION['game_id']}', '{$_SESSION['player_id']}', NOW( ), '{$_POST['txtChatbox']}', '{$private}')
";
$mysql->query($query, __LINE__, __FILE__);
}
// refresh the page to avoid double posts
if (!DEBUG) header('Location: chess.php');
}
//*/
//******************************************************************************
// send wake up email
//******************************************************************************
$wake_up_sent = false;
if ( isset($_POST['wakeID']) && $_SESSION['game_id'] == $_POST['wakeID'] )
{
call("webchessMail('wakeup',{$_SESSION['opponent']['p_email']},'',{$_SESSION['username']},{$_SESSION['game_id']})");
$wake_up_sent = webchessMail('wakeup',$_SESSION['opponent']['p_email'],'',$_SESSION['username'],$_SESSION['game_id']);
}
//*/
//******************************************************************************
// load game from database for display
//******************************************************************************
// get FEN array
$query = "
SELECT h_fen
FROM ".T_HISTORY."
WHERE h_game_id = '{$_SESSION['game_id']}'
ORDER BY h_time
";
$FENarray = $mysql->fetch_value_array($query, __LINE__, __FILE__);
$num_moves = count($FENarray) - 1; // remove one for initpos
loadGame( ); // sets up board using last entry in ".T_HISTORY." table (chessdb.inc.php)
// convert the current FEN array to an array of standard moves
FENtomoves( ); // (chessutils.inc.php)
// find out if it's the current player's turn
$FENitems = explode(' ',$FENarray[$num_moves]);
$curTurn = $colorArray[$FENitems[1]];
$isPlayersTurn = ($curTurn == $_SESSION['player']['p_color']) ? true : false;
//*/
// set the display to show whos turn, or shared
if ($_SESSION['shared'])
{
$turn = "Shared";
}
elseif ($isPlayersTurn)
{
$turn = "Your Move";
}
else
{
$turn = "Opponent's Move";
}
$head_extra = '
<script type="text/javascript">//<![CDATA[
var watchgame = false;
function redo( )
{
window.location.replace(\'chess.php\');
}
';
// ouput confirmation for wake up email
if ($wake_up_sent)
{
$head_extra .= "alert('Wake Up email sent');\n ";
}
elseif (isset($_POST['wakeID']) && ! $wake_up_sent)
{
$head_extra .= "alert('Wake Up email FAILED !!');\n ";
}
// transfer game data to javacript vars
$head_extra .= getJSFEN( ); // writes 'FEN' array, and 'result' (gui.inc.php)
$head_extra .= getTurn( ); // writes 'isBoardDisabled', 'isPlayersTurn', and 'perspective' (gui.inc.php)
$head_extra .= getMoves( ); // writes the 'moves' array (gui.inc.php)
$head_extra .= getStatus( ); // writes 'whosMove', 'gameState', and 'statusMsg' (gui.inc.php)
$head_extra .= "var DEBUG = ".JS_DEBUG.";\n ";
$head_extra .= "var numMoves = FEN.length - 1;\n ";
// if it's not the player's turn, enable auto-refresh
$autoRefresh = ( ! $isPlayersTurn && ! isBoardDisabled( ) && ! $_SESSION['shared'] );
$head_extra .= "var autoreload = ";
if ( ! $autoRefresh || (0 == $CFG_MINAUTORELOAD) )
{
$head_extra .= "0";
}
elseif ( $_SESSION['pref_auto_reload'] >= $CFG_MINAUTORELOAD )
{
$head_extra .= $_SESSION['pref_auto_reload'];
}
else
{
$head_extra .= $CFG_MINAUTORELOAD;
}
$head_extra .= ";
var gameId = '{$_SESSION['game_id']}';
var players = '{$_SESSION['white']['p_username']} - {$_SESSION['black']['p_username']}';
var promoting = '{$promoting}';
var isGameOver = '{$isGameOver}';
var lastMoveIndicator = '{$_SESSION['pref_show_last_move']}';
var id960 = '{$_SESSION['id960']}';
var initpos = '{$initpos}';
";
$head_extra .= "var currentTheme = '";
$head_extra .= (isset($_SESSION['pref_theme']) ? $_SESSION['pref_theme'] : "plain") . '\';
//]]>
</script>
<!-- the \'variables\' javascript must come first !! -->
<script type="text/javascript" src="javascript/variables.js"></script>
<script type="text/javascript" src="javascript/chessutils.js"></script>
<script type="text/javascript" src="javascript/commands.js"></script>
<script type="text/javascript" src="javascript/validation.js"></script>
';
if ($isPlayersTurn || $_SESSION['shared'] || $promoting)
{
$head_extra .= "\n <script type=\"text/javascript\" src=\"javascript/isCheckMate.js\"></script>";
}
if ( ! isBoardDisabled( ) || $_SESSION['shared'])
{
$head_extra .= "\n <script type=\"text/javascript\" src=\"javascript/squareclicked.js\"></script>";
}
$head_extra .= '<script type="text/javascript" src="javascript/board.js"></script>
<script type="text/javascript" src="javascript/highlight.js"></script>
';
echo get_header(null, $turn, $head_extra)
?>
<div id="history">
<h2 id="players"></h2>
<h3 id="gameid"></h3>
<div id="gamebody"></div>
</div>
<div id="board">
<div id="checkmsg"></div>
<div id="statusmsg"></div>
<div id="date"><?php echo date($CFG_LONGDATE); ?></div>
<form name="gamedata" method="post" action="chess.php">
<?php
if ($promoting && ( ! $isPlayersTurn || $_SESSION['shared'])) // Write promotion dialog only to the correct player
{
echo getPromotion( );
}
if ($isUndoRequested)
{
echo getUndoRequest( );
}
if ($isDrawRequested)
{
echo getDrawRequest( );
}
?>
<div id="chessboard"></div>
<div id="gamebuttons">
<span id="castle">To castle: click the king, then the rook on the castle side.
<a href="#" class="help" onclick="window.open('./help/c960castling.html','help','resizable,scrollbars,width=550,height=500,top=50,left=50','_blank');return false;">?</a></span>
<div>
<span id="curmove"> </span>
<input type="button" id="btnUndo" class="button" value="Request Undo" disabled="disabled" />
<input type="button" id="btnDraw" class="button" value="Request Draw" disabled="disabled" />
<input type="button" id="btnResign" class="button" value="Resign" disabled="disabled" />
</div>
</div>
<input type="hidden" name="requestUndo" value="no" />
<input type="hidden" name="requestDraw" value="no" />
<input type="hidden" name="resign" value="no" />
<input type="hidden" name="fromRow" value="" />
<input type="hidden" name="fromCol" value="" />
<input type="hidden" name="toRow" value="" />
<input type="hidden" name="toCol" value="<?php if ($promoting) echo $movesArray[$num_moves]['toCol']; ?>" />
<input type="hidden" name="castleMove" value="false" />
<input type="hidden" name="promoting" value="<?php echo ($promoting ? 'true' : 'false'); ?>" />
</form>
<div id="gamenav"></div>
<form name="gamemenu" id="gamemenu" method="post" action="chess.php" style="display:inline;">
<input type="button" id="btnMainMenu" value="Menu" disabled="disabled" />
<input type="button" id="btnReload" value="Reload" disabled="disabled" />
<input type="button" id="btnReplay" value="Replay" disabled="disabled" />
<input type="button" id="btnPGN" value="PGN" disabled="disabled" />
</form>
<form name="wakeup" id="wakeup" method="post" action="chess.php" style="display:inline;">
<?php
// test for opponents email, and if none, disable the wake up button
$temp = ('' == $_SESSION['opponent']['p_email']) ? ' disabled="disabled"' : ''; // check the var and disable if no email is found
?>
<input type="button" id="btnWakeUp" value="Wake Up" onclick="wakeUp( );"<?php echo $temp; ?> /> <a href="#" class="help" onclick="window.open('./help/wakeup.html','help','resizable,scrollbars,width=550,height=500,top=50,left=50','_blank');return false;">?</a>
<input type="hidden" name="wakeID" value="<?php echo $_SESSION['game_id']; ?>" />
</form>
<div id="captheading">Captured pieces</div>
<div id="captures"></div>
</div>
<div id="chat">
<h2>chat</h2>
<div class="info">Newest messages on top</div>
<div id="chatholder">
<table class="chat">
<col />
<col class="message" />
<tr>
<th>Player</th>
<th>Message</th>
</tr><?php
// collect the public chat messages
$query = "
SELECT c_message
, c_private
, p_username
FROM ".T_CHAT."
LEFT JOIN ".T_PLAYER."
ON ".T_CHAT.".c_player_id = ".T_PLAYER.".p_id
WHERE c_game_id = '{$_SESSION['game_id']}'
AND (
(c_private = 'No')
";
// include private message data if game is not shared
if ('1' != $_SESSION['shared'])
{
$query .= "
OR (c_private='Yes'
AND ".T_CHAT.".c_player_id = '{$_SESSION['player_id']}')
";
}
$query .= "
)
ORDER BY c_time DESC
";
$result = $mysql->fetch_array($query, __LINE__, __FILE__);
$i = 0;
foreach ($result as $chat)
{
$alt = ' class="';
$alt .= (0 == $i % 2) ? ' alt' : '';
$alt .= ('Yes' == $chat['c_private']) ? ' mine' : '';
$alt .= ( $_SESSION['username'] != $chat['p_username'] ) ? ' opp' : '';
$alt .= '"';
echo "
<tr{$alt}>
<td class=\"player\">{$chat['p_username']}:</td>
<td>{$chat['c_message']}</td>
</tr>";
$i++;
}
?>
</table>
</div>
<form action="chess.php" method="post" name="chatdata" style="display:inline;">
<input type="text" name="txtChatbox" tabindex="2" onfocus="clearTimeout(intervalId);" onblur="if(''==this.value){intervalId = setTimeout('redo( )', autoreload * 1000);}" /><br />
<label for="private"><input type="checkbox" id="private" name="private" tabindex="1" />Private</label> <a href="#" class="help" onclick="window.open('./help/private.html','help','resizable,scrollbars,width=550,height=500,top=50,left=50','_blank');return false;">?</a>
<input type="submit" id="btnSubmit" name="chat" tabindex="3" value="Submit" />
</form>
</div>
</div>
<div id="footerspacer"> </div>
<div id="FENblock"></div>
<?php call($GLOBALS); ?>
</body>
</html>