From 239571e3408a3187953bef1dd5d516461bad0e31 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 7 Nov 2024 22:52:25 +0200 Subject: Added turns, time limits and turn order randomization (feature/bug?) --- src/main/scala/Server/Client.scala | 47 +++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'src/main/scala/Server/Client.scala') diff --git a/src/main/scala/Server/Client.scala b/src/main/scala/Server/Client.scala index d9fa684..e557c22 100644 --- a/src/main/scala/Server/Client.scala +++ b/src/main/scala/Server/Client.scala @@ -16,6 +16,7 @@ class Client(val socket: Socket): private var character: Option[Entity] = None private var protocolIsIntact = true private var name: Option[String] = None + private var nextAction: Option[Action] = None /** Calculates the amount of bytes available for future incoming messages */ def spaceAvailable: Int = MAX_MSG_SIZE - incompleteMessageIndex @@ -106,12 +107,22 @@ class Client(val socket: Socket): else None - /** Causes the client to take the actions it has received */ + /** Makes the client play its turn */ + def act(): Unit = + this.nextAction.foreach(this.executeAction(_)) + this.nextAction = None + + /** Checks whether the client has chosen its next action + * + * @return whether the client is ready to act */ + def isReadyToAct: Boolean = this.nextAction.isDefined + + /** Causes the client to interpret the data it has received */ def interpretData(): Unit = LazyList.continually(this.nextLine()) .takeWhile(_.isDefined) .flatten - .foreach(s => takeAction(s)) + .foreach(s => interpretLine(s)) /** Makes the client execute the action specified by `line`. * If there is a protocol error, the function changes @@ -119,7 +130,7 @@ class Client(val socket: Socket): * * @param line the line to interpret */ - private def takeAction(line: String): Unit = + private def interpretLine(line: String): Unit = this.protocolIsIntact = this.protocolState match case WaitingForVersion => if line == GAME_VERSION then @@ -135,15 +146,29 @@ class Client(val socket: Socket): true case WaitingForGameStart => true case InGame => - println(line) - val action = Action(line) - 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(_)) + this.bufferAction(Action(line)) true + /** Buffers the action for execution or executes it immediately if it + * doesn't take a turn */ + private def bufferAction(action: Action) = + if ( + this.nextAction.isEmpty && + 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 */ + 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 -- cgit v1.2.3