Ejemplo n.º 1
0
def web_start_game(gameid):
    from interstellarage import current_user
    user = current_user()
    game = find(unique=int(gameid))

    if user is None:
        return "Not logged in", 400
    elif game is None:
        return "Game does not exist.", 400
    elif user.unique != game.creator_unique:
        return "Not authorized", 400

    game.start()
    return "Started game"
Ejemplo n.º 2
0
def web_join_game(gameid):
    """
    Called when the end user wants to join an existing game.

    Args:
        gameid (int):
            The unique of the `Game` the end user wants to join.

    Request Fields:
        join_code (str):
            The password to the `Game` that the end user provided.

    Returns:
        A 200 response if successful, an error response detailing what went
        wrong if not.
    """

    from interstellarage import current_user
    user = current_user()
    game = find(unique=int(gameid))

    if user is None:
        return "Not logged in", 400
    elif game is None:
        return "Game does not exist.", 400
    elif game.started:
        return "Game already started.", 400
    elif game.full():
        return "Game full", 400

    # Check to see if the join code is correct.
    join_code = request.form['join_code']
    if join_code != game.join_code:
        return "Incorrect password", 400

    # Is the desired faction alright?
    faction_code = int(request.form['faction'])
    if not player_lib.check_faction(faction_code):
        return "Invalid faction", 400
    elif game.faction_taken(faction_code):
        return "Faction taken", 400

    # Success!
    game.add_user(user, faction_code)
    return "Joined", 200
Ejemplo n.º 3
0
def web_create_game():
    """
    Called when the user wants to create a new game.

    Request Fields:
        name (str):
            The publicly displayed name of the `Game` we want to create.

        join_code (str):
            The password to join the `Game` we want to create.

    Returns:
        If successful, this function will return the URL to the lobby (and
        later, gameplay) of the new `Game`. If not, we return an error header
        with a message stating what went wrong in the body.
    """

    # Declare global variables
    global MIN_JOINCODE_LENGTH
    global MAX_JOINCODE_LENGTH

    from interstellarage import current_user
    user = current_user()

    if user is None:
        return "Not logged in", 400

    # Get the given join code and name.
    join_code = request.form['join_code']
    name = request.form['name']
    assert len(join_code) in irange(MIN_JOINCODE_LENGTH, MAX_JOINCODE_LENGTH)
    assert len(name) in irange(MIN_NAME_LENGTH, MAX_NAME_LENGTH)

    # Ensure that the faction code looks good
    faction_code = int(request.form['faction'])
    # TODO

    # Create the game and add the player who made it.
    new_game = Game(name, join_code)
    new_game.add_user(user, faction_code, creator=True)

    # Return the URL
    return "/game/{0}".format(str(new_game.unique))
Ejemplo n.º 4
0
def web_submit_orders():
    # Get the current user.
    from interstellarage import current_user
    user = current_user()
    if user is None:
        return "Not logged in", 400

    # Get the game from the input.
    game_id = int(request.form['game'])
    game = game_lib.find(unique=game_id)
    if game is None:
        return "No game with that ID exists", 400
    elif user not in game:
        return "You are not in this game", 400

    # Parse the JSON in the order fields
    move_order_dicts = json.loads(request.form['move'])
    ftl_order_dicts = json.loads(request.form['hyperspace'])
    colony_order_dicts = json.loads(request.form['colonize'])
    build_order_dicts = json.loads(request.form['build'])

    # Calls one of the "from_dict" functions defined below their respective
    # classes in this module.
    process = lambda f, L: [f(d, game, user=user) for d in L]

    # Parse the dicts into order objects.
    move = process(move_order_from_dict, move_order_dicts)
    ftl = process(hyperspace_order_from_dict, ftl_order_dicts)
    colonize = process(upgrade_planet_order_from_dict, colony_order_dicts)
    build = process(build_fleet_order_from_dict, build_order_dicts)

    # Queue the orders into the game.
    orders = []
    orders.extend(move)
    orders.extend(ftl)
    orders.extend(colonize)
    orders.extend(build)
    game.queue_orders(orders)

    # We're done here.
    return "Copy, Commander. Orders recieved.", 200
Ejemplo n.º 5
0
def web_entire_galaxy():
    """
    This function is called when the client needs to update its local galaxy
    information. This method therefore reads outputs the `Galaxy` dict as JSON
    along with some other useful information such as funds available and
    current turn number.

    Request Fields:
        game (int):
            The unique id for the game that is being played.

    Returns:
        A JSON `str` including the JSON for the galaxy in the "galaxy" field,
        the amount of money the player has in the "money" field, and the game's
        turn number in the "turn" field.
    """

    # Get the current user.
    from interstellarage import current_user
    user = current_user()
    if user is None:
        return "Not logged in", 400

    # Get the desired game
    import game as game_lib
    game_unique = int(request.form['game'])
    game = game_lib.find(unique=game_unique)
    if game is None:
        return "Invalid game", 400
    elif user not in game:
        return "You are not part of this game", 400

    # Return the galaxy as JSON
    return json.dumps({
        'galaxy' : game.galaxy.as_list(for_user=user),
        'turn' : game.on_turn,
        'money' : game.player_for_user(user).money
    })