Beispiel #1
0
def client_connect(cookie):
    if GAME_CODE_KEY not in session:
        # TODO: error handling
        return
    game_code_raw = session[GAME_CODE_KEY]
    game_code = GameCode(game_code_raw)

    if not game_store.contains_game(game_code):
        ErrorHandler.game_code_dne(ClientEvent.CONNECT, game_code)
        return

    game_manager = game_store.get_game(game_code)

    #try
    client, events = game_manager.handle_client_event(
        client_id=None, client_event=ClientEvent.CONNECT, data=cookie)
    #except Exception as e:
    #    emit('error', str(e))

    for event in events:
        event.emit()

    # Store the client id and game code in the session for further requests
    session[CLIENT_ID_KEY] = client.id

    # Add the client's socket to the socket room
    join_room(game_code)

    # emit an update to the clients
    game_manager.get_lobby_update_event().emit()
Beispiel #2
0
def add_to_lobby():
    game_code_str = request.form["game_code"]
    game_code = GameCode(game_code_str)
    if game_store.contains_game(game_code):
        return redirect(
            url_for('lobby.game_lobby', game_code=game_code.serialize()))
    else:
        return redirect(
            url_for('create.join_game', error_text="Invalid game code"))
Beispiel #3
0
def game_lobby(game_code):
    game_code_obj = GameCode(game_code)
    session[GAME_CODE_KEY] = game_code

    if game_store.contains_game(game_code_obj):
        return render_template(
            'lobby.html',
            game_code=game_code_obj.serialize(),
            client_id_key=CLIENT_ID_KEY,
            ClientEvent=ClientEvent,
        )
    else:
        abort(404)
Beispiel #4
0
def game_event_handler(game_event, data=None):
    try:
        game_code_raw, client_id = get_session_data(session)
    except ValueError as err:
        emit('error', str(err))
        return

    game_code = GameCode(game_code_raw)
    if not game_store.contains_game(game_code):
        ErrorHandler.game_code_dne(GameEvent.UPDATE, game_code)
        return

    game_manager = game_store.get_game(game_code)

    #try:
    game, events = game_manager.handle_game_event(client_id, game_event, data)
    for event in events: event.emit()
    return game
Beispiel #5
0
def game_data(game_code):
    game_code_obj = GameCode(game_code)
    if not game_store.contains_game(game_code_obj):
        # TODO error handling
        return render_template('rejoin.html')
    session[GAME_CODE_KEY] = game_code
    game_manager = game_store.get_game(game_code_obj)

    if CLIENT_ID_KEY not in session:
        # Somehow the client lost their session between the lobby and the game
        # TODO: handle reconnection
        return render_template('rejoin.html')

    # TODO: This is emulating a lobby reconnection event using the session data
    #       I don't think this is a very good way to transition clients from
    #       the lobby to the game.
    #       Ideally, we don't even have a separate page, we serve the lobby
    #       and game on a single page.
    cookie = {}
    cookie[CLIENT_ID_KEY] = session[CLIENT_ID_KEY]
    try:
        client, events = game_manager.handle_client_event(
            client_id=None,
            client_event=ClientEvent.CONNECT,
            data=cookie
        )
        session[CLIENT_ID_KEY] = client.id
    except PermissionError as e:
        return render_template('rejoin.html')

    role_to_serve = client.has_spymaster()

    return render_template(
       'game.html',
       game_bundle=game_store.get_full_game_bundle(game_code_obj, role_to_serve),
       GameEvent=GameEvent,
       ClientEvent=ClientEvent,
   )