aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Server/Client.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-09 16:32:09 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-09 16:32:09 +0200
commit129b49a3876ceb68f271c311d5e45efb2e205300 (patch)
tree246a47a5990167c73b95fc6e02452de7f6aca6b3 /src/main/scala/Server/Client.scala
parent239571e3408a3187953bef1dd5d516461bad0e31 (diff)
downloadscalevalapokalypsi-129b49a3876ceb68f271c311d5e45efb2e205300.tar.gz
scalevalapokalypsi-129b49a3876ceb68f271c311d5e45efb2e205300.zip
Made & implemented clearer protocol, added client functionality
Diffstat (limited to 'src/main/scala/Server/Client.scala')
-rw-r--r--src/main/scala/Server/Client.scala19
1 files changed, 8 insertions, 11 deletions
diff --git a/src/main/scala/Server/Client.scala b/src/main/scala/Server/Client.scala
index e557c22..cd557c6 100644
--- a/src/main/scala/Server/Client.scala
+++ b/src/main/scala/Server/Client.scala
@@ -53,7 +53,6 @@ class Client(val socket: Socket):
* @param entity the entity this client is to control
*/
def giveEntity(entity: Entity): Unit =
- println(entity)
this.character = Some(entity)
/** Gets the name of this client, which should match the name of the entity
@@ -90,18 +89,19 @@ class Client(val socket: Socket):
* @param data data to buffer for sending
*/
private def addDataToSend(data: String): Unit =
- this.outData += s"$data\n"
+ this.outData += s"$data\r\n"
/** Returns one line of data if there are any line breaks.
* Removes the parsed data from the message buffering area.
*/
private def nextLine(): Option[String] =
- val nextLF = this.incompleteMessage.indexOf(LF)
- if nextLF != -1 then
- val message = this.incompleteMessage.take(nextLF)
- val rest = this.incompleteMessage.drop(nextLF + 1)
- this.incompleteMessage = rest ++ Array.fill(nextLF + 1)(0.toByte)
+ var nextCRLF = this.incompleteMessage.indexOf(CRLF(0))
+ if this.incompleteMessage(nextCRLF + 1) != CRLF(1) then nextCRLF = -1
+ if nextCRLF != -1 then
+ val message = this.incompleteMessage.take(nextCRLF)
+ val rest = this.incompleteMessage.drop(nextCRLF + 1)
+ this.incompleteMessage = rest ++ Array.fill(nextCRLF + 1)(0.toByte)
// TODO: the conversion may probably be exploited to crash the server
Some(String(message))
else
@@ -157,17 +157,14 @@ class Client(val socket: Socket):
this.entity.exists(action.takesATurnFor(_))
) then
this.nextAction = Some(action)
- this.addDataToSend("Waiting for everyone to end their turns...")
else if this.nextAction.isEmpty then
executeAction(action)
- /** Executes the specified action */
+ /** Executes the specified action and buffers its description for sending */
private def executeAction(action: Action) =
this.character.flatMap(action.execute(_)) match
case Some(s) => this.addDataToSend((s))
case None => this.addDataToSend("You can't do that")
- this.character.map(_.location.fullDescription)
- .foreach(this.addDataToSend(_))
end Client