package runner import commands.ClientCommand import commands.CommandType import input.HumanBeingBuilder import input.IOManager import network.NetworkManager class CommandInvoker( private val io: IOManager, private val networkManager: NetworkManager ) { private val commands = mutableMapOf() private val history = ArrayDeque(12) fun register(command: ClientCommand) { commands[command.name] = command } fun execute(input: String) { val parts = input.trim().split(" ") val name = parts[0].lowercase().trim() val args = parts.drop(1) val command = commands[name] if (command == null) { io.print("Неизвестная команда: '$name'. Введите 'help' для справки.") return } addToHistory(name) try { if (command.type == CommandType.MODEL) { val model = HumanBeingBuilder(io, networkManager).build() val res = command.execute(args, model) io.print(res) } else { val res = command.execute(args) io.print(res) } } catch (e: IllegalStateException) { io.print("[Ошибка выполнения] ${e.message}") } } fun getHistory(): List = history.toList() fun getCommands(): Map = commands.toMap() private fun addToHistory(name: String) { if (history.size >= 12) history.removeFirst() history.addLast(name) } fun getCommandNames(): List = commands.keys.toList() }