Exemplo n.º 1
0
def game(game_id):
    """Render lobby page, auth required
    """
    # get game from db
    game = Game.objects.get(pk=game_id)
    if game is None:
        return render_template("404.html"), 404

    # authorise the user or redirect to index
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # if the game has already ended, don't allow anyone else into the room
    if game.ended:
        flash('Sorry, that game has ended', 'warning')
        return redirect(url_for('.lobby'))

    # ensure player is in this game
    if not player.user_id in game.players:
        flash('You are not authorised to access that game', 'warning')
        return redirect(url_for('.lobby'))

    # render lobby template if authorised
    return render_template("game/game.html", user=player)
Exemplo n.º 2
0
def room(game_id):
    """Render lobby page, auth required
    """
    # get game from db
    game = Game.objects.get(pk=game_id)
    if game is None:
        return render_template("404.html"), 404

    # authorise the user or redirect to index
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # if the game has already started, don't allow anyone else into the room
    if game.started:
        flash('Sorry, that game has already started', 'warning')
        return redirect(url_for('.lobby'))

    # add player to game
    game = game.add_player(player.user_id)
    if not player.user_id in game.players:
        flash('Sorry, that game is full', 'warning')
        return redirect(url_for('.lobby'))

    # timestamp to show as first message in chat
    timestamp = datetime.datetime.utcnow().strftime(
        '%Y-%m-%d %H:%M:%S') + ' UTC'

    # render lobby template if authorised
    return render_template("lobby/room.html",
                           user=player,
                           game=game,
                           timestamp=timestamp)
Exemplo n.º 3
0
def room(game_id):
    """Render lobby page, auth required
    """
    # get game from db
    game = Game.objects.get(pk=game_id)
    if game is None:
        return render_template("404.html"), 404

    # authorise the user or redirect to index
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # if the game has already started, don't allow anyone else into the room
    if game.started:
        flash('Sorry, that game has already started', 'warning')
        return redirect(url_for('.lobby'))

    # add player to game
    game = game.add_player(player.user_id)
    if not player.user_id in game.players:
        flash('Sorry, that game is full', 'warning')
        return redirect(url_for('.lobby'))

    # timestamp to show as first message in chat
    timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') + ' UTC'

    # render lobby template if authorised
    return render_template("lobby/room.html", user=player, game=game, timestamp=timestamp)
Exemplo n.º 4
0
def game(game_id):
    """Render lobby page, auth required
    """
    # get game from db
    game = Game.objects.get(pk=game_id)
    if game is None:
        return render_template("404.html"), 404

    # authorise the user or redirect to index
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # if the game has already ended, don't allow anyone else into the room
    if game.ended:
        flash('Sorry, that game has ended', 'warning')
        return redirect(url_for('.lobby'))

    # ensure player is in this game
    if not player.user_id in game.players:
        flash('You are not authorised to access that game', 'warning')
        return redirect(url_for('.lobby'))

    # render lobby template if authorised
    return render_template("game/game.html", user=player)
Exemplo n.º 5
0
def lobby():
    """Render lobby page, auth required
    """
    # authorise user
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # render lobby template if authorised
    return render_template("lobby/lobby.html", user=player)
Exemplo n.º 6
0
def lobby():
    """Render lobby page, auth required
    """
    # authorise user
    player = get_user(session)
    if player is None:
        flash('You need to log in before you can access that page', 'warning')
        return redirect(url_for('.index'))

    # render lobby template if authorised
    return render_template("lobby/lobby.html", user=player)
Exemplo n.º 7
0
    def post(self):
        """Create a new game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # create new game
        max_players = request.form.get('max_players', 4)
        game = Game.new_game(player_id=player.user_id, max_players=int(max_players))
        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 8
0
    def post(self):
        """Create a new game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # create new game
        max_players = request.form.get('max_players', 4)
        game = Game.new_game(player_id=player.user_id,
                             max_players=int(max_players))
        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 9
0
    def get(self):
        """List all open games
        """

        # authorise the user or return a 401
        user = get_user(session)
        if user is None:
            return jsonify(error='401 Unauthorized'), 401

        # get datetime exactly one day ago
        window = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
        # run query and serialise to python dict
        games_owned_dicts = [x.to_dict() for x in Game.objects.filter(created_at__gt=window).filter(started=False).filter(ended=False).filter(creator=user.user_id)]
        games_unowned_dicts = [x.to_dict() for x in Game.objects.filter(created_at__gt=window).filter(started=False).filter(ended=False).filter(creator__ne=user.user_id)]
        # return as JSON response
        return jsonify(owned=games_owned_dicts, unowned=games_unowned_dicts)
