Skip to content

Commit 9c213f8

Browse files
committed
feat(user): remove LastGames (#51)
1 parent 19a16aa commit 9c213f8

File tree

19 files changed

+344
-283
lines changed

19 files changed

+344
-283
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@ class CancelGame:
2424

2525
async def __call__(self, user_id: int) -> None:
2626
async with self.transaction:
27-
(
28-
game,
29-
user1_last_game_id,
30-
user2_last_game_id,
31-
) = await gather(
32-
self.games.game_with_game_location(user_id),
33-
self.uuids.random_uuid(),
34-
self.uuids.random_uuid(),
35-
)
27+
game = await self.games.game_with_game_location(user_id)
28+
3629
if game is None:
3730
await self.game_views.no_game_view(user_id)
3831
return
@@ -45,12 +38,7 @@ async def __call__(self, user_id: int) -> None:
4538

4639
try:
4740
tracking = Tracking()
48-
game.cancel(
49-
user_id,
50-
user1_last_game_id,
51-
user2_last_game_id,
52-
tracking,
53-
)
41+
game.cancel(user_id, tracking)
5442
except AlreadyCompletedGameError:
5543
await self.log.already_completed_game_to_cancel(game, user_id)
5644
await self.game_views.game_already_complteted_view(

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ttt.application.common.ports.transaction import Transaction
77
from ttt.application.common.ports.uuids import UUIDs
88
from ttt.application.game.game.ports.game_ai_gateway import GameAiGateway
9+
from ttt.application.game.game.ports.game_dao import GameDao
910
from ttt.application.game.game.ports.game_log import GameLog
1011
from ttt.application.game.game.ports.game_views import GameViews
1112
from ttt.application.game.game.ports.games import Games
@@ -32,6 +33,7 @@ class MakeMoveInGame:
3233
ai_gateway: GameAiGateway
3334
transaction: Transaction
3435
log: GameLog
36+
dao: GameDao
3537

3638
async def __call__(
3739
self,
@@ -52,21 +54,18 @@ async def __call__(
5254
)
5355
(
5456
random,
55-
current_user_last_game_id,
56-
not_current_user_last_game_id,
57+
games_played_by_player_id,
5758
) = await gather(
5859
self.randoms.random(),
59-
self.uuids.random_uuid(),
60-
self.uuids.random_uuid(),
60+
self.dao.games_played_by_player_id(game),
6161
)
6262

6363
try:
6464
tracking = Tracking()
6565
user_move = game.make_user_move(
6666
user_id,
6767
cell_number_int,
68-
current_user_last_game_id,
69-
not_current_user_last_game_id,
68+
games_played_by_player_id,
7069
random,
7170
tracking,
7271
)
@@ -119,19 +118,16 @@ async def __call__(
119118
(
120119
free_cell_random,
121120
ai_move_cell_number_int,
122-
not_current_user_last_game_id,
123121
) = await gather(
124122
self.randoms.random(),
125123
self.ai_gateway.next_move_cell_number_int(
126124
game,
127125
user_move.next_move_ai_id,
128126
),
129-
self.uuids.random_uuid(),
130127
)
131128
ai_move = game.make_ai_move(
132129
user_move.next_move_ai_id,
133130
ai_move_cell_number_int,
134-
not_current_user_last_game_id,
135131
free_cell_random,
136132
tracking,
137133
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from abc import ABC, abstractmethod
2+
3+
from ttt.entities.core.game.game import Game
4+
from ttt.entities.elo.rating import GamesPlayed
5+
6+
7+
class GameDao(ABC):
8+
@abstractmethod
9+
async def games_played_by_player_id(
10+
self,
11+
game: Game,
12+
/,
13+
) -> dict[int, GamesPlayed]: ...

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,16 @@ async def __call__(self, user_id: int, ai_type: AiType) -> None:
8484
(
8585
free_cell_random,
8686
ai_move_cell_number_int,
87-
not_current_user_last_game_id,
8887
) = await gather(
8988
self.randoms.random(),
9089
self.ai_gateway.next_move_cell_number_int(
9190
started_game.game,
9291
started_game.next_move_ai_id,
9392
),
94-
self.uuids.random_uuid(),
9593
)
9694
ai_move = started_game.game.make_ai_move(
9795
started_game.next_move_ai_id,
9896
ai_move_cell_number_int,
99-
not_current_user_last_game_id,
10097
free_cell_random,
10198
tracking,
10299
)

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

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
User,
3434
UserAlreadyInGameError,
3535
)
36+
from ttt.entities.elo.rating import GamesPlayed
3637
from ttt.entities.math.matrix import Matrix
3738
from ttt.entities.math.random import Random, choice
3839
from ttt.entities.math.vector import Vector
@@ -152,34 +153,30 @@ def is_completed(self) -> bool:
152153
def cancel(
153154
self,
154155
user_id: int,
155-
user1_last_game_id: UUID,
156-
user2_last_game_id: UUID,
157156
tracking: Tracking,
158157
) -> None:
159158
"""
160159
:raises ttt.entities.core.game.game.AlreadyCompletedGameError:
161160
:raises ttt.entities.core.game.game.NotPlayerError:
162-
:raises ttt.entities.core.user.user.UserAlreadyLeftGameError:
163161
"""
164162

165163
none(self.result, else_=AlreadyCompletedGameError)
166164
canceler = not_none(self.user(user_id), else_=NotPlayerError)
167165

168166
if isinstance(self.player1, User):
169-
self.player1.leave_game(user1_last_game_id, self.id, tracking)
167+
self.player1.leave_game(tracking)
170168
if isinstance(self.player2, User):
171-
self.player2.leave_game(user2_last_game_id, self.id, tracking)
169+
self.player2.leave_game(tracking)
172170

173171
self.result = CancelledGameResult(canceler.id)
174172
self.state = GameState.completed
175173
tracking.register_mutated(self)
176174

177-
def make_user_move( # noqa: C901, PLR0913, PLR0917
175+
def make_user_move( # noqa: C901
178176
self,
179177
user_id: int,
180178
cell_number_int: int,
181-
current_user_last_game_id: UUID,
182-
not_current_user_last_game_id: UUID,
179+
games_played_by_player_id: dict[int, GamesPlayed],
183180
player_win_random: Random,
184181
tracking: Tracking,
185182
) -> UserMove:
@@ -230,21 +227,17 @@ def make_user_move( # noqa: C901, PLR0913, PLR0917
230227
case User():
231228
win = current_player.win_against_user(
232229
not_current_player.rating,
230+
games_played_by_player_id[current_player.id],
233231
player_win_random,
234-
current_user_last_game_id,
235-
self.id,
236232
tracking,
237233
)
238234
loss = not_current_player.lose_to_user(
239235
current_player.rating,
240-
not_current_user_last_game_id,
241-
self.id,
236+
games_played_by_player_id[not_current_player.id],
242237
tracking,
243238
)
244239
case Ai():
245-
win = current_player.win_against_ai(
246-
current_user_last_game_id, self.id, tracking,
247-
)
240+
win = current_player.win_against_ai(tracking)
248241
loss = not_current_player.lose()
249242

250243
self._complete_as_decided(win, loss, tracking)
@@ -257,20 +250,16 @@ def make_user_move( # noqa: C901, PLR0913, PLR0917
257250
case User():
258251
draw1 = current_player.be_draw_against_user(
259252
not_current_player.rating,
260-
current_user_last_game_id,
261-
self.id,
253+
games_played_by_player_id[current_player.id],
262254
tracking,
263255
)
264256
draw2 = not_current_player.be_draw_against_user(
265257
not_current_player.rating,
266-
not_current_user_last_game_id,
267-
self.id,
258+
games_played_by_player_id[not_current_player.id],
268259
tracking,
269260
)
270261
case Ai():
271-
draw1 = current_player.be_draw_against_ai(
272-
current_user_last_game_id, self.id, tracking,
273-
)
262+
draw1 = current_player.be_draw_against_ai(tracking)
274263
draw2 = not_current_player.be_draw()
275264

276265
self._complete_as_draw(draw1, draw2, tracking)
@@ -292,7 +281,6 @@ def make_ai_move(
292281
self,
293282
ai_id: UUID,
294283
cell_number_int: int | None,
295-
not_current_user_last_game_id: UUID,
296284
free_cell_random: Random,
297285
tracking: Tracking,
298286
) -> AiMove:
@@ -316,7 +304,6 @@ def make_ai_move(
316304
return self._make_random_ai_move(
317305
current_player,
318306
not_current_player,
319-
not_current_user_last_game_id,
320307
free_cell_random,
321308
tracking,
322309
)
@@ -327,7 +314,6 @@ def make_ai_move(
327314
return self._make_random_ai_move(
328315
current_player,
329316
not_current_player,
330-
not_current_user_last_game_id,
331317
free_cell_random,
332318
tracking,
333319
)
@@ -340,7 +326,6 @@ def make_ai_move(
340326
return self._make_random_ai_move(
341327
current_player,
342328
not_current_player,
343-
not_current_user_last_game_id,
344329
free_cell_random,
345330
tracking,
346331
)
@@ -351,7 +336,6 @@ def make_ai_move(
351336
return self._make_random_ai_move(
352337
current_player,
353338
not_current_player,
354-
not_current_user_last_game_id,
355339
free_cell_random,
356340
tracking,
357341
)
@@ -361,16 +345,12 @@ def make_ai_move(
361345

362346
if self._is_player_winner(current_player, cell_position):
363347
win = current_player.win()
364-
loss = not_current_player.lose_to_ai(
365-
not_current_user_last_game_id, self.id, tracking,
366-
)
348+
loss = not_current_player.lose_to_ai(tracking)
367349
self._complete_as_decided(win, loss, tracking)
368350

369351
elif not self._can_continue():
370352
draw1 = current_player.be_draw()
371-
draw2 = not_current_player.be_draw_against_ai(
372-
not_current_user_last_game_id, self.id, tracking,
373-
)
353+
draw2 = not_current_player.be_draw_against_ai(tracking)
374354
self._complete_as_draw(draw1, draw2, tracking)
375355

376356
else:
@@ -411,7 +391,6 @@ def _make_random_ai_move(
411391
self,
412392
current_player: Ai,
413393
not_current_player: User,
414-
not_current_user_last_game_id: UUID,
415394
free_cell_random: Random,
416395
tracking: Tracking,
417396
) -> AiMove:
@@ -422,16 +401,12 @@ def _make_random_ai_move(
422401

423402
if self._is_player_winner(current_player, cell.board_position):
424403
win = current_player.win()
425-
loss = not_current_player.lose_to_ai(
426-
not_current_user_last_game_id, self.id, tracking,
427-
)
404+
loss = not_current_player.lose_to_ai(tracking)
428405
self._complete_as_decided(win, loss, tracking)
429406

430407
elif not self._can_continue():
431408
draw1 = current_player.be_draw()
432-
draw2 = not_current_player.be_draw_against_ai(
433-
not_current_user_last_game_id, self.id, tracking,
434-
)
409+
draw2 = not_current_player.be_draw_against_ai(tracking)
435410
self._complete_as_draw(draw1, draw2, tracking)
436411

437412
else:

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

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

0 commit comments

Comments
 (0)