Skip to content

Commit

Permalink
Say colorful commands and available maps in ingame chat
Browse files Browse the repository at this point in the history
  • Loading branch information
JensForstmann committed Nov 2, 2023
1 parent af32e04 commit c6fab12
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
20 changes: 20 additions & 0 deletions backend/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IPlayer, TTeamString } from '../../common';
import { colors } from './gameServer';
import { Match } from './match';
import { Settings } from './settings';

const Commands = [
'BAN',
Expand Down Expand Up @@ -80,3 +82,21 @@ export const onCommand = async (e: CommandEvent) => {
const handlers = commandHandlers.get(e.command) ?? [];
await Promise.all(handlers.map((h) => h(e)));
};

export const formatIngameCommand = (command: string, parameters?: string) => {
return (
colors.lightOrange +
Settings.COMMAND_PREFIXES[0] +
command +
(parameters ? ' ' + parameters : '') +
colors.white
);
};

export const formatFirstIngameCommand = (command: TCommand, parameters?: string) => {
const firstUserCommand = getUserCommandsByInternalCommand(command)[0];
if (!firstUserCommand) {
return '';
}
return formatIngameCommand(firstUserCommand, parameters);
};
67 changes: 30 additions & 37 deletions backend/src/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
TWho,
} from '../../common';
import * as commands from './commands';
import { getUserCommandsByInternalCommand } from './commands';
import * as Events from './events';
import { colors } from './gameServer';
import * as Match from './match';
import * as MatchMap from './matchMap';
import * as MatchService from './matchService';
Expand Down Expand Up @@ -120,11 +120,11 @@ const getAvailableCommands = (match: Match.Match, currentElectionStep: IElection
};

const sayAvailableCommands = async (match: Match.Match, currentElectionStep: IElectionStep) => {
const commands = getAvailableCommands(match, currentElectionStep);
if (commands.length > 0) {
const cmds = getAvailableCommands(match, currentElectionStep);
if (cmds.length > 0) {
await Match.say(
match,
`COMMANDS: ${commands.map((c) => Settings.COMMAND_PREFIXES[0] + c).join(', ')}`
`COMMANDS: ${cmds.map((cmd) => commands.formatIngameCommand(cmd)).join(', ')}`
);
}
};
Expand Down Expand Up @@ -161,9 +161,7 @@ const sayWhatIsUp = async (match: Match.Match) => {
if (currentElectionStep.map.mode === 'AGREE') {
await Match.say(
match,
`BOTH TEAMS MUST ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('AGREE')[0]
} ON THE SAME MAP`
`BOTH TEAMS MUST ${commands.formatFirstIngameCommand('AGREE')} ON THE SAME MAP`
);
if (match.data.election.currentAgree.teamA)
await Match.say(
Expand All @@ -185,33 +183,29 @@ const sayWhatIsUp = async (match: Match.Match) => {
if (validTeam)
await Match.say(
match,
`TEAM ${escapeRconString(Match.getTeamByAB(match, validTeam).name)} MUST ${
Settings.COMMAND_PREFIXES[0]
}${getUserCommandsByInternalCommand('BAN')[0]} A MAP`
`TEAM ${escapeRconString(
Match.getTeamByAB(match, validTeam).name
)} MUST ${commands.formatFirstIngameCommand('BAN')} A MAP`
);
if (!validTeam)
await Match.say(
match,
`BOTH TEAMS CAN START TO ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('BAN')[0]
} A MAP`
`BOTH TEAMS CAN START TO ${commands.formatFirstIngameCommand('BAN')} A MAP`
);
await sayAvailableMaps(match);
} else if (currentElectionStep.map.mode === 'PICK') {
const validTeam = getValidTeamAB(match, currentElectionStep.map.who);
if (validTeam)
await Match.say(
match,
`TEAM ${escapeRconString(Match.getTeamByAB(match, validTeam).name)} MUST ${
Settings.COMMAND_PREFIXES[0]
}${getUserCommandsByInternalCommand('PICK')[0]} A MAP`
`TEAM ${escapeRconString(
Match.getTeamByAB(match, validTeam).name
)} MUST ${commands.formatFirstIngameCommand('PICK')} A MAP`
);
if (!validTeam)
await Match.say(
match,
`BOTH TEAMS CAN START TO ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('PICK')[0]
} A MAP`
`BOTH TEAMS CAN START TO ${commands.formatFirstIngameCommand('PICK')} A MAP`
);
await sayAvailableMaps(match);
}
Expand All @@ -223,27 +217,32 @@ const sayWhatIsUp = async (match: Match.Match) => {
match,
`TEAM ${escapeRconString(
Match.getTeamByAB(match, validTeam).name
)} MUST CHOOSE A SIDE FOR MAP ${match.data.election.currentStepMap} (${
Settings.COMMAND_PREFIXES[0]
}${getUserCommandsByInternalCommand('CT')[0]}, ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('T')[0]
})`
)} MUST CHOOSE A SIDE FOR MAP ${
match.data.election.currentStepMap
} (${commands.formatFirstIngameCommand(
'CT'
)}, ${commands.formatFirstIngameCommand('T')})`
);
if (!validTeam)
await Match.say(
match,
`BOTH TEAMS CAN START TO CHOOSE A SIDE FOR MAP ${
match.data.election.currentStepMap
} (${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('CT')[0]
}, ${Settings.COMMAND_PREFIXES[0]}${getUserCommandsByInternalCommand('T')[0]})`
} (${commands.formatFirstIngameCommand(
'CT'
)}, ${commands.formatFirstIngameCommand('T')})`
);
}
}
};

