Esempio n. 1
0
     // teamAction
     // ------------------------------
 // ------------------------------
 // teamAction
 // ------------------------------
 case 'teamAction':
     // check inputs
     if (!isset($_GET['gameId']) or !isset($_GET['round']) or !isset($_GET['teamId']) or !isset($_GET['teamToken'])) {
         rollbackAndDie();
     }
     // get the game
     if (!$game->get($gameId)) {
         rollbackAndDie();
     }
     // update the game on the server side
     $game->update();
     // team valid?
     if (!$team->checkIfLegit($teamId, $teamToken)) {
         rollbackAndDie();
     }
     // is the team really in this game? get it
     if (!$team->get($gameId, $teamId)) {
         rollbackAndDie();
     }
     // from now on, before dying, we apply a penalty to this team if something goes wrong
     // can only add actions to a started game
     if ($game->getStatus() != Params::$gameStatus[1]) {
         punishAndDie($team, $game);
     }
     // check the round
     if ($round != $game->getRound()) {
Esempio n. 2
0
 public static function addEventToGame($game_id, $type, $first, $last, $score, $team)
 {
     $mysqli = new mysqli("classroom.cs.unc.edu", "hongkun", "CH@ngemenow99Please!hongkun", "hongkundb");
     /*check the connection*/
     if (mysqli_connect_errno()) {
         printf("Connection failed: %s\n", mysqli_connect_errno);
         exit;
     }
     // First: add the score to specific team, if team is found
     $teamSelected = $mysqli->query("select * from Game where (Team1 = '{$team}' || Team2 = '{$team}') && game_id = '{$game_id}'");
     if ($teamSelected->num_rows == 0) {
         header("HTTP/1.0 400 Bad Request");
         printMessageJSON("Bad Request, Team Not Match");
         exit;
     }
     $teamSelected = $teamSelected->fetch_row();
     $allAcore = [$teamSelected[1] => $teamSelected[3], $teamSelected[2] => $teamSelected[4]];
     if ($type == "fieldgoal") {
         $allAcore[$team] += 3;
     } else {
         if ($type == "passing") {
             $allAcore[$team] += 7;
         } else {
             if ($type == "rushing") {
                 $allAcore[$team] += 7;
             }
         }
     }
     // if the score is not match, exit();
     if ($allAcore[$team] != $score) {
         header("HTTP/1.0 400 Bad Request");
         printMessageJSON("Bad Request, Score Not Match");
         exit;
     }
     if ($allAcore[$teamSelected[1]] >= $allAcore[$teamSelected[2]]) {
         $winner = [$teamSelected[1] => $allAcore[$teamSelected[1]]];
         $loser = [$teamSelected[2] => $allAcore[$teamSelected[2]]];
     } else {
         $winner = [$teamSelected[2] => $allAcore[$teamSelected[2]]];
         $loser = [$teamSelected[1] => $allAcore[$teamSelected[1]]];
     }
     // Update the team score in Game Table.
     $gameUpdated = game::update($game_id, key($winner), key($loser), current($winner), current($loser));
     // Second: find the player in the team specified.
     // if it exists, return p_id, if not, insert it and return the p_id.
     $player_id = $mysqli->query("select player_id from Player where first_name = '{$first}' && last_name = '{$last}' && team_name = '{$team}'");
     if ($player_id->num_rows == 0) {
         $newPlayer = player::insert($first, $last, $team);
         $newPlayer = (array) $newPlayer;
         $player_id = intval($newPlayer["player_id"]);
     } else {
         $player_id = $player_id->fetch_row()[0];
     }
     // Use the p_id and game_id to find the event of the type,
     // if it exists, increase the number of the type
     // if not, insert the event.
     $eventSelected = $mysqli->query("select event_id from Event where p_id = '{$player_id}' && g_id = '{$game_id}'");
     $cnt = ["rushing" => 0, "passing" => 0, "fieldgoal" => 0, "passTo" => NULL];
     $cnt[$type]++;
     if ($eventSelected->num_rows == 0) {
         $event_id = event::insert($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
     } else {
         $event_id = event::update($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
     }
     // var_dump($event_id);
     $ids = [$game_id, $player_id];
     return $ids;
 }