コード例 #1
0
ファイル: routes.py プロジェクト: edwardvear/MultiGuessr
def leave_room():
    """ Allows a player to leave their room. If they are the host, deletes the room """

    roomname, username = (session['roomname'], session['username'])
    del session['roomname']
    del session['username']

    # Remove self from room
    if r.sismember("rooms:" + roomname + ":guesses", username):
        r.srem("rooms:" + roomname + ":guesses:", username)
        r.delete("rooms:" + roomname + ":guesses:" + username)
    r.srem("rooms:" + roomname + ":players", username)

    # If host, clear the rest of the data
    if username == r.get("rooms:" + roomname + ":host").decode('utf-8'):
        players = [
            player.decode('utf-8')
            for player in r.smembers("rooms:" + roomname + ":guesses")
        ]
        for player in players:
            r.delete("rooms:" + roomname + ":guesses:" + player)
            r.srem("rooms:" + roomname + ":guesses", player)
        r.delete("rooms:" + roomname + ":players")
        r.delete("rooms:" + roomname + ":guesses")
        r.delete("rooms:" + roomname + ":answer")
        r.delete("rooms:" + roomname + ":host")
        r.srem("rooms", roomname)

    return jsonify(success=True)
コード例 #2
0
 def __init__(self, roomname, username):
     self.roomname = roomname
     self.username = username
     self.is_host = r.get("rooms:" + roomname +
                          ":host").decode('utf-8') == session['username']
     self.already_guessed = r.sismember("rooms:" + roomname + ":guesses",
                                        username)
コード例 #3
0
def join(roomname, username):
    """ Adds the given username to the room if it exists. """

    if r.sismember('rooms', roomname) and not r.sismember(
            'rooms:' + roomname + ':players', username):
        session['roomname'] = roomname
        session['username'] = username

        r.sadd("rooms:" + roomname + ":players", username)
        return redirect('/')
    elif not r.sismember('rooms', roomname):
        return render_template('index.html', error="That Room doesn't exist!")
    else:
        return render_template(
            'index.html',
            error="That username is already in use in that room!")
コード例 #4
0
def host(roomname, username):
    """ If the room doesn't already exists, creates it and adds the given username as host. """

    if r.sismember('rooms', roomname):
        return render_template('index.html', error="That Room already esists!")
    else:
        session['roomname'] = roomname
        session['username'] = username

        r.sadd("rooms", roomname)
        r.sadd("rooms:" + roomname + ":players", username)
        r.set("rooms:" + roomname + ":host", username)
        return redirect('/')
コード例 #5
0
def index():
    """ Determines the current state of the user, and routes them to the proper place. """

    if 'roomname' in session and 'username' in session:
        if not r.sismember('rooms', session['roomname']):
            del session['roomname']
            del session['username']
            return render_template('index.html',
                                   error="That Room no longer exists!")
        else:
            player = Player(session['roomname'], session['username'])
            return game(player)
    elif all(field in request.form
             for field in ("username", "roomname", "host_or_join")):
        roomname, username = (request.form['roomname'],
                              request.form['username'])

        if request.form['host_or_join'] == 'host':
            return host(roomname, username)
        elif request.form['host_or_join'] == 'join':
            return join(roomname, username)
    else:
        return render_template('index.html')