def api_create(url): global ALL_USERS, USER_LOGIN_URLS dt, ok = _validate("create", { "name": { "t": str, "p": "body" }, "level": { "t": int, "p": "body", "range": [0, 2] } }, body=True) if (ok == False): return dt server.set_code(200) server.set_header("Content-Type", "application/json") _tl.acquire() id_ = secrets.token_hex(ID_LEN) while (id_ == "0" * ID_LEN * 2 or id_ in ALL_USERS): id_ = secrets.token_hex(ID_LEN) ALL_USERS[id_] = {"nm": dt["name"].title(), "lvl": dt["level"], "a": 0} l_id = secrets.token_hex(URL_ID_LEN) while (l_id in USER_LOGIN_URLS): l_id = secrets.token_hex(URL_ID_LEN) USER_LOGIN_URLS[l_id] = (id_, int(time.time()) + EXP_START_TIME) _tl.release() return {"url": f"/play/{l_id}"}
def rsrc(url): server.set_header("Content-Type", MIME_TYPES[url.split(".")[-1]]) if (os.path.exists(BASE_PATH + url)): server.set_code(200) return utils.cache(BASE_PATH + url) server.set_code(404) server.set_header("Content-Type", "text/plain") return b"Not Found"
def rsrc(url): server.set_header("Content-Type", MIME_TYPES[url.split(".")[-1]]) if (os.path.exists(BASE_PATH + url)): server.set_code(200) server.set_header("Cache-Control", "public,max-age=31536000,immutable") return utils.cache(BASE_PATH + url) server.set_code(404) server.set_header("Content-Type", "text/plain") return b"Not Found"
def play(url): tk = api.read_token() server.set_header("Content-Type", "text/html") if (tk is None or not api.is_valid(tk)): server.set_code(307) server.set_header("Location", "https://megimeru.herokuapp.com/") return b"" server.set_code(200) dt = api.get_user_data(tk) return GAME_TEMPLATE[0] + bytes( f"{{name:\"{dt['nm']}\",level:{dt['lvl']}}}", "utf-8") + GAME_TEMPLATE[1]
def api_answer(url): global ALL_USERS dt, ok = _validate("answer", {"a": {"t": int, "p": "body"}}, body=True) if (ok == False): return dt server.set_code(200) server.set_header("Content-Type", "text/plain") tk = read_token() if (tk is None or not is_valid(tk)): return b"" _tl.acquire() ALL_USERS[tk]["a"] += max(dt["a"], 0) _tl.release() utils.print(str(ALL_USERS)) return b""
def play_token(url): server.set_header("Content-Type", "text/html") tk = api.is_valid_login(url[6:]) if (tk is not None): server.set_code(307) server.set_header( "Set-cookie", f"__ctoken={tk};Max-Age={api.TOKEN_EXP_DATE};SameSite=Secure;Secure;HttpOnly;Path=/" ) server.set_header("Location", "https://megimeru.herokuapp.com/play") return b"" server.set_code(404) server.set_header("Content-Type", "text/html") return utils.cache("web/not_found.html")
def not_found(url): server.set_code(404) server.set_header("Content-Type", "text/html") return utils.cache("web/not_found.html")
def index(url): server.set_code(200) server.set_header("Content-Type", "text/html") server.set_header("Cache-Control", "public,max-age=31536000,immutable") return utils.cache("web/index.html")
def _validate(eb, t, body=False): b_dt = None if (body == True): try: b_dt = json.loads(server.body()) except json.JSONDecodeError as e: traceback.print_exception(None, e, e.__traceback__) server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": "E_JSON", "message": "Unable to Deserialize JSON" } }, False) q = server.queries() o = {} for k, v in t.items(): if (v["p"] == "body"): if (k not in b_dt and "d" not in v): server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD", "message": f"Required Field '{k}' is Missing from Request Body" } }, False) o[k] = (b_dt[k] if k in b_dt else v["d"]) if (type(o[k]) != v["t"]): server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD_TYPE", "message": f"Field '{k}' should have '{JSON_TYPE_MAP.get(v['t'],'object')}' type, but has '{JSON_TYPE_MAP.get(type(o[k]),'object')}' type" } }, False) if ("range" in v): if (o[k] < v["range"][0] or o[k] > v["range"][1]): server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD_RANGE", "message": f"Field '{k}' should be between '{v['range'][0]}' and '{v['range'][1]}', but has a value of '{o[k]}'" } }, False) elif (v["p"] == "query"): if (k not in q and "d" not in v): server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD", "message": f"Required Field '{k}' is Missing from Request Query" } }, False) o[k] = (q[k] if k in q else v["d"]) try: o[k] = v["t"](o[k]) except ValueError: server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD_TYPE", "message": f"Field '{k}' should have '{JSON_TYPE_MAP.get(v['t'],'object')}' type, but has '{JSON_TYPE_MAP.get(type(o[k]),'object')}' type" } }, False) if ("range" in v): if (o[k] < v["range"][0] or o[k] > v["range"][1]): server.set_code(400) server.set_header("Content-Type", "application/json") return ({ "error": { "code": f"E_{eb.upper()}_FIELD_RANGE", "message": f"Field '{k}' should be between '{v['range'][0]}' and '{v['range'][1]}', but has a value of '{o[k]}'" } }, False) else: raise RuntimeError(v["p"]) return (o, True)
def api_storyline(url): server.set_code(200) server.set_header("Content-Type", "application/json") return storage.read("storyline.json")
def not_found(url): server.set_code(307) server.set_header("Location", "https://foodtruck853.herokuapp.com/") return b""