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; } }
public static function matchPendingGames() { $sizes; /** @var $i is the size of the board. it can't be smaller than 2. #0 is set by default as the biggest */ for ($i = 2; $i < 30; $i++) { if (pending_game::get_count_pending_by_size($i) > 1) { $sizes[] = $i; } } if (isset($sizes)) { foreach ($sizes as $s) { GameControl::matchPendingGamesBySize($s); } } }
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()]"); }