Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Get available commands in election.ts like in matchMap.ts
  • Loading branch information
JensForstmann committed Nov 2, 2023
1 parent 2308160 commit af32e04
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions backend/src/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,59 @@ const checkValidConfiguration = (
}
};

const getAvailableCommands = (match: Match.Match, currentElectionStep: IElectionStep): string[] => {
const getAvailableCommandsEnums = (
match: Match.Match,
currentElectionStep: IElectionStep
): commands.TCommand[] => {
if (match.data.election.state === 'FINISHED') {
return [];
}
if (match.data.election.currentSubStep === 'MAP') {
switch (currentElectionStep.map.mode) {
case 'AGREE':
return [
...getUserCommandsByInternalCommand('AGREE'),
...getUserCommandsByInternalCommand('PICK'),
...getUserCommandsByInternalCommand('RESTART'),
];
return ['AGREE', 'PICK', 'RESTART'];
case 'BAN':
return [
...getUserCommandsByInternalCommand('BAN'),
...getUserCommandsByInternalCommand('RESTART'),
];
return ['BAN', 'RESTART'];
case 'PICK':
return [
...getUserCommandsByInternalCommand('PICK'),
...getUserCommandsByInternalCommand('RESTART'),
];
return ['PICK', 'RESTART'];
case 'FIXED':
case 'RANDOM_BAN':
case 'RANDOM_PICK':
return [];
}
} else {
if (isElectionStepSkip(currentElectionStep)) {
return [];
}
}
if (match.data.election.currentSubStep === 'SIDE' && isElectionStepAdd(currentElectionStep)) {
switch (currentElectionStep.side.mode) {
case 'PICK':
return [
...getUserCommandsByInternalCommand('T'),
...getUserCommandsByInternalCommand('CT'),
...getUserCommandsByInternalCommand('RESTART'),
];
return ['T', 'CT', 'RESTART'];
case 'FIXED':
case 'KNIFE':
case 'RANDOM':
return [];
}
}
return [];
};

const getAvailableCommands = (match: Match.Match, currentElectionStep: IElectionStep): string[] => {
return getAvailableCommandsEnums(match, currentElectionStep).reduce(
(pv: string[], cv: commands.TCommand) => [
...pv,
...commands.getUserCommandsByInternalCommand(cv),
],
[]
);
};

const sayAvailableCommands = async (match: Match.Match, currentElectionStep: IElectionStep) => {
const commands = getAvailableCommands(match, currentElectionStep);
if (commands.length > 0) {
await Match.say(
match,
`COMMANDS: ${commands.map((c) => Settings.COMMAND_PREFIXES[0] + c).join(', ')}`
);
}
};

const getCurrentElectionStep = (match: Match.Match): IElectionStep | undefined => {
Expand All @@ -135,16 +153,6 @@ export const sayPeriodicMessage = async (match: Match.Match) => {
}
};

const sayAvailableCommands = async (match: Match.Match, currentElectionStep: IElectionStep) => {
const commands = getAvailableCommands(match, currentElectionStep);
if (commands.length > 0) {
await Match.say(
match,
`COMMANDS: ${commands.map((c) => Settings.COMMAND_PREFIXES[0] + c).join(', ')}`
);
}
};

const sayWhatIsUp = async (match: Match.Match) => {
const currentElectionStep = getCurrentElectionStep(match);
if (!currentElectionStep) return;
Expand Down Expand Up @@ -541,6 +549,7 @@ const onRestartCommand: commands.CommandHandler = async (e) => {
match.log('Restart election process');
match.data.election = create(match.data.mapPool, match.data.electionSteps);
match.data.matchMaps = [];
await Match.say(match, `ELECTION PROCESS RESTARTED`);
} catch (err) {
match.log(`Error restarting the election process: ${err}`);
await Match.say(match, `ERROR RESTARTING THE ELECTION`);
Expand Down

0 comments on commit af32e04

Please sign in to comment.