Skip to content
Snippets Groups Projects
room.py 1.91 KiB
Newer Older
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))
    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_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"}))