Exemplo n.º 10
0
    def get(self, game_id):
        """Get Details of a single game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 11
0
def push_stream(rest):
    """SocketIO connection handler
    """

    # authorise the user or return a 401
    player = get_user(session)
    if player is None:
        return jsonify(error='401 Unauthorized'), 401

    try:
        # socketio never actually uses the request object we pass in, it's just
        # for convenience, so we just pass in the player object itself, to
        # avoid flask throwing errors about using the request out of context
        socketio_manage(request.environ, {'/lobby': LobbyNamespace, '/room': RoomNamespace}, player)
    except:
        sockets.logger.error("Exception in socket", exc_info=True)
    return Response()
Exemplo n.º 12
0
    def get(self, game_id):
        """Get Details of a single game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 13
0
def push_stream(rest):
    """SocketIO connection handler
    """

    # authorise the user or return a 401
    player = get_user(session)
    if player is None:
        return jsonify(error='401 Unauthorized'), 401

    try:
        # socketio never actually uses the request object we pass in, it's just
        # for convenience, so we just pass in the player object itself, to
        # avoid flask throwing errors about using the request out of context
        socketio_manage(request.environ, {
            '/lobby': LobbyNamespace,
            '/room': RoomNamespace
        }, player)
    except:
        sockets.logger.error("Exception in socket", exc_info=True)
    return Response()
Exemplo n.º 14
0
    def post(self, game_id):
        """Join a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # add player to game
        print type(player)
        game = game.add_player(player.user_id)
        if not player.user_id in game.players:
            return jsonify(error='403 Forbidden'), 403

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 15
0
    def post(self, game_id):
        """Join a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # add player to game
        print type(player)
        game = game.add_player(player.user_id)
        if not player.user_id in game.players:
            return jsonify(error='403 Forbidden'), 403

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 16
0
    def post(self, game_id):
        """Start a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # ensure player is the game's creator
        if not player.user_id == game.creator:
            return jsonify(error='403 Forbidden'), 403

        # mark the game as started and publish signal to other clients
        game = game.start()

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 17
0
    def post(self, game_id):
        """Leave a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # ensure player is the game's creator
        if not player.user_id in game.players:
            return jsonify(error='403 Forbidden'), 403

        # leave the game and let the other players know
        game = game.remove_player(player.user_id)

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 18
0
    def get(self):
        """List all open games
        """

        # authorise the user or return a 401
        user = get_user(session)
        if user is None:
            return jsonify(error='401 Unauthorized'), 401

        # get datetime exactly one day ago
        window = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
        # run query and serialise to python dict
        games_owned_dicts = [
            x.to_dict()
            for x in Game.objects.filter(created_at__gt=window).filter(
                started=False).filter(ended=False).filter(creator=user.user_id)
        ]
        games_unowned_dicts = [
            x.to_dict() for x in Game.objects.filter(
                created_at__gt=window).filter(started=False).filter(
                    ended=False).filter(creator__ne=user.user_id)
        ]
        # return as JSON response
        return jsonify(owned=games_owned_dicts, unowned=games_unowned_dicts)
Exemplo n.º 19
0
    def post(self, game_id):
        """Start a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # ensure player is the game's creator
        if not player.user_id == game.creator:
            return jsonify(error='403 Forbidden'), 403

        # mark the game as started and publish signal to other clients
        game = game.start()

        # return as JSON response
        return jsonify(**game.to_dict())
Exemplo n.º 20
0
    def post(self, game_id):
        """Leave a game
        """

        # authorise the user or return a 401
        player = get_user(session)
        if player is None:
            return jsonify(error='401 Unauthorized'), 401

        # get game from db
        try:
            game = Game.objects.get(pk=game_id)
        except db.DoesNotExist:
            return jsonify(error='404 Not Found'), 404

        # ensure player is the game's creator
        if not player.user_id in game.players:
            return jsonify(error='403 Forbidden'), 403

        # leave the game and let the other players know
        game = game.remove_player(player.user_id)

        # return as JSON response
        return jsonify(**game.to_dict())