Skip to content

Commit

Permalink
fix(core): find subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Nov 30, 2023
1 parent 1018e05 commit 0e63ccf
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions packages/core/src/controllers/player/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,51 @@ const [onCommandText] = defineEvent({
},
});

onCommandText(({ player, buffer }) => {
function generateCombinations(arr: any[]) {
const result = [];
let current = "";
for (let i = 0; i < arr.length; i++) {
current += arr[i] + " ";
result.push(current.trimEnd());
}
return result.reverse();
}

onCommandText(({ player, buffer, next }) => {
const rawCommand = I18n.decodeFromBuf(buffer, player.charset);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const matchedCommand = rawCommand.match(commandPattern)!;

const commandStr = matchedCommand.join(" ");
const maybes = generateCombinations(matchedCommand);

const maybeFind = maybes.find((maybe) => commandBus.get(maybe));

const definedCommands = commandBus.get(commandStr);
if (!definedCommands) {
return triggerOnError(player, commandStr, CommandErrors.NOT_EXIST);
const fullCommand = matchedCommand.join(" ");

if (!maybeFind) {
return triggerOnError(player, fullCommand, CommandErrors.NOT_EXIST);
}

const received = triggerOnReceived(player, commandStr);
const received = triggerOnReceived(player, fullCommand);
if (!received) return received;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const definedCommands = commandBus.get(maybeFind)!;

const [, triggerCommand] = definedCommands;
const middlewaresRet = triggerCommand(player, matchedCommand.slice(1));

if (middlewaresRet) return triggerOnPerformed(player, commandStr);
const subcommand = fullCommand.replace(maybeFind, "").trim().split(" ");

const middlewaresRet = triggerCommand(player, subcommand);

if (!middlewaresRet) {
return triggerOnError(player, fullCommand, CommandErrors.REJECTED);
}

const ret = triggerOnPerformed(player, fullCommand);
if (!ret) return ret;

return triggerOnError(player, commandStr, CommandErrors.REJECTED);
return next();
});

export const CmdBus = {
Expand Down

0 comments on commit 0e63ccf

Please sign in to comment.