aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/UI/Printer.scala
blob: a33864f2447ecf84ec399c0b6520b6827d7ac6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"