Ejemplo n.º 1
0
class LocalServer(object):
    def __init__(self, seed):
        self.seed = seed

    def submit_game_move(self, game_id, move_list, moves_made):
        # update local state, return json object
        game_state = self.game
        made_move = False
        if game_state.state != "failed":
            if time.time() - self.game.game_started_at > util.AI_CLIENT_TIMEOUT:
                game_state.state = "failed"
            else:
                game_state.send_commands(move_list)
                made_move = True
        game_state.total_steps += int(bool(made_move))

        if game_state.state == "failed":
            raise GameOverError(game_state.to_dict())
        return dict(
            ret="ok",
            game=dict(id=0, number_moves_made=game_state.total_steps, game_state=game_state.to_dict()),
            competition_seconds_remaining=util.AI_CLIENT_TIMEOUT - (time.time() - self.game.game_started_at) - 1,
        )

    def get_local_game(self):
        # return a json object
        self.game = Board(self.seed)
        self.game.game_started_at = time.time()
        self.game.game_id = 0
        self.game.total_steps = 0
        return dict(
            ret="ok",
            game=dict(id=0, number_moves_made=self.game.total_steps, game_state=self.game.to_dict()),
            competition_seconds_remaining=util.AI_CLIENT_TIMEOUT - 1,
        )
Ejemplo n.º 2
0
        def _create_game(self, competition_id, team_id, game_seed):
                assert isinstance(competition_id, ACCEPTABLE_INT_TYPES), "bad competition id%r" % (competition_id,)
                assert isinstance(team_id, ACCEPTABLE_INT_TYPES), "bad team id%r" % (team_id,)
                assert isinstance(game_seed, ACCEPTABLE_INT_TYPES), "bad game seed id%r" % (game_seed,)
                sql = """
INSERT INTO game (
number_moves_made,
game_state,
team_id,
competition_id
)
VALUES
(%s, %s, %s, %s)
"""
                gs = Board(game_seed)
                self.execute(sql, (0, json.dumps(gs.to_dict()), team_id, competition_id))
                game_id = self.lastrowid
                return Container(id=game_id,
                                 number_moves_made=0,
                                 game_state=gs,
                                 team_id=team_id,
                                 competition_id=competition_id)