Skip to content

Commit cd76b0f

Browse files
committed
ref(user): remove UserGameLocation
1 parent 5d3c8f3 commit cd76b0f

File tree

14 files changed

+107
-177
lines changed

14 files changed

+107
-177
lines changed

src/ttt/application/game/game/cancel_game.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from ttt.application.game.game.ports.game_views import GameViews
88
from ttt.application.game.game.ports.games import Games
99
from ttt.entities.core.game.game import AlreadyCompletedGameError
10-
from ttt.entities.core.user.user import User
11-
from ttt.entities.tools.assertion import not_none
1210
from ttt.entities.tools.tracking import Tracking
1311

1412

@@ -23,18 +21,12 @@ class CancelGame:
2321

2422
async def __call__(self, user_id: int) -> None:
2523
async with self.transaction:
26-
game = await self.games.game_with_game_location(user_id)
24+
game = await self.games.current_user_game(user_id)
2725

2826
if game is None:
2927
await self.game_views.no_game_view(user_id)
3028
return
3129

32-
locations = tuple(
33-
not_none(user.game_location)
34-
for user in (game.player1, game.player2)
35-
if isinstance(user, User)
36-
)
37-
3830
try:
3931
tracking = Tracking()
4032
game.cancel(user_id, tracking)
@@ -49,7 +41,4 @@ async def __call__(self, user_id: int) -> None:
4941
await self.log.game_cancelled(user_id, game)
5042

5143
await self.map_(tracking)
52-
await self.game_views.game_view_with_locations(
53-
locations,
54-
game,
55-
)
44+
await self.game_views.game_view(game)

src/ttt/application/game/game/make_move_in_game.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
NoCellError,
1818
NotCurrentPlayerError,
1919
)
20-
from ttt.entities.core.user.user import User
21-
from ttt.entities.tools.assertion import not_none
2220
from ttt.entities.tools.tracking import Tracking
2321

2422

@@ -41,17 +39,12 @@ async def __call__(
4139
cell_number_int: int,
4240
) -> None:
4341
async with self.transaction:
44-
game = await self.games.game_with_game_location(user_id)
42+
game = await self.games.current_user_game(user_id)
4543

4644
if game is None:
4745
await self.game_views.no_game_view(user_id)
4846
return
4947

