55
66from sqlalchemy .exc import IntegrityError
77
8- from api .db .models import User
8+ from api .db .enums import AgentType , BotKind
9+ from api .db .models import BotProfile , User
910from api .modules .identity .repository import UserRepository
1011from api .modules .identity .schemas import (
1112 BotProfileResponse ,
13+ BotProfileUpsertRequest ,
1214 PublicPlayerResponse ,
1315 UserCreateRequest ,
1416)
@@ -68,12 +70,54 @@ async def list_playable_bots(
6870 agent_type = profile .agent_type .value ,
6971 heuristic_level = profile .heuristic_level ,
7072 model_mode = profile .model_mode ,
73+ model_version_id = user .model_version_id ,
7174 enabled = profile .enabled ,
7275 )
7376 for user , profile in rows
7477 ]
7578 return total , items
7679
80+ async def upsert_bot_profile (self , payload : BotProfileUpsertRequest ) -> BotProfileResponse :
81+ user = await self .user_repository .get_by_id (payload .user_id )
82+ if user is None :
83+ raise LookupError (f"User not found: { payload .user_id } " )
84+
85+ profile = await self .user_repository .get_bot_profile (payload .user_id )
86+ if profile is None :
87+ profile = BotProfile (user_id = payload .user_id , agent_type = AgentType .HEURISTIC )
88+
89+ # Promote the account into bot mode so matchmaking/listing sees it.
90+ user .is_bot = True
91+ user .is_hidden_bot = False
92+
93+ if payload .agent_type == "heuristic" :
94+ profile .agent_type = AgentType .HEURISTIC
95+ profile .heuristic_level = payload .heuristic_level or "normal"
96+ profile .model_mode = None
97+ user .bot_kind = BotKind .HEURISTIC
98+ if payload .model_version_id is not None :
99+ user .model_version_id = payload .model_version_id
100+ else :
101+ profile .agent_type = AgentType .MODEL
102+ profile .model_mode = payload .model_mode or "fast"
103+ profile .heuristic_level = None
104+ user .bot_kind = BotKind .MODEL
105+ user .model_version_id = payload .model_version_id
106+
107+ profile .enabled = payload .enabled
108+ await self .user_repository .save_user (user )
109+ profile = await self .user_repository .save_bot_profile (profile )
110+ return BotProfileResponse (
111+ user_id = user .id ,
112+ username = user .username ,
113+ bot_kind = user .bot_kind ,
114+ agent_type = profile .agent_type .value ,
115+ heuristic_level = profile .heuristic_level ,
116+ model_mode = profile .model_mode ,
117+ model_version_id = user .model_version_id ,
118+ enabled = profile .enabled ,
119+ )
120+
77121 async def list_public_players (
78122 self , * , limit : int = 50 , offset : int = 0 , query : str | None = None
79123 ) -> tuple [int , list [PublicPlayerResponse ]]:
@@ -95,6 +139,7 @@ async def list_public_players(
95139 agent_type = profile .agent_type .value if profile else None ,
96140 heuristic_level = profile .heuristic_level if profile else None ,
97141 model_mode = profile .model_mode if profile else None ,
142+ model_version_id = user .model_version_id ,
98143 enabled = profile .enabled if profile else None ,
99144 )
100145 for user , profile in rows
0 commit comments