Ejemplo n.º 1
0
    def validate_round_request():
        if not flask.request.json:
            raise Exception("Invalid request: no JSON body.")
        game_id = flask.request.json.get('game_id')
        if not game_id:
            raise Exception("Invalid request: No game_id value")
        user = flask.request.json.get('user')
        if not user:
            raise Exception("Invalid request: No user value")
        db_user = game_service.find_player(user)
        if not db_user:
            raise Exception(
                "Invalid request: No user with name {}".format(user))
        roll = flask.request.json.get('roll')
        if not roll:
            raise Exception("Invalid request: No roll value")
        db_roll = game_service.find_roll(roll)
        if not db_roll:
            raise Exception(
                "Invalid request: No roll with name {}".format(roll))

        is_over = game_service.is_game_over(game_id)
        if is_over:
            raise Exception("This game is already over.")

        return db_roll, db_user, game_id
    def game_status(game_id: str):
        is_over = game_service.is_game_over(game_id)
        history = game_service.get_game_history(game_id)

        if not history:
            flask.abort(404)

        roll_lookup = {r.id: r for r in game_service.all_rolls()}
        player_lookup = {p.id: p for p in game_service.all_players()}

        player1 = game_service.find_player_by_id(history[0].player_id)
        player2 = game_service.find_player_by_id(history[1].player_id)

        wins_p1 = game_service.count_round_wins(player1.id, game_id)
        wins_p2 = game_service.count_round_wins(player2.id, game_id)

        data = {
            'is_over': is_over,
            'moves': [h.to_json(roll_lookup[h.roll_id], player_lookup[h.player_id]) for h in history],
            'player1': player1.to_json(),
            'player2': player2.to_json(),
            'winner': player1.to_json() if wins_p1 >= wins_p2 else player2.to_json()
        }

        return flask.jsonify(data)
Ejemplo n.º 3
0
    def game_status(game_id: str):
        is_over = game_service.is_game_over(game_id)
        history = game_service.get_game_history(game_id)

        if not history:
            flask.abort(404)

        roll_lookup = {r.id: r for r in game_service.all_rolls()}
        player_lookup = {p.id: p for p in game_service.all_players()}

        player1 = game_service.find_player_by_id(history[0].player_id)
        player2 = game_service.find_player_by_id(history[1].player_id)

        wins_p1 = game_service.count_round_wins(player1.id, game_id)
        wins_p2 = game_service.count_round_wins(player2.id, game_id)

        data = {
            'is_over':
            is_over,
            'moves': [
                h.to_json(roll_lookup[h.roll_id], player_lookup[h.player_id])
                for h in history
            ],
            'player1':
            player1.to_json(),
            'player2':
            player2.to_json(),
            'winner':
            player1.to_json() if wins_p1 >= wins_p2 else player2.to_json()
        }

        return flask.jsonify(data)
    def play(self):
        if self.is_over:
            raise Exception("Game is already over, cannot play further.")

        d = game_decider.decide(self.p1_roll, self.p2_roll)
        self.decision_p1_to_p2 = d

        self.record_roll(d, self.player1, self.p1_roll, self.player1_wins)
        self.record_roll(d.reversed(), self.player2, self.p2_roll, self.player2_wins)

        print("RECORDING ROUND")
        print("Player 1: {}, prior wins {}, outcome: {}".format(self.p1_roll.name, self.player1_wins, d))
        print("Player 2: {}, prior wins {}, outcome: {}".format(self.p2_roll.name, self.player2_wins, d.reversed()))
        print()

        self.is_over = game_service.is_game_over(self.game_id)
    def __init__(self, game_id: str, player1: Player, player2: Player,
                 p1_roll: Roll, p2_roll: Roll):
        self.p2_roll = p2_roll
        self.p1_roll = p1_roll
        self.game_id = game_id
        self.player1 = player1
        self.player2 = player2

        self.decision_p1_to_p2 = None
        history = game_service.get_game_history(game_id)
        self.round = len(history) // 2 + 1
        self.player1_wins = GameRound.count_wins(self.player1, history)
        self.player2_wins = GameRound.count_wins(self.player2, history)
        self.WIN_COUNT_MIN = 3
        self.PLAY_COUNT_MIN = 5
        self.is_over = game_service.is_game_over(game_id)
Ejemplo n.º 6
0
    def game_status(game_id: str):
        is_over = game_service.is_game_over(game_id)
        history = game_service.get_game_history(game_id)

        if not history:
            flask.abort(404)

        roll_lookup = {r.id: r for r in game_service.all_rolls()}
        player_lookup = {p.id: p for p in game_service.all_players()}
        player = game_service.find_player_by_id(history[0].player_id)

        data = {
            'is_over': is_over,
            'moves': [h.guess for h in history],
            'player': player.to_json(),
            'number': history[0].number
        }

        return flask.jsonify(data)
Ejemplo n.º 7
0
def get_game_status(gameid):
	is_over = game_service.is_game_over(gameid)
	history = game_service.get_game_history(gameid)
	if not history:
		abort(make_response(jsonify({"ERROR":"This game was not found"}), 404))
	r_lookup = {r.id:r for r in game_service.all_rolls()}
	p_lookup = {p.id:p for p in game_service.all_players()}
	player_0 = game_service.find_player_by_id(history[0].player_id)
	player_1 = game_service.find_player_by_id(history[1].player_id)

	wins_p1 = game_service.count_round_wins(player_0.id, gameid)
	wins_p2 = game_service.count_round_wins(player_1.id, gameid)

	data = {'is_over': is_over,
			'player1': player_0.to_json(),
			'player2': player_1.to_json(),
			'winner': player_0.to_json() if wins_p1>=wins_p2 else player_1.to_json(),
			'moves': [h.to_json(r_lookup[h.roll_id], p_lookup[h.player_id]) for h in history]
			}

	return jsonify(data)
Ejemplo n.º 8
0
 def validate_round_request():
     if not flask.request.json:
         raise Exception("Invalid request: no JSON body.")
     game_id = flask.request.json.get('game_id')
     if not game_id:
         raise Exception("Invalid request: No game_id value")
     the_number = flask.request.json.get('the_number')
     user = flask.request.json.get('user')
     if not user:
         raise Exception("Invalid request: No user value")
     db_user = game_service.find_player(user)
     if not db_user:
         raise Exception(
             "Invalid request: No user with name {}".format(user))
     move = flask.request.json.get('move')
     if not move:
         raise Exception("Invalid request: No roll value")
     db_move = move
     is_over = game_service.is_game_over(game_id)
     if is_over:
         raise Exception("This game is already over.")
     return db_move, db_user, game_id, the_number
    def validate_round_request():
        if not flask.request.json:
            raise Exception("Invalid request: no JSON body.")
        game_id = flask.request.json.get('game_id')
        if not game_id:
            raise Exception("Invalid request: No game_id value")
        user = flask.request.json.get('user')
        if not user:
            raise Exception("Invalid request: No user value")
        db_user = game_service.find_player(user)
        if not db_user:
            raise Exception("Invalid request: No user with name {}".format(user))
        roll = flask.request.json.get('roll')
        if not roll:
            raise Exception("Invalid request: No roll value")
        db_roll = game_service.find_roll(roll)
        if not db_roll:
            raise Exception("Invalid request: No roll with name {}".format(roll))

        is_over = game_service.is_game_over(game_id)
        if is_over:
            raise Exception("This game is already over.")

        return db_roll, db_user, game_id