50-
locations = tuple(
51-
not_none(user.game_location)
52-
for user in (game.player1, game.player2)
53-
if isinstance(user, User)
54-
)
5548
(
5649
random,
5750
games_played_by_player_id,
@@ -110,10 +103,7 @@ async def __call__(
110103
await self.log.user_move_maked(user_id, game, user_move)
111104

112105
if user_move.next_move_ai_id is not None:
113-
await self.game_views.game_view_with_locations(
114-
locations,
115-
game,
116-
)
106+
await self.game_views.game_view(game)
117107

118108
(
119109
free_cell_random,
@@ -142,7 +132,4 @@ async def __call__(
142132
await self.log.game_completed(user_id, game)
143133

144134
await self.map_(tracking)
145-
await self.game_views.game_view_with_locations(
146-
locations,
147-
game,
148-
)
135+
await self.game_views.game_view(game)

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
from abc import ABC, abstractmethod
2-
from collections.abc import Sequence
32

43
from ttt.entities.core.game.game import Game
5-
from ttt.entities.core.user.location import UserGameLocation
64

75

86
class GameViews(ABC):
97
@abstractmethod
108
async def current_game_view_with_user_id(self, user_id: int, /) -> None: ...
119

1210
@abstractmethod
13-
async def game_view_with_locations(
14-
self,
15-
user_locations: Sequence[UserGameLocation],
16-
game: Game,
17-
/,
18-
) -> None: ...
11+
async def game_view(self, game: Game, /) -> None: ...
1912

2013
@abstractmethod
21-
async def started_game_view_with_locations(
14+
async def started_game_view(
2215
self,
23-
user_locations: Sequence[UserGameLocation],
2416
game: Game,
2517
/,
2618
) -> None: ...

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@ class NoGameError(Exception): ...
88

99
class Games(ABC):
1010
@abstractmethod
11-
async def game_with_game_location(
12-
self,
13-
game_location_user_id: int,
14-
/,
15-
) -> Game | None: ...
11+
async def current_user_game(self, user_id: int, /) -> Game | None: ...

src/ttt/application/game/game/start_game_with_ai.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from ttt.application.user.common.ports.users import Users
1515
from ttt.entities.core.game.ai import AiType
1616
from ttt.entities.core.game.game import start_game_with_ai
17-
from ttt.entities.core.user.location import UserGameLocation
1817
from ttt.entities.core.user.user import UserAlreadyInGameError
1918
from ttt.entities.tools.tracking import Tracking
2019

@@ -71,15 +70,9 @@ async def __call__(self, user_id: int, ai_type: AiType) -> None:
7170

7271
if started_game.next_move_ai_id is None:
7372
await self.map_(tracking)
74-
await self.game_views.started_game_view_with_locations(
75-
[UserGameLocation(user_id, started_game.game.id)],
76-
started_game.game,
77-
)
73+
await self.game_views.started_game_view(started_game.game)
7874
else:
79-
await self.game_views.started_game_view_with_locations(
80-
[UserGameLocation(user_id, started_game.game.id)],
81-
started_game.game,
82-
)
75+
await self.game_views.started_game_view(started_game.game)
8376

8477
(
8578
free_cell_random,
@@ -104,7 +97,4 @@ async def __call__(self, user_id: int, ai_type: AiType) -> None:
10497
)
10598

10699
await self.map_(tracking)
107-
await self.game_views.game_view_with_locations(
108-
[UserGameLocation(user_id, started_game.game.id)],
109-
started_game.game,
110-
)
100+
await self.game_views.game_view(started_game.game)

src/ttt/application/matchmaking_queue/game/wait_game.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,4 @@ async def __call__(self, user_id: int) -> None:
9797
await self.game_log.game_against_user_started(game)
9898
await self.map_(tracking)
9999

100-
await self.game_views.started_game_view_with_locations(
101-
game.locations(),
102-
game,
103-
)
100+
await self.game_views.started_game_view(game)

src/ttt/entities/core/game/game.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
PlayerLoss,
2929
PlayerWin,
3030
)
31-
from ttt.entities.core.user.location import UserGameLocation
3231
from ttt.entities.core.user.user import (
3332
User,
3433
UserAlreadyInGameError,
@@ -111,7 +110,7 @@ class Game:
111110

112111
def __post_init__(self) -> None:
113112
assert_(
114-
not all(isinstance(player, Ai) for player in self._players()),
113+
not all(isinstance(player, Ai) for player in self.players()),
115114
else_=OnlyAiGameError,
116115
)
117116

@@ -141,7 +140,7 @@ def is_against_user(self) -> bool:
141140
return not self.is_against_ai()
142141

143142
def user(self, user_id: int) -> User | None:
144-
for user in self._users():
143+
for user in self.users():
145144
if user.id == user_id:
146145
return user
147146

@@ -195,10 +194,10 @@ def make_user_move( # noqa: C901
195194
if not isinstance(current_player, User):
196195
raise TypeError
197196

198-
not_current_player = not_none(self._not_current_player())
197+
not_current_player = not_none(self.not_current_player())
199198

200199
assert_(
201-
user_id in {user.id for user in self._users()},
200+
user_id in {user.id for user in self.users()},
202201
else_=NotPlayerError(),
203202
)
204203
assert_(current_player.id == user_id, else_=NotCurrentPlayerError())
@@ -296,7 +295,7 @@ def make_ai_move(
296295
if not isinstance(current_player, Ai):
297296
raise NotAiCurrentMoveError
298297

299-
not_current_player = self._not_current_player()
298+
not_current_player = self.not_current_player()
300299
if not isinstance(not_current_player, User):
301300
raise TypeError
302301

@@ -384,9 +383,6 @@ def is_player_move_expected(self, player_id: int | UUID) -> bool:
384383
case _:
385384
raise ValueError(self.state, player_id)
386385

387-
def locations(self) -> tuple[UserGameLocation, ...]:
388-
return tuple(not_none(user.game_location) for user in self._users())
389-
390386
def _make_random_ai_move(
391387
self,
392388
current_player: Ai,
@@ -459,15 +455,15 @@ def _current_player(self) -> Player | None:
459455
case GameState.completed:
460456
return None
461457

462-
def _players(self) -> tuple[Player, ...]:
458+
def players(self) -> tuple[Player, ...]:
463459
return self.player1, self.player2
464460

465-
def _users(self) -> tuple[User, ...]:
461+
def users(self) -> tuple[User, ...]:
466462
return tuple(
467-
player for player in self._players() if isinstance(player, User)
463+
player for player in self.players() if isinstance(player, User)
468464
)
469465

470-
def _not_current_player(self) -> Player | None:
466+
def not_current_player(self) -> Player | None:
471467
match self.state:
472468
case GameState.wait_player1:
473469
return self.player2

src/ttt/entities/core/user/location.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/ttt/entities/core/user/user.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
)
1313
from ttt.entities.core.user.draw import UserDraw
1414
from ttt.entities.core.user.emoji import UserEmoji
15-
from ttt.entities.core.user.location import UserGameLocation
1615
from ttt.entities.core.user.loss import UserLoss
1716
from ttt.entities.core.user.rank import Rank, rank_for_rating
1817
from ttt.entities.core.user.win import UserWin
@@ -83,7 +82,7 @@ class User:
8382
selected_emoji_id: UUID | None
8483
rating: EloRating
8584
admin_right: AdminRight | None
86-
game_location: UserGameLocation | None
85+
current_game_id: UUID | None
8786

8887
emoji_cost: ClassVar[Stars] = 1000
8988

@@ -221,7 +220,7 @@ def set_user_account(
221220
return user
222221

223222
def is_in_game(self) -> bool:
224-
return self.game_location is not None
223+
return self.current_game_id is not None
225224

226225
def be_in_game(
227226
self,
@@ -234,7 +233,7 @@ def be_in_game(
234233

235234
assert_(not self.is_in_game(), else_=UserAlreadyInGameError(self))
236235

237-
self.game_location = UserGameLocation(self.id, game_id)
236+
self.current_game_id = game_id
238237
tracking.register_mutated(self)
239238

240239
def lose_to_user(
@@ -346,7 +345,7 @@ def leave_game(self, tracking: Tracking) -> None:
346345

347346
assert_(self.is_in_game(), else_=UserNotInGameError(self))
348347

349-
self.game_location = None
348+
self.current_game_id = None
350349
tracking.register_mutated(self)
351350

352351
def buy_emoji(
@@ -428,16 +427,16 @@ def register_user(user_id: int, tracking: Tracking) -> User:
428427
emojis=[],
429428
selected_emoji_id=None,
430429
rating=initial_elo_rating,
431-
game_location=None,
430+
current_game_id=None,
432431
admin_right=None,
433432
)
434433
tracking.register_new(user)
435434

436435
return user
437436

438437

439-
def is_user_in_game(game_location: UserGameLocation | None) -> bool:
440-
return game_location is not None
438+
def is_user_in_game(current_game_id: UUID | None) -> bool:
439+
return current_game_id is not None
441440

442441

443442
def is_user_admin(admin_right: AdminRight | None) -> bool:

src/ttt/infrastructure/adapters/games.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@
1313
class InPostgresGames(Games):
1414
_session: AsyncSession
1515

16-
async def game_with_game_location(
17-
self,
18-
game_location_user_id: int,
19-
/,
20-
) -> Game | None:
16+
async def current_user_game(self, user_id: int, /) -> Game | None:
2117
lock_stmt = (
2218
select(TableGame.id)
23-
.where(TableUser.game_location_game_id == TableGame.id)
19+
.where(TableUser.current_game_id == TableGame.id)
2420
.with_for_update()
2521
)
2622
await self._session.execute(lock_stmt)
2723

2824
join_condition = (
29-
(TableUser.id == game_location_user_id)
30-
& (TableUser.game_location_game_id == TableGame.id)
25+
(TableUser.id == user_id)
26+
& (TableUser.current_game_id == TableGame.id)
3127
)
3228
stmt = select(TableGame).join(TableUser, join_condition)
3329
table_game = await self._session.scalar(stmt)

0 commit comments

Comments
 (0)