Skip to content

Commit

Permalink
Merge pull request #29 from mradigen/master
Browse files Browse the repository at this point in the history
feat: /api/team/me
  • Loading branch information
Meetesh-Saini authored Jan 28, 2024
2 parents c5da0d9 + f174533 commit f9f4afb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pwncore.routes.ctf.start import router as start_router
from pwncore.routes.ctf.pre_event import router as pre_event_router
from pwncore.routes.auth import RequireJwt
from pwncore.container import docker_client

# Metadata at the top for instant accessibility
metadata = {
Expand Down Expand Up @@ -92,17 +93,30 @@ async def flag_post(
response.status_code = 401
return {"msg_code": config.msg_codes["ctf_solved"]}

check_solved = await Container.exists(
team_id=team_id, flag=flag.flag, problem_id=ctf_id
)
if check_solved:
team_container = await Container.get_or_none(team_id=team_id, problem_id=ctf_id)
if not team_container:
return {"msg_code": config.msg_codes["container_not_found"]}

if team_container.flag == flag.flag:
hints = await Hint.filter(
problem_id=ctf_id,
viewedhints__team_id=team_id,
viewedhints__with_points=True,
)
pnlt = (100 - sum(map(lambda h: HINTPENALTY[h.order], hints))) / 100

# Stop container after submitting
try:
await Container.filter(team_id=team_id, problem_id=ctf_id).delete()
except Exception:
response.status_code = 500
return {"msg_code": config.msg_codes["db_error"]}

container = await docker_client.containers.get(team_container.docker_id)
await container.stop()
await container.delete()
#

await SolvedProblem.create(team_id=team_id, problem_id=ctf_id, penalty=pnlt)
create_task(update_points(req, ctf_id))
return {"status": True}
Expand Down
3 changes: 2 additions & 1 deletion src/pwncore/routes/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from pwncore.models import Team

# Metadata at the top for instant accessibility
metadata = {"name": "leaderboard", "description": "Operations on the leaderboard"}
metadata = {"name": "leaderboard",
"description": "Operations on the leaderboard"}

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

Expand Down
23 changes: 23 additions & 0 deletions src/pwncore/routes/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pwncore.config import config
from pwncore.models import Team, User, Team_Pydantic, User_Pydantic, Container
from pwncore.routes.auth import RequireJwt
# from pwncore.routes.leaderboard import gcache

# Metadata at the top for instant accessibility
metadata = {"name": "team", "description": "Operations with teams"}
Expand Down Expand Up @@ -40,6 +41,28 @@ async def team_members(jwt: RequireJwt):
return members


@router.get("/me")
async def get_self_team(jwt: RequireJwt):
team_id = jwt["team_id"]

team_model = await Team.get(id=team_id).prefetch_related("members")
team = dict(await Team_Pydantic.from_tortoise_orm(team_model))

# Get members
team["members"] = [
await User_Pydantic.from_tortoise_orm(member) for member in team_model.members
]

# Get points from leaderboard
# would be better is cache stores the values in a dict indexed by team id
# for leaderboard_team in gcache.data:
# if leaderboard_team["name"] == team["name"]:
# team["tpoints"] = leaderboard_team["tpoints"]
# break

return team


@atomic()
@router.post("/add")
async def add_member(user: UserAddBody, response: Response, jwt: RequireJwt):
Expand Down

0 comments on commit f9f4afb

Please sign in to comment.