/* notify opponent of invitation via email */
                     webchessMail('invitation', $opponentEmail, '', $_SESSION['nick']);
                 }
             }
         }
     }
     break;
 case 'ResponseToInvite':
     if ($_POST['response'] == 'accepted') {
         /* update game data */
         $tmpQuery = "UPDATE games SET gameMessage = '', messageFrom = '' WHERE gameID = " . $_POST['gameID'];
         mysql_query($tmpQuery);
         /* setup new board */
         $_SESSION['gameID'] = $_POST['gameID'];
         createNewGame($_POST['gameID']);
         saveGame();
     } else {
         $tmpQuery = "UPDATE games SET gameMessage = 'inviteDeclined', messageFrom = '" . $_POST['messageFrom'] . "' WHERE gameID = " . $_POST['gameID'];
         mysql_query($tmpQuery);
     }
     break;
 case 'WithdrawRequest':
     /* get opponent's player ID */
     $tmpOpponentID = mysql_query("SELECT whitePlayer FROM games WHERE gameID = " . $_POST['gameID']);
     if (mysql_num_rows($tmpOpponentID) > 0) {
         $opponentID = mysql_result($tmpOpponentID, 0);
         if ($opponentID == $_SESSION['playerID']) {
             $tmpOpponentID = mysql_query("SELECT blackPlayer FROM games WHERE gameID = " . $_POST['gameID']);
             $opponentID = mysql_result($tmpOpponentID, 0);
         }
         $tmpQuery = "DELETE FROM games WHERE gameID = " . $_POST['gameID'];
Example #2
0
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
require_once "db.inc.php";
$postData = file_get_contents("php://input");
$request = json_decode($postData);
$method = $request->method;
$username = $request->username;
if ($method == "save") {
    $title = $request->title;
    $hint = $request->hint;
    $wordFinal = $request->wordFinal;
    $wordTemp = $request->wordTemp;
    $wordLength = $request->wordLength;
    $correct = $request->correct;
    $clickedString = $request->clickedString;
    $bad = $request->bad;
    saveGame($username, $title, $hint, $wordFinal, $wordTemp, $wordLength, $correct, $bad, $clickedString);
} else {
    if ($method == "retrieve") {
        getGames($username);
    } else {
        if ($method == "delete") {
            $title = $request->title;
            deleteGame($username, $title);
        }
    }
}
/*Table: ID, Username, Title, wordFinal, wordTemp, wordLength, correct, bad*/
function saveGame($username, $title, $hint, $wordFinal, $wordTemp, $wordLength, $correct, $bad, $clickedString)
{
    $data = array();
    $connection = pdo_connect();
function create_games($id) {

global $_SESSION;

$t = mysql_query("SELECT * FROM tournaments WHERE id = '$id'");
$t = mysql_fetch_array($t);

if ($t['players'] == 4)
{

$games = array(
          1 => array('whitePlayer' => $t['player1'], 'blackPlayer' => $t['player2']),
          2 => array('whitePlayer' => $t['player1'], 'blackPlayer' => $t['player3']),
          3 => array('whitePlayer' => $t['player1'], 'blackPlayer' => $t['player4']),

          4 => array('whitePlayer' => $t['player2'], 'blackPlayer' => $t['player1']),
          5 => array('whitePlayer' => $t['player2'], 'blackPlayer' => $t['player3']),
          6 => array('whitePlayer' => $t['player2'], 'blackPlayer' => $t['player4']),

          7 => array('whitePlayer' => $t['player3'], 'blackPlayer' => $t['player1']),
          8 => array('whitePlayer' => $t['player3'], 'blackPlayer' => $t['player2']),
          9 => array('whitePlayer' => $t['player3'], 'blackPlayer' => $t['player4']),

         10 => array('whitePlayer' => $t['player4'], 'blackPlayer' => $t['player1']),
         11 => array('whitePlayer' => $t['player4'], 'blackPlayer' => $t['player2']),
         12 => array('whitePlayer' => $t['player4'], 'blackPlayer' => $t['player3']));

         while (list($key, $val) = each($games))
         {

                 $query = "INSERT INTO {$db_prefix}games (timelimit, gameMessage,messageFrom, whitePlayer, blackPlayer, dateCreated,
                 lastMove, ratingWhite, ratingBlack,ratingWhiteM,ratingBlackM,PVBlack,PVWhite,tournament,oficial,thematic)
                 VALUES (";

                 $white = $games[$key]['whitePlayer'];
        $black = $games[$key]['blackPlayer'];

                 $query .= "".$t['days'].", '', '', $white, $black, NOW(), NOW(),".getRating($white).",".getRating($black).",".getRatingMonth($white).",".getRatingMonth($black).",".getPV($black).",".getPV($white).", '$id', '".$t['official']."','".$t['thematic']."')";

                 mysql_query($query);

                 echo mysql_error();

         } // while

         $query = mysql_query("SELECT game_id FROM {$db_prefix}games WHERE tournament = '$id'");

    while ($g = mysql_fetch_array($query))
    {
                 if (!function_exists('createNewGame'))
                 {
                    include 'newgame.php';
                 }

                 $_SESSION['gameID'] = $g['gameID'];
        createNewGame($_SESSION['gameID']);
        saveGame();

         } // while

} // 4 players

}
Example #4
0
} else {
    $guess = $_POST['guess'];
    $_SESSION['score']++;
    if ($guess > $choice) {
        $_SESSION['response'] = "C'est moins";
    } elseif ($guess < $choice) {
        $_SESSION['response'] = "C'est plus";
    } else {
        $_SESSION['response'] = "C'est gagné";
        if (!isset($_SESSION['best_score']) || $_SESSION['best_score'] > $_SESSION['score']) {
            $_SESSION['best_score'] = $_SESSION['score'];
            updateBestScore($pdo);
        }
        unset($_SESSION['choice']);
    }
    saveGame($pdo);
}
function displayLeaderboard($pdo)
{
    $stmt = $pdo->prepare("SELECT login, best_score from user ORDER BY `best_score` LIMIT 0,10");
    $stmt->execute();
    echo '<table border="1px">';
    echo '<th>name</th><th>Score</th>';
    while ($result = $stmt->fetch()) {
        echo '<tr>' . '<td>' . $result['login'] . '</td>' . '<td>' . $result['best_score'] . '</td>' . '</tr>';
    }
    echo '</table>';
}
function updateBestScore($pdo)
{
    $stmt = $pdo->prepare("UPDATE user SET `best_score` = :best where id = :id");
function create_match_games($match_id){
   //load up all signed up players and arrange in order
   $tmp_teams = mysql_query("SELECT * FROM matches WHERE match_id = '$match_id'");
   $teams = db_result_to_array($tmp_teams);
   $team1 = $teams[0][1];
   $team2 = $teams[0][2];
   $boards = $teams[0][6];
   //load up player IDs for team 1
   $tmp_players = mysql_query("SELECT playerID FROM match_players WHERE match_id = '$match_id' AND teamID = '$team1'");
   $team1_players = db_result_to_array($tmp_players);
   for ($i=0;$i<$boards;$i++){
      $tmp_player_info = mysql_query("SELECT rating FROM {$db_prefix}players WHERE playerID = ".$team1_players[$i][0]);
      $tmp_rating = mysql_fetch_array($tmp_player_info);
      $rating=$tmp_rating[0];
      $team1_players[$i][1] = $rating;
#      echo "Player team 1, player $i: ".$team1_players[$i][0]." rating = ".$team1_players[$i][1]."<br>";
   }
   usort ($team1_players, 'compare');//sort in rank order

   //load up player IDs for team 2
   $tmp_players = mysql_query("SELECT playerID FROM match_players WHERE match_id = '$match_id' AND teamID = '$team2'");
   $team2_players = db_result_to_array($tmp_players);
   for ($i=0;$i<$boards;$i++){
      $tmp_player_info = mysql_query("SELECT rating FROM {$db_prefix}players WHERE playerID = ".$team2_players[$i][0]);
      $tmp_rating = mysql_fetch_array($tmp_player_info);
      $rating=$tmp_rating[0];
      $team2_players[$i][1] = $rating;
#      echo "Player team 2, player $i: ".$team2_players[$i][0]." rating = ".$team2_players[$i][1]."<br>";
   }
   usort ($team2_players, 'compare');//sort in rank order

   //debug line
#   echo "<br>".$team1_players[0][0]." : ".$team1_players[0][1]."<br>".$team1_players[1][0]." : ".$team1_players[1][1]."<br>".$team2_players[0][0]." : ".$team2_players[0][1]."<br>".$team2_players[1][0]." : ".$team2_players[1][1];


//for each pair, create first game
$tmpColor = 'white';//set first team 1 player to white
for ($i=0;$i<$boards;$i++){
   $tmpQuery = "INSERT INTO {$db_prefix}games (white_player, black_player, status, message_from, dateCreated, lastMove, ratingWhite, ratingBlack, ratingWhiteM, ratingBlackM, oficial, PVBlack, PVWhite, timelimit, teamMatch, team) VALUES (";
   if ($tmpColor == 'white'){
      $white = $team1_players[$i][0];
      $black = $team2_players[$i][0];
      $tmpColor = 'black';
   }else{
      $white = $team2_players[$i][0];
        $black = $team1_players[$i][0];
        $tmpColor = 'white';
    }
   $tmpQuery .= "$white, $black, '', '', NOW(), NOW(),".getRating($white).",".getRating($black).",".getRatingMonth($white).",".getRatingMonth($black).",'1',".getPV($black).",".getPV($white).",".getMatchTimeLimit($match_id).",'1',$match_id)";
   mysql_query($tmpQuery);

   /* if email notification is activated... */
#   if ($CFG_USEEMAILNOTIFICATION){
#      /* if opponent is using email notification... */
#      $tmpOpponentEmail = mysql_query("SELECT value FROM ch_preferences WHERE playerID = ".$_POST['opponent']." AND preference = 'emailNotification'");
#      if (mysql_num_rows($tmpOpponentEmail) > 0){
#         $opponentEmail = mysql_result($tmpOpponentEmail, 0);
#         if ($opponentEmail != ''){
#            /* notify opponent of invitation via email */
#            webchessMail('invitation', $opponentEmail, '', $_SESSION['nick']);
#         }
#      }
#   }

   // setup new board
   $game_id = mysql_insert_id();//   get ID of new game
   $_SESSION['game_id'] = $game_id;
   createNewGame($game_id);
   saveGame();
}
//for each pair, create second game
$tmpColor = 'black';//set first team 1 player to white
for ($i=0;$i<$boards;$i++){
   $tmpQuery = "INSERT INTO {$db_prefix}games (white_player, black_player, status, message_from, dateCreated, lastMove, ratingWhite, ratingBlack, ratingWhiteM, ratingBlackM, oficial, PVBlack, PVWhite, timelimit, teamMatch, team) VALUES (";
   if ($tmpColor == 'black'){
      $black = $team1_players[$i][0];
      $white = $team2_players[$i][0];
      $tmpColor = 'white';
   }else{
      $black = $team2_players[$i][0];
        $white = $team1_players[$i][0];
        $tmpColor = 'black';
    }
    $tmpQuery .= "$white, $black, '', '', NOW(), NOW(),".getRating($white).",".getRating($black).",".getRatingMonth($white).",".getRatingMonth($black).",'1',".getPV($black).",".getPV($white).",".getMatchTimeLimit($match_id).",'1',$match_id)";
   mysql_query($tmpQuery);

   /* if email notification is activated... */
#   if ($CFG_USEEMAILNOTIFICATION){
#      /* if opponent is using email notification... */
#      $tmpOpponentEmail = mysql_query("SELECT value FROM ch_preferences WHERE playerID = ".$_POST['opponent']." AND preference = 'emailNotification'");
#      if (mysql_num_rows($tmpOpponentEmail) > 0){
#         $opponentEmail = mysql_result($tmpOpponentEmail, 0);
#         if ($opponentEmail != ''){
#            /* notify opponent of invitation via email */
#            webchessMail('invitation', $opponentEmail, '', $_SESSION['nick']);
#         }
#      }
#   }

   // setup new board
   $game_id = mysql_insert_id();//   get ID of new game
   $_SESSION['game_id'] = $game_id;
   createNewGame($game_id);
   saveGame();
}
  //update match table
   mysql_query("UPDATE matches SET status = '' WHERE match_id = '$match_id'");
}
Example #6
0
function main()
{
    $nick = getNickFromArgs();
    $mapData = null;
    $charData = null;
    // This will force everyone into creating a Dynasty save.
    // We patching shit, yo.
    $dynPatch = false;
    $dynPath = getSaveFilePath($nick, SaveFileType::Dynasty);
    if (!file_exists($dynPath)) {
        saveGame($nick, SaveFileType::Dynasty);
        $dynPatch = true;
    }
    if (!checkIfNewGame($nick)) {
        // Load character save data.
        $charFilePath = getSaveFilePath($nick, SaveFileType::Character);
        $charData = FileIO::UnserializeFile($charFilePath);
        $charDataDirty = false;
        // Load map save data.
        $mapFilePath = getSaveFilePath($nick, SaveFileType::Map);
        $mapData = FileIO::UnserializeFile($mapFilePath);
        $mapDataDirty = false;
        // Load dynasty save data.
        $dynFilePath = getSaveFilePath($nick, SaveFileType::Dynasty);
        $dynData = FileIO::UnserializeFile($dynFilePath);
        $dynDataDirty = false;
        // Put everyone into the dynasty initialisation state, just this once.
        //$notYetPatched	= !isset($charData->patched);
        $notYetPatched = !isset($dynData->initialised);
        $notPatching = $charData->state != GameStates::DynastyInit;
        if (empty($dynData) || $notYetPatched && $notPatching) {
            $charData->patchState = $charData->state;
            $charData->patchPrevState = $charData->previousState;
            DEBUG_echo("Patching in Dynasty...");
            StateManager::ChangeState($charData, GameStates::DynastySplash);
        }
        // Patch the stat changes in.
        doStatPatchIfNeeded($charData);
        // Ensure it's sane.
        if (empty($charData) || empty($mapData)) {
            echo "ERROR: Save data's f****d.\n";
            exit(3);
        }
        // Read STDIN for input.
        $input = readStdin();
        switch ($charData->state) {
            case GameStates::DynastySplash:
                DEBUG_echo("DynastySplash");
                echo "Your Dynasty begins, and needs a name. Choose your name wisely - you cannot alter history.\n";
                StateManager::ChangeState($charData, GameStates::DynastyInit);
                $dynData->initialised = true;
                $charDataDirty = true;
                $dynDataDirty = true;
                break;
            case GameStates::DynastyInit:
                DEBUG_echo("DynastyInit");
                // Validate input.
                $validName = preg_match("/^[a-zA-Z]{1,16}\$/", $input, $output);
                if (!$validName) {
                    echo "Please enter a valid name. Letters only, between 1 and 16 characters.\n";
                    return;
                }
                $dynData->name = $input;
                $output = "The Dynasty of {$input} begins! Onwards, to adventure!";
                echo "{$output}\n";
                // Hook back up to where we were.
                $charData->state = $charData->patchState;
                $charData->previousState = $charData->patchPrevState;
                $charDataDirty = true;
                $dynDataDirty = true;
                break;
            case GameStates::NameSelect:
                DEBUG_echo("NameSelect");
                $validName = preg_match("/^[a-zA-Z]{1,16}\$/", $input, $output);
                if (!$validName) {
                    echo "Please enter a valid name. Letters only, between 1 and 16 characters.\n";
                    exit(13);
                }
                $output = "Please choose a class for {$input} {$dynData->name}: ";
                global $classSelect;
                foreach ($classSelect->commands as $fragment) {
                    $output .= "{$fragment->displayString}, ";
                }
                $output = rtrim($output, ", ") . "\n";
                echo $output;
                $charData->name = $input;
                StateManager::ChangeState($charData, GameStates::ClassSelect);
                $charDataDirty = true;
                break;
            case GameStates::ClassSelect:
                DEBUG_echo("ClassSelect");
                $input = strtolower($input);
                $setClass = classSelect($input, $charData, $dynData, $charData->name);
                if ($setClass) {
                    StateManager::ChangeState($charData, GameStates::FirstPlay);
                    $charDataDirty = true;
                }
                break;
                // Initialise the characters
            // Initialise the characters
            case GameStates::FirstPlay:
                DEBUG_echo("FirstPlay");
                firstPlay($charData, $dynData);
                StateManager::ChangeState($charData, GameStates::Adventuring);
                // purposeful fall-through!
                // The main loop for when we're romping around.
            // purposeful fall-through!
            // The main loop for when we're romping around.
            case GameStates::Adventuring:
                DEBUG_echo("Adventuring");
                adventuring($input, $charData, $mapData, $dynData);
                $charDataDirty = true;
                $mapDataDirty = true;
                break;
                // Sleepy nap time.
            // Sleepy nap time.
            case GameStates::Resting:
                DEBUG_echo("Resting");
                resting($input, $charData, $mapData);
                $charDataDirty = true;
                break;
                // IT'S CLOBBERING TIME
            // IT'S CLOBBERING TIME
            case GameStates::Combat:
                DEBUG_echo("Combat");
                combat($input, $charData, $mapData, $dynData);
                $charDataDirty = true;
                $mapDataDirty = true;
                break;
            case GameStates::Spellcasting:
                DEBUG_echo("Spellcasting");
                $nonCombat = isset($charData->previousState) && $charData->previousState != GameStates::Combat;
                spellcasting($input, $charData, $mapData, $nonCombat);
                $charDataDirty = true;
                $mapDataDirty = true;
                break;
            case GameStates::Looting:
                DEBUG_echo("Looting");
                looting($input, $charData, $mapData);
                $charDataDirty = true;
                $mapDataDirty = true;
                break;
            case GameStates::LevelUp:
                DEBUG_echo("LevelUp");
                levelUp($input, $charData, $mapData);
                $charDataDirty = true;
                break;
            case GameStates::UsingItem:
                DEBUG_echo("UsingItem");
                $nonCombat = isset($charData->previousState) && $charData->previousState != GameStates::Combat;
                usingItem($input, $charData, $mapData, $nonCombat);
                $charDataDirty = true;
                break;
            case GameStates::Shopping:
                DEBUG_echo("Shopping");
                shopping($input, $charData, $mapData);
                $charDataDirty = true;
                $mapDataDirty = true;
                break;
            case GameStates::Dynasty:
                DEBUG_echo("Dynasty");
                dynasty($input, $charData, $mapData, $dynData);
                $charDataDirty = true;
                $dynDataDirty = true;
                break;
            default:
                break;
        }
    } else {
        // Initialise the character save.
        saveGame($nick, SaveFileType::Character);
        // Initialise the map save.
        saveGame($nick, SaveFileType::Map);
        // Prompt for name/dynasty select.
        if (!$dynPatch) {
            echo "How do you want to alter your Dynasty?\n";
        } else {
            echo "Welcome to blaventure!\n";
        }
    }
    if (isset($charData) && $charDataDirty) {
        saveGame($nick, SaveFileType::Character, $charData);
    }
    if (isset($mapData) && $mapDataDirty) {
        saveGame($nick, SaveFileType::Map, $mapData);
    }
    if (isset($dynData) && $dynDataDirty) {
        saveGame($nick, SaveFileType::Dynasty, $dynData);
    }
}