def api_finish_problemset(challengeID: int, problemsetID: int): """ 申请完成一个挑战下的某个习题集 challengeID 挑战ID problemsetID 习题集ID """ if not permission_manager.has_permission( session.get("uid"), f"challenge.access.{challengeID}"): return make_response(-1, message="你没有权限访问该挑战") challenge: Challenge = db.session.query(Challenge.problemset_list).filter( Challenge.id == challengeID).one_or_none() if not challenge: return make_response(-1, message="该挑战不存在") if problemsetID not in challenge.problemset_list: return make_response(-1, message="该习题集ID不在该挑战之下") problemset: ProblemSet = db.session.query( ProblemSet.problems).filter(ProblemSet.id == problemsetID).one() for problem in problemset.problems: submission = db.session.query(Submission.id).filter( expr.and_(Submission.uid == session.get("uid"), Submission.problem_id == problem, Submission.status == "accepted")).one_or_none() if not submission: return make_response(-1, message="在该习题集之下,你尚存题目未完成.") permission_manager.add_permission( session.get("uid"), f"challenge.finish.{challengeID}.{problemsetID}") return make_response(0, message="操作完成")
def api_problemset_join_private_problemset(id: int, code: str): problemset: ProblemSet = db.session.query( ProblemSet.invitation_code).filter(ProblemSet.id == id).one_or_none() if not problemset: return make_response(-1, message="ID不存在") if code != problemset.invitation_code: return make_response(-1, message="邀请码错误") if not session.get("uid"): return make_response(-1, message="请先登录") permission_manager.add_permission(session.get("uid"), f"problemset.use.{id}") return make_response(0, message="ok")
def api_contest_unlock(contestID: int, inviteCode: str): """ 使用邀请码申请某比赛的权限 """ contest: Contest = db.session.query(Contest.private_contest, Contest.invite_code, Contest.id).filter_by( id=contestID).one_or_none() if not contest: return make_response(-1, message="此比赛不存在") if inviteCode != contest.invite_code: return make_response(-1, message="邀请码错误") if not session.get("uid", None): return make_response(-1, message="请登录") permission_manager.add_permission( session.get("uid"), f"contest.use.{contest.id}") return make_response(0, message="操作完成")
def api_problem_unlock(problemID: int, inviteCode: str): """ 使用邀请码申请某题目的权限 """ problem: Problem = db.session.query( Problem.public, Problem.invite_code, Problem.id).filter_by(id=problemID).one_or_none() if not problem: return make_response(-1, message="此题目不存在") if inviteCode != problem.invite_code: return make_response(-1, message="邀请码错误") if not session.get("uid", None): return make_response(-1, message="请登录") permission_manager.add_permission(session.get("uid"), f"problem.use.{problem.id}") return make_response(0, message="操作完成")
def api_finish_challenge(challengeID: int): """ 申请完成一个挑战 challengeID 挑战ID """ if not permission_manager.has_permission( session.get("uid"), f"challenge.access.{challengeID}"): return make_response(-1, message="你没有权限访问该挑战") challenge: Challenge = db.session.query(Challenge.problemset_list).filter( Challenge.id == challengeID).one_or_none() if not challenge: return make_response(-1, message="该挑战不存在") for problemset in challenge.problemset_list: if not permission_manager.has_permission( session.get("uid"), f"challenge.finish.{challengeID}.{problemset}"): return make_response(-1, message="该挑战之下存在未完成的习题集") permission_manager.add_permission(session.get("uid"), f"challenge.finish.{challengeID}.all") return make_response(0, message="操作完成")
def api_unlock_challenge(id: int): """ 尝试解锁一个挑战 id:挑战ID """ current_challenge: Challenge = db.session.query(Challenge).filter( Challenge.id == id).one_or_none() if not current_challenge: return make_response(-1, message="挑战不存在") lower_challenges: typing.Iterable[Challenge] = db.session.query( Challenge.id).filter(Challenge.level < current_challenge.level).all() for item in lower_challenges: if not permission_manager.has_permission( session.get("uid"), f"challenge.finish.{item.id}.all"): print(f"{item.id=} not finished yet") return make_response(-1, message=f"你还有至少一个等级低于当前挑战的挑战尚未完成") permission_manager.add_permission( session.get("uid"), f"challenge.access.{current_challenge.id}") for problemset in current_challenge.problemset_list: permission_manager.add_permission(session.get("uid"), f"problemset.use.{problemset}") return make_response(0, message="操作完成")
def create_contest(): """ 参数: { "" } { "code":0, "message":"", "contest_id":1 """ import datetime contest = Contest(owner_id=session.get("uid"), start_time=datetime.datetime.now(), end_time=datetime.datetime.now()+datetime.timedelta(hours=3) ) db.session.add(contest) db.session.commit() permission_manager.add_permission( session.get("uid"), f"contest.use.{contest.id}") return make_response(0, contest_id=contest.id)
def join_team(): """ 参数 { "uid":"用户ID", "team_id":"团队ID" "invite_code":"邀请码" } 返回: { "code":0, "message":"" } """ if not session.get("uid"): return make_response(-1, message="请先登录") owner: User = User.by_id(session.get("uid")) if int(owner.id) != int(request.form["uid"]): return make_response(-1, message="此操作只能由用户本人进行") team: Team = Team.by_id(request.form["team_id"]) if db.session.query(TeamMember).filter_by( uid=session.get("uid"), team_id=request.form["team_id"]).count(): return make_response(-1, message="您已经在本团队内") if team.private and not permission_manager.has_permission( session.get("uid"), f"team.use.{team.id}"): if team.invite_code != request.form.get("invite_code", ""): return make_response(-1, message="邀请码错误") else: permission_manager.add_permission(session.get("uid"), f"team.use.{team.id}") # team.members = [*team.members, owner.id] # owner.joined_teams = [*owner.joined_teams, team.id] db.session.add( TeamMember(uid=session.get("uid"), team_id=team.id, is_admin=False)) db.session.commit() return make_response(0, message="加入成功")