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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
|
<?php
global $gs_version;
$gs_version = "0.3";
// edit settings.php to change server' settings
include( "settings.php" );
// no end-user settings below this point
// enable verbose error reporting to detect uninitialized variables
error_reporting( E_ALL );
// page layout for web-based setup
$setup_header = "
<HTML>
<HEAD><TITLE>Game 7 Server Web-based setup</TITLE></HEAD>
<BODY BGCOLOR=#FFFFFF TEXT=#000000 LINK=#0000FF VLINK=#FF0000>
<CENTER>
<TABLE WIDTH=75% BORDER=0 CELLSPACING=0 CELLPADDING=1>
<TR><TD BGCOLOR=#000000>
<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=10>
<TR><TD BGCOLOR=#EEEEEE>";
$setup_footer = "
</TD></TR></TABLE>
</TD></TR></TABLE>
</CENTER>
</BODY></HTML>";
// ensure that magic quotes are on (adding slashes before quotes
// so that user-submitted data can be safely submitted in DB queries)
if( !get_magic_quotes_gpc() ) {
// force magic quotes to be added
$_GET = array_map( 'gs_addslashes_deep', $_GET );
$_POST = array_map( 'gs_addslashes_deep', $_POST );
$_REQUEST = array_map( 'gs_addslashes_deep', $_REQUEST );
$_COOKIE = array_map( 'gs_addslashes_deep', $_COOKIE );
}
// all calls need to connect to DB, so do it once here
gs_connectToDatabase();
// close connection down below (before function declarations)
// testing:
//sleep( 5 );
// general processing whenver server.php is accessed directly
gs_checkForFlush();
// grab POST/GET variables
$action = "";
if( isset( $_REQUEST[ "action" ] ) ) {
$action = $_REQUEST[ "action" ];
}
$debug = "";
if( isset( $_REQUEST[ "debug" ] ) ) {
$debug = $_REQUEST[ "debug" ];
}
if( $action == "version" ) {
global $gs_version;
echo "$gs_version";
}
else if( $action == "show_log" ) {
gs_showLog();
}
else if( $action == "clear_log" ) {
gs_clearLog();
}
else if( $action == "get_player_id" ) {
gs_getPlayerID();
}
else if( $action == "create_for_friend" ) {
gs_createForFriend();
}
else if( $action == "join_with_friend" ) {
gs_joinWithFriend();
}
else if( $action == "join_with_stranger" ) {
gs_joinWithStranger();
}
else if( $action == "is_partner_ready" ) {
gs_isPartnerReady();
}
else if( $action == "report_alive" ) {
gs_reportAlive();
}
else if( $action == "post_move" ) {
gs_postMove();
}
else if( $action == "get_changed_columns" ) {
gs_getChangedColumns();
}
else if( $action == "gs_setup" ) {
global $setup_header, $setup_footer;
echo $setup_header;
echo "<H2>Game 7 Server Web-based Setup</H2>";
echo "Creating tables:<BR>";
echo "<CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1>
<TR><TD BGCOLOR=#000000>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5>
<TR><TD BGCOLOR=#FFFFFF>";
gs_setupDatabase();
echo "</TD></TR></TABLE></TD></TR></TABLE></CENTER><BR><BR>";
echo $setup_footer;
}
else if( preg_match( "/server\.php/", $_SERVER[ "SCRIPT_NAME" ] ) ) {
// server.php has been called without an action parameter
// the preg_match ensures that server.php was called directly and
// not just included by another script
// quick (and incomplete) test to see if we should show instructions
global $tableNamePrefix;
// check if our "games" table exists
$tableName = $tableNamePrefix . "games";
$exists = gs_doesTableExist( $tableName );
if( $exists ) {
echo "Game 7 server database setup and ready";
}
else {
// start the setup procedure
global $setup_header, $setup_footer;
echo $setup_header;
echo "<H2>Game 7 Server Web-based Setup</H2>";
echo "Game 7 Server will walk you through a " .
"brief setup process.<BR><BR>";
echo "Step 1: ".
"<A HREF=\"server.php?action=gs_setup\">".
"create the database tables</A>";
echo $setup_footer;
}
}
// done processing
// only function declarations below
gs_closeDatabase();
/**
* Creates the database tables needed by seedBlogs.
*/
function gs_setupDatabase() {
global $tableNamePrefix;
$tableName = $tableNamePrefix . "server_globals";
if( ! gs_doesTableExist( $tableName ) ) {
// this table contains general info about the server
// use INNODB engine so table can be locked
$query =
"CREATE TABLE $tableName(" .
"next_player_id CHAR(7) NOT NULL, ".
"last_flush_time DATETIME NOT NULL ) ENGINE = INNODB;";
$result = gs_queryDatabase( $query );
echo "<B>$tableName</B> table created<BR>";
}
else {
echo "<B>$tableName</B> table already exists<BR>";
}
$tableName = $tableNamePrefix . "log";
if( ! gs_doesTableExist( $tableName ) ) {
// this table contains general info about the server
// use INNODB engine so table can be locked
$query =
"CREATE TABLE $tableName(" .
"entry TEXT NOT NULL, ".
"entry_time DATETIME NOT NULL );";
$result = gs_queryDatabase( $query );
echo "<B>$tableName</B> table created<BR>";
}
else {
echo "<B>$tableName</B> table already exists<BR>";
}
$tableName = $tableNamePrefix . "games";
if( ! gs_doesTableExist( $tableName ) ) {
// this table contains general info about each game
// use INNODB engine so table can be locked
$query =
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
$result = gs_queryDatabase( $query );
echo "<B>$tableName</B> table created<BR>";
}
else {
echo "<B>$tableName</B> table already exists<BR>";
}
$tableName = $tableNamePrefix . "columns";
if( ! gs_doesTableExist( $tableName ) ) {
// this table contains information for each user
$query =
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL," .
"column_index TINYINT NOT NULL," .
"blocks TEXT NOT NULL," .
"changed_in_state INT NOT NULL, ".
"PRIMARY KEY( game_id, column_index ) );";
$result = gs_queryDatabase( $query );
echo "<B>$tableName</B> table created<BR>";
}
else {
echo "<B>$tableName</B> table already exists<BR>";
}
}
function gs_showLog() {
$password = "";
if( isset( $_REQUEST[ "password" ] ) ) {
$password = $_REQUEST[ "password" ];
}
global $logAccessPassword, $tableNamePrefix;
if( $password != $logAccessPassword ) {
echo "Incorrect password.";
gs_log( "Failed show_log access with password: $password" );
}
else {
$query = "SELECT * FROM $tableNamePrefix"."log;";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
echo "$numRows log entries:<br><br><br>\n";
for( $i=0; $i<$numRows; $i++ ) {
$time = mysql_result( $result, $i, "entry_time" );
$entry = mysql_result( $result, $i, "entry" );
echo "<b>$time</b>:<br>$entry<hr>\n";
}
}
}
function gs_clearLog() {
$password = "";
if( isset( $_REQUEST[ "password" ] ) ) {
$password = $_REQUEST[ "password" ];
}
global $logAccessPassword, $tableNamePrefix;
if( $password != $logAccessPassword ) {
echo "Incorrect password.";
gs_log( "Failed clear_log access with password: $password" );
}
else {
$query = "DELETE FROM $tableNamePrefix"."log;";
$result = gs_queryDatabase( $query );
if( $result ) {
echo "Log cleared.";
}
else {
echo "DELETE operation failed?";
}
}
}
function gs_getPlayerID() {
// disable autocommit so that FOR UPDATE actually works
gs_queryDatabase( "SET AUTOCOMMIT = 0;" );
global $tableNamePrefix;
$query = "SELECT next_player_id FROM $tableNamePrefix"."server_globals ".
"FOR UPDATE;";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
// default if none in DB yet
// note that lucky first player gets this ID
$id_to_return = "0000000";
if( $numRows > 0 ) {
// get the one from DB
$id_to_return = mysql_result( $result, 0, "next_player_id" );
}
else {
// no rows
// create one
$query = "INSERT INTO $tableNamePrefix"."server_globals VALUES ( " .
"'$id_to_return', CURRENT_TIMESTAMP );";
$result = gs_queryDatabase( $query );
}
echo "$id_to_return";
// inser a new one by hashing this one
$next_player_id = md5( $id_to_return );
$next_player_id = strtoupper( $next_player_id );
$next_player_id = substr( $next_player_id, 0, 7 );
$query = "UPDATE $tableNamePrefix"."server_globals SET " .
"next_player_id = '$next_player_id';";
$result = gs_queryDatabase( $query );
gs_queryDatabase( "COMMIT;" );
gs_queryDatabase( "SET AUTOCOMMIT = 1;" );
}
function gs_createForFriend() {
$player_id = "";
if( isset( $_REQUEST[ "player_id" ] ) ) {
$player_id = $_REQUEST[ "player_id" ];
}
$player_id = strtoupper( $player_id );
gs_createGame( $player_id, 1 );
}
// inPrivate is 0 or 1
function gs_createGame( $inPlayerID, $inPrivate ) {
global $tableNamePrefix;
$found_unused_id = 0;
$salt = 0;
while( ! $found_unused_id ) {
$randVal = rand();
$hash = md5( $inPlayerID . uniqid( "$randVal"."$salt", true ) );
$hash = strtoupper( $hash );
$game_id = substr( $hash, 0, 7 );
// make code more human-friendly (alpha only)
$digitArray =
array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" );
$letterArray =
array( "W", "H", "J", "K", "X", "M", "N", "P", "T", "Y" );
$game_id = str_replace( $digitArray, $letterArray, $game_id );
$game_passcode = $hash;
$player_transforms = array( array(), array() );
for( $i=0; $i<16; $i++ ) {
array_push( $player_transforms[0], $i );
array_push( $player_transforms[1], $i );
}
// do random swaps to make transforms
for( $i=0; $i<15; $i++ ) {
for( $t=0; $t<2; $t++ ) {
if( $player_transforms[ $t ][ $i ] == $i ) {
// not swapped yet
// find an unswapped partner
$indB = rand( $i+1, 15 );
while( $player_transforms[$t][ $indB ] != $indB ) {
// value at indB already swapped
$indB = rand( $i+1, 15 );
}
$player_transforms[$t][ $i ] =
$player_transforms[$t][ $indB ];
$player_transforms[$t][ $indB ] = $i;
}
}
}
$player_1_transform = implode( ",", $player_transforms[0] );
$player_2_transform = implode( ",", $player_transforms[1] );
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
*/
$query = "INSERT INTO $tableNamePrefix". "games VALUES ( " .
"'$game_id', '$game_passcode', ".
"'$inPrivate', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ".
"'$inPlayerID', '0000000', ".
"'$player_1_transform', '$player_2_transform', '1', '0', ".
"CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '0' );";
$result = mysql_query( $query );
if( $result ) {
$found_unused_id = 1;
// build empty columns in table
global $blockColumns;
for( $c=0; $c<$blockColumns; $c++ ) {
$query = "INSERT INTO $tableNamePrefix". "columns VALUES ( " .
"'$game_id', '$c', '', 0 );";
gs_queryDatabase( $query );
}
$gameTypeString = "Stranger";
if( $inPrivate ) {
$gameTypeString = "Friend";
}
gs_log( "$gameTypeString game $game_id created by ".
"player $inPlayerID" );
echo "$game_id\n";
echo "$game_passcode\n";
echo "1\n";
echo "$player_1_transform";
}
else {
global $debug;
if( $debug == 1 ) {
echo "Duplicate ids? Error: " . mysql_error() ."<br>";
}
// try again
$salt += 1;
}
}
}
function gs_joinWithFriend() {
$game_id = "";
if( isset( $_REQUEST[ "game_id" ] ) ) {
$game_id = $_REQUEST[ "game_id" ];
}
$game_id = strtoupper( $game_id );
$player_id = "";
if( isset( $_REQUEST[ "player_id" ] ) ) {
$player_id = $_REQUEST[ "player_id" ];
}
$player_id = strtoupper( $player_id );
global $tableNamePrefix;
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
*/
$query = "SELECT * FROM $tableNamePrefix"."games ".
"WHERE game_id = '$game_id' AND player_1_id != '$player_id' ".
"AND player_2_ready = '0';";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
if( $numRows == 1 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$game_passcode = $row[ "game_passcode" ];
$player_2_transform = $row[ "player_2_transform" ];
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP, " .
"player_2_id = '$player_id', " .
"player_2_ready = '1', " .
"player_2_touch_date = CURRENT_TIMESTAMP " .
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
gs_log( "Friend game $game_id joined by player $player_id" );
echo "$game_id\n";
echo "$game_passcode\n";
echo "2\n";
echo "$player_2_transform";
}
else {
gs_operationError(
"Could not find joinable game matching " . $game_id );
}
}
function gs_joinWithStranger() {
$player_id = "";
if( isset( $_REQUEST[ "player_id" ] ) ) {
$player_id = $_REQUEST[ "player_id" ];
}
$player_id = strtoupper( $player_id );
// first, look for non-private games that aren't full
// sort by change date (prefer ones that have been checked by waiting
// opponent most recently)
// ignore those that haven't been checked for 20 seconds or more
// (abandoned?)
// disable autocommit so that FOR UPDATE actually works
gs_queryDatabase( "SET AUTOCOMMIT = 0;" );
global $tableNamePrefix;
$query = "SELECT * FROM $tableNamePrefix"."games ".
"WHERE private = '0' AND player_2_ready = '0' ".
"AND player_1_id != '$player_id' ".
"AND touch_date > SUBTIME( CURRENT_TIMESTAMP, '0 0:0:20.00' ) ".
"ORDER BY touch_date DESC ".
"FOR UPDATE;";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
if( $numRows > 0 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$game_id = $row[ "game_id" ];
$game_passcode = $row[ "game_passcode" ];
$player_2_transform = $row[ "player_2_transform" ];
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP, " .
"player_2_id = '$player_id', " .
"player_2_ready = '1', " .
"player_2_touch_date = CURRENT_TIMESTAMP " .
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
gs_log( "Stranger game $game_id joined by player $player_id" );
echo "$game_id\n";
echo "$game_passcode\n";
echo "2\n";
echo "$player_2_transform";
}
else {
// no empty games found
// create a new public game
gs_createGame( $player_id, 0 );
}
gs_queryDatabase( "COMMIT;" );
gs_queryDatabase( "SET AUTOCOMMIT = 1;" );
}
function gs_isPartnerReady() {
$game_id = "";
if( isset( $_REQUEST[ "game_id" ] ) ) {
$game_id = $_REQUEST[ "game_id" ];
}
$game_passcode = "";
if( isset( $_REQUEST[ "game_passcode" ] ) ) {
$game_passcode = $_REQUEST[ "game_passcode" ];
}
$player_number = "";
if( isset( $_REQUEST[ "player_number" ] ) ) {
$player_number = $_REQUEST[ "player_number" ];
}
$game_id = strtoupper( $game_id );
$game_passcode = strtoupper( $game_passcode );
global $tableNamePrefix;
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
*/
$query = "SELECT * FROM $tableNamePrefix"."games ".
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
if( $numRows == 1 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$otherReady = 0;
if( $player_number == 1 ) {
$otherReady = $row[ "player_2_ready" ];
}
else {
$otherReady = $row[ "player_1_ready" ];
}
echo "$otherReady";
// touched, update timestamp
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP " .
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
}
else {
gs_operationError( "No game matched ID and passcode" );
}
/*
?action=is_partner_ready&game_id=0a4d516&game_passcode=0a4d516e820579bb030f3eb66d0f3035&player_number=1
*/
}
function gs_reportAlive() {
$game_id = "";
if( isset( $_REQUEST[ "game_id" ] ) ) {
$game_id = $_REQUEST[ "game_id" ];
}
$game_passcode = "";
if( isset( $_REQUEST[ "game_passcode" ] ) ) {
$game_passcode = $_REQUEST[ "game_passcode" ];
}
$player_number = "";
if( isset( $_REQUEST[ "player_number" ] ) ) {
$player_number = $_REQUEST[ "player_number" ];
}
$playerTouchDateName = "player_1_touch_date";
if( $player_number == "2" ) {
$playerTouchDateName = "player_2_touch_date";
}
global $tableNamePrefix;
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP, " .
"$playerTouchDateName = CURRENT_TIMESTAMP " .
"WHERE game_id = '$game_id' ".
"AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
echo "1";
}
function gs_postMove() {
$game_id = "";
if( isset( $_REQUEST[ "game_id" ] ) ) {
$game_id = $_REQUEST[ "game_id" ];
}
$game_passcode = "";
if( isset( $_REQUEST[ "game_passcode" ] ) ) {
$game_passcode = $_REQUEST[ "game_passcode" ];
}
$player_number = "";
if( isset( $_REQUEST[ "player_number" ] ) ) {
$player_number = $_REQUEST[ "player_number" ];
}
$num_dirty_columns = "";
if( isset( $_REQUEST[ "num_dirty_columns" ] ) ) {
$num_dirty_columns = $_REQUEST[ "num_dirty_columns" ];
}
$game_id = strtoupper( $game_id );
$game_passcode = strtoupper( $game_passcode );
global $tableNamePrefix;
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
*/
// disable autocommit so that FOR UPDATE actually works
gs_queryDatabase( "SET AUTOCOMMIT = 0;" );
$query = "SELECT * FROM $tableNamePrefix"."games ".
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode' ".
"FOR UPDATE;";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
if( $numRows == 1 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$state_number = $row[ "state_number" ];
// new state
$state_number ++;
for( $c=0; $c<$num_dirty_columns; $c++ ) {
$paramName = "dirty_column_$c";
$columnData = "";
if( isset( $_REQUEST[ $paramName ] ) ) {
$columnData = $_REQUEST[ $paramName ];
}
else {
gs_operationError( "Column missing from input" );
}
//echo "column data = $columnData<br>";
$dataParts = explode( "_", $columnData );
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL," .
"column_index TINYINT NOT NULL," .
"blocks TEXT NOT NULL," .
"changed_in_state INT NOT NULL, ".
"PRIMARY KEY( game_id, column_index ) );";
*/
$column_index = array_shift( $dataParts );
if( $player_number == 2 ) {
// flip left-to-right
global $blockColumns;
$column_index = $blockColumns - 1 - $column_index;
}
$numParts = count( $dataParts );
//echo "numParts = $numParts<br>";
// put remaining blocks back together
$blocks = implode( "_", $dataParts );
//echo "blocks = $blocks<br>";
$query = "UPDATE $tableNamePrefix"."columns SET " .
"blocks = '$blocks', " .
"changed_in_state = '$state_number' " .
"WHERE game_id = '$game_id' AND ".
"column_index = '$column_index';";
$result = gs_queryDatabase( $query );
}
$playerTouchDateName = "player_1_touch_date";
if( $player_number == "2" ) {
$playerTouchDateName = "player_2_touch_date";
}
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP, " .
"$playerTouchDateName = CURRENT_TIMESTAMP, " .
"state_number = '$state_number' " .
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
echo "1";
}
else {
gs_operationError( "No game matched ID and passcode" );
}
/*
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
*/
// unlock rows that were locked by FOR UPDATE above
gs_queryDatabase( "COMMIT;" );
gs_queryDatabase( "SET AUTOCOMMIT = 1;" );
}
function gs_getChangedColumns() {
$game_id = "";
if( isset( $_REQUEST[ "game_id" ] ) ) {
$game_id = $_REQUEST[ "game_id" ];
}
$game_passcode = "";
if( isset( $_REQUEST[ "game_passcode" ] ) ) {
$game_passcode = $_REQUEST[ "game_passcode" ];
}
$player_number = "";
if( isset( $_REQUEST[ "player_number" ] ) ) {
$player_number = $_REQUEST[ "player_number" ];
}
$last_state_seen = "";
if( isset( $_REQUEST[ "last_state_seen" ] ) ) {
$last_state_seen = $_REQUEST[ "last_state_seen" ];
}
$game_id = strtoupper( $game_id );
$game_passcode = strtoupper( $game_passcode );
global $tableNamePrefix;
/*
"CREATE TABLE $tableName(" .
"game_id CHAR(7) NOT NULL PRIMARY KEY," .
"game_passcode CHAR(32) NOT NULL," .
"private TINYINT NOT NULL," .
"creation_date DATETIME NOT NULL," .
"touch_date DATETIME NOT NULL," .
"player_1_id CHAR(7) NOT NULL," .
"player_2_id CHAR(7) NOT NULL," .
"player_1_transform CHAR(37) NOT NULL," .
"player_2_transform CHAR(37) NOT NULL," .
"player_1_ready TINYINT NOT NULL," .
"player_2_ready TINYINT NOT NULL," .
"player_1_touch_date DATETIME NOT NULL," .
"player_2_touch_date DATETIME NOT NULL," .
"state_number INT NOT NULL ) ENGINE = INNODB;";
*/
$query = "SELECT * FROM $tableNamePrefix"."games ".
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
if( $numRows == 1 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$state_number = $row[ "state_number" ];
$partnerTouchDate = $row[ "player_1_touch_date" ];
if( $player_number == 1 ) {
$partnerTouchDate = $row[ "player_2_touch_date" ];
}
// compute seconds since partner touched
$query = "SELECT TIME_TO_SEC( TIMEDIFF( CURRENT_TIMESTAMP, ".
" '$partnerTouchDate' ) );";
$result = gs_queryDatabase( $query );
$secondsSincePartnerActed = mysql_result( $result, 0, 0 );
$query = "SELECT * FROM $tableNamePrefix"."columns ".
"WHERE game_id = '$game_id' AND ".
"changed_in_state > $last_state_seen;";
$result = gs_queryDatabase( $query );
$numChangedColumns = mysql_numrows( $result );
echo "$secondsSincePartnerActed\n";
echo "$state_number\n";
echo "$numChangedColumns\n";
for( $i=0; $i<$numChangedColumns; $i++ ) {
$blocks = mysql_result( $result, $i, "blocks" );
$column_index = mysql_result( $result, $i, "column_index" );
if( $player_number == 2 ) {
// flip left-to-right
global $blockColumns;
$column_index = $blockColumns - 1 - $column_index;
}
echo "$column_index" . "_" . "$blocks\n";
}
$playerTouchDateName = "player_1_touch_date";
if( $player_number == "2" ) {
$playerTouchDateName = "player_2_touch_date";
}
$query = "UPDATE $tableNamePrefix"."games SET " .
"touch_date = CURRENT_TIMESTAMP, " .
"$playerTouchDateName = CURRENT_TIMESTAMP " .
"WHERE game_id = '$game_id' AND game_passcode = '$game_passcode';";
gs_queryDatabase( $query );
}
else {
gs_operationError( "No game matched ID and passcode" );
}
/*
?action=get_changed_columns&game_id=0e86e62&game_passcode=0e86e6239c67bcc56b4b6955a8fd501c&player_number=1&last_state_seen=1
*/
}
// check if we should flush stale games from the database
// do this every 5 minutes
function gs_checkForFlush() {
global $tableNamePrefix;
$tableName = "$tableNamePrefix"."server_globals";
if( !gs_doesTableExist( $tableName ) ) {
return;
}
gs_queryDatabase( "SET AUTOCOMMIT = 0;" );
$query = "SELECT last_flush_time FROM $tableName ".
"WHERE last_flush_time < SUBTIME( CURRENT_TIMESTAMP, '0 0:05:0.000' ) ".
"FOR UPDATE;";
$result = gs_queryDatabase( $query );
if( mysql_numrows( $result ) > 0 ) {
// last flush time is old
global $tableNamePrefix;
$gamesTable = $tableNamePrefix . "games";
$columnsTable = $tableNamePrefix . "columns";
// Games that haven't been fully joined yet
// need to be checked by the creator (player 1) every 5 seconds.
// If they linger 20 seconds without being checked, they are ignored
// by strangers looking to join.
// If they linger 40 seconds without being checked, we drop them.
$query = "DELETE $gamesTable, $columnsTable ".
"FROM $gamesTable JOIN $columnsTable ".
"WHERE player_2_ready = '0' ".
"AND $gamesTable.touch_date < ".
" SUBTIME( CURRENT_TIMESTAMP, '0 0:00:40.00' ) ".
"AND $gamesTable.game_id = $columnsTable.game_id;";
$result = gs_queryDatabase( $query );
$numRowsRemoved = mysql_affected_rows();
gs_log( "Flush operation on unstarted games removed $numRowsRemoved".
" rows." );
// games that have been joined / started can linger for a long
// time with no server action (while players are thinking, etc).
// also, players can quit playing and resume playing later
// (hmmm... do we want to support this?)
// for now, give them 2 hours of idle time
$query = "DELETE $gamesTable, $columnsTable ".
"FROM $gamesTable JOIN $columnsTable ".
"WHERE player_2_ready = '1' ".
"AND $gamesTable.touch_date < ".
" SUBTIME( CURRENT_TIMESTAMP, '0 2:00:0.00' ) ".
"AND $gamesTable.game_id = $columnsTable.game_id;";
$result = gs_queryDatabase( $query );
$numRowsRemoved = mysql_affected_rows();
gs_log( "Flush operation on started games removed $numRowsRemoved".
" rows." );
global $enableLog;
if( $enableLog ) {
// count remaining games for log
$query = "SELECT COUNT(*) FROM $gamesTable;";
$result = gs_queryDatabase( $query );
$count = mysql_result( $result, 0, 0 );
gs_log( "After flush, $count games left." );
}
// set new flush time
$query = "UPDATE $tableName SET " .
"last_flush_time = CURRENT_TIMESTAMP;";
$result = gs_queryDatabase( $query );
}
gs_queryDatabase( "COMMIT;" );
gs_queryDatabase( "SET AUTOCOMMIT = 1;" );
}
// general-purpose functions down here, many copied from seedBlogs
/**
* Connects to the database according to the database variables.
*/
function gs_connectToDatabase() {
global $databaseServer,
$databaseUsername, $databasePassword, $databaseName;
mysql_connect( $databaseServer, $databaseUsername, $databasePassword )
or gs_fatalError( "Could not connect to database server: " .
mysql_error() );
mysql_select_db( $databaseName )
or gs_fatalError( "Could not select $databaseName database: " .
mysql_error() );
}
/**
* Closes the database connection.
*/
function gs_closeDatabase() {
mysql_close();
}
/**
* Queries the database, and dies with an error message on failure.
*
* @param $inQueryString the SQL query string.
*
* @return a result handle that can be passed to other mysql functions.
*/
function gs_queryDatabase( $inQueryString ) {
$result = mysql_query( $inQueryString )
or gs_fatalError( "Database query failed:<BR>$inQueryString<BR><BR>" .
mysql_error() );
return $result;
}
/**
* Checks whether a table exists in the currently-connected database.
*
* @param $inTableName the name of the table to look for.
*
* @return 1 if the table exists, or 0 if not.
*/
function gs_doesTableExist( $inTableName ) {
// check if our table exists
$tableExists = 0;
$query = "SHOW TABLES";
$result = gs_queryDatabase( $query );
$numRows = mysql_numrows( $result );
for( $i=0; $i<$numRows && ! $tableExists; $i++ ) {
$tableName = mysql_result( $result, $i, 0 );
if( $tableName == $inTableName ) {
$tableExists = 1;
}
}
return $tableExists;
}
function gs_log( $message ) {
global $enableLog, $tableNamePrefix;
$slashedMessage = addslashes( $message );
if( $enableLog ) {
$query = "INSERT INTO $tableNamePrefix"."log VALUES ( " .
"'$slashedMessage', CURRENT_TIMESTAMP );";
$result = gs_queryDatabase( $query );
}
}
/**
* Displays the error page and dies.
*
* @param $message the error message to display on the error page.
*/
function gs_fatalError( $message ) {
//global $errorMessage;
// set the variable that is displayed inside error.php
//$errorMessage = $message;
//include_once( "error.php" );
// for now, just print error message
$logMessage = "Fatal error: $message";
echo( $logMessage );
gs_log( $logMessage );
die();
}
/**
* Displays the operation error message and dies.
*
* @param $message the error message to display.
*/
function gs_operationError( $message ) {
// for now, just print error message
echo( "ERROR: $message" );
die();
}
/**
* Recursively applies the addslashes function to arrays of arrays.
* This effectively forces magic_quote escaping behavior, eliminating
* a slew of possible database security issues.
*
* @inValue the value or array to addslashes to.
*
* @return the value or array with slashes added.
*/
function gs_addslashes_deep( $inValue ) {
return
( is_array( $inValue )
? array_map( 'gs_addslashes_deep', $inValue )
: addslashes( $inValue ) );
}
/**
* Recursively applies the stripslashes function to arrays of arrays.
* This effectively disables magic_quote escaping behavior.
*
* @inValue the value or array to stripslashes from.
*
* @return the value or array with slashes removed.
*/
function gs_stripslashes_deep( $inValue ) {
return
( is_array( $inValue )
? array_map( 'sb_stripslashes_deep', $inValue )
: stripslashes( $inValue ) );
}
?>
|