Dan Becker's Game Page

Insert Tournament Play List

Purpose: Use this page to insert multiple tournament play using given form.


// 2006-09-12;Gaugemela;Aaron/Macedon/7;Charles/Persia/5
// Returns an array with keys: date, gameID, gameName, scenarioID, scenarioName, playerCount, nation0 (string), player0 (string), banners0 (string), etc.// Returns a string with error if bad.function validateRecord( $insertRecord, $insertPlayerNames=false ) { global $PHP_SELF; $insertData = array(); $insertRecord = trim( $insertRecord ); $tokens = explode( ";", $insertRecord ); // explode only likes 1 string literal // for ( $i = 0; $i < count( $tokens ); $i++ ) // echo "DEBUG token $i: " . $tokens[ $i ] . ".
\n"; if ( count( $tokens ) < 4 ) return array( "Not enough tokens, token count=" . count( $tokens ) ); // Validate date. $dateStr = $tokens[ 0 ]; $datePieces = explode( "-", $dateStr ); // int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] ) if ( count( $tokens ) < 3 ) return array( "Not enough tokens in date $dateStr." ); $timeStamp = mktime( 0, 0, 0, $datePieces[ 1 ], $datePieces[ 2 ], $datePieces[ 0 ] ); $dateStr = strftime( "%Y-%m-%d", $timeStamp ); $insertData[ "date" ] = $dateStr; // echo "DEBUG: date=$dateStr.
\n"; // Validate scenario/game. $scenarioName = $tokens[ 1 ]; $sql = "SELECT TScenarioNames.id AS scenarioID, TScenarioNames.title AS scenarioName, playerCount, idGameNames AS gameID, TGameNames.name AS gameName FROM TScenarioNames, TGameNames WHERE TScenarioNames.title='$scenarioName' AND (idGameNames = TGameNames.id)"; $sqlResults = sqlQuery( $sql, false ); $rowCount = 0; // echo "DEBUG: row count=" . count( $sqlResults ) . ".
\n"; while ( $row = mysql_fetch_array( $sqlResults, MYSQL_ASSOC )) { $insertData[ "selectedScenarios" ] = $row[ "scenarioID" ]; $insertData[ "scenarioName" ] = $row[ "scenarioName" ]; $insertData[ "playerCount" ] = $row[ "playerCount" ]; $insertData[ "selectedGames" ] = $row[ "gameID" ]; $insertData[ "gameName" ] = $row[ "gameName" ]; $rowCount++; } if ( $rowCount < 1 ) return array( "Bad scenario name '$scenarioName'. Could not find scenario data." ); // echo "DEBUG: found scenario $scenarioName at ID=" . $insertData[ "scenarioID" ] . ".
\n"; // Validate player data. for ( $i = 2; $i < count( $tokens ); $i++ ) { $playerCount = $i - 2; $playRecord = $tokens[ $i ]; $playData = explode( "/", $playRecord ); if ( count( $playData ) == 3 ) { // Player validation $playerName = $playData[ 0 ]; $sql = "SELECT id AS playerID, name AS playerName from TPlayerNames WHERE name='$playerName'"; $sqlResults = sqlQuery( $sql, false ); $rowCount = 0; $key = "player" . strval( $playerCount + 0 ); while ( $row = mysql_fetch_array( $sqlResults, MYSQL_ASSOC )) { $insertData[ $key ] = $row[ "playerID" ]; $rowCount++; } if ( $rowCount == 0 ) return array( "Did not find player '$playerName'." ); // echo "DEBUG: found player $playerName for player $playerCount.
\n"; // Country validation $countryName = $playData[ 1 ]; $sql = "SELECT TCountryNames.id AS countryID, name AS countryName, idGameNames, idScenarioNames, idCountryNames, maxFlags FROM TCountryNames, TScenarioCountry WHERE TCountryNames.name='$countryName' AND (TScenarioCountry.idScenarioNames='" .$insertData["selectedScenarios"]. "') AND (TCountryNames.id = TScenarioCountry.idCountryNames)"; $sqlResults = sqlQuery( $sql, false ); $rowCount = 0; $key = "nation" + $playerCount; $key = "nation" . strval( $playerCount + 0 ); while ( $row = mysql_fetch_array( $sqlResults, MYSQL_ASSOC )) { $insertData[ $key ] = $row[ "countryName" ]; $maxFlags = $row[ "maxFlags" ]; $rowCount++; } if ( $rowCount == 0 ) return array( "Did not find country '$countryName' for scenario '$scenarioName'." ); // echo "DEBUG: found country $countryName for nation $playerCount.
\n"; // Banner validation $banners = $playData[ 2 ] + 0; if (($banners < 0) || ($banners > $maxFlags )) return array( "Banner count '$banners' not in range 0..$maxFlags." ); $key = "banners" . strval( $playerCount + 0 ); $insertData[ $key ] = $banners; // echo "DEBUG: found $banners banners for player $playerCount.
\n"; } else { return array( "Bad player data count near player $playerCount input='$playRecord'." ); } } return $insertData;} // insertRecord// Outputs keys and names as select options.function displayOptions( $names, $selected=-1 ) { foreach ($names as $key => $name) { if ( $key == $selected ) echo " \n"; else echo " \n"; }}function displayInsertForm( $defaultText ) { global $PHP_SELF; global $countryNames, $scenarioNames;?>

Insert game plays here using the following form (warning, the names must be legal and exact):
Form:yyyy-mm-dd;ScenarioName;[Player/CountryName/Banners]*
Examples: 2006-09-12;Cannae;Brady/Rome/7;Dan/Carthage/6
2006-09-12;Gaugemela;Aaron/Macedon/7;Charles/Persia/5
Legal Names
ScenariosCountries