Newer
Older
from parameter_util import JSON_BODY, JSON_CONTAINS, JSON_TO_B64, B64_TO_JSON
from http_util import FAIL, SUCCESS, CODE_JSON, CODE_MISSING, CODE_SEMANTIC, CODE_GONE, CODE_NOTFOUND, CODE_CONFLICT, CODE_PERMISSION
from room_util import ROOM_GET_CODE, PLAYERS_GET
from cookie_util import COOKIE_SET, COOKIE_GET
def create():
parameters = JSON_BODY(request)
if parameters == None:
return(FAIL(CODE_JSON))
if not JSON_CONTAINS(parameters, [('room_code', str), ('user_name', str)]):
return(FAIL(CODE_MISSING))
room_code = parameters["room_code"]
user_name = parameters["user_name"]
room_code = ''.join(c for c in room_code if c.isalnum())
user_name = ''.join(c for c in user_name if c.isalnum())
if (len(room_code) != 4) or (len(user_name) == 0) or (len(user_name) > 15):
return(FAIL(CODE_SEMANTIC))
room_code = room_code.upper()
user_name = user_name.upper()
room_record = ROOM_GET_CODE(room_code)
if not room_record:
return(FAIL(CODE_NOTFOUND))
if room_record.started:
return(FAIL(CODE_GONE))
players = PLAYERS_GET(room_record)
if len(players) >= room_record["player_max"]:
return(FAIL(CODE_NOTFOUND))
for player in players:
if player["name"] == user_name:
return(FAIL(CODE_CONFLICT))
player_id = db.Player.insert(name=user_name, room_id=room_record.id)
player_record = db(db.Player.id == player_id).select().first()
COOKIE_SET(player_record)
return(SUCCESS())
def submit():
player_record, room_record = COOKIE_GET()
if not player_record or not room_record:
return(FAIL(CODE_PERMISSION))
if room_record.closed:
return(FAIL(CODE_GONE))
current_inputs = B64_TO_JSON(player_record.inputs)
#Check if the player has exceeded the maximum number of submits
if len(current_inputs) >= player_record.input_max:
return(FAIL(CODE_CONFLICT))
parameters = JSON_BODY(request)
if parameters == None:
return(FAIL(CODE_JSON))
if not JSON_CONTAINS(parameters, [('submit', str)]):
return(FAIL(CODE_MISSING))
parameters["submit"] = ''.join(c for c in parameters["submit"] if c.isalnum())
new_input = {"submit": parameters["submit"]}
if JSON_CONTAINS(parameters, [('selection', int)]):
new_input["selection"] = parameters["selection"]
if JSON_CONTAINS(parameters, [('inputs', dict)]):
#Load role definitions for additional validation (e.g. maxlength)
elements = B64_TO_JSON(room_record.roles)[player_record.role]["elements"]
input_definitions = {}
for element in elements:
if element["type"] == "input":
input_definitions[element["name"]] = element
inputs = parameters["inputs"]
for key in inputs:
keytest = ''.join(c for c in key if c.isalnum())
if (not isinstance(inputs[key], str)) or (keytest != key):
if "length" in input_definitions[key] and len(inputs[key]) > input_definitions[key]["length"]:
return(FAIL(CODE_MISSING))
inputs[key] = ''.join(c for c in inputs[key] if (c.isalnum() or c in " ,.!?-()\u2019"))
new_input["inputs"] = inputs
#Insert new input into table
current_inputs.append(new_input)
db(db.Player.id == player_record.id).update(inputs=JSON_TO_B64(current_inputs), submit_done=(len(current_inputs) >= player_record.input_max))