Skip to content

Commit

Permalink
feat: /api/admin/union for calculating team coins
Browse files Browse the repository at this point in the history
  • Loading branch information
mradigen committed Jan 8, 2024
1 parent e6015ea commit c97276d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/pwncore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Config:
config = Config(
development=True,
db_url="sqlite://:memory:",
docker_url=None, # None for default system docker
# docker_url=None, # None for default system docker
# Or set it to an arbitrary URL for testing without Docker
# docker_url="http://google.com",
docker_url="http://google.com",
flag="C0D",
max_containers_per_team=3,
jwt_secret="mysecret",
Expand Down
8 changes: 5 additions & 3 deletions src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Problem(Model):

flag = fields.TextField(null=True) # Static flag CTFs

image_name = fields.TextField()
image_name = fields.TextField(null=True)
image_config: fields.Field[dict[str, list]] = fields.JSONField(
null=True
) # type: ignore[assignment]
Expand All @@ -51,7 +51,8 @@ class Meta:


class SolvedProblem(Model):
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField("models.Team")
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField(
"models.Team")
problem: fields.ForeignKeyRelation[Problem] = fields.ForeignKeyField(
"models.Problem"
)
Expand All @@ -62,7 +63,8 @@ class Meta:


class ViewedHint(Model):
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField("models.Team")
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField(
"models.Team")
hint: fields.ForeignKeyRelation[Hint] = fields.ForeignKeyField(
"models.Hint",
)
Expand Down
42 changes: 41 additions & 1 deletion src/pwncore/routes/admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import logging
from fastapi import APIRouter
from passlib.hash import bcrypt

from pwncore.models import Team, Problem, Hint, User
from pwncore.models import Team, Problem, Hint, User, PreEventSolvedProblem
from pwncore.config import config

metadata = {
"name": "admin",
"description": "Admin routes (currently only running when development on)",
}

# TODO: Make this protected
router = APIRouter(prefix="/admin", tags=["admin"])

if config.development:
logging.basicConfig(level=logging.INFO)


@router.get("/union")
async def calculate_team_coins(): # Inefficient, anyways will be used only once
logging.info("Calculating team points form pre-event CTFs:")
team_ids = await Team.filter().values_list("id", flat=True)
for team_id in team_ids:
member_tags = await User.filter(team_id=team_id).values_list('tag', flat=True)

if not member_tags:
return 0

problems_solved = set(await PreEventSolvedProblem.filter(
tag__in=member_tags
).values_list('problem_id', flat=True))

team = await Team.get(id=team_id)
for ctf_id in problems_solved:
team.coins += (await Problem.get(id=ctf_id)).coins
logging.info(f"{team.id}) {team.name}: {team.coins}")
await team.save()


@router.get("/create")
async def init_db():
Expand All @@ -18,6 +45,7 @@ async def init_db():
description="Chod de tujhe se na ho paye",
author="Meetesh Saini",
points=300,
coins=20,
image_name="key:latest",
image_config={"PortBindings": {"22/tcp": [{}]}},
)
Expand Down Expand Up @@ -49,6 +77,18 @@ async def init_db():
await Team.create(
name="Triple A battery", secret_hash=bcrypt.hash("chotiwali"), coins=20
)
await PreEventSolvedProblem.create(
tag="23BCE1000",
problem_id="1"
)
await PreEventSolvedProblem.create(
tag="23BRS1000",
problem_id="2"
)
# await PreEventSolvedProblem.create(
# tag="23BAI1000",
# problem_id="2"
# )
await User.create(
tag="23BRS1000",
name="abc",
Expand Down
2 changes: 0 additions & 2 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ async def pre_event_flag_post(ctf_id: int, post_body: PreEventFlag, response: Re
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}

print(problem)

if await PreEventSolvedProblem.exists(tag=post_body.tag, problem_id=ctf_id):
response.status_code = 401
return {"msg_code": config.msg_codes["ctf_solved"]}
Expand Down

0 comments on commit c97276d

Please sign in to comment.