Beispiel #1
0
def get_world_state():
    try:
        world = world_state_provider.lock_and_get_world()
        num_cols = len(world.world_map.grid)
        num_rows = len(world.world_map.grid[0])
        grid = [[None for x in xrange(num_cols)] for y in xrange(num_rows)]
        for cell in world.world_map.all_cells():
            grid[cell.location.x][cell.location.y] = to_cell_type(cell)
        player_data = {
            p.player_id: player_dict(p)
            for p in world.avatar_manager.avatars
        }
        return flask.jsonify(
            **{
                'players':
                player_data,
                'score_locations': [(cell.location.x, cell.location.y)
                                    for cell in world.world_map.score_cells()],
                'pickup_locations': [(cell.location.x, cell.location.y) for
                                     cell in world.world_map.pickup_cells()],
                'map_changed':
                True,  # TODO: experiment with only sending deltas (not if not required)
                'width':
                num_cols,
                'height':
                num_rows,
                'layout':
                grid,
            })
    finally:
        world_state_provider.release_lock()
Beispiel #2
0
def code(request):
    if request.method == "POST":
        request.user.player.code = request.POST["code"]
        request.user.player.save()
        try:
            world = world_state_provider.lock_and_get_world()
            # TODO: deal with this in a better way
            if world is None:
                return _post_server_error_response("Your code was saved, but the game has not started yet!")

            world.player_changed_code(request.user.id, request.user.player.code)
        except UserCodeException as ex:
            return _post_code_error_response(ex.to_user_string())
        finally:
            world_state_provider.release_lock()

        return _post_code_success_response("Your code was saved!")
    else:
        return HttpResponse(request.user.player.code)
Beispiel #3
0
def get_world_state():
    try:
        world = world_state_provider.lock_and_get_world()
        num_cols = len(world.world_map.grid)
        num_rows = len(world.world_map.grid[0])
        grid = [[None for y in range(num_rows)] for x in range(num_cols)]
        for cell in world.world_map.all_cells():
            grid[cell.location.x][cell.location.y] = to_cell_type(cell)
        player_data = {p.player_id: player_dict(p) for p in world.avatar_manager.avatars}
        return {
                'players': player_data,
                'score_locations': [(cell.location.x, cell.location.y) for cell in world.world_map.score_cells()],
                'pickup_locations': [(cell.location.x, cell.location.y) for cell in world.world_map.pickup_cells()],
                'map_changed': True,  # TODO: experiment with only sending deltas (not if not required)
                'width': num_cols,
                'height': num_rows,
                'layout': grid,
            }
    finally:
        world_state_provider.release_lock()