aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server/Client.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalevalapokalypsi/Server/Client.scala')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala64
1 files changed, 30 insertions, 34 deletions
diff --git a/src/scalevalapokalypsi/Server/Client.scala b/src/scalevalapokalypsi/Server/Client.scala
index 17c3777..5727fc6 100644
--- a/src/scalevalapokalypsi/Server/Client.scala
+++ b/src/scalevalapokalypsi/Server/Client.scala
@@ -3,6 +3,7 @@ package scalevalapokalypsi.Server
import java.net.Socket
import scala.math.{min,max}
import scalevalapokalypsi.constants.*
+import scalevalapokalypsi.utils.*
import ServerProtocolState.*
import scalevalapokalypsi.Model.Action
import scalevalapokalypsi.Model.Entities.Player
@@ -20,13 +21,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 +78,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,8 +113,8 @@ 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)
- // TODO: the conversion may probably be exploited to crash the server
- Some(String(message))
+ this.incompleteMessageIndex = 0
+ byteArrayToString(message)
else
None
@@ -158,43 +162,35 @@ 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 =>
+
+ case None if isPrintable(line) =>
+
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)}"
- )*/
+
+ () // There were some illegal chars but whatever
+ end executeLine
end Client