async def logout(request: Request) -> JSONResponse: with session_manager() as session: session_info = SessionInfo.query(request.user.token, session) if session_info: request.user.session_info.remove(session_info) session_info.delete(session) del request.cookies["session"] return JSONResponse({"success": True})
async def deny_friend(request: Request, friend_form: FriendForm) -> JSONResponse: with session_manager() as session: friend = Friend.query_both( user_id=User.find(username=friend_form.username).id, friend_id=request.user.id, session=session, ) friend.delete(session=session) return renew(JSONResponse({"success": True}))
def exercise_database(): with open("exercises.csv") as file: reader = csv.DictReader(file) exercises = [] with session_manager() as session: for row in reader: difficulty = row["difficulty"].lower() tags = row["tags"].split("|") exercise = Exercise.create(row["name"], tags, difficulty) exercise.write(session) exercises.append(exercise)
async def add_friend(request: Request, friend_form: FriendForm) -> JSONResponse: id = User.find(username=friend_form.username).id with session_manager() as session: friend = Friend.query_both( user_id=User.find(username=friend_form.username).id, friend_id=request.user.id, session=session, ) if friend: return renew(JSONResponse({"success": False}), request.user.token) request.user.new_friend(id) return renew(JSONResponse({"success": True}), request.user.token)
async def buy(request: Request, form: BuyForm): if form.price > request.user.points: return renew( JSONResponse({"success": False}), request.user.token, ) with session_manager() as session: user = _DBUser.query_unique(session, {"id": request.user.id}) user.points -= form.price user.avatar = form.avatar user.write(session) return renew( JSONResponse({"success": True}), request.user.token, )
async def create_session(request: Request, form: SessionCreateForm): users = [] with session_manager() as session: for user in form.users: user = _DBUser.query_unique(session, {"username": user}) users.append(user) user.write(session) users.append( _DBUser.query_unique(session, {"username": request.user.username})) game_session = GameSession(name=form.name, users=users, tag=form.tag) game_session.write(session) return renew( JSONResponse({ "success": True, **game_session.as_dict() }), request.user.token, )
async def attack(request: Request, form: AttackForm): with session_manager() as session: game_session = GameSession.find(session, id=form.id) game_session.bossHealth -= form.damage if game_session.bossHealth <= 0: for user in game_session.users: user.points += 100 game_session.delete(session) return renew( JSONResponse({"success": True}), request.user.token, ) game_session.write(session) return renew( JSONResponse({ "success": True, **game_session.as_dict() }), request.user.token, )