diff options
Diffstat (limited to 'js/puzzle-dungeon/commandHandler.js')
-rw-r--r-- | js/puzzle-dungeon/commandHandler.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/js/puzzle-dungeon/commandHandler.js b/js/puzzle-dungeon/commandHandler.js new file mode 100644 index 0000000..12140c2 --- /dev/null +++ b/js/puzzle-dungeon/commandHandler.js @@ -0,0 +1,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; |