Esempio n. 1
0
 public static function newPendingGame($session, $size)
 {
     $session_info = session::get_session_by_hash($session);
     if ($session_info != null && safe_input::is_number($size) && $size > 1 && $size < 21) {
         $add_result = pending_game::add_new_pending_game($session_info['userID'], $size);
         GameControl::matchPendingGames();
         return $add_result;
     } else {
         Report::warning(__METHOD__ . "," . __LINE__, "trying to create a pending game with an invalid size:" . $size);
         return false;
     }
 }
Esempio n. 2
0
 public static function matchPendingGamesBySize($size)
 {
     //$critical = new CriticalSection(GameControl::$key) ;
     //$critical->lock() ;
     $games = pending_game::get_all_pending_games_by_size($size);
     $counter = count($games);
     //counter used to avoid an endless while loop when more than 1 pending game for the same user are consecutively follow each other in the array of games
     while (count($games) > 1 && $counter > 0) {
         if (pending_game::match($games[0]['id'], $games[1]['id'])) {
             $games = pending_game::get_all_pending_games_by_size($size);
         }
         $counter = count($games);
         while (count($games) > 1 && $games[0]['userID'] == $games[1]['userID'] && $counter > 0) {
             $tmp = array_shift($games);
             $games[] = $tmp;
             $counter--;
         }
     }
     //$critical->unlock() ;
 }
Esempio n. 3
0
<?php

include_once './include/table_user.php';
include_once './include/table_game.php';
include_once './include/table_message.php';
include_once './include/table_log.php';
include_once './include/table_pending_game.php';
include_once './include/table_session.php';
include_once './include/table_move.php';
//drop the tables:
$successful = True;
$successful = move::drop_table() && $successful;
$successful = session::drop_table() && $successful;
$successful = pending_game::drop_table() && $successful;
$successful = log::drop_table() && $successful;
$successful = message::drop_table() && $successful;
$successful = game::drop_table() && $successful;
$successful = user::drop_table() && $successful;
if (!$successful) {
    echo "Failed to delete tables";
} else {
    echo "OK";
}
Esempio n. 4
0
 public static function match($game_id1, $game_id2)
 {
     $game1 = pending_game::get_pending_game_by_id($game_id1);
     $game2 = pending_game::get_pending_game_by_id($game_id2);
     if (safe_input::is_number($game_id1) && safe_input::is_number($game_id2) && $game1 != null && $game2 != null && $game1['size'] == $game2['size']) {
         $date = time();
         $player1_id = $game1['userID'];
         $player2_id = $game2['userID'];
         if ($player1_id == $player2_id) {
             return false;
         }
         $size = $game1['size'];
         $g1_id = $game1['id'];
         $g2_id = $game2['id'];
         $db = new database();
         $query[] = "INSERT INTO `game` (`winnerID`, `createDate`, `currentTurnPlayerID`, `size`, `lastActivityDate`, `player1ID` , `player2ID`) VALUES ( null, '{$date}', '{$player1_id}', '{$size}', null , '{$player1_id}', '{$player2_id}');";
         $query[] = "DELETE FROM `pending_game` WHERE `id` = '{$g1_id}' or `id` = '{$g2_id}'";
         $res = $db->execute_transaction($query);
         return $res;
     } else {
         return false;
     }
 }
Esempio n. 5
0
 public function testNewGameRequest()
 {
     //startNewGame()
     $username = "******";
     $password = "******";
     $email = "*****@*****.**";
     $username2 = "sandy";
     $password2 = "booha";
     $email2 = "*****@*****.**";
     user::create_new_user($username, $password, $email);
     user::create_new_user($username2, $password2, $email2);
     $user_info = user::getUserByUsername($username);
     $user_info2 = user::getUserByUsername($username2);
     $session = md5("dfgfds4543");
     $session2 = md5("rtyertyerty");
     $this->assertTrue(session::add_new_session($user_info['id'], $session, "0"));
     $this->assertTrue(session::add_new_session($user_info2['id'], $session2, "0"));
     $xmlFile = file_get_contents("./files/newPendingGameRequest.xml");
     $p = simplexml_load_string($xmlFile);
     $p->body->session = $session;
     $p->body->size = 6;
     $req = $p->asXML();
     $obj = new XmlParseRequest();
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $this->assertEquals("5", $pr->body->id, "[new pending game]");
     $this->assertEquals($session, $pr->body->session, "[new pending game]");
     $games = pending_game::get_all_pending_games_for_user_id($user_info['id']);
     $this->assertEquals(1, count($games), "[new pending game]");
     $this->assertEquals(6, $games[0]['size'], "[new pending game]");
     //send an invalid session hash
     $p->body->session = md5("invalid_session");
     $req = $p->asXML();
     $obj = new XmlParseRequest();
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("failed", $pr->body->status, "[new pending game]");
     $this->assertEquals("1", $pr->body->error_code, "error code is not correct (invalid session hash passed)[new pending game]");
     // add a second pending game fo the same user and check that the system dodn't match the games since they belong to the same user!
     $p->body->session = $session;
     $req = $p->asXML();
     $obj = new XmlParseRequest();
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $p->body->session = $session;
     $req = $p->asXML();
     $obj = new XmlParseRequest();
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $this->AssertEquals(0, game::getNumberOfGames(), "one pending games with the same size added[new pending game]");
     $p->body->session = $session2;
     $req = $p->asXML();
     $obj = new XmlParseRequest();
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $this->assertEquals("5", $pr->body->id, "[new pending game]");
     $this->assertEquals($session2, $pr->body->session, "[new pending game]");
     $this->AssertEquals(1, game::getNumberOfGames(), "two pending games with the same size added[new pending game]");
     $obj->processRequest($req);
     $response = $obj->getResponse();
     $pr = simplexml_load_string($response);
     $this->assertEquals("successful", $pr->body->status, "[new pending game]");
     $this->assertEquals("5", $pr->body->id, "[new pending game]");
     $this->assertEquals($session2, $pr->body->session, "[new pending game]");
     $this->AssertEquals(2, game::getNumberOfGames(), "two pending games with the same size added[new pending game]");
 }
