@@ -154,7 +154,9 @@ export class MatchAssistantService {
154154 return matches_by_pk . server ;
155155 }
156156
157- public async isDedicatedServerAvailable ( matchId : string ) : Promise < boolean > {
157+ public async isDedicatedServerAvailable (
158+ matchId : string ,
159+ ) : Promise < string | undefined > {
158160 const server = await this . getMatchServer ( matchId ) ;
159161
160162 if ( ! server ) {
@@ -189,7 +191,9 @@ export class MatchAssistantService {
189191 throw Error ( "unable to find server" ) ;
190192 }
191193
192- return servers_by_pk . matches_aggregate . aggregate ?. count === 0 ;
194+ return (
195+ servers_by_pk . matches_aggregate . aggregate ?. count === 0 && servers_by_pk . id
196+ ) ;
193197 }
194198
195199 public async updateMatchStatus ( matchId : string , status : e_match_status_enum ) {
@@ -208,7 +212,7 @@ export class MatchAssistantService {
208212 } ) ;
209213 }
210214
211- public async assignServer ( matchId : string , tries = 0 ) : Promise < boolean > {
215+ public async assignServer ( matchId : string , tries = 0 ) : Promise < void > {
212216 const { matches_by_pk : match } = await this . hasura . query ( {
213217 matches_by_pk : {
214218 __args : {
@@ -229,14 +233,16 @@ export class MatchAssistantService {
229233 ) ;
230234
231235 if ( assignedDedicated ) {
232- return true ;
236+ await this . startMatch ( matchId ) ;
237+ return ;
233238 }
234239 }
235240
236241 try {
237242 const isAssignedOnDemand = await this . assignOnDemandServer ( matchId ) ;
238243 if ( isAssignedOnDemand ) {
239- return true ;
244+ await this . startMatch ( matchId ) ;
245+ return ;
240246 }
241247 } catch ( error ) {
242248 this . logger . error (
@@ -248,7 +254,7 @@ export class MatchAssistantService {
248254 this . logger . log ( `[${ matchId } ] try retry assign server....` ) ;
249255 await this . assignServer ( matchId , ++ tries ) ;
250256 } , tries * 1000 ) ;
251- return false ;
257+ return ;
252258 }
253259 }
254260
@@ -258,19 +264,53 @@ export class MatchAssistantService {
258264 `[${ matchId } ] unable to assign dedicated server, trying on demand` ,
259265 ) ;
260266 await this . updateMatchStatus ( match . id , "WaitingForServer" ) ;
261- return false ;
267+ return ;
262268 }
263269
264270 if ( await this . assignDedicatedServer ( match . id , match . region ) ) {
265- return true ;
271+ await this . startMatch ( matchId ) ;
272+ return ;
266273 }
267274
268275 this . logger . log (
269276 `[${ matchId } ] unable to assign dedicated server, updating match status to waiting for server` ,
270277 ) ;
278+
271279 await this . updateMatchStatus ( match . id , "WaitingForServer" ) ;
280+ }
281+
282+ private async startMatch ( matchId : string ) {
283+ await this . updateMatchStatus ( matchId , "Live" ) ;
284+
285+ await this . sendServerMatchId ( matchId ) ;
286+ }
287+
288+ public async reserveDedicatedServer ( matchId : string ) {
289+ const serverId = await this . isDedicatedServerAvailable ( matchId ) ;
290+ if ( ! serverId ) {
291+ this . logger . warn (
292+ `[${ matchId } ] another match is currently live, moving back to scheduled` ,
293+ ) ;
294+ await this . updateMatchStatus ( matchId , "WaitingForServer" ) ;
295+
296+ return ;
297+ }
298+
299+ await this . hasura . mutation ( {
300+ update_servers_by_pk : {
301+ __args : {
302+ pk_columns : {
303+ id : serverId ,
304+ } ,
305+ _set : {
306+ reserved_by_match_id : matchId ,
307+ } ,
308+ } ,
309+ __typename : true ,
310+ } ,
311+ } ) ;
272312
273- return false ;
313+ await this . startMatch ( matchId ) ;
274314 }
275315
276316 private async assignDedicatedServer (
0 commit comments