diff options
author | Aleksi Heikkila <aleksi.e.heikkila@aalto.fi> | 2024-11-23 22:33:53 +0200 |
---|---|---|
committer | Aleksi Heikkila <aleksi.e.heikkila@aalto.fi> | 2024-11-23 22:33:53 +0200 |
commit | 27dd937617cce1e43df1c16e12050f6e88763d54 (patch) | |
tree | 8247736f7c4d5b1a0c1541c25f477e73e5783944 /src/scalevalapokalypsi/UI/Printer.scala | |
parent | b11c02e8f1f4dea09847da999c2acb2f96df8a58 (diff) | |
parent | 28b83db50f33cb704311ffe608dcd8c4412635cf (diff) | |
download | scalevalapokalypsi-27dd937617cce1e43df1c16e12050f6e88763d54.tar.gz scalevalapokalypsi-27dd937617cce1e43df1c16e12050f6e88763d54.zip |
Merge branch 'master' of ssh://cron4.fi/~/O1-game
Diffstat (limited to 'src/scalevalapokalypsi/UI/Printer.scala')
-rw-r--r-- | src/scalevalapokalypsi/UI/Printer.scala | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/scalevalapokalypsi/UI/Printer.scala b/src/scalevalapokalypsi/UI/Printer.scala new file mode 100644 index 0000000..a33864f --- /dev/null +++ b/src/scalevalapokalypsi/UI/Printer.scala @@ -0,0 +1,78 @@ +package scalevalapokalypsi.UI +import scalevalapokalypsi.Client.GameEvent +import java.lang.System.currentTimeMillis +import scalevalapokalypsi.utils.isPrintable + +/** A singleton for printing. Keeps track about the "action query" at the start + * of lines and has a helper function "pryntGameEvent". + */ +object Printer: + var inputIndicatorAtStartOfLine = false + var queriedLineToSing = false + var singStartTime: Option[Long] = None + + /** Prints the given game event. + * + * @param gameEvent the event to print + */ + 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 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 && + lineToSing.isEmpty && + !inputIndicatorAtStartOfLine + then + this.inputIndicatorAtStartOfLine = true + print(s"[$timeLeft s]> ") + + if gameEvent.playerCanAct && lineToSing.isEmpty then + print(this.timeIndicatorUpdater(timeLeft)) + + end printGameEvent + + /** Prints the given string with a trailing newline added. Should be used + * instead of ordinary println because printing outside of the Printer + * might cause weird-looking output. + * + * @param s the line to print + */ + def printLn(s: String): Unit = + if isPrintable(s) then + println(s) + else + println("Virhe: epätavallinen merkki havaittu tulosteessa.") + this.inputIndicatorAtStartOfLine = false + + private def timeIndicatorUpdater(t: String): String = + s"\u001b7\u001b[0E[$t s]> \u001b8" + |