def create_game_info_player(self, number):
        player = Player()
        player.hit_game_info = False

        def send_message_and_await_response(json_message):
            message = json.loads(json_message)
            if message["type"] == "player-name-request":
                return json.dumps({
                    "type": "player-name-reply",
                    "player_number": message["player_number"],
                    "name": "nothing-bot{}".format(number),
                    "version": 1
                })
            elif message["type"] == "game-info":
                self.assertEquals(["nothing-bot1", "nothing-bot2"],
                                  message["player_bot_names"])
                self.assertEquals([
                    CELLAR, MARKET, MILITIA, MINE, MOAT, REMODEL, SMITHY,
                    VILLAGE, WOODCUTTER, WORKSHOP
                ], message["kingdom_cards"])
                player.hit_game_info = True
            else:
                return json.dumps({"type": "play-reply", "phase": "cleanup"})

        player.send_message_and_await_response = send_message_and_await_response
        player.send_message = send_message_and_await_response
        return player
    def test_engine_exits_early_if_invalid_response_to_player_name(self):
        player = Player()
        player.send_message_and_await_response = lambda msg: ""
        player.send_message = lambda msg: self.fail

        with self.assertRaisesRegexp(Exception,
                                     "Player 1 did not respond correctly"):
            engine = DominionEngine([player], FIRST_GAME)
Beispiel #3
0
    def create_player(self):
        player = Player()
        player.received_message = None

        def receive_message(json_message):
            player.received_message = json.loads(json_message)

        player.send_message = receive_message
        return player
def create_player_that_responds_to_first_message():
    player = Player()

    def send_message_and_await_response(json_message):
        message = json.loads(json_message)
        return json.dumps({
            "type": "player-name-reply",
            "player_number": message["player_number"],
            "name": "nothing-bot",
            "version": 1
        })

    player.send_message_and_await_response = send_message_and_await_response

    def respond_message(json_message):
        player.last_message = json.loads(json_message)

    player.send_message = respond_message
    return player
def get_player(cmd, workdir, num):
    player = Player()
    if cmd == "starter_bot":
        starter_bot = StarterBot()
        comm = DirectInvocationCommunication(
            lambda msg: starter_bot.send_message(msg),
            lambda: starter_bot.get_response())
        player.set_communication(comm)
    elif cmd == "interactive":
        bot = InteractiveBot(num)
        comm = DirectInvocationCommunication(lambda msg: bot.send_message(msg),
                                             lambda: bot.get_response())
        player.set_communication(comm)
    else:
        comm = PlayerCommunication(cmd, workdir)
        player.set_communication(comm)
    return player
    def test_game_ends_if_no_player_can_make_a_move(self):
        player = Player()

        def send_message_and_await_response(json_message):
            message = json.loads(json_message)
            if message["type"] == "player-name-request":
                return json.dumps({
                    "type": "player-name-reply",
                    "player_number": message["player_number"],
                    "name": "nothing-bot",
                    "version": 1
                })
            else:
                return json.dumps({"type": "play-reply", "phase": "cleanup"})

        player.send_message_and_await_response = send_message_and_await_response
        player.send_message = lambda msg: None

        players = [player]
        engine = DominionEngine(players, FIRST_GAME)
        engine.run_until_game_end()

        self.assertEquals(3, player.get_score())
        self.assertEquals(500, player.get_number_of_turns_taken())