Пример #1
0
def create_room(arg):
    game_info = Game.get(arg['game']['id'])
    room = Room(name=arg['name'],
                capacity=arg['capacity'],
                game=game_info,
                difficulty=arg['game']['difficulty'])
    room.put()
    return room
Пример #2
0
def create_room(arg):
    assert arg['game'].has_key(
        'map_size'), 'Required argument is missing: "game.map_size".'
    player = arg['sender']
    game_info = Game.get(arg['game']['id'])
    room = Room(name=arg['name'],
                capacity=arg['capacity'],
                game=game_info,
                map_size=arg['game']['map_size'])
    room.put()
    player.room = room
    player.is_ready = True
    player.put()

    return {'room': str(player.room.key())}
Пример #3
0
def room_api(room_name=None):
    user = users.get_current_user()
    if flask.request.method == 'GET':
        if room_name is None:
            rooms = []
            if not user:
                rooms = Room.fetch_all_anonymous()
            else:
                rooms = Room.fetch_all()
            for room in rooms:
                update_current_time(room)
            return json_response([room.to_dict() for room in rooms])
        else:
            room = Room.fetch_by_name(room_name)
            if room is None:
                return json_fail(Reason.NO_ROOM_FOUND)
            update_current_time(room)
            return json_response(room.to_dict()) 
    elif flask.request.method == 'POST':
        if not user:
            return json_fail(Reason.LOGIN_REQUIRED)
        else:
            room_info = flask.request.get_json(silent=True)
            if room_info is None:
                return json_fail(Reason.INVALID_PARAMETER)

            if "name" not in room_info:
                return json_fail(Reason.ROOM_NAME_REQUIRED)

            room = Room.fetch_by_name(room_info["name"])
            logging.info('create_room(%s):%s'%(room_info["name"],room.__class__.__name__))
            if room is not None:
                return json_fail(Reason.EXIST_ROOM_NAME)

            room = Room(creator_email=user.email(), name=room_info["name"])
            if "current_time" in room_info:
                room.current_time=int(room_info["current_time"])
            if "cover" in room_info:
                room.cover=room_info["cover"]
            room.put()
            update_current_time(room)
            return json_succ(data=room.to_dict())
    elif flask.request.method == 'PUT':
        if not user:
            return json_fail(Reason.LOGIN_REQUIRED)

        room = Room.fetch_by_name(room_name)
        logging.info('update_room(%s):%s'%(room_name,room.__class__.__name__))
        if room is None:
            return json_fail(Reason.NO_ROOM_FOUND)
        if user.email() != room.creator_email:
            return json_fail(Reason.FORBIDDEN_OPERATION)

        room_info = flask.request.get_json(silent=True)
        if room_info is None:
            return json_fail(Reason.INVALID_PARAMETER)

        if "current_time" in room_info:
            room.current_time=int(room_info["current_time"])
        if "cover" in room_info:
            room.cover=room_info["cover"]
        room.put()
        return json_succ()
    elif flask.request.method == 'DELETE':
        if not user:
            return json_fail(Reason.LOGIN_REQUIRED)

        room = Room.fetch_by_name(room_name)
        logging.info('delete_room(%s):%s'%(room_name,room.__class__.__name__))
        if room is not None:
            if user.email() != room.creator_email:
                return json_fail(Reason.FORBIDDEN_OPERATION)
            else:
                room.key.delete()
                return json_succ()
        else:
            return json_fail(Reason.NO_ROOM_FOUND, room_name)

    else:
        return json_fail(Reason.INVALID_PARAMETER)