aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server/Client.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 17:06:56 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 17:06:56 +0200
commitc954ca4d1ec677a34a6d787a23f9d01396f7e585 (patch)
treec6b00b5046bde3a98c18f9557198f852b4ce9d46 /src/scalevalapokalypsi/Server/Client.scala
parenta6b0330c845d4edad87c7059bac56e194a276c6f (diff)
downloadscalevalapokalypsi-c954ca4d1ec677a34a6d787a23f9d01396f7e585.tar.gz
scalevalapokalypsi-c954ca4d1ec677a34a6d787a23f9d01396f7e585.zip
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
Diffstat (limited to 'src/scalevalapokalypsi/Server/Client.scala')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/scalevalapokalypsi/Server/Client.scala b/src/scalevalapokalypsi/Server/Client.scala
index 8716ca9..ceeff1f 100644
--- a/src/scalevalapokalypsi/Server/Client.scala
+++ b/src/scalevalapokalypsi/Server/Client.scala
@@ -1,11 +1,12 @@
package scalevalapokalypsi.Server
import java.net.Socket
-import scala.math.min
+import scala.math.{min,max}
import scalevalapokalypsi.constants.*
import ServerProtocolState.*
import scalevalapokalypsi.Model.Action
import scalevalapokalypsi.Model.Entities.Player
+import java.lang.System.currentTimeMillis
class Client(val socket: Socket):
private var incompleteMessage: Array[Byte] =
@@ -17,6 +18,11 @@ class Client(val socket: Socket):
private var protocolIsIntact = true
private var name: Option[String] = None
private var nextAction: Option[Action] = None
+ private var singStartTime: Option[Long] = None
+
+ def clientHasSong = this.singStartTime.isDefined
+ def startSong(): Unit =
+ this.singStartTime = Some(currentTimeMillis() / 1000)
/** Calculates the amount of bytes available for future incoming messages */
def spaceAvailable: Int = MAX_MSG_SIZE - incompleteMessageIndex
@@ -109,7 +115,6 @@ class Client(val socket: Socket):
/** Makes the client play its turn */
def act(): Unit =
- this.addDataToSend(ACTION_BLOCKING_INDICATOR.toString)
this.nextAction.foreach(a => this.addDataToSend(
s"$ACTION_BLOCKING_INDICATOR${this.executeAction(a)}"
))
@@ -161,7 +166,19 @@ class Client(val socket: Socket):
) then
this.nextAction = Some(action)
else if this.nextAction.isEmpty then
- this.addDataToSend(s"$ACTION_NONBLOCKING_INDICATOR${this.executeAction(action)}")
+ this.singStartTime match
+ case Some(t) =>
+ val timePassed = currentTimeMillis()/1000 - t
+ this.player.flatMap(_.applySingEffect(
+ 5 / max(5, timePassed)
+ )).foreach(s => this.player.foreach((c: Player) =>
+ c.observe(s"Lakkaat laulamasta.\n$s")
+ ))
+ this.singStartTime = None
+ case None =>
+ this.addDataToSend(
+ s"$ACTION_NONBLOCKING_INDICATOR${this.executeAction(action)}"
+ )
/** Executes the specified action and returns its description */
private def executeAction(action: Action): String =