Skip to content

Commit

Permalink
fix: tweak formula
Browse files Browse the repository at this point in the history
misc: format
  • Loading branch information
WizzyGeek committed Jan 12, 2024
1 parent b7f3cb5 commit 3d38f9d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Problem(BaseProblem):
null=True
) # type: ignore[assignment]

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

hints: fields.ReverseRelation[Hint]

Expand All @@ -44,8 +44,9 @@ async def _solves(self) -> int:
return await SolvedProblem.filter(problem=self).count()

async def update_points(self) -> None:
# by 125 solves 76% value is gone
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()) / 125))
)
await self.save()

Expand Down
16 changes: 12 additions & 4 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ def _invalid_order():
logger.warn("= Invalid penalty lookup by order occured =")
return 0


# 0 - 10 - 10
# 1 - 7 - 17
# 2 - 8 - 25
HINTPENALTY = defaultdict(_invalid_order, {0: 10, 1: 7, 2: 8})


class Flag(BaseModel):
flag: str

Expand All @@ -65,10 +67,11 @@ async def update_points(req: Request, ctf_id: int):
logger.exception("An error occured while updating points")



@atomic()
@router.post("/{ctf_id}/flag")
async def flag_post(req: Request, ctf_id: int, flag: Flag, response: Response, jwt: RequireJwt):
async def flag_post(
req: Request, ctf_id: int, flag: Flag, response: Response, jwt: RequireJwt
):
team_id = jwt["team_id"]
problem = await Problem.get_or_none(id=ctf_id)
if not problem:
Expand All @@ -84,7 +87,9 @@ async def flag_post(req: Request, ctf_id: int, flag: Flag, response: Response, j
team_id=team_id, flag=flag.flag, problem_id=ctf_id
)
if check_solved:
hints = await Hint.filter(problem_id=ctf_id, viewedhints__team_id=team_id, with_points=True)
hints = await Hint.filter(
problem_id=ctf_id, viewedhints__team_id=team_id, with_points=True
)
pnlt = (100 - sum(map(lambda h: HINTPENALTY[h.order], hints))) / 100

await SolvedProblem.create(team_id=team_id, problem_id=ctf_id, penalty=pnlt)
Expand Down Expand Up @@ -127,7 +132,10 @@ async def hint_get(ctf_id: int, response: Response, jwt: RequireJwt):
await team.save()

await ViewedHint.create(hint_id=hint.id, team_id=team_id, with_points=with_points)
return {"text": hint.text, "order": hint.order, }
return {
"text": hint.text,
"order": hint.order,
}


@router.get("/{ctf_id}/viewed_hints")
Expand Down
18 changes: 15 additions & 3 deletions src/pwncore/routes/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

router = APIRouter(prefix="/leaderboard", tags=["leaderboard"])


class ExpiringLBCache:
period: float
last_update: float
Expand All @@ -25,21 +26,32 @@ def __init__(self, period: float) -> None:

async def _do_update(self):
self.data = dict(
await Team.all() # type: ignore[call-overload, arg-type]
await Team.all()
.filter(solved_problem__problem__id__gt=-1)
.annotate(tpoints = Sum(RawSQL('"solvedproblem"."penalty" * "solvedproblem__problem"."points"'))) # type: ignore[pylance]
.annotate(
tpoints=Sum(
RawSQL(
'"solvedproblem"."penalty" * "solvedproblem__problem"."points"'
)
)
)
.values_list("name", "tpoints")
)
self.last_update = monotonic()

async def get_lb(self, req: Request):
if getattr(req.app.state, "force_expire", False) or (monotonic() - self.last_update) > self.period:
if (
getattr(req.app.state, "force_expire", False)
or (monotonic() - self.last_update) > self.period # noqa: W503
):
await self._do_update()
req.app.state.force_expire = False
return self.data


gcache = ExpiringLBCache(30.0)


@router.get("")
async def fetch_leaderboard(req: Request):
return await gcache.get_lb(req)

0 comments on commit 3d38f9d

Please sign in to comment.