initial commit2
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
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<String, ClientCommand>()
|
||||
private val history = ArrayDeque<String>(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<String> = history.toList()
|
||||
|
||||
fun getCommands(): Map<String, ClientCommand> = commands.toMap()
|
||||
|
||||
private fun addToHistory(name: String) {
|
||||
if (history.size >= 12) history.removeFirst()
|
||||
history.addLast(name)
|
||||
}
|
||||
|
||||
fun getCommandNames(): List<String> = commands.keys.toList()
|
||||
}
|
||||
Reference in New Issue
Block a user