Skip to content
Snippets Groups Projects
room.py 1.28 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
    
    
    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))
    
        player_max = parameters['player_max']
    
        if player_max <= 0:
            return(FAIL(CODE_SEMANTIC))
    
        room_id = db.Room.insert(player_max=player_max)
        room_record = db(db.Room.id == room_id).select().first()
    
        json = {"room_id": room_id, "room_code": room_record.code, "room_pw": room_record.hashcode}
    
        return(response.json(json))
    
    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 = {"players": players}
    
    Nils G.'s avatar
    Nils G. committed
    
        return(response.json(json))