def post(self): """ POST the required parameters to register the pocketing/unpocketing of a ball * `ball`: The ball that was pocketed/unpocketed """ gamemaster = self.get_current_user() ball = self.body['ball'] game_id = get_player(self.db_conn, gamemaster)["current_game_id"] game = Game(self.db_conn, "game_id", game_id) res = {"game_id": game_id} # Authenticate api_assert( game["gamemaster"] == gamemaster, 401, log_message="You are not the gamemaster of the current game" ) # If ball is already sunk, retable it if ball not in get_balls_on_table(self.db_conn, game_id): game = Game(self.db_conn, "game_id", game_id) # Look for ball in game's set of unclaimed balls if ball in game["orig_unclaimed_balls"]: game["unclaimed_balls"] += [ball] # If it's not there, look for it from each player's set else: for pname in game["players"]: p = Player(self.db_conn, "name", pname) if ball in p["orig_balls"]: p["balls"] += [ball] break res['message'] = "Ball {} was retabled.".format(ball) return res # Otherwise, sink the ball # Look for it in players' first for pname in Game(self.db_conn, "game_id", game_id)["players"]: p = get_player(self.db_conn, pname) if ball in p["balls"]: p["balls"] = list(set(p["balls"]) - {ball}) break else: # Remove ball from unclaimed g = Game(self.db_conn, "game_id", game_id) uballs = list(game["unclaimed_balls"]) uballs.remove(ball) game["unclaimed_balls"] = uballs res["message"] = "Ball {} was sunk.".format(ball) return res
def post(self): """ POST the required parameters to register the pocketing/unpocketing of a ball * `ball`: The ball that was pocketed/unpocketed """ gamemaster = self.get_current_user() ball = self.body['ball'] game_id = get_player(self.db_conn, gamemaster)["current_game_id"] game = Game(self.db_conn, "game_id", game_id) res = {"game_id": game_id} # Authenticate api_assert( game["gamemaster"] == gamemaster, 401, log_message="You are not the gamemaster of the current game") # If ball is already sunk, retable it if ball not in get_balls_on_table(self.db_conn, game_id): game = Game(self.db_conn, "game_id", game_id) # Look for ball in game's set of unclaimed balls if ball in game["orig_unclaimed_balls"]: game["unclaimed_balls"] += [ball] # If it's not there, look for it from each player's set else: for pname in game["players"]: p = Player(self.db_conn, "name", pname) if ball in p["orig_balls"]: p["balls"] += [ball] break res['message'] = "Ball {} was retabled.".format(ball) return res # Otherwise, sink the ball # Look for it in players' first for pname in Game(self.db_conn, "game_id", game_id)["players"]: p = get_player(self.db_conn, pname) if ball in p["balls"]: p["balls"] = list(set(p["balls"]) - {ball}) break else: # Remove ball from unclaimed g = Game(self.db_conn, "game_id", game_id) uballs = list(game["unclaimed_balls"]) uballs.remove(ball) game["unclaimed_balls"] = uballs res["message"] = "Ball {} was sunk.".format(ball) return res
def get(self): """ GET to receive state of current_game `winner`: Potential winner of the game, or empty string `balls_on_table`: Array of balls still on the table """ player_name = self.get_current_user() player = Player(self.db_conn, "name", player_name) game_id = player["current_game_id"] api_assert(game_id, 400, log_message="You are not currently in" " a game.") res = {} res["balls_on_table"] = get_balls_on_table(self.db_conn, game_id) res["winner"] = get_game_winner(self.db_conn, game_id) return res