Esempio n. 6
0
 public function test()
 {
     $username = "******";
     $password = "******";
     $email = "*****@*****.**";
     $username2 = "bla2";
     $password2 = "pass2";
     $email2 = "*****@*****.**";
     $username3 = "gue";
     $password3 = "pass3";
     $email3 = "*****@*****.**";
     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");
     user::create_new_user($username3, $password3, $email3);
     $this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     $user1ID = user::getUserByUsername($username)['id'];
     $user2ID = user::getUserByUsername($username2)['id'];
     $user3ID = user::getUserByUsername($username3)['id'];
     //GameControl::matchPendingGames() ;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g1_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g2_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g3_id = pending_game::$last_inserted_id;
     $this->assertEquals(0, game::getNumberOfGames(), "[pending_game::mathc()]");
     $this->assertEquals(2, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     GameControl::matchPendingGames();
     $this->assertEquals(1, game::getNumberOfGames(), "[pending_game::mathc()]");
     $this->assertEquals(1, pending_game::get_count_pending_by_size(3), "[pending_game::mathc()]");
     $this->assertEquals(0, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g4_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g5_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g6_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g7_id = pending_game::$last_inserted_id;
     $this->assertEquals(4, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     GameControl::matchPendingGames();
     $this->assertEquals(0, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     $this->assertEquals(3, game::getNumberOfGames(), "[pending_game::mathc()]");
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g8_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g9_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g10_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g11_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 5), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g12_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 5), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g13_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 7), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g14_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 7), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g15_id = pending_game::$last_inserted_id;
     $this->assertEquals(3, game::getNumberOfGames(), "[pending_game::mathc()]");
     GameControl::matchPendingGames();
     $this->assertEquals(7, game::getNumberOfGames(), "[pending_game::mathc()]");
 }
Esempio n. 7
0
<?php

