Example #1
0
def show(id):
    """
    /games/{id}
    Parameters:
   + `id` (required): The ID of the game
    Response:
     type: object
     properties:
        id:
           type: integer
        token:
            type: string
        duration:
            type: integer
        board:
            type: string
        time_left:
            type: integer
        points:
            type: integer
    """
    game = get_game(id)
    if not game:
        return prepare_response("Game not found", 403)
    result = {
        "id": id,
        "token": game["token"],
        "duration": game["duration"],
        "board": game["board"],
        "time_left": get_time_left(game["end_time"]),
        "points": game["points"]
    }

    return prepare_response(result, 200)
  def POST(self, game_id):
    you = require_you()
    game = get_game(game_id)

    params = web.input(from_space=None, to_space=None)
    if not (params["from_space"] and params["to_space"]):
      raise http.badrequest("Moves must include from_space and to_space")

    from_space = tag_2d(params["from_space"])
    to_space   = tag_2d(params["to_space"])
    
    if not (len(from_space) == len(to_space) == 2):
      raise http.badrequest("Invalid space tags")

    # check if it's your turn
    player = game["order"][game["turn"] % len(game["order"])]
    if game["players"][player] != you.id:
      raise http.precondition("It's not your turn")

    board = game["board"]
    attacking_piece = game["board"][from_space[0]][from_space[1]]

    if attacking_piece == None:
      raise http.forbidden("No piece to move from %s" % params["from_space"])

    if attacking_piece["player"] != player:
      raise http.forbidden("It's not %s's turn" % attacking_piece["player"])

    if not valid_move(attacking_piece["kind"], from_space, to_space):
      raise http.forbidden("You can't move %s from %s to %s" % (attacking_piece["kind"], params["from_space"], params["to_space"]))
    
    defending_piece = game["board"][to_space[0]][to_space[1]]
    if defending_piece != None:
    
      if attacking_piece["player"] == defending_piece["player"]:
        raise http.forbidden("You can't attack yourself")
    
      if not battle(attacking_piece["kind"], defending_piece["kind"]):
        raise http.forbidden("You can't beat %s with %s" % (defending_piece["kind"], attacking_piece["kind"]))
      
    # actually move the piece
    game["board"][from_space[0]][from_space[1]] = None
    game["board"][to_space[0]][to_space[1]] = attacking_piece
    
    # advance the turn
    game["turn"] += 1

    # trigger an event
    move = {"type":"move", "game":game_id, "user":you.id, "name":you["name"],
            "from_space":params["from_space"], "to_space":params["to_space"]}
    
    # update timestamps just before saving
    game["timestamp"] = move["timestamp"] = timestamp = make_timestamp()    
    db[game_id] = game
    move_id = db.create(move)
      
    return timestamp
  def POST(self, game_id):
    you = require_you()
    game = get_game(game_id)

    params = web.input(text='')
    text = params["text"]
    if len(text) == 0:
      raise http.unacceptable("Chat messages must contain some text")

    chat = {"type":"chat", "text":text, "game":game_id, "user":you.id, "name":you["name"]}
    
    # synchronize timestamps
    game["timestamp"] = chat["timestamp"] = make_timestamp()    
    db[game_id] = game
    db.create(chat)
    return "OK"
Example #4
0
def submit_word(id, input):
    """
    /games/{id}
     Parameters:
     - name: id
        in: path
     - name: input
        in: body
        type: object
        required:
            - id
            - token
            - word
     Response:
        type: object
        properties:
            id:
                type: integer
            token:
                type: string
            duration:
                type: integer
            board:
                type: string
            time_left:
                type: integer
            points:
                type: integer
    """
    if input.get("id") != id:
        return prepare_response(None, 403)
    token = input.get("token")
    word = input.get("word")

    game = get_game(id)
    if (not game) or (game["token"] != token):
        print("Incorrect id or token")
        return prepare_response("Non-matching game IDs or tokens, please check your input.", 406)

    time_left = get_time_left(game["end_time"])
    points = game["points"]

    result = {
            "id": id,
            "token": game["token"],
            "duration": game["duration"],
            "board": game["board"],
            "time_left": time_left,
            "points": points
        }

    if time_left == 0:
        return prepare_response("This game has run out of time.", 406)

    if not check_valid_word(word):
        return prepare_response("The word does not exist in the dictionary.", 406)

    if word in game["wordsUsed"]:
        return prepare_response("This word has already been used.", 406)

    if not check_board_for_word(game['board'], word):
        return prepare_response("Word not found", 406)

    points = points + get_word_points(word)

    add_used_word(id, word)
    update_points(id, points)

    result["points"] = points
    return prepare_response(result, 200)