Skip to content

Commit

Permalink
feat: auto update problem points
Browse files Browse the repository at this point in the history
  • Loading branch information
WizzyGeek committed Jan 8, 2024
1 parent f6474df commit 0aba798
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

from math import tanh
from typing import Any, Optional, Tuple, Type
from tortoise.backends.base.client import BaseDBAsyncClient

from tortoise.models import Model
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
Expand All @@ -25,17 +29,26 @@ class Problem(Model):
image_name = fields.TextField()
image_config: fields.Field[dict[str, list]] = fields.JSONField() # type: ignore[assignment]

mi = fields.IntField()
ma = fields.IntField()

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 "
)
text = fields.TextField()

Expand Down

0 comments on commit 0aba798

Please sign in to comment.