示例#1
0
    def test_save_and_load(self):
        deck_template = 'decks/deck_test_30.csv'
        p1 = Player("peter")
        p2 = Player("manan")
        g = GameState(42, p1, 2, deck_template)
        g.add_player(p2)
        g.start_game()

        p1.gold = 32
        p1.take_cards(g.building_card_deck.cards)

        session_maker = create_session_maker()
        session = session_maker()
        session.add(g)
        session.commit()

        gs_id = g.id
        session.close()

        new_session = session_maker()

        g_loaded = new_session.query(GameState).filter(GameState.id == gs_id).one()

        g_loaded.get_random_gen()

        self.assertEqual(g_loaded.base_seed, 42)

        p1_loaded = session.query(Player).filter(Player.name == 'peter', Player.gamestate_id == gs_id).one()
        p1_bad_copy = Player("peter")
        self.assertEqual("peter", p1_loaded.name)
        self.assertNotEqual(p1_loaded, p1_bad_copy)
        self.assertEqual(len(p1_loaded.buildings_buffer), 2)
示例#2
0
    def do_ai_test(self, seed, num_players):
        names = ['PeterAI', 'MananAI', 'AndyAI', 'MarkAI', 'KevinAI', 'RyanAI', 'TabithaAI']
        ais = {}
        players = []
        for n in names[0:num_players]:
            ais[n] = SimpleAIPlayer(n)
            players.append(Player(n))

        game = GameState(seed, players[0], num_players, deck_template='decks/deck_test_60.csv')
        for p in players[1:num_players]:
            game.add_player(p)
        game.start_game()
        ref = Referee(game)
        num_steps = 100 * len(players)

        for i in range(num_steps):
            logging.debug("On step %s of simulation" % i)
            cur_plyr = game.players[game.cur_player_index]
            logging.debug("Cur plyr index is %s" % game.cur_player_index)
            logging.debug("Cur plyr is %s" % cur_plyr)
            cur_ai = ais[cur_plyr.name]
            move = cur_ai.decide_what_to_do_native(game)
            ref.perform_move(move)
            #            logging.warning("Stage is %s" % game.stage)
            if game.stage == Stage.GAME_OVER:
                logging.warning("**********************************************")
                logging.warning("******* Success, game over and %s won   ******" % game.winner)
                for p in game.players:
                    logging.warning("Player %s had %s pts" % (p.name, p.points))
                return game.round_num
        logging.error("Didn't finish game in %s steps, ending test" % num_steps)
        self.assertTrue(False, "didn't finish game in right amount of steps")
示例#3
0
 def create_new_game(self, seed, sess, players):
     game = GameState(seed, players[0], len(players), deck_template="decks/deck_test_60.csv")
     for p in players[1:]:
         game.add_player(p)
     game.start_game()
     sess.add(game)
     sess.commit()
     return game.id
示例#4
0
    def do_ai_test_with_json(self, seed, num_players):
        names = ['PeterAI', 'MananAI', 'AndyAI', 'MarkAI', 'KevinAI', 'RyanAI', 'TabithaAI']
        ais = {}
        players = []
        for n in names[0:num_players]:
            ais[n] = SimpleAIPlayer(n)
            players.append(Player(n))

        game = GameState(seed, players[0], num_players, deck_template='decks/deck_test_60.csv')
        for p in players[1:num_players]:
            game.add_player(p)
        game.start_game()

        ref = Referee(game)

        json = ref.get_current_state_as_json_for_player(game.cur_player_index)
        parsed_json = from_json(json)
        json_game = parsed_json['game']

        game = None #force ourselves to use json from now on, and fail fast if
        #accidentally use game

        num_steps = 100 * len(players)

        logging.debug(json)

        for i in range(num_steps):
            logging.debug("On step %s of simulation" % i)
            cur_plyr = json_game['players'][json_game['cur_player_index']]
            cur_ai = ais[cur_plyr['name']]
            move = cur_ai.decide_what_to_do_json(parsed_json)
            json = ref.perform_move(move)
            parsed_json = from_json(json)
            json_game = parsed_json['game']
            stage = json_game['stage']
            if stage not in [Stage.GAME_OVER, Stage.END_GAME, Stage.PLAYING]:
                self.assertTrue(False, "stage is not valid: %s" % stage)
            logging.debug("stage is %s" % stage)
            if stage == Stage.GAME_OVER:
                logging.warning("**********************************************")
                logging.warning("******* Success, game over and %s won after %s rounds  ******" % (
                json_game['winner'], json_game['round_num']))
                json_players = json_game['players']

                buffer = ""
                for p in json_players:
                    buffer += "Player %s had %s pts, " % (p['name'], p['points'])
                logging.warning(buffer)
                return json_game['round_num']
        logging.error("Didn't finish game in %s steps, ending test" % num_steps)
        self.assertTrue(False, "didn't finish game in right amount of steps")
示例#5
0
    def do_test_using_json_from_file(self, file, move_set):
        players = [fake_player("peter"), fake_player("manan")]

        test_deck = Building.create_deck_from_csv('decks/deck_test_30.csv')
        game = GameState()
        game.initialize_game(42, players, deck=test_deck)

        ref = Referee(r, game)

        with open(file) as f:
            move_sets = json.loads(f.read())

        moves = move_sets[move_set]
        for move in moves:
            ref.perform_move(move)
示例#6
0
    def test_wrong_turn(self):
        with self.assertRaises(NotYourTurnError):
            players = [fake_player("Peter"), fake_player("Manan")]
            game = GameState(42, players[0], 2, deck_template='decks/default.csv')
            game.add_player(players[1])
            game.start_game()

            ref = Referee(game)

            with open("tests/moves.json") as f:
                move_sets = json.loads(f.read())

            moves = move_sets["wrong_turn"]
            for move in moves:
                ref.perform_move(move)
示例#7
0
    def test_create_round(self):
        players = [fake_player("Peter"), fake_player("Manan")]
        game = GameState(42, players[0], 2, deck_template="decks/deck_test_30.csv")
        game.add_player(players[1])
        self.assertIsNotNone(game.to_dict_for_public(),
                             "to_dict shouldn't crash even if called before game is started and round is created")
        game.start_game()
        r = Round(game)

        self.assertEqual(len(r.face_up_roles), 0)
        self.assertEqual(len(r.face_down_roles), 1)
        self.assertEqual(len(r.role_draw_pile), 7)