if ($game_i[5] == "passing") {
        $fn = $game_i[6];
        $ln = $game_i[7];
        $tn = $game_i[2];
        $passToPlayer = $conn->query("select player_id from Player where first_name = '{$fn}' && last_name = '{$ln}' && team_name = '{$tn}' ");
        if ($passToPlayer->num_rows == 0) {
            printf("Error: No passingTO player found!");
            exit;
        }
        $cnt["passTo"] = $passToPlayer->fetch_row()[0];
    }
    $exist = $conn->query("select * from Event where p_id = '{$player_id}' AND g_id = '{$game_id}'");
    if ($exist->num_rows == 0) {
        // this is a new Event, insert to the Event table.
        // rushing touch down should be initialized.
        $result = event::insert($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
    } else {
        // the event is already inserted.
        // update the table.
        $result = event::update($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
    }
    $event_id = $result->event_id;
    $player_id = $result->player_id;
    $game_id = $result->game_id;
    printf("insert Event " . "{$event_id}: " . "{$player_id}" . " and " . "{$game_id}" . " succeeded!");
    ?>
	<br>
	<?php 
}
?>
예제 #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;
 }