Skip to content
Snippets Groups Projects
room.py 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • from parameter_util import JSON_BODY, JSON_CONTAINS
    
    from http_util import FAIL, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE
    
    from room_util import ROOM_GET, PLAYERS_GET, ROOM_CLOSE, ROOM_HEARTBEAT
    
    
    import random
    
    def create():
        parameters = JSON_BODY(request)
    
        if parameters == None:
    
            return(FAIL(CODE_JSON))
    
        if not JSON_CONTAINS(parameters, [('player_max', int)]):
            return(FAIL(CODE_MISSING))
    
        #TODO: Do not allow to not specify a game_name
        if not JSON_CONTAINS(parameters, [('game_name', str)]):
            parameters['game_name'] = "Lynchburg"
    
    
        player_max = parameters['player_max']
    
        game_name = parameters['game_name']
    
        if player_max <= 0 or not game_name or not game_name.strip():
    
            return(FAIL(CODE_SEMANTIC))
    
        room_id = db.Room.insert(player_max=player_max, game_name=game_name)
    
        room_record = db(db.Room.id == room_id).select().first()
    
    
        json_response = {"room_id": room_id, "room_code": room_record.code, "room_pw": room_record.hashcode}
        return(response.json(json_response))
    
    Nils G.'s avatar
    Nils G. committed
    
    
    def status():
        parameters = JSON_BODY(request)
    
        if parameters == None:
            return(FAIL(CODE_JSON))
    
        if not JSON_CONTAINS(parameters, [('room_id', int), ('room_pw', str)]):
            return(FAIL(CODE_MISSING))
    
        room_id = parameters['room_id']
        room_pw = parameters['room_pw']
    
        room_record = ROOM_GET(room_id, room_pw)
    
        if not room_record:
            return(FAIL(CODE_SEMANTIC))
    
    
        if room_record.closed:
            return(FAIL(CODE_GONE))
    
    
        players = PLAYERS_GET(room_record)
    
    Nils G.'s avatar
    Nils G. committed
    
    
        json_response = {"players": players}
        return(response.json(json_response))
    
    Nils G.'s avatar
    Nils G. committed
    
    
    def close():
        parameters = JSON_BODY(request)
    
        if parameters == None:
            return(FAIL(CODE_JSON))
    
        if not JSON_CONTAINS(parameters, [('room_id', int), ('room_pw', str)]):
            return(FAIL(CODE_MISSING))
    
        room_id = parameters['room_id']
        room_pw = parameters['room_pw']
    
        room_record = ROOM_GET(room_id, room_pw)
    
        if not room_record:
            return(FAIL(CODE_SEMANTIC))
    
        if room_record.closed:
            return(FAIL(CODE_GONE))
    
        ROOM_CLOSE(room_record)
    
        return(response.json({"status": "success"}))