const sayAvailableMaps = async (match: Match.Match) => {
await Match.say(match, `AVAILABLE MAPS: ${match.data.election.remainingMaps.join(', ')}`);
await Match.say(
match,
`AVAILABLE MAPS: ${match.data.election.remainingMaps
.map((map) => colors.purple + map + colors.white)
.join(', ')}`
);
};

export const registerCommandHandlers = () => {
Expand Down Expand Up @@ -351,12 +350,7 @@ const onAgreeCommand: commands.CommandHandler = async (e) => {
await next(match);
} else {
await Match.say(match, `MAP ${map} SUGGESTED BY ${escapeRconString(team.name)}`);
await Match.say(
match,
`AGREE WITH ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('AGREE')[0]
}`
);
await Match.say(match, `AGREE WITH ${commands.formatFirstIngameCommand('AGREE')}`);
match.log(`${teamAB} (${team.name} - ${player.name}) suggests map ${map}`);
}
MatchService.scheduleSave(match);
Expand Down Expand Up @@ -550,6 +544,7 @@ const onRestartCommand: commands.CommandHandler = async (e) => {
match.data.election = create(match.data.mapPool, match.data.electionSteps);
match.data.matchMaps = [];
await Match.say(match, `ELECTION PROCESS RESTARTED`);
await sayWhatIsUp(match);
} catch (err) {
match.log(`Error restarting the election process: ${err}`);
await Match.say(match, `ERROR RESTARTING THE ELECTION`);
Expand All @@ -562,9 +557,7 @@ const onRestartCommand: commands.CommandHandler = async (e) => {

await Match.say(
match,
`TYPE ${Settings.COMMAND_PREFIXES[0]}${
getUserCommandsByInternalCommand('RESTART')[0]
} TO CONFIRM AND RESTART`
`TYPE ${commands.formatFirstIngameCommand('RESTART')} TO CONFIRM AND RESTART`
);
}
MatchService.scheduleSave(match);
Expand Down
9 changes: 5 additions & 4 deletions backend/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,13 @@ export const sayNotAssigned = async (match: Match, player: IPlayer) => {
await say(match, `PLAYER ${escapeRconString(player.name)} NOT ASSIGNED!`);
await say(
match,
`TYPE "${Settings.COMMAND_PREFIXES[0]}team a" TO JOIN ${escapeRconString(
`TYPE ${commands.formatFirstIngameCommand('TEAM', 'a')} TO JOIN ${escapeRconString(
match.data.teamA.name
)}`
);
await say(
match,
`TYPE "${Settings.COMMAND_PREFIXES[0]}team b" TO JOIN ${escapeRconString(
`TYPE ${commands.formatFirstIngameCommand('TEAM', 'b')} TO JOIN ${escapeRconString(
match.data.teamB.name
)}`
);
Expand All @@ -712,9 +712,10 @@ const sayWrongTeamOrSide = async (
);
await say(
match,
`CHECK SCOREBOARD AND CHANGE TEAM OR TYPE "${Settings.COMMAND_PREFIXES[0]}team ${
`CHECK SCOREBOARD AND CHANGE TEAM OR TYPE ${commands.formatFirstIngameCommand(
'TEAM',
currentTeamAB === 'TEAM_A' ? 'b' : 'a'
}" TO CHANGE REGISTRATION`
)} TO CHANGE REGISTRATION`
);
};

Expand Down
11 changes: 4 additions & 7 deletions backend/src/matchMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ const getAvailableCommands = (state: TMatchMapSate): string[] => {
};

const sayAvailableCommands = async (match: Match.Match, matchMap: IMatchMap) => {
const commands = getAvailableCommands(matchMap.state);
if (commands.length > 0) {
const cmds = getAvailableCommands(matchMap.state);
if (cmds.length > 0) {
await Match.say(
match,
`COMMANDS: ${commands.map((c) => Settings.COMMAND_PREFIXES[0] + c).join(', ')}`
`COMMANDS: ${cmds.map((cmd) => commands.formatIngameCommand(cmd)).join(', ')}`
);
}
};
Expand Down Expand Up @@ -303,10 +303,7 @@ const onRestartCommand: commands.CommandHandler = async (e) => {
} else {
await Match.say(match, `${escapeRconString(team.name)} WANTS TO RESTART THE KNIFE ROUND`);
match.log(`${teamAB} (${team.name} - ${player.name}) wants to restart the knife round`);
await Match.say(
match,
`AGREE WITH ${commands.getUserCommandsByInternalCommand('RESTART')[0]}`
);
await Match.say(match, `AGREE WITH ${commands.formatFirstIngameCommand('RESTART')}`);
}

MatchService.scheduleSave(match);
Expand Down

0 comments on commit c6fab12

Please sign in to comment.