blob: e35784535b1dd1857d5850cc35ad488215f1659f (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package scalevalapokalypsi
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
import scala.io.StdIn.readLine
// TODO: add proper logic for starting the game
@main def main(): Unit =
print("How do you want to play?\n1) Host and join local game\n2) Join local game\n> ")
readLine().toIntOption match
case Some(1) =>
Thread(() => new Server(2267, 5, 30, true).startServer()).start()
println("Server started in background.")
print("Choose a name:\n> ")
val name = readLine()
Try(newClient(name, "127.0.0.1", 2267))
.toOption
.flatten match
case Some(client) =>
startClient(client)
case None =>
println("Starting the client failed.")
case Some(2) =>
print("Choose a name:\n> ")
val name = readLine()
Try(newClient(name, "127.0.0.1", 2267))
.toOption
.flatten match
case Some(client) =>
startClient(client)
case None =>
println("Starting the client failed.")
case _ => println("Invalid input")
def startClient(client: Client): Unit =
var hasQuit = false
val stdinReader = StdinLineReader()
stdinReader.startReading()
val printer = Printer()
while !hasQuit do
sleep(POLL_INTERVAL)
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 ||
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
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"
|