Skip to content

Commit db55176

Browse files
committed
ref(entities): merge matchmaking_queue and user (#58)
1 parent 160397c commit db55176

File tree

45 files changed

+853
-458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+853
-458
lines changed

deploy/dev/docker-compose.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ services:
3535
TTT_NATS_URL: nats://nats:4222
3636

3737
TTT_GEMINI_URL: https://my-openai-gemini-sigma-sandy.vercel.app
38+
39+
TTT_MATCHMAKING_MAX_WORKERS: 4
40+
TTT_MATCHMAKING_WORKER_MAX_USERS: 100
41+
TTT_MATCHMAKING_WORKER_CREATION_INTERVAL_SECONDS: 0.5
3842
secrets:
3943
- secrets
4044
command: ttt-dev

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ ignore = [
111111
"EM101",
112112
"N807",
113113
"FURB118",
114+
"DOC402",
114115
]
115116

116117
[tool.ruff.lint.per-file-ignores]

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66

77

88
class GameLog(ABC):
9-
@abstractmethod
10-
async def game_against_user_started(
11-
self,
12-
game: Game,
13-
/,
14-
) -> None: ...
15-
169
@abstractmethod
1710
async def game_against_ai_started(
1811
self,

src/ttt/application/matchmaking/common/matchmaking_log.py

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

src/ttt/application/matchmaking/common/matchmaking_views.py

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

src/ttt/application/matchmaking/common/shared_matchmaking.py

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

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

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

src/ttt/application/user/common/ports/users.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ async def users_with_ids(
3838
ids: Sequence[int],
3939
/,
4040
) -> tuple[User | None, ...]: ...
41+
42+
@abstractmethod
43+
async def some_users_waiting_for_matchmaking_to_matchmake(
44+
self,
45+
) -> list[User]: ...
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from asyncio import gather
2+
from dataclasses import dataclass
3+
4+
from ttt.application.common.ports.emojis import Emojis
5+
from ttt.application.common.ports.map import Map
6+
from ttt.application.common.ports.transaction import Transaction
7+
from ttt.application.common.ports.uuids import UUIDs
8+
from ttt.application.user.common.ports.users import Users
9+
from ttt.application.user.game.ports.user_log import GameUserLog
10+
from ttt.application.user.game.ports.user_views import GameUserViews
11+
from ttt.entities.core.game.game import Game
12+
from ttt.entities.core.user.matchmaking import MatchmakingInput, matchmaking
13+
from ttt.entities.tools.tracking import Tracking
14+
15+
16+
@dataclass(frozen=True, unsafe_hash=False)
17+
class Matchmake:
18+
map_: Map
19+
transaction: Transaction
20+
users: Users
21+
log: GameUserLog
22+
views: GameUserViews
23+
uuids: UUIDs
24+
emojis: Emojis
25+
26+
async def __call__(self) -> None:
27+
async with self.transaction:
28+
tracking = Tracking()
29+
games = await self._result(tracking)
30+
31+
await self.log.games_were_matched(games)
32+
await gather(
33+
self.views.matched_games_view(games),
34+
self.map_(tracking),
35+
)
36+
37+
async def _result(self, tracking: Tracking) -> list[Game]:
38+
users, input_ = await gather(
39+
self.users.some_users_waiting_for_matchmaking_to_matchmake(),
40+
self._matchmaking_input(),
41+
)
42+
43+
games = list[Game]()
44+
matchmaking_ = matchmaking(users, input_, tracking)
45+
46+
try:
47+
while True:
48+
games.append(matchmaking_.send(await self._matchmaking_input()))
49+
except StopIteration:
50+
return games
51+
52+
async def _matchmaking_input(self) -> MatchmakingInput:
53+
(
54+
cell_id_matrix,
55+
game_id,
56+
player1_random_emoji,
57+
player2_random_emoji,
58+
) = await gather(
59+
self.uuids.random_uuid_matrix((3, 3)),
60+
self.uuids.random_uuid(),
61+
self.emojis.random_emoji(),
62+
self.emojis.random_emoji(),
63+
)
64+
return MatchmakingInput(
65+
cell_id_matrix,
66+
game_id,
67+
player1_random_emoji,
68+
player2_random_emoji,
69+
)

0 commit comments

Comments
 (0)