/
chess.php
285 lines (245 loc) · 9.31 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
<?php
// $Id: chess.php,v 1.13 2010/08/18 09:38:56 sandking 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/>.
*/
session_start();
/* load settings */
if (!isset($_CONFIG))
require 'config.php';
/* define constants */
require 'chessconstants.php';
/* include outside functions */
if (!isset($_CHESSUTILS))
require 'chessutils.php';
require 'gui.php';
require 'chessdb.php';
require 'move.php';
require 'undo.php';
/* allow WebChess to be run on PHP systems < 4.1.0, using old http vars */
fixOldPHPVersions();
/* check session status */
require 'sessioncheck.php';
/* check if loading game */
if (isset($_POST['gameID']))
$_SESSION['gameID'] = $_POST['gameID'];
/* debug flag */
define ("DEBUG", 0);
/* connect to database */
require 'connectdb.php';
/* get White's nick */
$tmpNick = mysql_query("SELECT nick FROM " . $CFG_TABLE[players] . ", " . $CFG_TABLE[games] . " WHERE playerID = whitePlayer AND gameID = " . $_SESSION['gameID']);
$whiteNick = mysql_result($tmpNick, 0);
/* get Black's nick */
$tmpNick = mysql_query("SELECT nick FROM " . $CFG_TABLE[players] . ", " . $CFG_TABLE[games] . " WHERE playerID = blackPlayer AND gameID = " . $_SESSION['gameID']);
$blackNick = mysql_result($tmpNick, 0);
/* load game */
$isInCheck = ($_POST['isInCheck'] == 'true');
$isCheckMate = false;
$isPromoting = false;
$isUndoing = false;
loadHistory();
loadGame();
processMessages();
if ($isUndoing)
{
doUndo();
saveGame();
}
elseif (($_POST['promotion'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != ""))
{
savePromotion();
$board[$_POST['toRow']][$_POST['toCol']] = $_POST['promotion'] | ($board[$_POST['toRow']][$_POST['toCol']] & BLACK);
saveGame();
}
elseif (($_POST['fromRow'] != "") && ($_POST['fromCol'] != "") && ($_POST['toRow'] != "") && ($_POST['toCol'] != ""))
{
/* 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 */
$tmpIsValid = true;
if (($numMoves == -1) || ($numMoves % 2 == 1))
{
/* White's move... ensure that piece being moved is white */
if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != 0) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0))
/* invalid move */
$tmpIsValid = false;
}
else
{
/* Black's move... ensure that piece being moved is black */
if ((($board[$_POST['fromRow']][$_POST['fromCol']] & BLACK) != BLACK) || ($board[$_POST['fromRow']][$_POST['fromCol']] == 0))
/* invalid move */
$tmpIsValid = false;
}
if ($tmpIsValid)
{
saveHistory();
doMove();
saveGame();
}
}
elseif($history[$numMoves]['curPiece'] == 'pawn' && $history[$numMoves]['promotedTo'] == null)
{ // Incomplete promotion?
if($history[$numMoves]['toRow'] == 7 || $history[$numMoves]['toRow'] == 0)
{
$isPromoting = true;
}
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="pragma" content="no-cache" />
<link rel="stylesheet" href="chess2.css" type="text/css" />
<script type="text/javascript" src="fluidic.js"></script>
<link rel="stylesheet" href="chess.css" type="text/css" />
<?php
echo("<link rel='stylesheet' href='images/");
echo($_SESSION['pref_theme'] . "/wctheme.css' type='text/css' />\n");
/* 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;
if ($_SESSION['isSharedPC'])
echo("<title>iPad WebChess</title>\n");
else if ($isPlayersTurn)
echo("<title>iPad WebChess - Your Move</title>\n");
else
echo("<title>iPad WebChess - Opponent's Move</title>\n");
?>
<script type="text/javascript">
<?php
echo("var cfgImageExt = '$CFG_IMAGE_EXT';\n");
/* transfer board data to javacript */
writeJSboard();
/* if it's not the player's turn, enable auto-refresh */
$autoRefresh = !$isPlayersTurn && !isBoardDisabled() && !$_SESSION['isSharedPC'];
echo('var autoreload = ');
if(!$autoRefresh)
echo('0');
else if ($_SESSION['pref_autoreload'] >= $CFG_MINAUTORELOAD)
echo ($_SESSION['pref_autoreload']);
else
echo ($CFG_MINAUTORELOAD);
echo(";\n");
writeJShistory();
drawboard();
echo 'var gameId = ' . $_SESSION['gameID'] . ";\n";
echo 'var players = "' . $whiteNick . ' VS ' . $blackNick . "\";\n";
echo 'var playersColor = "' . $playersColor . "\";\n";
echo 'var isPromoting = "'.$isPromoting. "\";\n";
echo 'var isKingInCheck = "'.$isInCheck. "\";\n";
echo 'var isGameOver = "'.$isGameOver. "\";\n";
echo 'var historyLayout = "'.$_SESSION['pref_historylayout']. "\";\n";
writeStatus();
writeHistory();
require 'capt.php';
?>
</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>
<?php
if($isPlayersTurn || $_SESSION['isSharedPC'] || $isPromoting)
echo('
<script type="text/javascript" src="javascript/isCheckMate.js"></script>');
if(!isBoardDisabled() || $_SESSION['isSharedPC'])
echo('
<script type="text/javascript" src="javascript/squareclicked.js"></script>');
?>
<script type="text/javascript" src="javascript/board.js"></script>
</head>
<body>
<div id="wrapper">
<div id="innerWrapper">
<div class="panels" id="panel1">
<!-- Panel1 content goes inside here -->
<div id="content">
<div id="GameMenuButtons">
<form name="gamedata" method="post" action="chess.php">
<input type="button" value="Close" class="button" onclick="slideIn()"><br>
<input type="button" id="btnUndo" class="button" value="Undo" disabled="disabled" /><br>
<input type="button" id="btnDraw" class="button" value="Draw" disabled="disabled" /><br>
<input type="button" id="btnResign" class="button" value="Resign" disabled="disabled" />
<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="<?php if (isPromoting) echo ($_POST['fromRow']); ?>" />
<input type="hidden" name="fromCol" value="<?php if (isPromoting) echo ($_POST['fromCol']); ?>" />
<input type="hidden" name="toRow" value="<?php if (isPromoting) echo ($_POST['toRow']); ?>" />
<input type="hidden" name="toCol" value="<?php if (isPromoting) echo ($_POST['toCol']); ?>" />
<input type="hidden" name="isInCheck" value="false" />
<input type="hidden" name="isCheckMate" value="false" />
</div>
</form>
<div id="players" class="move_header"></div>
<div id="gameid" class="move_header"></div>
<div id="castle">
<p>When castling, just move the king<br> (the rook moves automatically).</p>
</div>
<div id="gamebody" class="move_header"></div>
<br><div id="captheading">Captured pieces</div>
<div id="captures"></div>
</div>
</div>
<div class="panels" id="panel2">
<!-- Panel2 content goes inside here -->
<div id="wrapper2">
<div id="NavMenuButtons">
<form name="gamemenu" method="post" action="chess.php">
<input type="button" value="Game Menu" class="button" onclick="slideOut()" />
<input type="button" id="btnMainMenu" class="button" value="Main Menu" disabled="disabled" />
<input type="button" id="btnReload" class="button" value="Reload" disabled="disabled" />
<input type="button" id="btnPGN" class="button" value="PGN" disabled="disabled" />
<input type="button" id="btnLogout" class="button" value="Logout" disabled="disabled" />
<input type="hidden" name="ToDo" value="Logout" /> <!-- NOTE: this field is only used to Logout -->
</form>
<div id="heading">
<div id="moveinfo">
<div id="whosmove"> </div>
<div id="curmove"> </div>
</div>
</div>
</div>
<div id="boardsection">
<form name="gamedata2" method="post" action="chess.php">
<?php
if ($isPromoting && (!$isPlayersTurn || $_SESSION['isSharedPC'])) // Write promotion dialog only to the correct player
writePromotion();
if ($isUndoRequested)
writeUndoRequest();
if ($isDrawRequested)
writeDrawRequest();
?>
</form>
<div id="checkmsg"></div>
<div id="statusmsg"></div>
<div id="chessboard"></div>
</div>
<div id="footer">
<p>.</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>