예제 #1
0
def create_game(request):
    game_type = 'default'
    game = new_game(game_type)
    save_game(game)
    return Ok({
        'game': game.uuid,
        'guilds': [key for key in game.guilds.keys()],
    })
예제 #2
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)