def answer_question(question_number: int, answer: str, user: UserTable) -> dict: answer = (answer or "").strip() if len(answer) > 1000: return incorrect_answer if not answer: return incorrect_answer question: Questions = get_ques_by_id(question_number) if not question: return no_question(question_number) correct = replace("", question.answer) current = replace("", answer) is_correct = correct == current data_dict = { "user": user.user, "school": user.school, "attempt": current, "is_correct": is_correct, "timestamp": js_time(), "level": question_number, } js = f"{user.user} ({user.school}) tried to answer {user.current_level} with {current} ({'✅' if is_correct else '❌'})" run_threaded_tasks(js, data_dict, is_correct) if is_correct: # no user.current_level = user.current_level + 1 # +=1 yeah user.last_question_answered_at = js_time() save_to_db() return {"result": True, "next_level": user.current_level} else: return incorrect_answer
def add_player_to_clan(clan_data: TeamTable, user_data: UserTable): if user_data.user not in clan_data.members: clan_data.members.append(user_data.user) event = clan_data.team_event _dict = user_data.team_data.get(event) if _dict is None: user_data.team_data[event] = {} mutate(user_data.team_data, event, "name", clan_data.team_name)
def answer_question(question_number: int, answer: str, user: UserTable) -> dict: answer = (answer or "").strip() if not answer: return incorrect_answer question: Questions = get_ques_by_id(question_number) if not question: return no_question(question_number) correct = replace("", question.answer) current = replace("", answer) is_correct = correct == current js = f"{user.user} ({user.school}) tried to answer {user.current_level} with {current} ({'✅' if is_correct else '❌'})" if is_correct: post_level_up_webhook(js) else: post_incorrect_webhook(js) if is_correct: # no user.current_level = user.current_level + 1 # +=1 yeah user.last_question_answered_at = js_time() save_to_db() return {"result": True, "next_level": user.current_level} else: return incorrect_answer
def register(request: _Parsed): json = request.json get = json.get user = get("user") name = get("name") password = get("password") try: user_data = UserTable(user, name, password) add_to_db(user_data) print("registered ", user) return {"user_data": user_data.as_json} except Exception as e: if isinstance(getattr(e, "orig", None), IntegrityError): raise AppException("User exists", 409) raise e
def add_user(data: dict): invalid = is_invalid_data(data) if invalid: return { "error": "Could not create account", "reason": invalid }, BAD_REQUEST user = data["user"].strip().lower() if sanitize(user) != user: return { "error": "Could not create account", "reason": "username contains invalid character(s)", } if len(user) < 3 or len(user) > 30: return { "error": "Could not create account", "reason": "username length" } password = data.pop("password") if len(password) < 5: return { "errpr": "Could not create account", "reason": "password length too short", } pw_hash = generate_password_hash(password) data["password_hash"] = pw_hash data["user"] = user try: user = UserTable(**data) add_to_db(user) webhook(user) return user.as_json except Exception as e: if isinstance(getattr(e, "orig", 0), IntegrityError): return {"error": "User exists"} print(e) return {"error": "Could not create account"}
def register(request: _Parsed): json = request.json get = json.get user = get("user") name = get("name") email = get("email") school = get("school") password = get("password") try: user_data = UserTable( user=user, name=name, email=email, school=school, password=password, team_data=init_user_event_dict(), ) add_to_db(user_data) return user_data.as_json except Exception as e: if isinstance(getattr(e, "orig", None), IntegrityError): raise AppException("User exists") raise e
def requalify(user: UserTable): user.is_disqualified = False save_to_db() return {"user_data": user.as_json}
def set_last_question_answered_at(user: UserTable, tstamp): ts = safe_int(tstamp) user.last_question_answered_at = ts save_to_db() return SUCCESS
def set_level(user: UserTable, level_to_set): level_to_set = safe_int(level_to_set) user.current_level = level_to_set save_to_db() return SUCCESS
def add_registration_data(user_data: UserTable, event_name: str, registration_data: dict = None): if user_data.team_data[event_name].get("registration_data") is None: user_data.team_data["registration_data"] = validate( registration_data, event_name, None)