fuck it
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import ch.qos.logback.core.net.server.Client
|
||||
import commands.ExecuteScriptCommand
|
||||
import commands.ExitCommand
|
||||
import commands.HelpCommand
|
||||
import commands.SyncCommand
|
||||
import commands.UndefinedCommand
|
||||
import network.NetworkManager
|
||||
import input.HumanBeingBuilder
|
||||
import input.IOManager
|
||||
import network.Request
|
||||
import runner.CommandValidator
|
||||
import runner.CommandInvoker
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val host = if (args.isNotEmpty()) args[0] else "localhost"
|
||||
@@ -10,7 +15,7 @@ fun main(args: Array<String>) {
|
||||
|
||||
val io = IOManager() // Name by Boris Bosenko
|
||||
val client = NetworkManager(host, port)
|
||||
val validator = CommandValidator()
|
||||
val invoker = CommandInvoker(io)
|
||||
|
||||
// Shutdown hook
|
||||
Runtime.getRuntime().addShutdownHook(Thread {
|
||||
@@ -23,70 +28,32 @@ fun main(args: Array<String>) {
|
||||
|════════════════════════════════════════════
|
||||
""".trimMargin())
|
||||
|
||||
runClientRepl(io, client, validator)
|
||||
registerBasicCommands(invoker, client, io)
|
||||
runClientRepl(io, invoker)
|
||||
}
|
||||
|
||||
fun runClientRepl(io: IOManager, client: NetworkManager, validator: CommandValidator) {
|
||||
fun registerBasicCommands(invoker: CommandInvoker,client: NetworkManager, io: IOManager) {
|
||||
listOf(
|
||||
HelpCommand(invoker),
|
||||
ExecuteScriptCommand(io),
|
||||
SyncCommand(invoker, client)
|
||||
).forEach { invoker.register(it)}
|
||||
}
|
||||
|
||||
val initialRequest = Request(
|
||||
commandName = "getCommandsCommand",
|
||||
humanBeing = null
|
||||
)
|
||||
fun runClientRepl(io: IOManager, invoker : CommandInvoker) {
|
||||
// sync commands w server
|
||||
invoker.execute("sync")
|
||||
|
||||
if (client.sendRequest(initialRequest) == null) {
|
||||
io.print("[!] Сервер недоступен. Попробуйте позже.")
|
||||
} else {
|
||||
io.print("|Введите 'help' для справки.")
|
||||
}
|
||||
var isRunning = true
|
||||
invoker.register(ExitCommand {isRunning = false})
|
||||
|
||||
while (true) {
|
||||
while (isRunning) {
|
||||
val line = io.readLine(" ☭ ") ?: break
|
||||
if (line.isBlank()) continue
|
||||
|
||||
val parts = line.trim().split(" ")
|
||||
val commandName = parts[0].lowercase()
|
||||
val args = parts.drop(1)
|
||||
|
||||
val (isValidCommand, message) = validator.validate(commandName, args)
|
||||
|
||||
if (!isValidCommand) {
|
||||
if (message != null) io.print(message)
|
||||
continue
|
||||
}
|
||||
|
||||
if (commandName == "exit") {
|
||||
io.print("До свидания!")
|
||||
break
|
||||
}
|
||||
|
||||
if (commandName == "execute_script") {
|
||||
io.addScriptScanner(args[0])
|
||||
continue
|
||||
}
|
||||
|
||||
val humanBeing = if (validator.isBuildsHumanBeing(commandName)) {
|
||||
try {
|
||||
HumanBeingBuilder(io).build()
|
||||
} catch (e: IllegalStateException) {
|
||||
io.print("[Ошибка] Ввод прерван: ${e.message}")
|
||||
continue
|
||||
}
|
||||
} else null
|
||||
|
||||
// Формируем запрос и отправляем
|
||||
val request = Request(
|
||||
commandName = commandName,
|
||||
args = args,
|
||||
humanBeing = humanBeing
|
||||
)
|
||||
|
||||
val response = client.sendRequest(request)
|
||||
|
||||
// Обрабатываем ответ
|
||||
if (response == null) {
|
||||
io.print("[!] Сервер недоступен. Попробуйте позже.")
|
||||
} else {
|
||||
io.print(response.message)
|
||||
}
|
||||
invoker.execute(line)
|
||||
}
|
||||
|
||||
// and here we exit that crazy circus of code and tears
|
||||
io.print("До свидания!")
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user