aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Server/Client.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 22:52:25 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 22:52:25 +0200
commit239571e3408a3187953bef1dd5d516461bad0e31 (patch)
tree67c062a635affc90c08a92ac61adfed2445985d8 /src/main/scala/Server/Client.scala
parentdef8975617e5c6da431e41cc889d167b0f2e2bb0 (diff)
downloadscalevalapokalypsi-239571e3408a3187953bef1dd5d516461bad0e31.tar.gz
scalevalapokalypsi-239571e3408a3187953bef1dd5d516461bad0e31.zip
Added turns, time limits and turn order randomization (feature/bug?)
Diffstat (limited to 'src/main/scala/Server/Client.scala')
-rw-r--r--src/main/scala/Server/Client.scala47
1 files changed, 36 insertions, 11 deletions
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