include_once './include/table_user.php';
include_once './include/table_game.php';
include_once './include/table_message.php';
include_once './include/table_log.php';
include_once './include/table_pending_game.php';
include_once './include/table_session.php';
include_once './include/table_move.php';
//create the tables:
$successful = True;
$successful = user::create_table() && $successful;
if (!$successful) {
    echo "Failed to create tables";
}
$successful = game::create_table() && $successful;
$successful = message::create_table() && $successful;
$successful = log::create_table() && $successful;
$successful = pending_game::create_table() && $successful;
$successful = session::create_table() && $successful;
$successful = move::create_table() && $successful;
if (!$successful) {
    echo "Failed to create tables";
} else {
    echo "OK";
}
Esempio n. 8
0
 public function test()
 {
     $username = "******";
     $password = "******";
     $email = "*****@*****.**";
     $username2 = "bla2";
     $password2 = "pass2";
     $email2 = "*****@*****.**";
     $username3 = "gue";
     $password3 = "pass3";
     $email3 = "*****@*****.**";
     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");
     user::create_new_user($username3, $password3, $email3);
     $this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user");
     $user1ID = user::getUserByUsername($username)['id'];
     $user2ID = user::getUserByUsername($username2)['id'];
     $user3ID = user::getUserByUsername($username3)['id'];
     //pending_game::add_new_pending_game($user_id,$size) ;
     //pending_game::get_pending_game_by_id($id) ;
     $this->assertFalse(pending_game::add_new_pending_game($user1ID, -1), "[pending_game::add_new_pending_game()]");
     $this->assertFalse(pending_game::add_new_pending_game("sdfdsf", 3), "[pending_game::add_new_pending_game()]");
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $pg_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $pg_info = pending_game::get_pending_game_by_id($pg_id);
     $this->assertEquals(3, $pg_info['size'], '[pending_game::add_new_pending_game()]');
     $this->assertEquals($user1ID, $pg_info['userID'], '[pending_game::add_new_pending_game()]');
     //get_all_pending_games_for_user_id($user_id)
     $this->assertEquals(1, count(pending_game::get_all_pending_games_for_user_id($user1ID)), "[get_all_pending_games_for_user_id()]");
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertEquals(2, count(pending_game::get_all_pending_games_for_user_id($user1ID)), "[get_all_pending_games_for_user_id()]");
     $this->assertNull(pending_game::get_all_pending_games_for_user_id("hhh"), "[get_all_pending_games_for_user_id()]");
     $this->assertNull(pending_game::get_all_pending_games_for_user_id($user3ID), "[get_all_pending_games_for_user_id()]");
     $this->assertEquals($user1ID, pending_game::get_all_pending_games_for_user_id($user1ID)[0]['userID'], "[get_all_pending_games_for_user_id()]");
     //get_all_pending_games_by_size($size)
     pending_game::clear_table();
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $pg_id1 = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertEquals(2, count(pending_game::get_all_pending_games_by_size(3)), "[get_all_pending_games_by_size()]");
     $this->assertEquals(1, count(pending_game::get_all_pending_games_by_size(4)), "[get_all_pending_games_by_size()]");
     $this->assertNull(pending_game::get_all_pending_games_by_size(5), "[get_all_pending_games_by_size()]");
     //get_all_pending_games()
     $this->assertEquals(3, count(pending_game::get_all_pending_games()), "[get_all_pending_games()]");
     //get_count_pending_games()
     $this->assertEquals(3, pending_game::get_count_pending_games(), "[get_count_pending_games()]");
     //get_count_pending_by_size($size)
     $this->assertEquals(0, pending_game::get_count_pending_by_size(10), "[get_count_pending_by_size()]");
     $this->assertEquals(0, pending_game::get_count_pending_by_size("sdfdf"), "[get_count_pending_by_size()]");
     $this->assertEquals(2, pending_game::get_count_pending_by_size(3), "[get_count_pending_by_size()]");
     $this->assertEquals(1, pending_game::get_count_pending_by_size(4), "[get_count_pending_by_size()]");
     //pending_game::delete_pending_game_by_id($id)
     $this->assertFalse(pending_game::delete_pending_game_by_id("sds"), "[pending_game::delete_pending_game_by_id()]");
     $this->assertEquals($pg_id1, pending_game::get_pending_game_by_id($pg_id1)['id'], "[delete_pending_game_by_id()]");
     $this->assertTrue(pending_game::delete_pending_game_by_id($pg_id1), "[pending_game::delete_pending_game_by_id()]");
     $this->assertNull(pending_game::get_pending_game_by_id($pg_id1), "[delete_pending_game_by_id()]");
     //pending_game::delete_all_pending_games_for_user_id($user_id)
     $this->assertFalse(pending_game::delete_all_pending_games_for_user_id("ddd"), "[pending_game::delete_all_pending_games_for_user_id()]");
     pending_game::clear_table();
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $this->assertEquals(2, count(pending_game::get_all_pending_games_for_user_id($user1ID)), "[get_all_pending_games_for_user_id()]");
     $this->assertEquals(3, pending_game::get_count_pending_games(), "[get_count_pending_games()]");
     $this->assertTrue(pending_game::delete_all_pending_games_for_user_id($user1ID), "[pending_game::delete_all_pending_games_for_user_id()]");
     $this->assertEquals(1, pending_game::get_count_pending_games(), "[get_count_pending_games()]");
     $this->assertEquals(1, count(pending_game::get_all_pending_games_for_user_id($user2ID)), "[get_all_pending_games_for_user_id()]");
     $this->assertNull(pending_game::get_all_pending_games_for_user_id($user1ID), "[get_all_pending_games_for_user_id()]");
     //pending_game::match($game_id1,$game_id2)
     pending_game::clear_table();
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 3), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g1_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user2ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g2_id = pending_game::$last_inserted_id;
     $this->assertTrue(pending_game::add_new_pending_game($user1ID, 4), "failed to add new pending_game[pending_game::add_new_pending_game()]");
     $g3_id = pending_game::$last_inserted_id;
     $this->assertFalse(pending_game::match($g1_id, $g2_id));
     $this->assertFalse(pending_game::match("d", $g2_id));
     $this->assertFalse(pending_game::match($g1_id, "d"));
     $this->assertEquals(0, game::getNumberOfGames(), "[pending_game::mathc()]");
     $this->assertEquals(2, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     $this->assertTrue(pending_game::match($g3_id, $g2_id));
     $this->assertEquals(1, game::getNumberOfGames(), "[pending_game::mathc()]");
     $this->assertEquals(0, pending_game::get_count_pending_by_size(4), "[pending_game::mathc()]");
     $this->assertTrue(!empty(game::getAllGamesForUserId($user1ID)), "[pending_game::mathc()]");
     $this->assertTrue(!empty(game::getAllGamesForUserId($user2ID)), "[pending_game::mathc()]");
 }