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