Skip to content

Commit d440b1e

Browse files
committed
chore: optimize assigning servers
1 parent b1da20f commit d440b1e

4 files changed

Lines changed: 63 additions & 54 deletions

File tree

src/matches/events/KnifeSwitch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class KnifeSwitch extends MatchEventProcessor<void> {
2727
id: match.current_match_map_id,
2828
},
2929
_set: {
30+
status: "Live",
3031
lineup_1_side: currentMap.lineup_2_side,
3132
lineup_2_side: currentMap.lineup_1_side,
3233
},

src/matches/match-assistant/match-assistant.service.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

src/matches/matches.controller.ts

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export class MatchesController {
388388
server: {
389389
id: true,
390390
is_dedicated: true,
391+
reserved_by_match_id: true,
391392
game_server_node_id: true,
392393
},
393394
},
@@ -399,33 +400,13 @@ export class MatchesController {
399400

400401
if (status === "Live" && data.old.status !== "WaitingForServer") {
401402
if (match.server) {
402-
if (
403-
match.server.is_dedicated &&
404-
!(await this.matchAssistant.isDedicatedServerAvailable(matchId))
405-
) {
406-
this.logger.warn(
407-
`[${matchId}] another match is currently live, moving back to scheduled`,
408-
);
409-
await this.matchAssistant.updateMatchStatus(
410-
match.id,
411-
"WaitingForServer",
412-
);
403+
if (match.server.reserved_by_match_id === matchId) {
413404
return;
414405
}
415406

416-
await this.hasura.mutation({
417-
update_servers_by_pk: {
418-
__args: {
419-
pk_columns: {
420-
id: match.server.id,
421-
},
422-
_set: {
423-
reserved_by_match_id: matchId,
424-
},
425-
},
426-
__typename: true,
427-
},
428-
});
407+
if (match.server.is_dedicated) {
408+
await this.matchAssistant.reserveDedicatedServer(matchId);
409+
}
429410
} else {
430411
/**
431412
* if we don't have a server id it means we need to assign it one
@@ -434,10 +415,6 @@ export class MatchesController {
434415
}
435416
}
436417

437-
if (match.server?.id) {
438-
await this.matchAssistant.sendServerMatchId(matchId);
439-
}
440-
441418
await this.discordMatchOverview.updateMatchOverview(matchId);
442419
}
443420

@@ -547,10 +524,6 @@ export class MatchesController {
547524
);
548525
}
549526

550-
if (updated_match.server?.game_server_node_id === null) {
551-
await this.matchAssistant.sendServerMatchId(match_id);
552-
}
553-
554527
return {
555528
success: true,
556529
};
@@ -825,11 +798,7 @@ export class MatchesController {
825798
return;
826799
}
827800

828-
if (!(await this.matchAssistant.assignServer(match.id))) {
829-
return;
830-
}
831-
832-
await this.matchAssistant.updateMatchStatus(match.id, "Live");
801+
await this.matchAssistant.assignServer(match.id);
833802
}
834803

835804
@HasuraEvent()
@@ -895,10 +864,7 @@ export class MatchesController {
895864
});
896865

897866
for (const match of matches) {
898-
if (!(await this.matchAssistant.assignServer(match.id))) {
899-
break;
900-
}
901-
await this.matchAssistant.updateMatchStatus(match.id, "Live");
867+
await this.matchAssistant.assignServer(match.id);
902868
}
903869
}
904870

src/type-sense/type-sense.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,12 @@ export class TypeSenseService {
235235
},
236236
});
237237

238-
for (const matchLineupPlayer of match_lineup_players) {
239-
await this.matchAssistant.sendServerMatchId(
240-
matchLineupPlayer.lineup.v_match_lineup.match_id,
241-
);
238+
if (player.is_banned || player.is_gagged || player.is_muted) {
239+
for (const matchLineupPlayer of match_lineup_players) {
240+
await this.matchAssistant.sendServerMatchId(
241+
matchLineupPlayer.lineup.v_match_lineup.match_id,
242+
);
243+
}
242244
}
243245

244246
// this is to allow filtering

0 commit comments

Comments
 (0)