def join_room(db, room_name, password, player_name): """Join room ``room_name`` Updates ``current_players`` entry for room ``room_name`` with player ``player_name`` to the database. :raises APIError: If a room with ``room_name`` does not exist; or if the password is incorrect for room ``room_name``, or if player ``player_name`` does not exist """ # Ensure that the following preconditions are met: # * Correct password to room is give if required # * The player is not already in a room room = get_room(db, room_name) api_assert(password == room['password'], 403, log_message="Bad password for room `{}`.".format(room_name)) player = get_player(db, player_name) api_assert( player_name not in room["current_players"], 409, log_message="Player `{}` already in room `{}`".format( player_name, room_name) ) # If they are met, add the player to the room room["current_players"] += [player_name] player["current_room"] = room_name
def join_room(db, room_name, password, player_name): """Join room `room_name` Updates `current_players` entry for room `room_name` with player `player_name` to the database. :raises APIError: If a room with `room_name` does not exist; or if the password is incorrect for room `room_name`, or if player `player_name` does not exist """ room = get_room(db, room_name) api_assert(password == room['password'], 403, log_message="Bad password for room `{}`.".format(room_name)) player = get_player(db, player_name) api_assert( player_name not in room["current_players"], 409, log_message="Player `{}` already in room `{}`".format( player_name, room_name) ) room["current_players"] += [player_name] player["current_room"] = room_name
def post(self): """POST the required parameter to create a new game; only the owner of a room can make this request * `nbpp`: Number of balls per player """ game_id = uuid.uuid4().hex gamemaster = self.get_current_user() player = get_player(self.db_conn, gamemaster) room_name = player["current_room"] room = get_room(self.db_conn, room_name) api_assert(room["owner"] == gamemaster, 403, log_message="You must own a room to create a game.") player_names = room["current_players"] nplayers = len(player_names) nbpp = self.body["nbpp"] # Make sure values make sense api_assert(nplayers <= nbpp * nplayers <= TOTAL_NUM_BALLS, 400, log_message=("Your math seems to be a little off; " "please pick a `number of balls per player` " "such that each player has at least one ball " "and there are enough to go around for " "everyone.") ) # Create set of 15 balls, shuffle, and then match to players balls = generate_balls(TOTAL_NUM_BALLS) shuffle(balls) players = {} for i in xrange(nplayers): _balls = [] for i in xrange(nbpp): _balls.append(balls.pop()) pname = player_names.pop() players[pname] = _balls unclaimed_balls = balls[:] # Create game self.db_conn["games"].insert( { "game_id": game_id, "players": stringify_list(players.keys()), "orig_players": stringify_list(players.keys()), "unclaimed_balls": stringify_list(unclaimed_balls), "orig_unclaimed_balls": stringify_list(unclaimed_balls), "gamemaster": gamemaster, "winner": None } ) # Assign each player their set of balls, set game, leave room for name, balls in players.iteritems(): p = get_player(self.db_conn, name) p["current_game_id"] = game_id p["balls"] = balls p["orig_balls"] = balls p["current_room"] = None # The room can be deleted self.db_conn["rooms"].delete(name=room_name) return {"game_id": game_id}
def post(self): """POST the required parameter to create a new game; only the owner of a room can make this request * `nbpp`: Number of balls per player """ game_id = uuid.uuid4().hex gamemaster = self.get_current_user() player = get_player(self.db_conn, gamemaster) room_name = player["current_room"] room = get_room(self.db_conn, room_name) api_assert(room["owner"] == gamemaster, 403, log_message="You must own a room to create a game.") player_names = room["current_players"] nplayers = len(player_names) nbpp = self.body["nbpp"] # Make sure values make sense api_assert(nplayers <= nbpp * nplayers <= TOTAL_NUM_BALLS, 400, log_message=("Your math seems to be a little off; " "please pick a `number of balls per player` " "such that each player has at least one ball " "and there are enough to go around for " "everyone.")) # Create set of 15 balls, shuffle, and then match to players balls = generate_balls(TOTAL_NUM_BALLS) shuffle(balls) players = {} for i in xrange(nplayers): _balls = [] for i in xrange(nbpp): _balls.append(balls.pop()) pname = player_names.pop() players[pname] = _balls unclaimed_balls = balls[:] # Create game self.db_conn["games"].insert({ "game_id": game_id, "players": stringify_list(players.keys()), "orig_players": stringify_list(players.keys()), "unclaimed_balls": stringify_list(unclaimed_balls), "orig_unclaimed_balls": stringify_list(unclaimed_balls), "gamemaster": gamemaster, "winner": None }) # Assign each player their set of balls, set game, leave room for name, balls in players.iteritems(): p = get_player(self.db_conn, name) p["current_game_id"] = game_id p["balls"] = balls p["orig_balls"] = balls p["current_room"] = None # The room can be deleted self.db_conn["rooms"].delete(name=room_name) return {"game_id": game_id}