| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
global $gs_version; |
| 6 |
$gs_version = "0.3"; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
// edit settings.php to change server' settings |
| 11 |
include( "settings.php" ); |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
// no end-user settings below this point |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
// enable verbose error reporting to detect uninitialized variables |
| 22 |
error_reporting( E_ALL ); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
// page layout for web-based setup |
| 27 |
$setup_header = " |
| 28 |
<HTML> |
| 29 |
<HEAD><TITLE>Game 7 Server Web-based setup</TITLE></HEAD> |
| 30 |
<BODY BGCOLOR=#FFFFFF TEXT=#000000 LINK=#0000FF VLINK=#FF0000> |
| 31 |
|
| 32 |
<CENTER> |
| 33 |
<TABLE WIDTH=75% BORDER=0 CELLSPACING=0 CELLPADDING=1> |
| 34 |
<TR><TD BGCOLOR=#000000> |
| 35 |
<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=10> |
| 36 |
<TR><TD BGCOLOR=#EEEEEE>"; |
| 37 |
|
| 38 |
$setup_footer = " |
| 39 |
</TD></TR></TABLE> |
| 40 |
</TD></TR></TABLE> |
| 41 |
</CENTER> |
| 42 |
</BODY></HTML>"; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
// ensure that magic quotes are on (adding slashes before quotes |
| 49 |
// so that user-submitted data can be safely submitted in DB queries) |
| 50 |
if( !get_magic_quotes_gpc() ) { |
| 51 |
// force magic quotes to be added |
| 52 |
$_GET = array_map( 'gs_addslashes_deep', $_GET ); |
| 53 |
$_POST = array_map( 'gs_addslashes_deep', $_POST ); |
| 54 |
$_REQUEST = array_map( 'gs_addslashes_deep', $_REQUEST ); |
| 55 |
$_COOKIE = array_map( 'gs_addslashes_deep', $_COOKIE ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
// all calls need to connect to DB, so do it once here |
| 64 |
gs_connectToDatabase(); |
| 65 |
|
| 66 |
// close connection down below (before function declarations) |
| 67 |
|
| 68 |
|
| 69 |
// testing: |
| 70 |
//sleep( 5 ); |
| 71 |
|
| 72 |
|
| 73 |
// general processing whenver server.php is accessed directly |
| 74 |
|
| 75 |
|
| 76 |
gs_checkForFlush(); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
// grab POST/GET variables |
| 81 |
$action = ""; |
| 82 |
if( isset( $_REQUEST[ "action" ] ) ) { |
| 83 |
$action = $_REQUEST[ "action" ]; |
| 84 |
} |
| 85 |
|
| 86 |
$debug = ""; |
| 87 |
if( isset( $_REQUEST[ "debug" ] ) ) { |
| 88 |
$debug = $_REQUEST[ "debug" ]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if( $action == "version" ) { |
| 94 |
global $gs_version; |
| 95 |
echo "$gs_version"; |
| 96 |
} |
| 97 |
else if( $action == "show_log" ) { |
| 98 |
gs_showLog(); |
| 99 |
} |
| 100 |
else if( $action == "clear_log" ) { |
| 101 |
gs_clearLog(); |
| 102 |
} |
| 103 |
else if( $action == "get_player_id" ) { |
| 104 |
gs_getPlayerID(); |
| 105 |
} |
| 106 |
else if( $action == "create_for_friend" ) { |
| 107 |
gs_createForFriend(); |
| 108 |
} |
| 109 |
else if( $action == "join_with_friend" ) { |
| 110 |
gs_joinWithFriend(); |
| 111 |
} |
| 112 |
else if( $action == "join_with_stranger" ) { |
| 113 |
gs_joinWithStranger(); |
| 114 |
} |
| 115 |
else if( $action == "is_partner_ready" ) { |
| 116 |
gs_isPartnerReady(); |
| 117 |
} |
| 118 |
else if( $action == "report_alive" ) { |
| 119 |
gs_reportAlive(); |
| 120 |
} |
| 121 |
else if( $action == "post_move" ) { |
| 122 |
gs_postMove(); |
| 123 |
} |
| 124 |
else if( $action == "get_changed_columns" ) { |
| 125 |
gs_getChangedColumns(); |
| 126 |
} |
| 127 |
else if( $action == "gs_setup" ) { |
| 128 |
global $setup_header, $setup_footer; |
| 129 |
echo $setup_header; |
| 130 |
|
| 131 |
echo "<H2>Game 7 Server Web-based Setup</H2>"; |
| 132 |
|
| 133 |
echo "Creating tables:<BR>"; |
| 134 |
|
| 135 |
echo "<CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1> |
| 136 |
<TR><TD BGCOLOR=#000000> |
| 137 |
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5> |
| 138 |
<TR><TD BGCOLOR=#FFFFFF>"; |
| 139 |
|
| 140 |
gs_setupDatabase(); |
| 141 |
|
| 142 |
echo "</TD></TR></TABLE></TD></TR></TABLE></CENTER><BR><BR>"; |
| 143 |
|
| 144 |
echo $setup_footer; |
| 145 |
} |
| 146 |
else if( preg_match( "/server\.php/", $_SERVER[ "SCRIPT_NAME" ] ) ) { |
| 147 |
// server.php has been called without an action parameter |
| 148 |
|
| 149 |
// the preg_match ensures that server.php was called directly and |
| 150 |
// not just included by another script |
| 151 |
|
| 152 |
// quick (and incomplete) test to see if we should show instructions |
| 153 |
global $tableNamePrefix; |
| 154 |
|
| 155 |
// check if our "games" table exists |
| 156 |
$tableName = $tableNamePrefix . "games"; |
| 157 |
|
| 158 |
$exists = gs_doesTableExist( $tableName ); |
| 159 |
|
| 160 |
if( $exists ) { |
| 161 |
echo "Game 7 server database setup and ready"; |
| 162 |
} |
| 163 |
else { |
| 164 |
// start the setup procedure |
| 165 |
|
| 166 |
global $setup_header, $setup_footer; |
| 167 |
echo $setup_header; |
| 168 |
|
| 169 |
echo "<H2>Game 7 Server Web-based Setup</H2>"; |
| 170 |
|
| 171 |
echo "Game 7 Server will walk you through a " . |
| 172 |
"brief setup process.<BR><BR>"; |
| 173 |
|
| 174 |
echo "Step 1: ". |
| 175 |
"<A HREF=\"server.php?action=gs_setup\">". |
| 176 |
"create the database tables</A>"; |
| 177 |
|
| 178 |
echo $setup_footer; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
// done processing |
| 185 |
// only function declarations below |
| 186 |
|
| 187 |
gs_closeDatabase(); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Creates the database tables needed by seedBlogs. |
| 197 |
*/ |
| 198 |
function gs_setupDatabase() { |
| 199 |
global $tableNamePrefix; |
| 200 |
|
| 201 |
|
| 202 |
$tableName = $tableNamePrefix . "server_globals"; |
| 203 |
if( ! gs_doesTableExist( $tableName ) ) { |
| 204 |
|
| 205 |
// this table contains general info about the server |
| 206 |
// use INNODB engine so table can be locked |
| 207 |
$query = |
| 208 |
"CREATE TABLE $tableName(" . |
| 209 |
"next_player_id CHAR(7) NOT NULL, ". |
| 210 |
"last_flush_time DATETIME NOT NULL ) ENGINE = INNODB;"; |
| 211 |
|
| 212 |
$result = gs_queryDatabase( $query ); |
| 213 |
|
| 214 |
echo "<B>$tableName</B> table created<BR>"; |
| 215 |
} |
| 216 |
else { |
| 217 |
echo "<B>$tableName</B> table already exists<BR>"; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
$tableName = $tableNamePrefix . "log"; |
| 222 |
if( ! gs_doesTableExist( $tableName ) ) { |
| 223 |
|
| 224 |
// this table contains general info about the server |
| 225 |
// use INNODB engine so table can be locked |
| 226 |
$query = |
| 227 |
"CREATE TABLE $tableName(" . |
| 228 |
"entry TEXT NOT NULL, ". |
| 229 |
"entry_time DATETIME NOT NULL );"; |
| 230 |
|
| 231 |
$result = gs_queryDatabase( $query ); |
| 232 |
|
| 233 |
echo "<B>$tableName</B> table created<BR>"; |
| 234 |
} |
| 235 |
else { |
| 236 |
echo "<B>$tableName</B> table already exists<BR>"; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
$tableName = $tableNamePrefix . "games"; |
| 242 |
if( ! gs_doesTableExist( $tableName ) ) { |
| 243 |
|
| 244 |
// this table contains general info about each game |
| 245 |
// use INNODB engine so table can be locked |
| 246 |
$query = |
| 247 |
"CREATE TABLE $tableName(" . |
| 248 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 249 |
"game_passcode CHAR(32) NOT NULL," . |
| 250 |
"private TINYINT NOT NULL," . |
| 251 |
"creation_date DATETIME NOT NULL," . |
| 252 |
"touch_date DATETIME NOT NULL," . |
| 253 |
"player_1_id CHAR(7) NOT NULL," . |
| 254 |
"player_2_id CHAR(7) NOT NULL," . |
| 255 |
"player_1_transform CHAR(37) NOT NULL," . |
| 256 |
"player_2_transform CHAR(37) NOT NULL," . |
| 257 |
"player_1_ready TINYINT NOT NULL," . |
| 258 |
"player_2_ready TINYINT NOT NULL," . |
| 259 |
"player_1_touch_date DATETIME NOT NULL," . |
| 260 |
"player_2_touch_date DATETIME NOT NULL," . |
| 261 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 262 |
|
| 263 |
$result = gs_queryDatabase( $query ); |
| 264 |
|
| 265 |
echo "<B>$tableName</B> table created<BR>"; |
| 266 |
} |
| 267 |
else { |
| 268 |
echo "<B>$tableName</B> table already exists<BR>"; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
$tableName = $tableNamePrefix . "columns"; |
| 273 |
if( ! gs_doesTableExist( $tableName ) ) { |
| 274 |
|
| 275 |
// this table contains information for each user |
| 276 |
$query = |
| 277 |
"CREATE TABLE $tableName(" . |
| 278 |
"game_id CHAR(7) NOT NULL," . |
| 279 |
"column_index TINYINT NOT NULL," . |
| 280 |
"blocks TEXT NOT NULL," . |
| 281 |
"changed_in_state INT NOT NULL, ". |
| 282 |
"PRIMARY KEY( game_id, column_index ) );"; |
| 283 |
|
| 284 |
$result = gs_queryDatabase( $query ); |
| 285 |
|
| 286 |
echo "<B>$tableName</B> table created<BR>"; |
| 287 |
} |
| 288 |
else { |
| 289 |
echo "<B>$tableName</B> table already exists<BR>"; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
function gs_showLog() { |
| 296 |
$password = ""; |
| 297 |
if( isset( $_REQUEST[ "password" ] ) ) { |
| 298 |
$password = $_REQUEST[ "password" ]; |
| 299 |
} |
| 300 |
|
| 301 |
global $logAccessPassword, $tableNamePrefix; |
| 302 |
|
| 303 |
if( $password != $logAccessPassword ) { |
| 304 |
echo "Incorrect password."; |
| 305 |
|
| 306 |
gs_log( "Failed show_log access with password: $password" ); |
| 307 |
} |
| 308 |
else { |
| 309 |
$query = "SELECT * FROM $tableNamePrefix"."log;"; |
| 310 |
$result = gs_queryDatabase( $query ); |
| 311 |
|
| 312 |
$numRows = mysql_numrows( $result ); |
| 313 |
|
| 314 |
echo "$numRows log entries:<br><br><br>\n"; |
| 315 |
|
| 316 |
|
| 317 |
for( $i=0; $i<$numRows; $i++ ) { |
| 318 |
$time = mysql_result( $result, $i, "entry_time" ); |
| 319 |
$entry = mysql_result( $result, $i, "entry" ); |
| 320 |
|
| 321 |
echo "<b>$time</b>:<br>$entry<hr>\n"; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
function gs_clearLog() { |
| 329 |
$password = ""; |
| 330 |
if( isset( $_REQUEST[ "password" ] ) ) { |
| 331 |
$password = $_REQUEST[ "password" ]; |
| 332 |
} |
| 333 |
|
| 334 |
global $logAccessPassword, $tableNamePrefix; |
| 335 |
|
| 336 |
if( $password != $logAccessPassword ) { |
| 337 |
echo "Incorrect password."; |
| 338 |
|
| 339 |
gs_log( "Failed clear_log access with password: $password" ); |
| 340 |
} |
| 341 |
else { |
| 342 |
$query = "DELETE FROM $tableNamePrefix"."log;"; |
| 343 |
$result = gs_queryDatabase( $query ); |
| 344 |
|
| 345 |
if( $result ) { |
| 346 |
echo "Log cleared."; |
| 347 |
} |
| 348 |
else { |
| 349 |
echo "DELETE operation failed?"; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
function gs_getPlayerID() { |
| 358 |
|
| 359 |
// disable autocommit so that FOR UPDATE actually works |
| 360 |
gs_queryDatabase( "SET AUTOCOMMIT = 0;" ); |
| 361 |
|
| 362 |
global $tableNamePrefix; |
| 363 |
|
| 364 |
$query = "SELECT next_player_id FROM $tableNamePrefix"."server_globals ". |
| 365 |
"FOR UPDATE;"; |
| 366 |
$result = gs_queryDatabase( $query ); |
| 367 |
|
| 368 |
$numRows = mysql_numrows( $result ); |
| 369 |
|
| 370 |
// default if none in DB yet |
| 371 |
// note that lucky first player gets this ID |
| 372 |
$id_to_return = "0000000"; |
| 373 |
|
| 374 |
if( $numRows > 0 ) { |
| 375 |
// get the one from DB |
| 376 |
$id_to_return = mysql_result( $result, 0, "next_player_id" ); |
| 377 |
} |
| 378 |
else { |
| 379 |
// no rows |
| 380 |
// create one |
| 381 |
$query = "INSERT INTO $tableNamePrefix"."server_globals VALUES ( " . |
| 382 |
"'$id_to_return', CURRENT_TIMESTAMP );"; |
| 383 |
$result = gs_queryDatabase( $query ); |
| 384 |
} |
| 385 |
|
| 386 |
echo "$id_to_return"; |
| 387 |
|
| 388 |
|
| 389 |
// inser a new one by hashing this one |
| 390 |
$next_player_id = md5( $id_to_return ); |
| 391 |
|
| 392 |
$next_player_id = strtoupper( $next_player_id ); |
| 393 |
|
| 394 |
$next_player_id = substr( $next_player_id, 0, 7 ); |
| 395 |
|
| 396 |
$query = "UPDATE $tableNamePrefix"."server_globals SET " . |
| 397 |
"next_player_id = '$next_player_id';"; |
| 398 |
|
| 399 |
$result = gs_queryDatabase( $query ); |
| 400 |
|
| 401 |
|
| 402 |
gs_queryDatabase( "COMMIT;" ); |
| 403 |
|
| 404 |
gs_queryDatabase( "SET AUTOCOMMIT = 1;" ); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
function gs_createForFriend() { |
| 410 |
$player_id = ""; |
| 411 |
if( isset( $_REQUEST[ "player_id" ] ) ) { |
| 412 |
$player_id = $_REQUEST[ "player_id" ]; |
| 413 |
} |
| 414 |
|
| 415 |
$player_id = strtoupper( $player_id ); |
| 416 |
|
| 417 |
gs_createGame( $player_id, 1 ); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
// inPrivate is 0 or 1 |
| 423 |
function gs_createGame( $inPlayerID, $inPrivate ) { |
| 424 |
global $tableNamePrefix; |
| 425 |
|
| 426 |
$found_unused_id = 0; |
| 427 |
$salt = 0; |
| 428 |
|
| 429 |
|
| 430 |
while( ! $found_unused_id ) { |
| 431 |
|
| 432 |
$randVal = rand(); |
| 433 |
|
| 434 |
$hash = md5( $inPlayerID . uniqid( "$randVal"."$salt", true ) ); |
| 435 |
|
| 436 |
$hash = strtoupper( $hash ); |
| 437 |
|
| 438 |
|
| 439 |
$game_id = substr( $hash, 0, 7 ); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
// make code more human-friendly (alpha only) |
| 444 |
|
| 445 |
$digitArray = |
| 446 |
array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); |
| 447 |
$letterArray = |
| 448 |
array( "W", "H", "J", "K", "X", "M", "N", "P", "T", "Y" ); |
| 449 |
|
| 450 |
$game_id = str_replace( $digitArray, $letterArray, $game_id ); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
$game_passcode = $hash; |
| 456 |
|
| 457 |
$player_transforms = array( array(), array() ); |
| 458 |
|
| 459 |
for( $i=0; $i<16; $i++ ) { |
| 460 |
array_push( $player_transforms[0], $i ); |
| 461 |
array_push( $player_transforms[1], $i ); |
| 462 |
} |
| 463 |
|
| 464 |
// do random swaps to make transforms |
| 465 |
for( $i=0; $i<15; $i++ ) { |
| 466 |
for( $t=0; $t<2; $t++ ) { |
| 467 |
|
| 468 |
if( $player_transforms[ $t ][ $i ] == $i ) { |
| 469 |
// not swapped yet |
| 470 |
|
| 471 |
// find an unswapped partner |
| 472 |
$indB = rand( $i+1, 15 ); |
| 473 |
|
| 474 |
while( $player_transforms[$t][ $indB ] != $indB ) { |
| 475 |
// value at indB already swapped |
| 476 |
$indB = rand( $i+1, 15 ); |
| 477 |
} |
| 478 |
|
| 479 |
$player_transforms[$t][ $i ] = |
| 480 |
$player_transforms[$t][ $indB ]; |
| 481 |
$player_transforms[$t][ $indB ] = $i; |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
$player_1_transform = implode( ",", $player_transforms[0] ); |
| 487 |
$player_2_transform = implode( ",", $player_transforms[1] ); |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
/* |
| 492 |
"CREATE TABLE $tableName(" . |
| 493 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 494 |
"game_passcode CHAR(32) NOT NULL," . |
| 495 |
"private TINYINT NOT NULL," . |
| 496 |
"creation_date DATETIME NOT NULL," . |
| 497 |
"touch_date DATETIME NOT NULL," . |
| 498 |
"player_1_id CHAR(7) NOT NULL," . |
| 499 |
"player_2_id CHAR(7) NOT NULL," . |
| 500 |
"player_1_transform CHAR(37) NOT NULL," . |
| 501 |
"player_2_transform CHAR(37) NOT NULL," . |
| 502 |
"player_1_ready TINYINT NOT NULL," . |
| 503 |
"player_2_ready TINYINT NOT NULL," . |
| 504 |
"player_1_touch_date DATETIME NOT NULL," . |
| 505 |
"player_2_touch_date DATETIME NOT NULL," . |
| 506 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 507 |
*/ |
| 508 |
|
| 509 |
|
| 510 |
$query = "INSERT INTO $tableNamePrefix". "games VALUES ( " . |
| 511 |
"'$game_id', '$game_passcode', ". |
| 512 |
"'$inPrivate', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ". |
| 513 |
"'$inPlayerID', '0000000', ". |
| 514 |
"'$player_1_transform', '$player_2_transform', '1', '0', ". |
| 515 |
"CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '0' );"; |
| 516 |
|
| 517 |
|
| 518 |
$result = mysql_query( $query ); |
| 519 |
|
| 520 |
if( $result ) { |
| 521 |
$found_unused_id = 1; |
| 522 |
|
| 523 |
// build empty columns in table |
| 524 |
global $blockColumns; |
| 525 |
|
| 526 |
for( $c=0; $c<$blockColumns; $c++ ) { |
| 527 |
$query = "INSERT INTO $tableNamePrefix". "columns VALUES ( " . |
| 528 |
"'$game_id', '$c', '', 0 );"; |
| 529 |
|
| 530 |
gs_queryDatabase( $query ); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
$gameTypeString = "Stranger"; |
| 535 |
if( $inPrivate ) { |
| 536 |
$gameTypeString = "Friend"; |
| 537 |
} |
| 538 |
|
| 539 |
gs_log( "$gameTypeString game $game_id created by ". |
| 540 |
"player $inPlayerID" ); |
| 541 |
|
| 542 |
|
| 543 |
echo "$game_id\n"; |
| 544 |
echo "$game_passcode\n"; |
| 545 |
echo "1\n"; |
| 546 |
echo "$player_1_transform"; |
| 547 |
|
| 548 |
} |
| 549 |
else { |
| 550 |
global $debug; |
| 551 |
if( $debug == 1 ) { |
| 552 |
echo "Duplicate ids? Error: " . mysql_error() ."<br>"; |
| 553 |
} |
| 554 |
// try again |
| 555 |
$salt += 1; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
function gs_joinWithFriend() { |
| 564 |
$game_id = ""; |
| 565 |
if( isset( $_REQUEST[ "game_id" ] ) ) { |
| 566 |
$game_id = $_REQUEST[ "game_id" ]; |
| 567 |
} |
| 568 |
|
| 569 |
$game_id = strtoupper( $game_id ); |
| 570 |
|
| 571 |
|
| 572 |
$player_id = ""; |
| 573 |
if( isset( $_REQUEST[ "player_id" ] ) ) { |
| 574 |
$player_id = $_REQUEST[ "player_id" ]; |
| 575 |
} |
| 576 |
|
| 577 |
$player_id = strtoupper( $player_id ); |
| 578 |
|
| 579 |
|
| 580 |
global $tableNamePrefix; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
/* |
| 585 |
"CREATE TABLE $tableName(" . |
| 586 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 587 |
"game_passcode CHAR(32) NOT NULL," . |
| 588 |
"private TINYINT NOT NULL," . |
| 589 |
"creation_date DATETIME NOT NULL," . |
| 590 |
"touch_date DATETIME NOT NULL," . |
| 591 |
"player_1_id CHAR(7) NOT NULL," . |
| 592 |
"player_2_id CHAR(7) NOT NULL," . |
| 593 |
"player_1_transform CHAR(37) NOT NULL," . |
| 594 |
"player_2_transform CHAR(37) NOT NULL," . |
| 595 |
"player_1_ready TINYINT NOT NULL," . |
| 596 |
"player_2_ready TINYINT NOT NULL," . |
| 597 |
"player_1_touch_date DATETIME NOT NULL," . |
| 598 |
"player_2_touch_date DATETIME NOT NULL," . |
| 599 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 600 |
*/ |
| 601 |
|
| 602 |
|
| 603 |
$query = "SELECT * FROM $tableNamePrefix"."games ". |
| 604 |
"WHERE game_id = '$game_id' AND player_1_id != '$player_id' ". |
| 605 |
"AND player_2_ready = '0';"; |
| 606 |
$result = gs_queryDatabase( $query ); |
| 607 |
|
| 608 |
$numRows = mysql_numrows( $result ); |
| 609 |
|
| 610 |
if( $numRows == 1 ) { |
| 611 |
|
| 612 |
$row = mysql_fetch_array( $result, MYSQL_ASSOC ); |
| 613 |
|
| 614 |
$game_passcode = $row[ "game_passcode" ]; |
| 615 |
$player_2_transform = $row[ "player_2_transform" ]; |
| 616 |
|
| 617 |
|
| 618 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 619 |
"touch_date = CURRENT_TIMESTAMP, " . |
| 620 |
"player_2_id = '$player_id', " . |
| 621 |
"player_2_ready = '1', " . |
| 622 |
"player_2_touch_date = CURRENT_TIMESTAMP " . |
| 623 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 624 |
|
| 625 |
$result = gs_queryDatabase( $query ); |
| 626 |
|
| 627 |
gs_log( "Friend game $game_id joined by player $player_id" ); |
| 628 |
|
| 629 |
|
| 630 |
echo "$game_id\n"; |
| 631 |
echo "$game_passcode\n"; |
| 632 |
echo "2\n"; |
| 633 |
echo "$player_2_transform"; |
| 634 |
} |
| 635 |
else { |
| 636 |
gs_operationError( |
| 637 |
"Could not find joinable game matching " . $game_id ); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
function gs_joinWithStranger() { |
| 644 |
|
| 645 |
$player_id = ""; |
| 646 |
if( isset( $_REQUEST[ "player_id" ] ) ) { |
| 647 |
$player_id = $_REQUEST[ "player_id" ]; |
| 648 |
} |
| 649 |
|
| 650 |
$player_id = strtoupper( $player_id ); |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
// first, look for non-private games that aren't full |
| 655 |
|
| 656 |
// sort by change date (prefer ones that have been checked by waiting |
| 657 |
// opponent most recently) |
| 658 |
|
| 659 |
// ignore those that haven't been checked for 20 seconds or more |
| 660 |
// (abandoned?) |
| 661 |
|
| 662 |
// disable autocommit so that FOR UPDATE actually works |
| 663 |
gs_queryDatabase( "SET AUTOCOMMIT = 0;" ); |
| 664 |
|
| 665 |
global $tableNamePrefix; |
| 666 |
|
| 667 |
$query = "SELECT * FROM $tableNamePrefix"."games ". |
| 668 |
"WHERE private = '0' AND player_2_ready = '0' ". |
| 669 |
"AND player_1_id != '$player_id' ". |
| 670 |
"AND touch_date > SUBTIME( CURRENT_TIMESTAMP, '0 0:0:20.00' ) ". |
| 671 |
"ORDER BY touch_date DESC ". |
| 672 |
"FOR UPDATE;"; |
| 673 |
$result = gs_queryDatabase( $query ); |
| 674 |
|
| 675 |
$numRows = mysql_numrows( $result ); |
| 676 |
|
| 677 |
if( $numRows > 0 ) { |
| 678 |
$row = mysql_fetch_array( $result, MYSQL_ASSOC ); |
| 679 |
|
| 680 |
$game_id = $row[ "game_id" ]; |
| 681 |
$game_passcode = $row[ "game_passcode" ]; |
| 682 |
$player_2_transform = $row[ "player_2_transform" ]; |
| 683 |
|
| 684 |
|
| 685 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 686 |
"touch_date = CURRENT_TIMESTAMP, " . |
| 687 |
"player_2_id = '$player_id', " . |
| 688 |
"player_2_ready = '1', " . |
| 689 |
"player_2_touch_date = CURRENT_TIMESTAMP " . |
| 690 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 691 |
|
| 692 |
$result = gs_queryDatabase( $query ); |
| 693 |
|
| 694 |
gs_log( "Stranger game $game_id joined by player $player_id" ); |
| 695 |
|
| 696 |
|
| 697 |
echo "$game_id\n"; |
| 698 |
echo "$game_passcode\n"; |
| 699 |
echo "2\n"; |
| 700 |
echo "$player_2_transform"; |
| 701 |
} |
| 702 |
else { |
| 703 |
// no empty games found |
| 704 |
|
| 705 |
// create a new public game |
| 706 |
gs_createGame( $player_id, 0 ); |
| 707 |
} |
| 708 |
|
| 709 |
gs_queryDatabase( "COMMIT;" ); |
| 710 |
|
| 711 |
gs_queryDatabase( "SET AUTOCOMMIT = 1;" ); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
function gs_isPartnerReady() { |
| 717 |
$game_id = ""; |
| 718 |
if( isset( $_REQUEST[ "game_id" ] ) ) { |
| 719 |
$game_id = $_REQUEST[ "game_id" ]; |
| 720 |
} |
| 721 |
$game_passcode = ""; |
| 722 |
if( isset( $_REQUEST[ "game_passcode" ] ) ) { |
| 723 |
$game_passcode = $_REQUEST[ "game_passcode" ]; |
| 724 |
} |
| 725 |
$player_number = ""; |
| 726 |
if( isset( $_REQUEST[ "player_number" ] ) ) { |
| 727 |
$player_number = $_REQUEST[ "player_number" ]; |
| 728 |
} |
| 729 |
|
| 730 |
$game_id = strtoupper( $game_id ); |
| 731 |
$game_passcode = strtoupper( $game_passcode ); |
| 732 |
|
| 733 |
global $tableNamePrefix; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
/* |
| 738 |
"CREATE TABLE $tableName(" . |
| 739 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 740 |
"game_passcode CHAR(32) NOT NULL," . |
| 741 |
"private TINYINT NOT NULL," . |
| 742 |
"creation_date DATETIME NOT NULL," . |
| 743 |
"touch_date DATETIME NOT NULL," . |
| 744 |
"player_1_id CHAR(7) NOT NULL," . |
| 745 |
"player_2_id CHAR(7) NOT NULL," . |
| 746 |
"player_1_transform CHAR(37) NOT NULL," . |
| 747 |
"player_2_transform CHAR(37) NOT NULL," . |
| 748 |
"player_1_ready TINYINT NOT NULL," . |
| 749 |
"player_2_ready TINYINT NOT NULL," . |
| 750 |
"player_1_touch_date DATETIME NOT NULL," . |
| 751 |
"player_2_touch_date DATETIME NOT NULL," . |
| 752 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 753 |
*/ |
| 754 |
|
| 755 |
|
| 756 |
$query = "SELECT * FROM $tableNamePrefix"."games ". |
| 757 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 758 |
$result = gs_queryDatabase( $query ); |
| 759 |
|
| 760 |
$numRows = mysql_numrows( $result ); |
| 761 |
|
| 762 |
if( $numRows == 1 ) { |
| 763 |
$row = mysql_fetch_array( $result, MYSQL_ASSOC ); |
| 764 |
|
| 765 |
$otherReady = 0; |
| 766 |
|
| 767 |
if( $player_number == 1 ) { |
| 768 |
$otherReady = $row[ "player_2_ready" ]; |
| 769 |
} |
| 770 |
else { |
| 771 |
$otherReady = $row[ "player_1_ready" ]; |
| 772 |
} |
| 773 |
|
| 774 |
echo "$otherReady"; |
| 775 |
|
| 776 |
// touched, update timestamp |
| 777 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 778 |
"touch_date = CURRENT_TIMESTAMP " . |
| 779 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 780 |
|
| 781 |
$result = gs_queryDatabase( $query ); |
| 782 |
} |
| 783 |
else { |
| 784 |
gs_operationError( "No game matched ID and passcode" ); |
| 785 |
} |
| 786 |
|
| 787 |
/* |
| 788 |
?action=is_partner_ready&game_id=0a4d516&game_passcode=0a4d516e820579bb030f3eb66d0f3035&player_number=1 |
| 789 |
*/ |
| 790 |
|
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
function gs_reportAlive() { |
| 796 |
$game_id = ""; |
| 797 |
if( isset( $_REQUEST[ "game_id" ] ) ) { |
| 798 |
$game_id = $_REQUEST[ "game_id" ]; |
| 799 |
} |
| 800 |
$game_passcode = ""; |
| 801 |
if( isset( $_REQUEST[ "game_passcode" ] ) ) { |
| 802 |
$game_passcode = $_REQUEST[ "game_passcode" ]; |
| 803 |
} |
| 804 |
$player_number = ""; |
| 805 |
if( isset( $_REQUEST[ "player_number" ] ) ) { |
| 806 |
$player_number = $_REQUEST[ "player_number" ]; |
| 807 |
} |
| 808 |
|
| 809 |
$playerTouchDateName = "player_1_touch_date"; |
| 810 |
if( $player_number == "2" ) { |
| 811 |
$playerTouchDateName = "player_2_touch_date"; |
| 812 |
} |
| 813 |
|
| 814 |
global $tableNamePrefix; |
| 815 |
|
| 816 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 817 |
"touch_date = CURRENT_TIMESTAMP, " . |
| 818 |
"$playerTouchDateName = CURRENT_TIMESTAMP " . |
| 819 |
"WHERE game_id = '$game_id' ". |
| 820 |
"AND game_passcode = '$game_passcode';"; |
| 821 |
|
| 822 |
$result = gs_queryDatabase( $query ); |
| 823 |
|
| 824 |
echo "1"; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
function gs_postMove() { |
| 830 |
$game_id = ""; |
| 831 |
if( isset( $_REQUEST[ "game_id" ] ) ) { |
| 832 |
$game_id = $_REQUEST[ "game_id" ]; |
| 833 |
} |
| 834 |
$game_passcode = ""; |
| 835 |
if( isset( $_REQUEST[ "game_passcode" ] ) ) { |
| 836 |
$game_passcode = $_REQUEST[ "game_passcode" ]; |
| 837 |
} |
| 838 |
$player_number = ""; |
| 839 |
if( isset( $_REQUEST[ "player_number" ] ) ) { |
| 840 |
$player_number = $_REQUEST[ "player_number" ]; |
| 841 |
} |
| 842 |
$num_dirty_columns = ""; |
| 843 |
if( isset( $_REQUEST[ "num_dirty_columns" ] ) ) { |
| 844 |
$num_dirty_columns = $_REQUEST[ "num_dirty_columns" ]; |
| 845 |
} |
| 846 |
|
| 847 |
$game_id = strtoupper( $game_id ); |
| 848 |
$game_passcode = strtoupper( $game_passcode ); |
| 849 |
|
| 850 |
|
| 851 |
global $tableNamePrefix; |
| 852 |
|
| 853 |
|
| 854 |
/* |
| 855 |
"CREATE TABLE $tableName(" . |
| 856 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 857 |
"game_passcode CHAR(32) NOT NULL," . |
| 858 |
"private TINYINT NOT NULL," . |
| 859 |
"creation_date DATETIME NOT NULL," . |
| 860 |
"touch_date DATETIME NOT NULL," . |
| 861 |
"player_1_id CHAR(7) NOT NULL," . |
| 862 |
"player_2_id CHAR(7) NOT NULL," . |
| 863 |
"player_1_transform CHAR(37) NOT NULL," . |
| 864 |
"player_2_transform CHAR(37) NOT NULL," . |
| 865 |
"player_1_ready TINYINT NOT NULL," . |
| 866 |
"player_2_ready TINYINT NOT NULL," . |
| 867 |
"player_1_touch_date DATETIME NOT NULL," . |
| 868 |
"player_2_touch_date DATETIME NOT NULL," . |
| 869 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 870 |
*/ |
| 871 |
|
| 872 |
|
| 873 |
// disable autocommit so that FOR UPDATE actually works |
| 874 |
gs_queryDatabase( "SET AUTOCOMMIT = 0;" ); |
| 875 |
|
| 876 |
|
| 877 |
$query = "SELECT * FROM $tableNamePrefix"."games ". |
| 878 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode' ". |
| 879 |
"FOR UPDATE;"; |
| 880 |
$result = gs_queryDatabase( $query ); |
| 881 |
|
| 882 |
$numRows = mysql_numrows( $result ); |
| 883 |
|
| 884 |
if( $numRows == 1 ) { |
| 885 |
$row = mysql_fetch_array( $result, MYSQL_ASSOC ); |
| 886 |
|
| 887 |
$state_number = $row[ "state_number" ]; |
| 888 |
|
| 889 |
// new state |
| 890 |
$state_number ++; |
| 891 |
|
| 892 |
|
| 893 |
for( $c=0; $c<$num_dirty_columns; $c++ ) { |
| 894 |
$paramName = "dirty_column_$c"; |
| 895 |
|
| 896 |
$columnData = ""; |
| 897 |
|
| 898 |
if( isset( $_REQUEST[ $paramName ] ) ) { |
| 899 |
$columnData = $_REQUEST[ $paramName ]; |
| 900 |
} |
| 901 |
else { |
| 902 |
gs_operationError( "Column missing from input" ); |
| 903 |
} |
| 904 |
//echo "column data = $columnData<br>"; |
| 905 |
|
| 906 |
$dataParts = explode( "_", $columnData ); |
| 907 |
|
| 908 |
/* |
| 909 |
"CREATE TABLE $tableName(" . |
| 910 |
"game_id CHAR(7) NOT NULL," . |
| 911 |
"column_index TINYINT NOT NULL," . |
| 912 |
"blocks TEXT NOT NULL," . |
| 913 |
"changed_in_state INT NOT NULL, ". |
| 914 |
"PRIMARY KEY( game_id, column_index ) );"; |
| 915 |
*/ |
| 916 |
|
| 917 |
$column_index = array_shift( $dataParts ); |
| 918 |
|
| 919 |
if( $player_number == 2 ) { |
| 920 |
// flip left-to-right |
| 921 |
global $blockColumns; |
| 922 |
|
| 923 |
$column_index = $blockColumns - 1 - $column_index; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
$numParts = count( $dataParts ); |
| 928 |
|
| 929 |
//echo "numParts = $numParts<br>"; |
| 930 |
|
| 931 |
// put remaining blocks back together |
| 932 |
$blocks = implode( "_", $dataParts ); |
| 933 |
|
| 934 |
//echo "blocks = $blocks<br>"; |
| 935 |
|
| 936 |
$query = "UPDATE $tableNamePrefix"."columns SET " . |
| 937 |
"blocks = '$blocks', " . |
| 938 |
"changed_in_state = '$state_number' " . |
| 939 |
"WHERE game_id = '$game_id' AND ". |
| 940 |
"column_index = '$column_index';"; |
| 941 |
$result = gs_queryDatabase( $query ); |
| 942 |
} |
| 943 |
|
| 944 |
$playerTouchDateName = "player_1_touch_date"; |
| 945 |
if( $player_number == "2" ) { |
| 946 |
$playerTouchDateName = "player_2_touch_date"; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 951 |
"touch_date = CURRENT_TIMESTAMP, " . |
| 952 |
"$playerTouchDateName = CURRENT_TIMESTAMP, " . |
| 953 |
"state_number = '$state_number' " . |
| 954 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 955 |
|
| 956 |
$result = gs_queryDatabase( $query ); |
| 957 |
|
| 958 |
|
| 959 |
echo "1"; |
| 960 |
} |
| 961 |
else { |
| 962 |
gs_operationError( "No game matched ID and passcode" ); |
| 963 |
} |
| 964 |
|
| 965 |
/* |
| 966 |
http://localhost/jcr13/game7/server.php?action=post_move&game_id=fac4e7c&game_passcode=fac4e7c03cf367f954ace1a3480966fe&player_number=1&num_dirty_columns=1&dirty_column_0=5_16A |
| 967 |
*/ |
| 968 |
|
| 969 |
// unlock rows that were locked by FOR UPDATE above |
| 970 |
gs_queryDatabase( "COMMIT;" ); |
| 971 |
|
| 972 |
gs_queryDatabase( "SET AUTOCOMMIT = 1;" ); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
function gs_getChangedColumns() { |
| 978 |
$game_id = ""; |
| 979 |
if( isset( $_REQUEST[ "game_id" ] ) ) { |
| 980 |
$game_id = $_REQUEST[ "game_id" ]; |
| 981 |
} |
| 982 |
$game_passcode = ""; |
| 983 |
if( isset( $_REQUEST[ "game_passcode" ] ) ) { |
| 984 |
$game_passcode = $_REQUEST[ "game_passcode" ]; |
| 985 |
} |
| 986 |
$player_number = ""; |
| 987 |
if( isset( $_REQUEST[ "player_number" ] ) ) { |
| 988 |
$player_number = $_REQUEST[ "player_number" ]; |
| 989 |
} |
| 990 |
$last_state_seen = ""; |
| 991 |
if( isset( $_REQUEST[ "last_state_seen" ] ) ) { |
| 992 |
$last_state_seen = $_REQUEST[ "last_state_seen" ]; |
| 993 |
} |
| 994 |
|
| 995 |
$game_id = strtoupper( $game_id ); |
| 996 |
$game_passcode = strtoupper( $game_passcode ); |
| 997 |
|
| 998 |
|
| 999 |
global $tableNamePrefix; |
| 1000 |
|
| 1001 |
|
| 1002 |
/* |
| 1003 |
"CREATE TABLE $tableName(" . |
| 1004 |
"game_id CHAR(7) NOT NULL PRIMARY KEY," . |
| 1005 |
"game_passcode CHAR(32) NOT NULL," . |
| 1006 |
"private TINYINT NOT NULL," . |
| 1007 |
"creation_date DATETIME NOT NULL," . |
| 1008 |
"touch_date DATETIME NOT NULL," . |
| 1009 |
"player_1_id CHAR(7) NOT NULL," . |
| 1010 |
"player_2_id CHAR(7) NOT NULL," . |
| 1011 |
"player_1_transform CHAR(37) NOT NULL," . |
| 1012 |
"player_2_transform CHAR(37) NOT NULL," . |
| 1013 |
"player_1_ready TINYINT NOT NULL," . |
| 1014 |
"player_2_ready TINYINT NOT NULL," . |
| 1015 |
"player_1_touch_date DATETIME NOT NULL," . |
| 1016 |
"player_2_touch_date DATETIME NOT NULL," . |
| 1017 |
"state_number INT NOT NULL ) ENGINE = INNODB;"; |
| 1018 |
*/ |
| 1019 |
|
| 1020 |
|
| 1021 |
$query = "SELECT * FROM $tableNamePrefix"."games ". |
| 1022 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 1023 |
$result = gs_queryDatabase( $query ); |
| 1024 |
|
| 1025 |
$numRows = mysql_numrows( $result ); |
| 1026 |
|
| 1027 |
if( $numRows == 1 ) { |
| 1028 |
$row = mysql_fetch_array( $result, MYSQL_ASSOC ); |
| 1029 |
|
| 1030 |
$state_number = $row[ "state_number" ]; |
| 1031 |
|
| 1032 |
|
| 1033 |
$partnerTouchDate = $row[ "player_1_touch_date" ]; |
| 1034 |
if( $player_number == 1 ) { |
| 1035 |
$partnerTouchDate = $row[ "player_2_touch_date" ]; |
| 1036 |
} |
| 1037 |
|
| 1038 |
// compute seconds since partner touched |
| 1039 |
$query = "SELECT TIME_TO_SEC( TIMEDIFF( CURRENT_TIMESTAMP, ". |
| 1040 |
" '$partnerTouchDate' ) );"; |
| 1041 |
|
| 1042 |
|
| 1043 |
$result = gs_queryDatabase( $query ); |
| 1044 |
|
| 1045 |
$secondsSincePartnerActed = mysql_result( $result, 0, 0 ); |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
$query = "SELECT * FROM $tableNamePrefix"."columns ". |
| 1051 |
"WHERE game_id = '$game_id' AND ". |
| 1052 |
"changed_in_state > $last_state_seen;"; |
| 1053 |
|
| 1054 |
$result = gs_queryDatabase( $query ); |
| 1055 |
|
| 1056 |
$numChangedColumns = mysql_numrows( $result ); |
| 1057 |
|
| 1058 |
echo "$secondsSincePartnerActed\n"; |
| 1059 |
echo "$state_number\n"; |
| 1060 |
echo "$numChangedColumns\n"; |
| 1061 |
|
| 1062 |
|
| 1063 |
for( $i=0; $i<$numChangedColumns; $i++ ) { |
| 1064 |
|
| 1065 |
$blocks = mysql_result( $result, $i, "blocks" ); |
| 1066 |
$column_index = mysql_result( $result, $i, "column_index" ); |
| 1067 |
|
| 1068 |
if( $player_number == 2 ) { |
| 1069 |
// flip left-to-right |
| 1070 |
global $blockColumns; |
| 1071 |
|
| 1072 |
$column_index = $blockColumns - 1 - $column_index; |
| 1073 |
} |
| 1074 |
|
| 1075 |
echo "$column_index" . "_" . "$blocks\n"; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
$playerTouchDateName = "player_1_touch_date"; |
| 1080 |
if( $player_number == "2" ) { |
| 1081 |
$playerTouchDateName = "player_2_touch_date"; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
$query = "UPDATE $tableNamePrefix"."games SET " . |
| 1087 |
"touch_date = CURRENT_TIMESTAMP, " . |
| 1088 |
"$playerTouchDateName = CURRENT_TIMESTAMP " . |
| 1089 |
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';"; |
| 1090 |
gs_queryDatabase( $query ); |
| 1091 |
} |
| 1092 |
else { |
| 1093 |
gs_operationError( "No game matched ID and passcode" ); |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* |
| 1097 |
?action=get_changed_columns&game_id=0e86e62&game_passcode=0e86e6239c67bcc56b4b6955a8fd501c&player_number=1&last_state_seen=1 |
| 1098 |
*/ |
| 1099 |
|
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
// check if we should flush stale games from the database |
| 1105 |
// do this every 5 minutes |
| 1106 |
function gs_checkForFlush() { |
| 1107 |
global $tableNamePrefix; |
| 1108 |
|
| 1109 |
$tableName = "$tableNamePrefix"."server_globals"; |
| 1110 |
|
| 1111 |
if( !gs_doesTableExist( $tableName ) ) { |
| 1112 |
return; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
gs_queryDatabase( "SET AUTOCOMMIT = 0;" ); |
| 1117 |
|
| 1118 |
|
| 1119 |
$query = "SELECT last_flush_time FROM $tableName ". |
| 1120 |
"WHERE last_flush_time < SUBTIME( CURRENT_TIMESTAMP, '0 0:05:0.000' ) ". |
| 1121 |
"FOR UPDATE;"; |
| 1122 |
|
| 1123 |
$result = gs_queryDatabase( $query ); |
| 1124 |
|
| 1125 |
if( mysql_numrows( $result ) > 0 ) { |
| 1126 |
|
| 1127 |
// last flush time is old |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
global $tableNamePrefix; |
| 1132 |
|
| 1133 |
$gamesTable = $tableNamePrefix . "games"; |
| 1134 |
$columnsTable = $tableNamePrefix . "columns"; |
| 1135 |
|
| 1136 |
|
| 1137 |
// Games that haven't been fully joined yet |
| 1138 |
// need to be checked by the creator (player 1) every 5 seconds. |
| 1139 |
// If they linger 20 seconds without being checked, they are ignored |
| 1140 |
// by strangers looking to join. |
| 1141 |
// If they linger 40 seconds without being checked, we drop them. |
| 1142 |
$query = "DELETE $gamesTable, $columnsTable ". |
| 1143 |
"FROM $gamesTable JOIN $columnsTable ". |
| 1144 |
"WHERE player_2_ready = '0' ". |
| 1145 |
"AND $gamesTable.touch_date < ". |
| 1146 |
" SUBTIME( CURRENT_TIMESTAMP, '0 0:00:40.00' ) ". |
| 1147 |
"AND $gamesTable.game_id = $columnsTable.game_id;"; |
| 1148 |
|
| 1149 |
|
| 1150 |
$result = gs_queryDatabase( $query ); |
| 1151 |
|
| 1152 |
$numRowsRemoved = mysql_affected_rows(); |
| 1153 |
|
| 1154 |
gs_log( "Flush operation on unstarted games removed $numRowsRemoved". |
| 1155 |
" rows." ); |
| 1156 |
|
| 1157 |
|
| 1158 |
// games that have been joined / started can linger for a long |
| 1159 |
// time with no server action (while players are thinking, etc). |
| 1160 |
|
| 1161 |
// also, players can quit playing and resume playing later |
| 1162 |
// (hmmm... do we want to support this?) |
| 1163 |
|
| 1164 |
// for now, give them 2 hours of idle time |
| 1165 |
|
| 1166 |
$query = "DELETE $gamesTable, $columnsTable ". |
| 1167 |
"FROM $gamesTable JOIN $columnsTable ". |
| 1168 |
"WHERE player_2_ready = '1' ". |
| 1169 |
"AND $gamesTable.touch_date < ". |
| 1170 |
" SUBTIME( CURRENT_TIMESTAMP, '0 2:00:0.00' ) ". |
| 1171 |
"AND $gamesTable.game_id = $columnsTable.game_id;"; |
| 1172 |
|
| 1173 |
|
| 1174 |
$result = gs_queryDatabase( $query ); |
| 1175 |
|
| 1176 |
|
| 1177 |
$numRowsRemoved = mysql_affected_rows(); |
| 1178 |
|
| 1179 |
gs_log( "Flush operation on started games removed $numRowsRemoved". |
| 1180 |
" rows." ); |
| 1181 |
|
| 1182 |
global $enableLog; |
| 1183 |
|
| 1184 |
if( $enableLog ) { |
| 1185 |
// count remaining games for log |
| 1186 |
$query = "SELECT COUNT(*) FROM $gamesTable;"; |
| 1187 |
|
| 1188 |
$result = gs_queryDatabase( $query ); |
| 1189 |
|
| 1190 |
$count = mysql_result( $result, 0, 0 ); |
| 1191 |
|
| 1192 |
gs_log( "After flush, $count games left." ); |
| 1193 |
} |
| 1194 |
|
| 1195 |
// set new flush time |
| 1196 |
|
| 1197 |
$query = "UPDATE $tableName SET " . |
| 1198 |
"last_flush_time = CURRENT_TIMESTAMP;"; |
| 1199 |
|
| 1200 |
$result = gs_queryDatabase( $query ); |
| 1201 |
|
| 1202 |
|
| 1203 |
} |
| 1204 |
|
| 1205 |
gs_queryDatabase( "COMMIT;" ); |
| 1206 |
|
| 1207 |
gs_queryDatabase( "SET AUTOCOMMIT = 1;" ); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
// general-purpose functions down here, many copied from seedBlogs |
| 1216 |
|
| 1217 |
/** |
| 1218 |
* Connects to the database according to the database variables. |
| 1219 |
*/ |
| 1220 |
function gs_connectToDatabase() { |
| 1221 |
global $databaseServer, |
| 1222 |
$databaseUsername, $databasePassword, $databaseName; |
| 1223 |
|
| 1224 |
|
| 1225 |
mysql_connect( $databaseServer, $databaseUsername, $databasePassword ) |
| 1226 |
or gs_fatalError( "Could not connect to database server: " . |
| 1227 |
mysql_error() ); |
| 1228 |
|
| 1229 |
mysql_select_db( $databaseName ) |
| 1230 |
or gs_fatalError( "Could not select $databaseName database: " . |
| 1231 |
mysql_error() ); |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
/** |
| 1237 |
* Closes the database connection. |
| 1238 |
*/ |
| 1239 |
function gs_closeDatabase() { |
| 1240 |
mysql_close(); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
/** |
| 1246 |
* Queries the database, and dies with an error message on failure. |
| 1247 |
* |
| 1248 |
* @param $inQueryString the SQL query string. |
| 1249 |
* |
| 1250 |
* @return a result handle that can be passed to other mysql functions. |
| 1251 |
*/ |
| 1252 |
function gs_queryDatabase( $inQueryString ) { |
| 1253 |
|
| 1254 |
$result = mysql_query( $inQueryString ) |
| 1255 |
or gs_fatalError( "Database query failed:<BR>$inQueryString<BR><BR>" . |
| 1256 |
mysql_error() ); |
| 1257 |
|
| 1258 |
return $result; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
/** |
| 1264 |
* Checks whether a table exists in the currently-connected database. |
| 1265 |
* |
| 1266 |
* @param $inTableName the name of the table to look for. |
| 1267 |
* |
| 1268 |
* @return 1 if the table exists, or 0 if not. |
| 1269 |
*/ |
| 1270 |
function gs_doesTableExist( $inTableName ) { |
| 1271 |
// check if our table exists |
| 1272 |
$tableExists = 0; |
| 1273 |
|
| 1274 |
$query = "SHOW TABLES"; |
| 1275 |
$result = gs_queryDatabase( $query ); |
| 1276 |
|
| 1277 |
$numRows = mysql_numrows( $result ); |
| 1278 |
|
| 1279 |
|
| 1280 |
for( $i=0; $i<$numRows && ! $tableExists; $i++ ) { |
| 1281 |
|
| 1282 |
$tableName = mysql_result( $result, $i, 0 ); |
| 1283 |
|
| 1284 |
if( $tableName == $inTableName ) { |
| 1285 |
$tableExists = 1; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
return $tableExists; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
function gs_log( $message ) { |
| 1294 |
global $enableLog, $tableNamePrefix; |
| 1295 |
|
| 1296 |
$slashedMessage = addslashes( $message ); |
| 1297 |
|
| 1298 |
if( $enableLog ) { |
| 1299 |
$query = "INSERT INTO $tableNamePrefix"."log VALUES ( " . |
| 1300 |
"'$slashedMessage', CURRENT_TIMESTAMP );"; |
| 1301 |
$result = gs_queryDatabase( $query ); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
/** |
| 1308 |
* Displays the error page and dies. |
| 1309 |
* |
| 1310 |
* @param $message the error message to display on the error page. |
| 1311 |
*/ |
| 1312 |
function gs_fatalError( $message ) { |
| 1313 |
//global $errorMessage; |
| 1314 |
|
| 1315 |
// set the variable that is displayed inside error.php |
| 1316 |
//$errorMessage = $message; |
| 1317 |
|
| 1318 |
//include_once( "error.php" ); |
| 1319 |
|
| 1320 |
// for now, just print error message |
| 1321 |
$logMessage = "Fatal error: $message"; |
| 1322 |
|
| 1323 |
echo( $logMessage ); |
| 1324 |
|
| 1325 |
gs_log( $logMessage ); |
| 1326 |
|
| 1327 |
die(); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
/** |
| 1333 |
* Displays the operation error message and dies. |
| 1334 |
* |
| 1335 |
* @param $message the error message to display. |
| 1336 |
*/ |
| 1337 |
function gs_operationError( $message ) { |
| 1338 |
|
| 1339 |
// for now, just print error message |
| 1340 |
echo( "ERROR: $message" ); |
| 1341 |
die(); |
| 1342 |
} |
| 1343 |
|
| 1344 |
|
| 1345 |
/** |
| 1346 |
* Recursively applies the addslashes function to arrays of arrays. |
| 1347 |
* This effectively forces magic_quote escaping behavior, eliminating |
| 1348 |
* a slew of possible database security issues. |
| 1349 |
* |
| 1350 |
* @inValue the value or array to addslashes to. |
| 1351 |
* |
| 1352 |
* @return the value or array with slashes added. |
| 1353 |
*/ |
| 1354 |
function gs_addslashes_deep( $inValue ) { |
| 1355 |
return |
| 1356 |
( is_array( $inValue ) |
| 1357 |
? array_map( 'gs_addslashes_deep', $inValue ) |
| 1358 |
: addslashes( $inValue ) ); |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
/** |
| 1364 |
* Recursively applies the stripslashes function to arrays of arrays. |
| 1365 |
* This effectively disables magic_quote escaping behavior. |
| 1366 |
* |
| 1367 |
* @inValue the value or array to stripslashes from. |
| 1368 |
* |
| 1369 |
* @return the value or array with slashes removed. |
| 1370 |
*/ |
| 1371 |
function gs_stripslashes_deep( $inValue ) { |
| 1372 |
return |
| 1373 |
( is_array( $inValue ) |
| 1374 |
? array_map( 'sb_stripslashes_deep', $inValue ) |
| 1375 |
: stripslashes( $inValue ) ); |
| 1376 |
} |
| 1377 |
|
| 1378 |
?> |