Example #1
0
def post_turn(request):
    """
    Expected data:
    ```
    [
        {
            "slug": "character slug",
            "actions": [{
                "place": "place_slug",
                "action": "action_slug",
                "target": {
                    "guild": "guild_slug",
                    "character": "character_slug"
                }
            }]
        }
    ]
    ```
    """

    # Dummy fix for the CORS problem.
    # Front don't send the Content-Type header and we make the decoding
    # manually on the handler.
    if isinstance(request.body, bytes):
        request.body = json.loads(request.body.decode('utf-8'))

    game_uuid = request.get_params.get('game', None)
    guild_slug = request.get_params.get('guild', None)

    # Validate the post schema
    try:
        schema = RecursiveList(turn_character_schema)
        schema.validate(request.body)

    except SchemaError as error:
        return BadRequest({
            "errors": error.error
        })
    except SchemaErrors as error:
        return BadRequest({
            "errors": error.errors
        })

    # Load game and guild runtime
    game = load_game(game_uuid)
    guild = game.guilds[guild_slug]

    # Convert turn to runtime objects
    turn = turn_to_runtime(request.body, game, guild)

    # Submit turn to core
    submit_turn(game, turn)
    save_game(game)

    # Convert new game data
    converted_game = convert_game(game, guild)

    return Created(converted_game)
Example #2
0
def get_turn(request):
    game_uuid = request.get_params.get('game', None)
    guild_slug = request.get_params.get('guild', None)

    # Dummy data for development
    if not game_uuid:
        return Ok(DUMMY_GET)

    game = load_game(game_uuid)

    guild = game.guilds[guild_slug]
    converted_game = convert_game(game, guild)

    return Ok(converted_game)