コード例 #1
0
ファイル: table_move.php プロジェクト: uunuu/dot_boxes
 public static function add_new_move($game_id, $x, $y, $edgePosition, $player_id)
 {
     $date = microtime(true);
     //TEST:
     //game_id is valid
     //player_id is valid
     //consistent $x and $y coordinates with the game size
     //valid edgePosition (it has to be one of the values: 1 (NORTH), 2 (SOUTH), 3 (EAST) , 4 (WEST) )
     //secure the input
     if (safe_input::is_number($game_id) && safe_input::is_number($x) && safe_input::is_number($y) && safe_input::is_number($edgePosition) && safe_input::is_number($player_id)) {
         //validate game_id
         $game_info = game::getGameById($game_id);
         if ($game_info == false) {
             return false;
             //no game exists with the given id
         }
         $other_player_id = -1;
         //check if player_id is a player in the game
         if ($player_id == $game_info['player1ID']) {
             $other_player_id = $game_info['player2ID'];
         } elseif ($player_id == $game_info['player2ID']) {
             $other_player_id = $game_info['player1ID'];
         } else {
             return false;
             //ERROR!! (player tring to play is not part of the game
         }
         //check if it is the player's turn
         if ($player_id != $game_info['currentTurnPlayerID']) {
             return false;
             //it is not the player's turn!
         }
         //check coordinates consistency
         if ($x > $game_info['size'] || $y > $game_info['size'] || $x < 1 || $y < 1) {
             return false;
             //invalid move coordinates
         }
         //check edigePosition
         if ($edgePosition != 1 && $edgePosition != 2 && $edgePosition != 3 && $edgePosition != 4) {
             return false;
             //invalid edgePosition value!
         }
         //add the move
         $db = new database();
         $query[] = "INSERT INTO `move` (`gameID`, `date`, `x`, `y`, `edgePosition`, `playerID`) VALUES ( '{$game_id}', '{$date}', '{$x}', '{$y}', '{$edgePosition}' , '{$player_id}');";
         $query[] = "UPDATE  `game` SET  `currentTurnPlayerID` =  '{$other_player_id}' , `lastActivityDate` = '{$date}' WHERE  `id` = '{$game_id}'";
         $res = $db->execute_transaction($query);
         return $res;
     } else {
         return false;
         //ERROR!!!! (invalid input!!)
     }
 }
