aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/UI/Printer.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalevalapokalypsi/UI/Printer.scala')
-rw-r--r--src/scalevalapokalypsi/UI/Printer.scala78
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"
+