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"