def do_login(): username = request.form["username"] access_phrase = request.form["access_phrase"] database.access_db() result = database.login(username, access_phrase) if result in ["new", "login"]: session['username'] = username session["result"] = result return redirect(url_for("main")) h = krampus18.init("login") with h.tag("html"): krampus18.header("login") with h.tag("body"): with h.tag("h1"): h.content("Could not login " + username + ".") h.tag2("br") if result == "phrase": h.content("The access phrase did not match.") if result == "name": h.content("The name was not valid.") if result == "error": h.content("The database connection failed.") with h.tag("p"): with h.tag("a", href="/"): h.content("Back") return krampus18.get_html()
def lab(): init("lab") dna = "{}" if "username" in session: username = session["username"] if username: # this should be asynchronous, but whatever database.access_db() dna = database.get_pet_dna(username) with h.tag("html"): with h.tag("head"): title("lab") client("client.start_lab('" + dna + "');") style("lab") with h.tag("body"): with h.tag("h1"): h.content("Lab of " + username) with h.tag("div.left"): editor() with h.tag("canvas", width="512px", height="512px"): h.content("here be dragons") return r
def arena(whose=None): init("arena") username = None if "username" in session: username = session["username"] if username: database.access_db() if whose is None: whose = username database.enter_arena(username, whose) if username is None: username = "******" if whose is None: whose = "null" t = database.server_get_t(1).timestamp() with h.tag("html"): with h.tag("head"): title("arena") client("client.start_arena(\"" + username + "\", \"" + whose + "\", " + str(t) + ");") style("arena") with h.tag("body"): with h.tag("h1"): h.content("Arena of " + whose) with h.tag("form", action="/"): h.tag2("input", type="hidden", name="whose", value=whose) h.tag2("input", type="submit", value="Back") with h.tag("canvas", width="512px", height="512px"): h.content("here be dragons") return r
def save(): if "username" in session: username = session["username"] dna = request.form["dna"] if username and dna: database.access_db() database.save_pet(username, dna) return redirect(url_for("main"))
def start(self): database.access_db() self.active = {} self.t0 = time.time() self.t1 = self.t0 average = 0 i = 0 while True: self.tick() self.t1 += 1 dt = self.t1 - time.time() if dt < 0.5: print("sleep %.2f" % dt) if dt > 0: time.sleep(dt) average += dt database.server_heartbeat(1) i += 1 if i % 60 == 0: print("average sleep in last 60 seconds: %.2f" % (average / 60)) average = 0
def arena_round(): name = request.form["name"] database.access_db() r = database.get_arena_round(name) j = [r] return json.dumps(j)
def arena_t(): name = request.form["name"] database.access_db() t = database.server_get_t(1).timestamp() jt = [t] return json.dumps(jt)
def arena_pos(): name = request.form["name"] database.access_db() pets = database.get_pos_in_arena(name=name) return json.dumps(pets)