about summary refs log tree commit diff stats
path: root/js/puzzle-dungeon/commandHandler.js
blob: 12140c28c4ee15d80f3e0a0ffdfe953ce0abd820 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const createCommandHandler = () => {
    const commands = {};

    const registerCommand = (name, callback) => {
        commands[name] = callback;
    };

    const executeCommand = (parsedCommand) => {
        const { command, args } = parsedCommand;
        if (commands[command]) {
            commands[command](...args);
        } else {
            console.error(`Unknown command: ${command}`);
        }
    };

    const executeCommands = (parsedCommands) => {
        parsedCommands.forEach(cmd => executeCommand(cmd));
    };

    return {
        registerCommand,
        executeCommand,
        executeCommands
    };
};

export default createCommandHandler;