33
44from ttt .application .common .ports .map import Map
55from ttt .application .common .ports .randoms import Randoms
6- from ttt .application .common .ports .transaction import Transaction
6+ from ttt .application .common .ports .transaction import SerializableTransaction
77from ttt .application .common .ports .uuids import UUIDs
88from ttt .application .game .game .ports .game_ai_gateway import GameAiGateway
99from ttt .application .game .game .ports .game_dao import GameDao
1010from ttt .application .game .game .ports .game_log import GameLog
11+ from ttt .application .game .game .ports .game_tasks import GameTasks
1112from ttt .application .game .game .ports .game_views import GameViews
1213from ttt .application .game .game .ports .games import Games
1314from ttt .application .user .common .ports .users import Users
@@ -29,20 +30,25 @@ class MakeMoveInGame:
2930 uuids : UUIDs
3031 randoms : Randoms
3132 ai_gateway : GameAiGateway
32- transaction : Transaction
33+ transaction : SerializableTransaction
3334 log : GameLog
3435 dao : GameDao
36+ tasks : GameTasks
3537
3638 async def __call__ (
3739 self ,
3840 user_id : int ,
3941 cell_number_int : int ,
4042 ) -> None :
43+ """
44+ :raises ttt.application.common.errors.serialization_error.SerializationError:
45+ """ # noqa: E501
46+
4147 async with self .transaction :
4248 game = await self .games .current_user_game (user_id )
4349
4450 if game is None :
45- await self .game_views .no_game_view (user_id )
51+ await self .game_views .no_current_game_view (user_id )
4652 return
4753
4854 (
@@ -68,6 +74,7 @@ async def __call__(
6874 user_id ,
6975 cell_number_int ,
7076 )
77+ await self .transaction .commit ()
7178 await self .game_views .game_already_complteted_view (
7279 user_id ,
7380 game ,
@@ -78,6 +85,7 @@ async def __call__(
7885 user_id ,
7986 cell_number_int ,
8087 )
88+ await self .transaction .commit ()
8189 await self .game_views .not_current_user_view (
8290 user_id ,
8391 game ,
@@ -88,48 +96,31 @@ async def __call__(
8896 user_id ,
8997 cell_number_int ,
9098 )
99+ await self .transaction .commit ()
91100 await self .game_views .no_cell_view (user_id , game )
92101 except AlreadyFilledCellError :
93102 await self .log .already_filled_cell_to_make_move (
94103 game ,
95104 user_id ,
96105 cell_number_int ,
97106 )
107+ await self .transaction .commit ()
98108 await self .game_views .already_filled_cell_error (
99109 user_id ,
100110 game ,
101111 )
102112 else :
103113 await self .log .user_move_maked (user_id , game , user_move )
104114
105- if user_move . next_move_ai_id is not None :
106- await self .game_views . game_view ( game )
115+ if game . is_completed () :
116+ await self .log . game_was_completed_by_user ( user_id , game )
107117
108- (
109- free_cell_random ,
110- ai_move_cell_number_int ,
111- ) = await gather (
112- self .randoms .random (),
113- self .ai_gateway .next_move_cell_number_int (
114- game ,
115- user_move .next_move_ai_id ,
116- ),
117- )
118- ai_move = game .make_ai_move (
119- user_move .next_move_ai_id ,
120- ai_move_cell_number_int ,
121- free_cell_random ,
122- tracking ,
123- )
118+ await self .map_ (tracking )
124119
125- await self .log .ai_move_maked (
126- user_id ,
127- game ,
128- ai_move ,
120+ if user_move .next_move_ai_id is not None :
121+ await self .tasks .make_ai_move (
122+ game .id , user_move .next_move_ai_id ,
129123 )
130124
131- if game .is_completed ():
132- await self .log .game_completed (user_id , game )
133-
134- await self .map_ (tracking )
125+ await self .transaction .commit ()
135126 await self .game_views .game_view (game )
0 commit comments