diff options
Diffstat (limited to 'src/scalevalapokalypsi/main.scala')
-rw-r--r-- | src/scalevalapokalypsi/main.scala | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/scalevalapokalypsi/main.scala b/src/scalevalapokalypsi/main.scala index f953751..e357845 100644 --- a/src/scalevalapokalypsi/main.scala +++ b/src/scalevalapokalypsi/main.scala @@ -4,6 +4,7 @@ import scalevalapokalypsi.Client.{newClient, Client, StdinLineReader, GameEvent} import scalevalapokalypsi.Server.Server import scalevalapokalypsi.constants.* import java.lang.Thread.sleep +import java.lang.System.currentTimeMillis import scala.util.Try import java.lang.Thread @@ -45,16 +46,27 @@ def startClient(client: Client): Unit = val printer = Printer() while !hasQuit do sleep(POLL_INTERVAL) - val gameEvent = client.clientStep(stdinReader.newLine()) - printer.printGameEvent(gameEvent) + val line = stdinReader.newLine() + if line.map(_.length).getOrElse(0) > 1024 then + printer.printLn("Virhe: Syötteesi oli liian pitkä.") + else if line == Some("quit") then + hasQuit = true + else + val gameEvent = client.clientStep(line) + printer.printGameEvent(gameEvent) class Printer: var inputIndicatorAtStartOfLine = false + var queriedLineToSing = false + var singStartTime: Option[Long] = None + 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 || @@ -62,17 +74,42 @@ class Printer: lineToSing.isDefined) then this.printLn("") + actions.foreach(this.printLn(_)) + roomState.foreach(this.printLn(_)) - lineToSing.foreach(this.printLn(_)) + + lineToSing match + case Some(l) => + if this.singStartTime.isEmpty then + this.singStartTime = Some(currentTimeMillis() / 1000) + print(s"Laula: “$l”\n ") + val timeSpent = this.singStartTime.map((t: Long) => + (currentTimeMillis / 1000 - t).toString + ).getOrElse("?") + print(this.timeIndicatorUpdater(timeSpent)) + case None => + this.singStartTime = None + val timeLeft = s"${gameEvent.timeToNextTurn.getOrElse("∞")}" - if gameEvent.playerCanAct && !inputIndicatorAtStartOfLine then + + if + gameEvent.playerCanAct && + lineToSing.isEmpty && + !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 = + if gameEvent.playerCanAct && lineToSing.isEmpty then + print(this.timeIndicatorUpdater(timeLeft)) + + end printGameEvent + + def printLn(s: String): Unit = println(s) this.inputIndicatorAtStartOfLine = false + + private def timeIndicatorUpdater(t: String): String = + s"\u001b[s\u001b[0E[$t s]> \u001b[u" |