Beispiel #1
0
def leave(request):
    """To stop and leave the game.
If one player chooses to leave the game, the whole game
will be stopped and other users will be notified.
"""
    # Check permission.
    if not request.session.has_key("username"):
        return HttpResponseForbidden(
            json.dumps({"error": "You are not logged in."}))

    # Check to see if game has been chosen.
    (game, requested_players) = Play.get_game(request.session["username"])
    if not game:
        return HttpResponseBadRequest(
            json.dumps({"error": "You have not chosen a game."}))

    # Stops the game.
    last_status = json.loads(game.last_status)
    already_stopped = ("winner" in last_status or "stopped" in last_status)
    response = Play.stop(game, requested_players, request.session["username"],
                            already_stopped=already_stopped)

    return response
Beispiel #2
0
def signin(request):
    """Sign into the server."""
    # Check to see if user is already logged in.
    if request.session.has_key("username"):
        return HttpResponseBadRequest("Error: You are already logged in.")

    if request.method != "POST":
        return HttpResponseBadRequest("Error: Send your data via POST.")
    post = request.POST.dict()
    
    # Check data validity.
    if (set(post.keys()) != {"username", "password"}
        or not _is_username_valid(post["username"].lower())
        or not _is_password_valid(post["password"])
):
        return HttpResponseBadRequest("Error: Your POST data is corrupted.")

    # Get user from database.
    try:
        user = User.objects.get(username=post["username"].lower())
    except User.DoesNotExist:
        return HttpResponseForbidden("No such user exists.")

    # Verify password hash.
    if not pbkdf2_sha256.verify(post["password"], user.password_hash):
        return HttpResponseForbidden("Your password is incorrect.")

    # Set up session.
    request.session["username"] = user.username

    # Check to see if the user is already in a game.
    (game, requested_players) = Play.get_game(request.session["username"])
    if game:
        return HttpResponse("You're already in a game.")

    return HttpResponse("You are now logged in, %s." % user.name)
Beispiel #3
0
def two_or_four(request):
    """Page to choose the number of players."""
    # Check permission.
    if not request.session.has_key("username"):
        return HttpResponseForbidden("Error: You are not logged in.")

    # Check to see if the user is already in a game.
    (game, requested_players) = Play.get_game(request.session["username"])
    if game:
        return HttpResponseBadRequest("Error: You're already in a game.")

    # Check data validity.
    if request.method != "POST":
        return HttpResponseBadRequest("Error: Send your data via POST.")
    post = request.POST.dict()
    if (set(post.keys()) != {"players"}
        or (post["players"] != "two" and post["players"] != "four")
):
        return HttpResponseBadRequest("Error: Your POST data is corrupted.")

    # Add user to a new game.
    _add_to_game(request.session["username"], post["players"])

    return HttpResponse("You've been added to the waitlist.")
Beispiel #4
0
def play_and_status(request):
    """Play moves if game has started.
If not, check waitlist and start a new game.
"""
    # Check permission.
    if not request.session.has_key("username"):
        return HttpResponseForbidden(
            json.dumps({"error": "You are not logged in."}))

    # Check to see if game has been chosen.
    (game, requested_players) = Play.get_game(request.session["username"])
    if not game:
        return HttpResponseBadRequest(
            json.dumps({"error": "You have not chosen a game."}))

    # Check to see if game has started.
    if not game.started:
        return HttpResponse(game.last_status)

    # Check to see if the game has been won by someone.
    if "winner" in json.loads(game.last_status):
        response = HttpResponse(game.last_status + "\n"
                + game.main_grid + "\n" + game.wallh_grid + "\n"
                + game.wallv_grid + "\n" + game.wallfills_grid
                                + "\n" + json.dumps(dict()))
        Play.stop(game, requested_players,
                  request.session["username"],
                  already_stopped=True)
        return response

    if "stopped" in json.loads(game.last_status):
        response = Play.stop(game, requested_players,
                             request.session["username"],
                             already_stopped=True)
        return response

    # Check turn.
    if game.turn.username != request.session["username"]:
        return HttpResponse(game.last_status + "\n"
                + game.main_grid + "\n" + game.wallh_grid + "\n"
                + game.wallv_grid + "\n" + game.wallfills_grid
               + "\n" + Play.get_walls(game, requested_players))

    # Check to see if any move has been made.
    if request.method != "POST":
        return HttpResponse(game.last_status + "\n"
                + game.main_grid + "\n" + game.wallh_grid + "\n"
                + game.wallv_grid + "\n" + game.wallfills_grid
               + "\n" + Play.get_walls(game, requested_players))
    post = request.POST.dict()

    # Check data validity.
    if (set(post.keys()) != {"move"} 
        or not _is_move_format_valid(json.loads(post["move"]))
):
        return HttpResponseBadRequest(
            json.dumps({"error": "Your POST data is corrupted."}))

    # Play the move.
    return Play.play(game,
                     requested_players,
                     json.loads(post["move"]))