def delete(self): """ DELETE to leave current room. If the room owner leaves, the room will be deleted. """ player_name = self.get_current_user() player = Player(self.db_conn, "name", player_name) room_name = player["current_room"] if room_name: room = Room(self.db_conn, "name", room_name) else: raise APIError(409, log_message="You are currently not in a room.") with DBLock(): if room["owner"] == player_name: # Set all players' current_room to None for pname in room["current_players"]: p = Player(self.db_conn, "name", pname) p["current_room"] = None # Delete the room self.db_conn["rooms"].delete(name=room_name) return "{} successfully deleted {}".format(player_name, room_name) else: # Set player's current_room to None and remove from room's # current_players list player["current_room"] = None room["current_players"] = [ p for p in room["current_players"] if p != player_name] return "{} successfully left {}".format(player_name, room_name)
def delete(self): """DELETE to remove yourself from current game""" # Get player and the game_id he's in player_name = self.get_current_user() player = Player(self.db_conn, "name", player_name) game_id = player["current_game_id"] api_assert(game_id, 409, log_message="You are not currently in a game.") with DBLock(): # Get game game = Game(self.db_conn, "game_id", game_id) # Get remaining set of players rem_players = list(set(game["players"]) - {player_name}) # Set new gamemaster if not rem_players: game["gamemaster"] = None # If gamemaster is leaving, assign to a random player elif game["gamemaster"] == player_name: game["gamemaster"] = choice(rem_players) # Set remaining players in game and add players' balls to game's # unclaimed set game["players"] = rem_players game["unclaimed_balls"] += player["balls"] # Set the player's game_id to None and his list of balls to empty player["current_game_id"] = None player["balls"] = [] return {"game_id": game_id}
def get(self): """ GET to receive list of players in current room * `players` array includes ALL players (including owner) * `owner` field is useful for highlighting the room owner in the UI """ player_name = self.get_current_user() # The reason we don't use get_player in a case like # this, is because this is an authenticated method, # so a player with the name of the current user # SHOULD exist; if he doesn't, then that indicates # a problem on our side (if that happens, the user # will see a 500 Internal Server Error) which # is arguably appropriate in this case. player = Player(self.db_conn, "name", player_name) room_name = player["current_room"] api_assert(room_name, 400, log_message="You are not currently in" " a room.") # Again, same thing here; if the player has joined # a room, the room name must exist, otherwise, it # indicates a failure somewhere earlier down the line. room = Room(self.db_conn, "name", room_name) return { "players": room["current_players"], "owner": room["owner"] }
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_player(db, player_name, err_code=409): """Get player ``player_name`` :returns: Player with ``player_name`` :raises APIError: If no Player with ``player_name`` found """ try: player = Player(db, "name", player_name) except NotFoundError: raise APIError( err_code, log_message="No user {} exists.".format(player_name) ) return player
def get(self): """ GET to receive list of players in current game * `players` array includes ALL players (including gamemaster) * `gamemaster` field is useful for highlighting the gamemaster in the UI """ 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.") game = Game(self.db_conn, "game_id", game_id) return {"players": game["players"], "gamemaster": game["gamemaster"]}
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