aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server
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
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')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala23
-rw-r--r--src/scalevalapokalypsi/Server/Clients.scala3
-rw-r--r--src/scalevalapokalypsi/Server/Server.scala21
3 files changed, 42 insertions, 5 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 =
diff --git a/src/scalevalapokalypsi/Server/Clients.scala b/src/scalevalapokalypsi/Server/Clients.scala
index 377050d..9ad0e84 100644
--- a/src/scalevalapokalypsi/Server/Clients.scala
+++ b/src/scalevalapokalypsi/Server/Clients.scala
@@ -25,6 +25,9 @@ class Clients(maxClients: Int):
* @return an iterable of all the clients
*/
def allClients: Iterable[Client] = clients.toVector.flatten
+
+ def filter(p: Client => Boolean): Iterable[Client] =
+ this.allClients.filter(p)
/** Applies the function `f` to all the clients for its side effects. */
def foreach(f: Client => Any): Unit = this.clients.flatten.foreach(f)
diff --git a/src/scalevalapokalypsi/Server/Server.scala b/src/scalevalapokalypsi/Server/Server.scala
index f18d5c0..db30283 100644
--- a/src/scalevalapokalypsi/Server/Server.scala
+++ b/src/scalevalapokalypsi/Server/Server.scala
@@ -52,6 +52,7 @@ class Server(
this.readFromAll()
this.clients.foreach(_.interpretData())
this.writeClientDataToClients()
+ this.makeClientsSing()
this.writeObservations()
if this.canExecuteTurns then
this.clients.inRandomOrder(_.act())
@@ -68,7 +69,11 @@ class Server(
)
startGameForClient(c)
)
- else if this.adventure.isEmpty && !this.clients.isEmpty && this.clients.forall(_.isReadyForGameStart) then
+ else if
+ this.adventure.isEmpty &&
+ !this.clients.isEmpty &&
+ this.clients.forall(_.isReadyForGameStart)
+ then
this.adventure = Some(Adventure(this.clients.names))
this.clients.foreach(startGameForClient(_))
this.previousTurn = currentTimeMillis() / 1000
@@ -97,7 +102,9 @@ class Server(
this.clients.foreach(c =>
if c.player != playerEntity then
- c.player.foreach(_.observe(s"${name.getOrElse("Unknown player")} joins the game."))
+ c.player.foreach(_.observe(
+ s"${name.getOrElse("Unknown player")} joins the game.")
+ )
)
@@ -109,6 +116,16 @@ class Server(
)
)
+ private def makeClientsSing(): Unit =
+ this.clients.foreach(c =>
+ if c.player.exists(_.isSinging) && !c.clientHasSong then
+ this.writeToClient(
+ s"${SING_INDICATOR}Esimerkkirivi laulettavaksi, lirulirulei\r\n",
+ c
+ )
+ c.startSong()
+ )
+
/** Helper function to determine if the next turn can be taken */
private def canExecuteTurns: Boolean =
val requirement1 = this.adventure.isDefined