TBG.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:ThatBeanGame 作者: tuchfarber 项目源码 文件源码
def login() -> Dict:
    '''
    Adds user to the requested game as long as game isn't full or already started,
    game exists, and user name isn't already used in game.
    '''
    post_data: Dict = request.get_json()
    try:
        name: str = post_data['name']
        game_id: str = post_data['game']
    except KeyError:
        abort(400, util.error('Incorrect JSON data'))

    # Get first public game
    game: Game = util.shrink([games[game_id] for game_id in games if games[game_id].game_type == 'public'])

    # If game game_id isn't a blank string, use that game
    if game_id in games:
        game = games[game_id]

    if name in [player.name for player in game.players]:
        abort(400, util.error('User already exists with that name'))
    if game.status != 'Awaiting':
        abort(400, util.error('Game has already started or ended'))
    if game.is_full():
        abort(400, util.error('Game is full'))

    player: Player = Player(name)
    game.add_player(player)
    clients[player.token] = game_id
    response = make_response(jsonify(util.success('Successfully logged into game')))
    response.set_cookie('tbg_token', player.token, max_age=6000)
    update_client(game)
    return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号