diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-21 21:09:52 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-21 21:09:52 +0200 |
commit | 607b43a84d3bc8edffa05c722c7b8c3e6f72e964 (patch) | |
tree | 8503eeb4822f11ca1f0f86ef17283aacb9a7a6e2 /src/scalevalapokalypsi/main.scala | |
parent | fe2543627bcec1ea0f7a429bede20ca293458ba9 (diff) | |
download | scalevalapokalypsi-607b43a84d3bc8edffa05c722c7b8c3e6f72e964.tar.gz scalevalapokalypsi-607b43a84d3bc8edffa05c722c7b8c3e6f72e964.zip |
Wooooohooo time limits and correcter printing
Diffstat (limited to 'src/scalevalapokalypsi/main.scala')
-rw-r--r-- | src/scalevalapokalypsi/main.scala | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/src/scalevalapokalypsi/main.scala b/src/scalevalapokalypsi/main.scala index 50e89e5..f953751 100644 --- a/src/scalevalapokalypsi/main.scala +++ b/src/scalevalapokalypsi/main.scala @@ -1,7 +1,10 @@ package scalevalapokalypsi -import scalevalapokalypsi.Client.newClient +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 @@ -15,11 +18,61 @@ import scala.io.StdIn.readLine println("Server started in background.") print("Choose a name:\n> ") val name = readLine() - newClient(name, "127.0.0.1", 2267).foreach(_.startClient()) + 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() - newClient(name, "127.0.0.1", 2267).foreach(_.startClient()) + 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 |