aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server/Client.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-18 02:01:12 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-18 02:01:12 +0200
commitfe2543627bcec1ea0f7a429bede20ca293458ba9 (patch)
tree42c9630d65292c90bee37444fde14fcf99cc3ffe /src/scalevalapokalypsi/Server/Client.scala
parenta98f089035dbcc94c14c9cd6246c3150bee84241 (diff)
downloadscalevalapokalypsi-fe2543627bcec1ea0f7a429bede20ca293458ba9.tar.gz
scalevalapokalypsi-fe2543627bcec1ea0f7a429bede20ca293458ba9.zip
Major change! Changed Events to have multiple first persons & changed sending action results to clients using this. This improves code and made it easy to finally make arrival/leaving messages when going from one area to another!
This commit is too big indeed, and there are probably bugs. I'm annoyed we didn't set up unit testing during the course. I should've taken my time to set up IDEA myself for that. Now the bugs will just have to be fixed in future commits.
Diffstat (limited to 'src/scalevalapokalypsi/Server/Client.scala')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala43
1 files changed, 27 insertions, 16 deletions
diff --git a/src/scalevalapokalypsi/Server/Client.scala b/src/scalevalapokalypsi/Server/Client.scala
index 1af83bf..17c3777 100644
--- a/src/scalevalapokalypsi/Server/Client.scala
+++ b/src/scalevalapokalypsi/Server/Client.scala
@@ -18,6 +18,7 @@ class Client(val socket: Socket):
private var protocolIsIntact = true
private var name: Option[String] = None
private var nextAction: Option[Action] = None
+ private var turnUsed = false
private var singStartTime: Option[Long] = None
def clientHasSong = this.singStartTime.isDefined
@@ -114,16 +115,13 @@ class Client(val socket: Socket):
None
/** Makes the client play its turn */
- def act(): Unit =
- this.nextAction.foreach(a => this.addDataToSend(
- s"$ACTION_BLOCKING_INDICATOR${this.executeAction(a)}"
- ))
- this.nextAction = None
+ def giveTurn(): Unit =
+ this.turnUsed = false
/** Checks whether the client has chosen its next action
*
* @return whether the client is ready to act */
- def isReadyToAct: Boolean = this.nextAction.isDefined
+ def hasActed: Boolean = this.turnUsed
/** Causes the client to interpret the data it has received */
def interpretData(): Unit =
@@ -154,12 +152,31 @@ class Client(val socket: Socket):
true
case WaitingForGameStart => true
case InGame =>
- this.bufferAction(Action(line))
+ this.executeLine(line)
true
/** Buffers the action for execution or executes it immediately if it
* doesn't take a turn */
- private def bufferAction(action: Action) =
+ private def executeLine(line: String) =
+ if !this.turnUsed then
+ this.singStartTime match
+ case Some(t) =>
+ val timePassed = currentTimeMillis()/1000 - t
+ this.player.foreach(_.applySingEffect(
+ 1 / max(5, timePassed) * 5
+ ))
+ this.singStartTime = None
+ case None =>
+ val action = Action(line)
+ val takesATurn = this.character.exists(p => action.execute(p))
+ if takesATurn then
+ this.addDataToSend(s"$ACTION_BLOCKING_INDICATOR")
+ this.turnUsed = true
+
+ /*
+ val takesATurn = this.character.exists(action.execute(_))
+ if takesATurn then
+ this.addDataToSend(s"$ACTION_BLOCKING_INDICATOR")
if (
this.nextAction.isEmpty &&
this.player.exists(action.takesATurnFor(_))
@@ -170,19 +187,13 @@ class Client(val socket: Socket):
case Some(t) =>
val timePassed = currentTimeMillis()/1000 - t
this.player.foreach(_.applySingEffect(
- 5 / max(5, timePassed)
+ 1 / max(5, timePassed) * 5
))
this.singStartTime = None
case None =>
this.addDataToSend(
s"$ACTION_NONBLOCKING_INDICATOR${this.executeAction(action)}"
- )
-
- /** Executes the specified action and returns its description */
- private def executeAction(action: Action): String =
- this.character.flatMap(action.execute(_)) match
- case Some(s) => s
- case None => "You can't do that"
+ )*/
end Client