diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 21:00:04 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 21:00:04 +0200 |
commit | 891b6f877ba848a3f78851a757734ebd1f066798 (patch) | |
tree | 849d4f9bac3b3e83f84b6319d7148f93f5a44c06 /src/main/scala/Server/Client.scala | |
parent | fdaec6534eb7ee902c75be3e4b732b6970abd859 (diff) | |
download | scalevalapokalypsi-891b6f877ba848a3f78851a757734ebd1f066798.tar.gz scalevalapokalypsi-891b6f877ba848a3f78851a757734ebd1f066798.zip |
Added comments for documentation and reformatted old ones
Diffstat (limited to 'src/main/scala/Server/Client.scala')
-rw-r--r-- | src/main/scala/Server/Client.scala | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/src/main/scala/Server/Client.scala b/src/main/scala/Server/Client.scala index 1e5dd00..d9bb529 100644 --- a/src/main/scala/Server/Client.scala +++ b/src/main/scala/Server/Client.scala @@ -17,24 +17,53 @@ class Client(val socket: Socket): private var protocolIsIntact = true private var name: Option[String] = None - /** Calculates the amount of bytes available for future incoming messages - */ + /** Calculates the amount of bytes available for future incoming messages */ def spaceAvailable: Int = MAX_MSG_SIZE - incompleteMessageIndex + /** Tests whether the client has behaved according to protocol. + * + * @return false if there has been a protocol violation, true otherwise + */ def isIntactProtocolWise: Boolean = protocolIsIntact + + /** Tests whether this client is initialized and ready to start the game + * + * @return true if the client is ready to join the game + */ def isReadyForGameStart: Boolean = this.protocolState == WaitingForGameStart + + /** Signals this client that it's joining the game. This is important so + * that this object knows to update its protocol state. + */ def gameStart(): Unit = this.protocolState = InGame + + /** Returns the entity this client controls in the model. + * + * @return an option containing the entity + */ def entity: Option[Entity] = this.character + + /** Tells this client object that it controls the specified entity. + * + * @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 + * that is given to this client. Not very useful if the client hasn't yet + * received the name or if it already has an entity. + * + * @return the name of this client + */ def getName: Option[String] = this.name /** Sets `data` as received for the client. - * - * @return false means there was not enough space to receive the message - */ + * + * @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) @@ -44,8 +73,8 @@ class Client(val socket: Socket): data.length < spaceAvailable /** Returns data that should be sent to this client. - * The data is cleared when calling. - */ + * The data is cleared when calling. + */ def dataToThisClient(): String = val a = this.outData this.outData = "" @@ -61,7 +90,7 @@ class Client(val socket: Socket): /** Returns one line of data if there are any line breaks. - * Removes the parsed data from the message buffering area. + * Removes the parsed data from the message buffering area. */ private def nextLine(): Option[String] = val nextLF = this.incompleteMessage.indexOf(LF) @@ -74,8 +103,7 @@ class Client(val socket: Socket): else None - /** Causes the client to take the actions it has received - */ + /** Causes the client to take the actions it has received */ def interpretData(): Unit = LazyList.continually(this.nextLine()) .takeWhile(_.isDefined) @@ -83,11 +111,11 @@ class Client(val socket: Socket): .foreach(s => takeAction(s)) /** Makes the client execute the action specified by `line`. - * If there is a protocol error, the function changes - * the variable `protocolIsIntact` to false. - * - * @param line the line to interpret - */ + * If there is a protocol error, the function changes + * the variable `protocolIsIntact` to false. + * + * @param line the line to interpret + */ private def takeAction(line: String): Unit = this.protocolIsIntact = this.protocolState match case WaitingForVersion => @@ -109,8 +137,9 @@ class Client(val socket: Socket): 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) match - case Some(s) => this.addDataToSend(s) + this.character + .map(_.location.fullDescription) + .foreach(this.addDataToSend(_)) true end Client |