Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dynamic Points Calculation #13

Merged
merged 6 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

src/pwncore/utils.py
mradigen marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 21 additions & 2 deletions src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from math import tanh

from tortoise.models import Model
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
Expand Down Expand Up @@ -30,14 +32,29 @@ class Problem(BaseProblem):
null=True
) # type: ignore[assignment]

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:
return await SolvedProblem.filter(problem=self).count()

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


class Hint(Model):
id = fields.IntField(pk=True)
order = fields.SmallIntField() # 0, 1, 2
problem: fields.ForeignKeyRelation[Problem] = fields.ForeignKeyField(
"models.Problem"
"models.Problem", related_name="hints"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno why i did this, but it is there now

)
text = fields.TextField()

Expand All @@ -59,7 +76,9 @@ class Meta:


class ViewedHint(Model):
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField("models.Team")
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField(
"models.Team", related_name="viewedhints"
)
hint: fields.ForeignKeyRelation[Hint] = fields.ForeignKeyField(
"models.Hint",
)
Expand Down
4 changes: 4 additions & 0 deletions src/pwncore/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ async def init_db():
points=300,
image_name="key:latest",
image_config={"PortBindings": {"22/tcp": [{}]}},
mi=200,
ma=400,
)
await PreEventProblem.create(
name="Static_test",
Expand All @@ -81,6 +83,8 @@ async def init_db():
points=300,
image_name="key:latest",
image_config={"PortBindings": {"22/tcp": [{}]}},
mi=200,
ma=400,
)
await Problem.create(
name="GitGood",
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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Background update

return {"status": True}
return {"status": False}

Expand Down