Skip to content

Commit f7e1baf

Browse files
committed
feat: add AttackDefTeam model and routes
1 parent ab7b890 commit f7e1baf

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

src/pwncore/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"insufficient_coins": 22,
4141
"user_or_email_exists": 23,
4242
"users_not_found": 24,
43+
"attack_def_team_not_found": 25
4344
}
4445

4546

src/pwncore/models/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
)
2727
from pwncore.models.round2 import (
2828
AttackDefProblem,
29+
AttackDefTeam
2930
)
31+
3032
from pwncore.models.pre_event import (
3133
PreEventProblem,
3234
PreEventSolvedProblem,
@@ -55,6 +57,7 @@
5557
"Problem_Pydantic",
5658
"BaseProblem",
5759
"AttackDefProblem",
60+
"AttackDefTeam",
5861
)
5962

6063

src/pwncore/models/round2.py

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tortoise.models import Model
44
__all__ = (
55
"AttackDefProblem",
6+
"AttackDefTeam",
67
"Problem",
78
)
89
#TODO: Actually implement this.
@@ -14,3 +15,9 @@ class AttackDefProblem(Model):
1415
attack_def_team: fields.ForeignKeyRelation[AttackDefTeam] = fields.ForeignKeyField(
1516
"models.AttackDefTeam", related_name="assigned_attack_def_problem"
1617
)
18+
19+
class AttackDefTeam(Model):
20+
team: fields.OneToOneRelation[Team] = fields.OneToOneField(
21+
"models.Team", null=False
22+
)
23+
assigned_attack_def_problem: fields.ReverseRelation(AttackDefProblem)

src/pwncore/routes/admin.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,16 @@ async def init_db(
148148
image_name="reg.lugvitc.net/key:latest",
149149
# image_config={"PortBindings": {"22/tcp": [{}]}},
150150
)
151+
await Problem.create(
152+
name="GitGood2",
153+
description="How to master the art of solving CTFs? Git good nub.",
154+
author="Aadivishnu and Shoubhit",
155+
points=300,
156+
image_name="reg.lugvitc.net/key:latest",
157+
# image_config={"PortBindings": {"22/tcp": [{}]}},
158+
)
151159
await Team.create(name="CID Squad", secret_hash=bcrypt.hash("veryverysecret"))
152-
await Team.create(
160+
triple_a_battery = await Team.create(
153161
name="Triple A battery", secret_hash=bcrypt.hash("chotiwali"), coins=20
154162
)
155163
triple_b_battery = await Team.create(

src/pwncore/routes/ctf/__init__.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
SolvedProblem,
1919
Team,
2020
ViewedHint,
21+
AttackDefProblem,
22+
AttackDefTeam
2123
)
2224
from pwncore.models.ctf import Problem_Pydantic
2325
from pwncore.routes.auth import RequireJwt
@@ -62,11 +64,7 @@ async def completed_problem_get(jwt: RequireJwt):
6264
)
6365
return problems
6466

65-
66-
@router.get("/list")
67-
async def ctf_list(jwt: RequireJwt):
68-
team_id = jwt["team_id"]
69-
problems = await Problem_Pydantic.from_queryset(Problem.filter(visible=True))
67+
async def calculate_problem_points(problems: [Problem_Pydantic], team_id: int) -> [Problem_Pydantic]:
7068
acc: dict[int, float] = defaultdict(lambda: 1.0)
7169
for k, v in map(
7270
lambda x: (x.hint.problem_id, HINTPENALTY[x.hint.order]), # type: ignore[attr-defined]
@@ -79,6 +77,20 @@ async def ctf_list(jwt: RequireJwt):
7977
i.points = int(acc[i.id] * i.points) # type: ignore[attr-defined]
8078
return problems
8179

80+
@router.get("/list")
81+
async def ctf_list(jwt: RequireJwt):
82+
team_id = jwt["team_id"]
83+
problems = await Problem_Pydantic.from_queryset(Problem.filter(visible=True))
84+
return await calculate_problem_points(problems, team_id)
85+
86+
@router.get("/round2/list")
87+
async def ctf_list(jwt: RequireJwt):
88+
team_id = jwt["team_id"]
89+
attack_def_team = await AttackDefTeam.get(team_id=team_id)
90+
if (attack_def_team is None):
91+
return {"msg_code": config.msg_codes["attack_def_team_not_found"]}
92+
problems = await Problem_Pydantic.from_queryset(Problem.filter(attackdefproblem__attack_def_team__id=attack_def_team.id))
93+
return await calculate_problem_points(problems, team_id)
8294

8395
async def update_points(req: Request, ctf_id: int):
8496
try:
@@ -88,6 +100,9 @@ async def update_points(req: Request, ctf_id: int):
88100
except Exception:
89101
logger.exception("An error occured while updating points")
90102

103+
@router.get("/round2/list_all")
104+
async def ctf_list():
105+
return await Problem_Pydantic.from_queryset(Problem.all())
91106

92107
@atomic()
93108
@router.post("/{ctf_id}/flag")

0 commit comments

Comments
 (0)