package scalevalapokalypsi import scalevalapokalypsi.Client.{newClient, Client, StdinLineReader, GameEvent} import scalevalapokalypsi.Server.Server import scalevalapokalypsi.constants.* import java.lang.Thread.sleep import scala.util.Try import java.lang.Thread import scala.io.StdIn.readLine // TODO: add proper logic for starting the game @main def main(): Unit = print("How do you want to play?\n1) Host and join local game\n2) Join local game\n> ") readLine().toIntOption match case Some(1) => Thread(() => new Server(2267, 5, 30, true).startServer()).start() println("Server started in background.") print("Choose a name:\n> ") val name = readLine() Try(newClient(name, "127.0.0.1", 2267)) .toOption .flatten match case Some(client) => startClient(client) case None => println("Starting the client failed.") case Some(2) => print("Choose a name:\n> ") val name = readLine() Try(newClient(name, "127.0.0.1", 2267)) .toOption .flatten match case Some(client) => startClient(client) case None => println("Starting the client failed.") case _ => println("Invalid input") def startClient(client: Client): Unit = var hasQuit = false val stdinReader = StdinLineReader() stdinReader.startReading() val printer = Printer() while !hasQuit do sleep(POLL_INTERVAL) val gameEvent = client.clientStep(stdinReader.newLine()) printer.printGameEvent(gameEvent) class Printer: var inputIndicatorAtStartOfLine = false def printGameEvent(gameEvent: GameEvent): Unit = val actions = gameEvent.actions.map(_.mkString("\n")) val roomState = gameEvent.roomState.map(_.toString) val lineToSing = gameEvent.lineToSing if inputIndicatorAtStartOfLine && (actions.isDefined || roomState.isDefined || lineToSing.isDefined) then this.printLn("") actions.foreach(this.printLn(_)) roomState.foreach(this.printLn(_)) lineToSing.foreach(this.printLn(_)) val timeLeft = s"${gameEvent.timeToNextTurn.getOrElse("∞")}" if gameEvent.playerCanAct && !inputIndicatorAtStartOfLine then this.inputIndicatorAtStartOfLine = true print(s"[$timeLeft s]> ") if gameEvent.playerCanAct then print(s"\u001b[s\u001b[0E[$timeLeft s]\u001b[u") private def printLn(s: String): Unit = println(s) this.inputIndicatorAtStartOfLine = false