Skip to content

Commit 137da13

Browse files
committed
fix: implement new io (#62)
1 parent a1d4ea8 commit 137da13

34 files changed

+588
-215
lines changed

deploy/dev/nats/streams/game.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "GAME",
3+
"subjects": ["game.>"],
4+
"retention": "limits",
5+
"max_consumers": -1,
6+
"max_msgs": -1,
7+
"max_bytes": -1,
8+
"max_age": 31536000000000000,
9+
"max_msg_size": -1,
10+
"storage": "file",
11+
"discard": "old",
12+
"num_replicas": 1,
13+
"duplicate_window": 0
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "STARS_PURCHASE",
3+
"subjects": ["stars_purchase.>"],
4+
"retention": "limits",
5+
"max_consumers": -1,
6+
"max_msgs": -1,
7+
"max_bytes": -1,
8+
"max_age": 31536000000000000,
9+
"max_msg_size": -1,
10+
"storage": "file",
11+
"discard": "old",
12+
"num_replicas": 1,
13+
"duplicate_window": 0
14+
}

deploy/prod/nats/streams/game.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "GAME",
3+
"subjects": ["game.>"],
4+
"retention": "limits",
5+
"max_consumers": -1,
6+
"max_msgs": -1,
7+
"max_bytes": -1,
8+
"max_age": 31536000000000000,
9+
"max_msg_size": -1,
10+
"storage": "file",
11+
"discard": "old",
12+
"num_replicas": 1,
13+
"duplicate_window": 0
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "STARS_PURCHASE",
3+
"subjects": ["stars_purchase.>"],
4+
"retention": "limits",
5+
"max_consumers": -1,
6+
"max_msgs": -1,
7+
"max_bytes": -1,
8+
"max_age": 31536000000000000,
9+
"max_msg_size": -1,
10+
"storage": "file",
11+
"discard": "old",
12+
"num_replicas": 1,
13+
"duplicate_window": 0
14+
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"openai==1.97.0",
2626
"structlog==25.4.0",
2727
"structlog-sentry==2.2.1",
28+
"taskiq==0.11.18",
2829
]
2930

3031
[dependency-groups]

src/ttt/application/game/game/make_ai_move_in_game.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
from ttt.application.common.ports.map import Map
66
from ttt.application.common.ports.randoms import Randoms
7-
from ttt.application.common.ports.transaction import SerializableTransaction
7+
from ttt.application.common.ports.transaction import (
8+
NotSerializableTransaction,
9+
)
810
from ttt.application.common.ports.uuids import UUIDs
911
from ttt.application.game.game.ports.game_ai_gateway import GameAiGateway
1012
from ttt.application.game.game.ports.game_dao import GameDao
1113
from ttt.application.game.game.ports.game_log import GameLog
1214
from ttt.application.game.game.ports.game_views import GameViews
1315
from ttt.application.game.game.ports.games import Games
16+
from ttt.application.user.common.ports.user_locks import UserLocks
1417
from ttt.application.user.common.ports.users import Users
1518
from ttt.entities.core.game.game import (
1619
AlreadyCompletedGameError,
@@ -28,17 +31,15 @@ class MakeAiMoveInGame:
2831
uuids: UUIDs
2932
randoms: Randoms
3033
ai_gateway: GameAiGateway
31-
transaction: SerializableTransaction
34+
transaction: NotSerializableTransaction
3235
log: GameLog
3336
dao: GameDao
37+
locks: UserLocks
3438

35-
async def __call__(self, game_id: UUID, ai_id: UUID) -> None:
36-
"""
37-
:raises ttt.application.common.errors.serialization_error.SerializationError:
38-
""" # noqa: E501
39-
39+
async def __call__(self, user_id: int, game_id: UUID, ai_id: UUID) -> None:
4040
async with self.transaction:
41-
game = await self.games.game_with_id(game_id)
41+
await self.locks.lock_user_by_id(user_id)
42+
game = await self.games.not_locked_game_with_id(game_id)
4243

4344
if game is None:
4445
await self.log.no_game_to_make_ai_move(game_id)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ttt.application.game.game.ports.game_tasks import GameTasks
1212
from ttt.application.game.game.ports.game_views import GameViews
1313
from ttt.application.game.game.ports.games import Games
14+
from ttt.application.user.common.ports.user_locks import UserLocks
1415
from ttt.application.user.common.ports.users import Users
1516
from ttt.entities.core.game.cell import AlreadyFilledCellError
1617
from ttt.entities.core.game.game import (
@@ -34,6 +35,7 @@ class MakeMoveInGame:
3435
log: GameLog
3536
dao: GameDao
3637
tasks: GameTasks
38+
locks: UserLocks
3739

3840
async def __call__(
3941
self,
@@ -118,8 +120,9 @@ async def __call__(
118120
await self.map_(tracking)
119121

120122
if user_move.next_move_ai_id is not None:
123+
await self.locks.lock_user_by_id(user_id)
121124
await self.tasks.make_ai_move(
122-
game.id, user_move.next_move_ai_id,
125+
user_id, game.id, user_move.next_move_ai_id,
123126
)
124127

125128
await self.transaction.commit()

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ async def no_game_to_make_ai_move(
2828
/,
2929
) -> None: ...
3030

31-
@abstractmethod
32-
async def no_current_game(
33-
self,
34-
user_id: int,
35-
/,
36-
) -> None: ...
37-
3831
@abstractmethod
3932
async def game_against_ai_started(
4033
self,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class GameTasks(ABC):
66
@abstractmethod
77
async def make_ai_move(
88
self,
9+
user_id: int,
910
game_id: UUID,
1011
ai_id: UUID,
1112
/,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Games(ABC):
1212
async def current_user_game(self, user_id: int, /) -> Game | None: ...
1313

1414
@abstractmethod
15-
async def game_with_id(self, game_id: UUID, /) -> Game | None: ...
15+
async def not_locked_game_with_id(self, game_id: UUID, /) -> Game | None:
16+
...

0 commit comments

Comments
 (0)