Skip to content

Commit

Permalink
added response models for KeyTime and GameStat
Browse files Browse the repository at this point in the history
  • Loading branch information
ychebyshev committed Apr 18, 2024
1 parent 7267227 commit 93fece0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
37 changes: 37 additions & 0 deletions shvatka/api/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,43 @@ def from_core(cls, core: dto.KeyTime | None):
)


@dataclass
class LevelTime:
id: int
team: Team
level_number: int
start_at: datetime
is_finished: bool

@classmethod
def from_core(cls, core: dto.LevelTimeOnGame | None):
if core is None:
return None
return cls(
id=core.id,
team=Team.from_core(core.team),
level_number=core.level_number,
start_at=core.start_at,
is_finished=core.is_finished,
)


@dataclass
class GameStat:
level_times: dict[int, list[LevelTime]]

@classmethod
def from_core(cls, core: dto.GameStat | None):
if core is None:
return None
return cls(
level_times={
team.id: [LevelTime.from_core(lt) for lt in lts]
for team, lts in core.level_times.items()
},
)


@dataclass
class CurrentHintResponse:
hints: list[scn.TimeHint]
Expand Down
10 changes: 6 additions & 4 deletions shvatka/api/routes/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ async def get_game_keys(
interactor: FromDishka[GameKeysReaderInteractor],
user: FromDishka[dto.User],
id_: Annotated[int, Path(alias="id")],
):
return await interactor(user=user, game_id=id_)
) -> dict[int, list[responses.KeyTime | None]]:
keys = await interactor(user=user, game_id=id_)
return {k: [responses.KeyTime.from_core(key_time) for key_time in v] for k, v in keys.items()}


@inject
async def get_game_stat(
interactor: FromDishka[GameStatReaderInteractor],
user: FromDishka[dto.User],
id_: Annotated[int, Path(alias="id")],
):
return await interactor(user=user, game_id=id_)
) -> responses.GameStat:
stat = await interactor(user=user, game_id=id_)
return responses.GameStat.from_core(stat)


@inject
Expand Down

0 comments on commit 93fece0

Please sign in to comment.