Skip to content

Commit

Permalink
feat: dynamic points
Browse files Browse the repository at this point in the history
  • Loading branch information
WizzyGeek committed Jan 10, 2024
1 parent bdd2c4a commit ca54896
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ class Problem(BaseProblem):
null=True
) # type: ignore[assignment]

mi = fields.IntField(default=100) # Arbitrary meaning full defaults
mi = fields.IntField(default=100) # Arbitrary meaning full defaults
ma = fields.IntField(default=600)

hints: fields.ReverseRelation[Hint]

class PydanticMeta:
exclude = ["image_name", "image_config"]

async def solves(self) -> int:
async def _solves(self) -> int:
return await SolvedProblem.filter(problem=self).count()

async def _update_points(self) -> None:
async def update_points(self) -> None:
self.points = round(
self.mi + (self.ma - self.mi) * (1 - tanh(await self.solves()))
self.mi + (self.ma - self.mi) * (1 - tanh(await self._solves()))
)
await self.save()

Expand Down Expand Up @@ -76,7 +76,9 @@ class Meta:


class ViewedHint(Model):
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField("models.Team", related_name="viewedhints")
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField(
"models.Team", related_name="viewedhints"
)
hint: fields.ForeignKeyRelation[Hint] = fields.ForeignKeyField(
"models.Hint",
)
Expand Down
14 changes: 14 additions & 0 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from asyncio import create_task
from logging import getLogger

from fastapi import APIRouter, Response
from pydantic import BaseModel
from tortoise.transactions import atomic
Expand Down Expand Up @@ -30,6 +33,8 @@
router.include_router(start_router)
router.include_router(pre_event_router)

logger = getLogger("routes.ctf")


class Flag(BaseModel):
flag: str
Expand All @@ -41,6 +46,14 @@ async def ctf_list():
return problems


async def update_points(ctf_id: int):
try:
p = await Problem.get(id=ctf_id)
await p.update_points()
except Exception:
logger.exception("An error occured while updating points")


@atomic()
@router.post("/{ctf_id}/flag")
async def flag_post(ctf_id: int, flag: Flag, response: Response, jwt: RequireJwt):
Expand All @@ -60,6 +73,7 @@ async def flag_post(ctf_id: int, flag: Flag, response: Response, jwt: RequireJwt
)
if check_solved:
await SolvedProblem.create(team_id=team_id, problem_id=ctf_id)
create_task(update_points(ctf_id))
return {"status": True}
return {"status": False}

Expand Down

0 comments on commit ca54896

Please sign in to comment.