From c954ca4d1ec677a34a6d787a23f9d01396f7e585 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 17 Nov 2024 17:06:56 +0200 Subject: Template for singing, WIP. * The line to sing is always the same. * The client recovers weirdly from singing before the next turn and my brain is currently too fried to figure out why --- src/scalevalapokalypsi/Client/Client.scala | 52 +++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'src/scalevalapokalypsi/Client/Client.scala') diff --git a/src/scalevalapokalypsi/Client/Client.scala b/src/scalevalapokalypsi/Client/Client.scala index 41b1003..f37b1cc 100644 --- a/src/scalevalapokalypsi/Client/Client.scala +++ b/src/scalevalapokalypsi/Client/Client.scala @@ -18,7 +18,7 @@ import java.lang.System.currentTimeMillis */ enum ServerLineState: case WaitingForTimeLimit, - ActionDescription, + ActionsAndSong, TurnIndicator, AreaDescription, Directions, @@ -29,8 +29,8 @@ enum ServerLineState: /** Creates a new client. * * @param name the name the client and its player should have - * @ip the ip of the server to connect to - * @port the port of the server to connect to + * @param ip the ip of the server to connect to + * @param port the port of the server to connect to * @return the client created, if all was successful */ def newClient(name: String, ip: String, port: Int): Option[Client] = @@ -66,10 +66,12 @@ class Client(socket: Socket): private var serverLineState = ServerLineState.WaitingForTimeLimit /** Variables about the status of the current turn for the client */ - private var canAct = false + private var canAct = false // TODO: is really never true when it should private var timeLimit: Long = 0 private var lastTurnStart: Long = 0 private var lastExecutedTurn: Long = 0 + private var isSinging: Boolean = false + private val bufferedActions: Buffer[String] = Buffer.empty assert( lastTurnStart <= lastExecutedTurn, "don't initialize with unexecuted turn" @@ -83,14 +85,23 @@ class Client(socket: Socket): stdinReader.startReading() while true do + sleep(POLL_INTERVAL) this.readAndParseDataFromServer() - if this.lastExecutedTurn < this.lastTurnStart then + this.displayActions() + + if + this.lastExecutedTurn < this.lastTurnStart && + !this.isSinging + then print(this.giveTurn()) + // TODO: we probably want to quit at EOF stdinReader.newLine().foreach((s: String) => + println("not singing anymore!") + this.isSinging = false output.write(stringToByteArray(s+"\r\n")) ) @@ -111,11 +122,21 @@ class Client(socket: Socket): this.lastExecutedTurn = currentTimeMillis / 1000 s"\n\n${this.turnInfo}\n${this.actionGetterIndicator}" - private def displayAction(action: String): Unit = - println(s"$action") - if this.canAct then + private def bufferAction(action: String): Unit = + this.bufferedActions += action + + private def displayActions(): Unit = + val somethingToShow = this.bufferedActions.nonEmpty + if !this.isSinging then + this.bufferedActions.foreach(println(_)) + this.bufferedActions.clear() + if !this.isSinging && this.canAct && somethingToShow then print(this.actionGetterIndicator) + private def startSong(verse: String): Unit = + this.isSinging = true + print(s"\nLaula: “$verse”\n> ") + private def actionGetterIndicator = val timeOfTurnEnd = this.lastTurnStart + this.timeLimit val timeToTurnEnd = -currentTimeMillis()/1000 + timeOfTurnEnd @@ -148,10 +169,17 @@ class Client(socket: Socket): this.serverLineState = ServerLineState.TurnIndicator this.lastTurnStart = currentTimeMillis / 1000 - case ServerLineState.ActionDescription => - if line.nonEmpty && line.head == ACTION_BLOCKING_INDICATOR then + case ServerLineState.ActionsAndSong => + if line.headOption.exists(_.toString == SING_INDICATOR) then + this.startSong(line.tail) + this.canAct = false + else if line.headOption.contains(ACTION_BLOCKING_INDICATOR) then this.canAct = false - this.displayAction(line.tail) + this.bufferAction(line.tail) + else if line.nonEmpty then + this.bufferAction((line.tail)) + else + println("We should not get empty lines from the server!") case ServerLineState.TurnIndicator => this.serverLineState = ServerLineState.AreaDescription @@ -170,7 +198,7 @@ class Client(socket: Socket): case ServerLineState.Entities => this.turnInfo.visibleEntities = line.split(LIST_SEPARATOR) - this.serverLineState = ServerLineState.ActionDescription + this.serverLineState = ServerLineState.ActionsAndSong this.lastTurnStart = currentTimeMillis() / 1000 end parseLineFromServer -- cgit v1.2.3