Ejemplo n.º 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
Ejemplo n.º 2
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"]))