コード例 #2
0
ファイル: table_moveTest.php プロジェクト: uunuu/dot_boxes
 public function test()
 {
     $username = "******";
     $password = "******";
     $email = "*****@*****.**";
     $username2 = "bla2";
     $password2 = "pass2";
     $email2 = "*****@*****.**";
     user::create_new_user($username, $password, $email);
     $this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     user::create_new_user($username2, $password2, $email2);
     $this->assertEquals(2, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     $user1ID = user::getUserByUsername($username)['id'];
     $user2ID = user::getUserByUsername($username2)['id'];
     $game_size = 6;
     $this->assertTrue(game::add_new_game($user1ID, $game_size, $user1ID, $user2ID), "failed to add a new game");
     $game_id1 = game::$last_inserted_id;
     $this->assertFalse(move::add_new_move(100, 1, 1, 1, $user1ID), "accepted invalid game_id [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, -1, 1, 1, $user1ID), "accepted invalid x coordinate [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 1, -5, 1, $user1ID), "accepted invalid y coordinate [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 8, 1, 1, $user1ID), "accepted invalid x coordinate (larger than the size) [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 1, 8, 1, $user1ID), "accepted invalid y coordinate (larger than the size) [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 1, 1, 5, $user1ID), "accepted invalid edge position  [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 1, 1, 2, 90), "accepted invalid playerID (ID doesn't even exist)  [move::add_new_move]");
     $this->assertFalse(move::add_new_move($game_id1, 1, 1, 2, $user2ID), "accepted invalid playerID (not the player turn)  [move::add_new_move]");
     $game_info = game::getGameById($game_id1);
     $this->assertNull($game_info['lastActivityDate'], "when move fails the transaction is not rolledback![move::add_new_move]");
     $this->assertTrue(move::add_new_move($game_id1, 1, 1, 1, $user1ID), "failed to add new move");
     $game_info = game::getGameById($game_id1);
     $this->assertTrue($game_info['currentTurnPlayerID'] == $user2ID, "when adding a new move, method failed to change the player's turn");
     //get_last_move_for_game_id($game_id)
     $move = move::get_last_move_for_game_id($game_id1);
     $this->assertEquals($game_id1, $move['gameID'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(1, $move['x'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(1, $move['y'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(1, $move['edgePosition'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals($user1ID, $move['playerID'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals($game_info['lastActivityDate'], $move['date'], "the last activity date in the game table is not consistent with the move table! [move::add_new_move]");
     $this->assertTrue(move::add_new_move($game_id1, 2, 3, 4, $user2ID), "failed to add new move");
     $move2 = move::get_last_move_for_game_id($game_id1);
     $this->assertEquals($user2ID, $move2['playerID'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(2, $move2['x'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(3, $move2['y'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(4, $move2['edgePosition'], "inccorect last move [get_last_move_for_game_id()]");
     move::add_new_move($game_id1, 2, 1, 4, $user1ID);
     move::add_new_move($game_id1, 2, 2, 4, $user2ID);
     $move3 = move::get_last_move_for_game_id($game_id1);
     $this->assertEquals($user2ID, $move3['playerID'], "inccorect last move [get_last_move_for_game_id()]");
     $this->assertEquals(2, $move3['y'], "inccorect last move [get_last_move_for_game_id()]");
     move::clear_table();
     $this->assertFalse(move::get_last_move_for_game_id($game_id1), "inccorect last move [get_last_move_for_game_id()]");
     //count_moves($game_id)
     $this->assertEquals(0, move::count_moves($game_id1), "number of moves should be ZERO [count_moves()]");
     $this->assertTrue(move::add_new_move($game_id1, 2, 3, 4, $user1ID), "failed to add new move");
     $this->assertEquals(1, move::count_moves($game_id1), "number of moves should be ONE [count_moves()]");
     //get_all_moves_after_given_date($game_id,$timestamp)
     $move4 = move::get_last_move_for_game_id($game_id1);
     $this->assertNull(move::get_all_moves_after_given_date($game_id1, $move4['date']), "[move::get_all_moves_after_given_date()]");
     $this->assertTrue(move::add_new_move($game_id1, 4, 3, 1, $user2ID), "failed to add new move");
     $this->assertEquals(2, move::count_moves($game_id1), "number of moves should be two [count_moves()]");
     $move5 = move::get_all_moves_after_given_date($game_id1, $move4['date']);
     $this->assertEquals(1, count($move5), "number of moves returned should be one [move::get_all_moves_after_given_date()]");
     $this->assertEquals($game_id1, $move5[0]['gameID'], "[move::get_all_moves_after_given_date()]");
     $this->assertTrue(move::add_new_move($game_id1, 5, 5, 2, $user1ID), "failed to add new move");
     $this->assertEquals(2, count(move::get_all_moves_after_given_date($game_id1, $move4['date'])), "number of moves returned should be two [move::get_all_moves_after_given_date()]");
     //move::delete_move($move_id)
     $move_id = $move5[0]['gameID'];
     move::delete_move($move_id);
     $this->assertEquals(1, count(move::get_all_moves_after_given_date($game_id1, $move5[0]['date'])), "[move::delet_move()]");
     //move::get_all_moves_for_game_id($game_id)
     $this->assertEquals(move::count_moves($game_id1), count(move::get_all_moves_for_game_id($game_id1)), "[move::get_all_moves_for_game_id()]");
     $this->assertNull(move::get_all_moves_for_game_id("bla"), "[move::get_all_moves_for_game_id()]");
     $this->assertNull(move::get_all_moves_for_game_id("0"), "[move::get_all_moves_for_game_id()]");
 }
コード例 #3
0
ファイル: table_gameTest.php プロジェクト: uunuu/dot_boxes
 public function test()
 {
     $username = "******";
     $password = "******";
     $email = "*****@*****.**";
     $username2 = "bla2";
     $password2 = "pass2";
     $email2 = "*****@*****.**";
     user::create_new_user($username, $password, $email);
     $this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     user::create_new_user($username2, $password2, $email2);
     $this->assertEquals(2, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     $user1ID = user::getUserByUsername($username)['id'];
     $user2ID = user::getUserByUsername($username2)['id'];
     $gmae_size = 6;
     $this->assertFalse(game::add_new_game($user1ID, "z", $user1ID, $user2ID), "invalid size went through OK!");
     $this->assertFalse(game::add_new_game("s", $gmae_size, $user1ID, $user2ID), "invalid curent player id went through OK!");
     $this->assertFalse(game::add_new_game($user1ID, $gmae_size, "x", $user2ID), "invalid player 1 id went through OK!");
     $this->assertFalse(game::add_new_game($user1ID, $gmae_size, $user1ID, "d"), "invalid player 2 id went through OK!");
     $this->assertFalse(game::add_new_game(20, $gmae_size, $user1ID, $user2ID), "current turn id was different from p1 and p2 and it when through!");
     $this->assertTrue(game::add_new_game($user1ID, $gmae_size, $user1ID, $user2ID), "failed to add a new game");
     $this->assertEquals($gmae_size, game::getGameById(1)['size'], "incorrect game size");
     $this->assertEquals($user1ID, game::getGameById(1)['player1ID'], "incorrect player 1 id ");
     $this->assertEquals($user2ID, game::getGameById(1)['player2ID'], "incorrect player 2 id ");
     $this->assertNull(game::getGameById(1)['winnerID'], "winnder ID should be null");
     $this->assertNull(game::getGameById(1)['lastActivityDate'], "lastActivityDate should be null");
     $this->assertEquals(1, game::getNumberOfGames(), "number of games should be equal to 1");
     game::clear_table();
     $this->assertEquals(0, game::getNumberOfGames(), "number of games should be equal to 0");
     game::add_new_game($user1ID, $gmae_size, $user1ID, $user2ID);
     $game_id = game::$last_inserted_id;
     $this->assertTrue(game::setLastActivityDate($game_id), "setLastActivityDate is not working");
     $this->assertEquals(time() / 60 % 60, game::getGameById($game_id)['lastActivityDate'] / 60 % 60);
     game::clear_table();
     //security test: passing a user id that is not a number
     $this->assertEquals(-1, game::getNumberOfGamesForUserId("ff"), "getNumberOfGamesForUserId() should accept only numbers");
     //the user shouldn't have any games
     $this->assertEquals(0, game::getNumberOfGamesForUserId($user2ID), "The player shouldn't have any games [getNumberOfGamesForUserId()]");
     game::add_new_game($user1ID, $gmae_size, $user1ID, $user2ID);
     $game_id1 = game::$last_inserted_id;
     $this->assertEquals(1, game::getNumberOfGamesForUserId($user1ID), "player should have 1 game [getNumberOfGamesForUserId()]");
     $username3 = "bla3";
     $password3 = "pass3";
     $email3 = "*****@*****.**";
     user::create_new_user($username3, $password3, $email3);
     $user3ID = user::getUserByUsername($username3)['id'];
     $this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     $this->assertTrue(game::add_new_game($user3ID, $gmae_size, $user3ID, $user1ID), "failed to add a new game");
     $game_id2 = game::$last_inserted_id;
     $this->assertEquals(2, game::getNumberOfGames(), "number of games should be equal to 2");
     $this->assertEquals(2, game::getNumberOfGamesForUserId($user1ID), "player should have two games [getNumberOfGamesForUserId()]");
     $this->assertEquals(1, game::getNumberOfGamesForUserId($user2ID), "player should have one game [getNumberOfGamesForUserId()]");
     $this->assertEquals(1, game::getNumberOfGamesForUserId($user3ID), "player should have one game [getNumberOfGamesForUserId()]");
     $this->assertEquals(2, count(game::getAllGamesForUserId($user1ID)), "two games should be returned [game::getAllGamesForUserId()]");
     $this->assertEquals($user1ID, game::getAllGamesForUserId($user1ID)[0]['player1ID'], "[game::getAllGamesForUserId()]");
     $this->assertEquals($user1ID, game::getAllGamesForUserId($user1ID)[1]['player2ID'], "[game::getAllGamesForUserId()]");
     $this->assertNull(game::getAllGamesForUserId(88), "should return null because player doesn't have games [game::getAllGamesForUserId()]");
     $this->assertNull(game::getOpponentId(1000, 1000), "[game::getOpponentId]");
     $this->assertNull(game::getOpponentId(1000, 1000), "[game::getOpponentId]");
     $this->assertNull(game::getOpponentId($game_id1, 1000), "[game::getOpponentId]");
     $this->assertNull(game::getOpponentId(1000, $user1ID), "[game::getOpponentId]");
     $this->assertEquals($user2ID, game::getOpponentId($game_id1, $user1ID), "[game::getOpponentId]");
     $this->assertEquals($user1ID, game::getOpponentId($game_id1, $user2ID), "[game::getOpponentId]");
     $this->assertEquals($user1ID, game::getOpponentId($game_id2, $user3ID), "[game::getOpponentId]");
     $this->assertEquals($user3ID, game::getOpponentId($game_id2, $user1ID), "[game::getOpponentId]");
     $this->assertNull(game::getOpponentId($game_id2, $user2ID), "[game::getOpponentId]");
 }
コード例 #4
0
ファイル: table_game.php プロジェクト: uunuu/dot_boxes
 public static function getOpponentId($game_id, $player_id)
 {
     if (safe_input::is_number($game_id) && safe_input::is_number($player_id)) {
         $game = game::getGameById($game_id);
         if ($game == false) {
             //no game with that id
             return null;
         } else {
             if ($game['player1ID'] == $player_id) {
                 return $game['player2ID'];
             } elseif ($game['player2ID'] == $player_id) {
                 return $game['player1ID'];
             } else {
                 return null;
                 //player id is not a player in the game with the id $game_id
             }
         }
     } else {
         return null;
         //ERROR: either the game id or player id or both are not numbers! (invalid)
     }
 }