aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server/Client.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-21 23:19:00 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-21 23:19:00 +0200
commit49985d1d11c426968fc298469671326aace96d00 (patch)
tree1d6eacf11e0791a93c216ecfeca4a5d71602f1ee /src/scalevalapokalypsi/Server/Client.scala
parent607b43a84d3bc8edffa05c722c7b8c3e6f72e964 (diff)
downloadscalevalapokalypsi-49985d1d11c426968fc298469671326aace96d00.tar.gz
scalevalapokalypsi-49985d1d11c426968fc298469671326aace96d00.zip
Fixed singing from last commit
Diffstat (limited to 'src/scalevalapokalypsi/Server/Client.scala')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala58
1 files changed, 25 insertions, 33 deletions
diff --git a/src/scalevalapokalypsi/Server/Client.scala b/src/scalevalapokalypsi/Server/Client.scala
index 17c3777..d689356 100644
--- a/src/scalevalapokalypsi/Server/Client.scala
+++ b/src/scalevalapokalypsi/Server/Client.scala
@@ -20,13 +20,15 @@ class Client(val socket: Socket):
private var nextAction: Option[Action] = None
private var turnUsed = false
private var singStartTime: Option[Long] = None
+ private var verseToSing: String = ""
def clientHasSong = this.singStartTime.isDefined
- def startSong(): Unit =
+ def startSong(verse: String): Unit =
+ this.verseToSing = verse
this.singStartTime = Some(currentTimeMillis() / 1000)
/** Calculates the amount of bytes available for future incoming messages */
- def spaceAvailable: Int = MAX_MSG_SIZE - incompleteMessageIndex
+ def spaceAvailable: Int = this.incompleteMessage.size - incompleteMessageIndex - 1
/** Tests whether the client has behaved according to protocol.
*
@@ -75,12 +77,13 @@ class Client(val socket: Socket):
* @return false means there was not enough space to receive the message
*/
def receiveData(data: Vector[Byte]): Boolean =
- for i <- 0 until min(data.length, spaceAvailable) do
- this.incompleteMessage(this.incompleteMessageIndex + i) = data(i)
- this.incompleteMessageIndex += data.length
- this.incompleteMessageIndex =
- min(this.incompleteMessageIndex, MAX_MSG_SIZE)
- data.length < spaceAvailable
+ if data.length > this.spaceAvailable then
+ false
+ else
+ for i <- 0 until min(data.length, this.spaceAvailable) do
+ this.incompleteMessage(this.incompleteMessageIndex+i) = data(i)
+ this.incompleteMessageIndex += data.length
+ true
/** Returns data that should be sent to this client.
* The data is cleared when calling.
@@ -109,6 +112,7 @@ class Client(val socket: Socket):
val message = this.incompleteMessage.take(nextCRLF)
val rest = this.incompleteMessage.drop(nextCRLF + 2)
this.incompleteMessage = rest ++ Array.fill(nextCRLF + 1)(0.toByte)
+ this.incompleteMessageIndex = 0
// TODO: the conversion may probably be exploited to crash the server
Some(String(message))
else
@@ -158,43 +162,31 @@ class Client(val socket: Socket):
/** Buffers the action for execution or executes it immediately if it
* doesn't take a turn */
private def executeLine(line: String) =
+
if !this.turnUsed then
this.singStartTime match
case Some(t) =>
+
val timePassed = currentTimeMillis()/1000 - t
- this.player.foreach(_.applySingEffect(
- 1 / max(5, timePassed) * 5
- ))
+
+ val quality = if line == this.verseToSing then
+ 5.0 / max(5.0, timePassed.toDouble)
+ else
+ 0.0
+
+ this.player.foreach(_.applySingEffect(quality.toFloat))
+
this.singStartTime = None
+
case None =>
+
val action = Action(line)
val takesATurn = this.character.exists(p => action.execute(p))
if takesATurn then
this.addDataToSend(s"$ACTION_BLOCKING_INDICATOR")
this.turnUsed = true
- /*
- val takesATurn = this.character.exists(action.execute(_))
- if takesATurn then
- this.addDataToSend(s"$ACTION_BLOCKING_INDICATOR")
- if (
- this.nextAction.isEmpty &&
- this.player.exists(action.takesATurnFor(_))
- ) then
- this.nextAction = Some(action)
- else if this.nextAction.isEmpty then
- this.singStartTime match
- case Some(t) =>
- val timePassed = currentTimeMillis()/1000 - t
- this.player.foreach(_.applySingEffect(
- 1 / max(5, timePassed) * 5
- ))
- this.singStartTime = None
- case None =>
- this.addDataToSend(
- s"$ACTION_NONBLOCKING_INDICATOR${this.executeAction(action)}"
- )*/
-
+ end executeLine
end Client