diff options
Diffstat (limited to 'src/scalevalapokalypsi/Server/Client.scala')
-rw-r--r-- | src/scalevalapokalypsi/Server/Client.scala | 23 |
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 = |