aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Server
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalevalapokalypsi/Server')
-rw-r--r--src/scalevalapokalypsi/Server/Client.scala43
-rw-r--r--src/scalevalapokalypsi/Server/Server.scala24
2 files changed, 40 insertions, 27 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
diff --git a/src/scalevalapokalypsi/Server/Server.scala b/src/scalevalapokalypsi/Server/Server.scala
index 609b581..bfb0893 100644
--- a/src/scalevalapokalypsi/Server/Server.scala
+++ b/src/scalevalapokalypsi/Server/Server.scala
@@ -3,7 +3,7 @@ package scalevalapokalypsi.Server
// TODO: TLS/SSL / import javax.net.ssl.SSLServerSocketFactory
-import scalevalapokalypsi.Model.Adventure
+import scalevalapokalypsi.Model.{Adventure, Event}
import scalevalapokalypsi.Model.Entities.Player
import scalevalapokalypsi.constants.*
import scalevalapokalypsi.utils.stringToByteArray
@@ -55,9 +55,9 @@ class Server(
this.makeClientsSing()
this.writeObservations()
if this.canExecuteTurns then
- this.clients.inRandomOrder(_.act())
- this.writeClientDataToClients()
- this.writeObservations()
+ this.clients.foreach(_.giveTurn())
+ //this.writeClientDataToClients()
+ //this.writeObservations()
this.clients.foreach(c =>
this.writeToClient(this.turnStartInfo(c), c)
)
@@ -100,12 +100,14 @@ class Server(
s"$timeLimit\r\n${this.turnStartInfo(c)}", c
)
- this.clients.foreach(c =>
- if c.player != playerEntity then
- c.player.foreach(_.observeString(
- s"${name.getOrElse("Unknown player")} joins the game.")
- )
- )
+ val joinEvent = c.player.map(p => Event(
+ Map.from(Vector((p, ""))),
+ s"${p.name} joins the game."
+ ))
+ joinEvent.foreach(ev => this.clients.foreach(cl =>
+ if cl != c then
+ cl.player.foreach(_.observe(ev))
+ ))
private def writeObservations(): Unit =
@@ -137,7 +139,7 @@ class Server(
// to the game after everyone
// left and everything is just
// as before!
- val allPlayersReady = this.clients.forall(_.isReadyToAct)
+ val allPlayersReady = this.clients.forall(_.hasActed)
val requirement3 = (allPlayersReady
|| currentTimeMillis() / 1000 >= previousTurn + timeLimit)
requirement1 && requirement2 && requirement3