Skip to content

Commit

Permalink
Dynamic map change delay (depends on mp_match_restart_delay)
Browse files Browse the repository at this point in the history
  • Loading branch information
JensForstmann committed Apr 22, 2024
1 parent 194e5da commit b800c38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ export const update = async (match: Match, dto: IMatchUpdateDto) => {
const nextMap = match.data.matchMaps[dto.currentMap];
if (nextMap) {
match.data.currentMap = dto.currentMap;
await MatchMap.loadMap(match, nextMap);
await MatchMap.loadMap(match, nextMap, true);
} else {
throw new ValidateError(
{
Expand Down
28 changes: 23 additions & 5 deletions backend/src/matchMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,33 @@ export const onRoundEnd = async (
}
};

export const loadMap = async (match: Match.Match, matchMap: IMatchMap) => {
export const loadMap = async (match: Match.Match, matchMap: IMatchMap, useDefaultDelay = false) => {
const internalMapName = parseMapParts(matchMap.name).internal;

await Match.say(match, `MAP WILL BE CHANGED TO ${formatMapName(matchMap.name)} IN 15 SECONDS`);
match.log(`Change map to ${matchMap.name} (in 15 seconds)`);
let delayInSeconds: number | undefined;
if (useDefaultDelay || match.data.currentMap === 0) {
delayInSeconds = 15;
} else {
const cVar = await Match.getConfigVar(match, 'mp_match_restart_delay');
if (/^\d+$/.test(cVar)) {
delayInSeconds = parseInt(cVar);
} else {
match.log('Config var mp_match_restart_delay cannot be parsed to number: ' + cVar);
}
}
if (!delayInSeconds || isNaN(delayInSeconds) || delayInSeconds < 15) {
delayInSeconds = 15;
}

await Match.say(
match,
`MAP WILL BE CHANGED TO ${formatMapName(matchMap.name)} IN ${delayInSeconds} SECONDS`
);
match.log(`Change map to ${matchMap.name} (in ${delayInSeconds} seconds)`);
match.data.state = 'MATCH_MAP';
matchMap.state = 'MAP_CHANGE';
MatchService.scheduleSave(match);
await sleep(15000);
await sleep(delayInSeconds * 1000);

await Match.setTeamNames(match);

Expand Down Expand Up @@ -604,7 +622,7 @@ export const update = async (
if (dto.name && matchMap.name !== dto.name) {
matchMap.name = dto.name;
if (match.data.currentMap === mapNumber) {
await loadMap(match, matchMap);
await loadMap(match, matchMap, true);
}
}

Expand Down

0 comments on commit b800c38

Please sign in to comment.