Skip to content

Commit 5fee602

Browse files
committed
feat: fix issues (#10, #11)
1 parent 640bcac commit 5fee602

File tree

9 files changed

+86
-93
lines changed

9 files changed

+86
-93
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ ignore = [
104104
]
105105

106106
[tool.ruff.lint.per-file-ignores]
107+
"src/ttt/infrastructure/adapters/*" = ["ARG002"]
108+
"src/ttt/presentation/adapters/*" = ["ARG002"]
107109
"src/ttt/presentation/aiogram/*" = ["RUF001"]
108110
"tests/*" = ["S101", "PT013", "PLR2004", "D400", "D415"]
109111
"tests/test_tgdb/test_entities/test_horizon.py" = ["D400", "D415"]

src/ttt/application/common/ports/players.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ async def contains_player_with_id(
1818
) -> bool:
1919
...
2020

21-
@abstractmethod
22-
async def player_with_id(self, id_: int, /) -> Player:
23-
"""
24-
:raises ttt.application.common.ports.players.NoPlayerWithIDError:
25-
"""
26-
2721
@abstractmethod
2822
@overload
2923
async def players_with_ids(

src/ttt/application/game/make_move_in_game.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from ttt.application.common.ports.uuids import UUIDs
99
from ttt.application.game.ports.game_views import GameViews
1010
from ttt.application.game.ports.games import Games
11+
from ttt.entities.core.game.cell import AlreadyFilledCellError
12+
from ttt.entities.core.game.game import (
13+
AlreadyCompletedGameError,
14+
NoCellError,
15+
NotCurrentPlayerError,
16+
)
1117
from ttt.entities.core.player.location import PlayerLocation
1218
from ttt.entities.math.vector import Vector
1319
from ttt.entities.tools.assertion import not_none
@@ -29,22 +35,13 @@ async def __call__(
2935
location: PlayerLocation,
3036
cell_position: Vector,
3137
) -> None:
32-
"""
33-
:raises ttt.application.common.ports.players.NoPlayerWithIDError:
34-
:raises ttt.application.game.ports.games.NoGameError:
35-
:raises ttt.entities.core.game.game.AlreadyCompletedGameError:
36-
:raises ttt.entities.core.game.game.NotCurrentPlayerError:
37-
:raises ttt.entities.core.game.game.NoCellError:
38-
:raises ttt.entities.core.game.cell.AlreadyFilledCellError:
39-
"""
40-
4138
async with self.transaction:
42-
player = await self.players.player_with_id(location.player_id)
43-
game = await self.games.game_with_id(
44-
None
45-
if player.game_location is None
46-
else player.game_location.game_id,
47-
)
39+
game = await self.games.game_with_game_location(location.player_id)
40+
41+
if game is None:
42+
await self.game_views.render_no_game_view(location)
43+
return
44+
4845
locations = tuple(
4946
not_none(player.game_location)
5047
for player in (game.player1, game.player2)
@@ -54,12 +51,31 @@ async def __call__(
5451
self.randoms.random(),
5552
)
5653

57-
tracking = Tracking()
58-
game.make_move(
59-
player.id, cell_position, game_result_id, random, tracking,
60-
)
61-
62-
await self.map_(tracking)
63-
await self.game_views.render_game_view_with_locations(
64-
locations, game,
65-
)
54+
try:
55+
tracking = Tracking()
56+
game.make_move(
57+
location.player_id,
58+
cell_position,
59+
game_result_id,
60+
random,
61+
tracking,
62+
)
63+
except AlreadyCompletedGameError:
64+
await self.game_views.render_game_already_complteted_view(
65+
location, game,
66+
)
67+
except NotCurrentPlayerError:
68+
await self.game_views.render_not_current_player_view(
69+
location, game,
70+
)
71+
except NoCellError:
72+
await self.game_views.render_no_cell_view(location, game)
73+
except AlreadyFilledCellError:
74+
await self.game_views.render_already_filled_cell_error(
75+
location, game,
76+
)
77+
else:
78+
await self.map_(tracking)
79+
await self.game_views.render_game_view_with_locations(
80+
locations, game,
81+
)

src/ttt/application/game/ports/game_views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ async def render_no_game_view(
3131
async def render_game_already_complteted_view(
3232
self, player_location: PlayerLocation, game: Game, /,
3333
) -> None: ...
34+
35+
@abstractmethod
36+
async def render_not_current_player_view(
37+
self, player_location: PlayerLocation, game: Game, /,
38+
) -> None: ...
39+
40+
@abstractmethod
41+
async def render_no_cell_view(
42+
self, player_location: PlayerLocation, game: Game, /,
43+
) -> None: ...
44+
45+
@abstractmethod
46+
async def render_already_filled_cell_error(
47+
self, player_location: PlayerLocation, game: Game, /,
48+
) -> None: ...

src/ttt/application/game/ports/games.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import ABC, abstractmethod
2-
from uuid import UUID
32

43
from ttt.entities.core.game.game import Game
54

@@ -8,12 +7,6 @@ class NoGameError(Exception): ...
87

98

109
class Games(ABC):
11-
@abstractmethod
12-
async def game_with_id(self, id_: UUID | None, /) -> Game:
13-
"""
14-
:raises ttt.application.game.ports.games.NoGameError:
15-
"""
16-
1710
@abstractmethod
1811
async def game_with_game_location(
1912
self, game_location_player_id: int, /,

src/ttt/infrastructure/adapters/games.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from dataclasses import dataclass
2-
from uuid import UUID
32

43
from sqlalchemy import select
54
from sqlalchemy.ext.asyncio import AsyncSession
65

7-
from ttt.application.game.ports.games import Games, NoGameError
6+
from ttt.application.game.ports.games import Games
87
from ttt.entities.core.game.game import Game
98
from ttt.infrastructure.sqlalchemy.tables import TableGame, TablePlayer
109

@@ -13,14 +12,6 @@
1312
class InPostgresGames(Games):
1413
_session: AsyncSession
1514

16-
async def game_with_id(self, id_: UUID | None, /) -> Game:
17-
table_game = await self._session.get(TableGame, id_)
18-
19-
if table_game is None:
20-
raise NoGameError
21-
22-
return table_game.entity()
23-
2415
async def game_with_game_location(
2516
self, game_location_player_id: int, /,
2617
) -> Game | None:

src/ttt/infrastructure/adapters/players.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ async def contains_player_with_id(
2222

2323
return bool(await self._session.execute(stmt))
2424

25-
async def player_with_id(self, id_: int, /) -> Player:
26-
table_player = await self._session.get(TablePlayer, id_)
27-
28-
if table_player is None:
29-
raise NoPlayerWithIDError(id_)
30-
31-
return table_player.entity()
32-
3325
@overload
3426
async def players_with_ids(
3527
self, ids: Sequence[int], /,

src/ttt/presentation/adapters/game_views.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
from ttt.infrastructure.background_tasks import BackgroundTasks
1111
from ttt.presentation.aiogram.game.messages import (
1212
already_completed_game_message,
13+
already_filled_cell_message,
1314
completed_game_messages,
1415
maked_move_message,
16+
no_cell_message,
1517
no_game_message,
18+
not_current_player_message,
1619
started_game_message,
1720
)
1821

@@ -60,8 +63,29 @@ async def render_no_game_view(
6063
no_game_message(self._bot, player_location.chat_id),
6164
)
6265

66+
async def render_not_current_player_view(
67+
self, player_location: PlayerLocation, game: Game, /,
68+
) -> None:
69+
self._tasks.create_task(
70+
not_current_player_message(self._bot, player_location.chat_id),
71+
)
72+
73+
async def render_no_cell_view(
74+
self, player_location: PlayerLocation, game: Game, /,
75+
) -> None:
76+
self._tasks.create_task(
77+
no_cell_message(self._bot, player_location.chat_id),
78+
)
79+
80+
async def render_already_filled_cell_error(
81+
self, player_location: PlayerLocation, game: Game, /,
82+
) -> None:
83+
self._tasks.create_task(
84+
already_filled_cell_message(self._bot, player_location.chat_id),
85+
)
86+
6387
async def render_game_already_complteted_view(
64-
self, player_location: PlayerLocation, game: Game, /, # noqa: ARG002
88+
self, player_location: PlayerLocation, game: Game, /,
6589
) -> None:
6690
self._tasks.create_task(
6791
already_completed_game_message(self._bot, player_location.chat_id),
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1-
21
from aiogram import F, Router
32
from aiogram.types import Message
43
from dishka.integrations.aiogram import FromDishka, inject
54

6-
from ttt.application.common.ports.players import NoPlayerWithIDError
75
from ttt.application.game.make_move_in_game import MakeMoveInGame
8-
from ttt.application.game.ports.games import NoGameError
9-
from ttt.entities.core.game.cell import AlreadyFilledCellError
10-
from ttt.entities.core.game.game import (
11-
AlreadyCompletedGameError,
12-
NoCellError,
13-
NotCurrentPlayerError,
14-
)
156
from ttt.entities.core.player.location import PlayerLocation
167
from ttt.entities.tools.assertion import not_none
178
from ttt.presentation.aiogram.common.messages import (
189
anons_are_rohibited_message,
19-
need_to_start_message,
20-
)
21-
from ttt.presentation.aiogram.game.messages import (
22-
already_completed_game_message,
23-
already_filled_cell_message,
24-
no_cell_message,
25-
no_game_message,
26-
not_current_player_message,
2710
)
2811

2912

@@ -44,23 +27,6 @@ async def _(
4427
x -= 1
4528
y -= 1
4629

47-
try:
48-
await make_move_in_game(
49-
PlayerLocation(message.from_user.id, message.chat.id), (x, y),
50-
)
51-
except NoPlayerWithIDError:
52-
await need_to_start_message(message)
53-
except NoGameError:
54-
await no_game_message(not_none(message.bot), message.chat.id)
55-
except AlreadyCompletedGameError:
56-
await already_completed_game_message(
57-
not_none(message.bot), message.chat.id,
58-
)
59-
except NotCurrentPlayerError:
60-
await not_current_player_message(not_none(message.bot), message.chat.id)
61-
except NoCellError:
62-
await no_cell_message(not_none(message.bot), message.chat.id)
63-
except AlreadyFilledCellError:
64-
await already_filled_cell_message(
65-
not_none(message.bot), message.chat.id,
66-
)
30+
await make_move_in_game(
31+
PlayerLocation(message.from_user.id, message.chat.id), (x, y),
32+
)

0 commit comments

Comments
 (0)