aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Server/Server.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-15 16:45:09 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-15 16:45:09 +0200
commiteeb83ca379e7f4ab1a86596b80e206df48371454 (patch)
tree0f595308b7ba9077650e8a368b94ba75c5683c71 /src/main/scala/Server/Server.scala
parentea18a265a22ffc4c3f6ec3ca9d2f542552da9705 (diff)
downloadscalevalapokalypsi-eeb83ca379e7f4ab1a86596b80e206df48371454.tar.gz
scalevalapokalypsi-eeb83ca379e7f4ab1a86596b80e206df48371454.zip
Added observations for Players in model & implemented sending them to other clients
Diffstat (limited to 'src/main/scala/Server/Server.scala')
-rw-r--r--src/main/scala/Server/Server.scala21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/main/scala/Server/Server.scala b/src/main/scala/Server/Server.scala
index 5eb15cb..ac0e010 100644
--- a/src/main/scala/Server/Server.scala
+++ b/src/main/scala/Server/Server.scala
@@ -7,8 +7,7 @@ import java.lang.Thread.{currentThread, sleep}
import java.io.IOException
import java.net.{ServerSocket, Socket}
import o1game.constants.*
-import o1game.Model.Adventure
-import o1game.Model.Entity
+import o1game.Model.{Adventure,Entity,Player}
import o1game.utils.stringToByteArray
import java.lang.System.currentTimeMillis
@@ -53,9 +52,11 @@ class Server(
this.readFromAll()
this.clients.foreach(_.interpretData())
this.writeClientDataToClients()
+ this.writeObservations()
if this.canExecuteTurns then
this.clients.inRandomOrder(_.act())
this.writeClientDataToClients()
+ this.writeObservations()
this.clients.foreach(c =>
this.writeToClient(this.turnStartInfo(c), c)
)
@@ -83,17 +84,25 @@ class Server(
c.gameStart()
val name = c.getName
- val entity: Option[Entity] = name match
+ val playerEntity: Option[Player] = name match
case Some(n) => this.adventure match
case Some(a) => a.getPlayer(n)
case None => None
case None => None
- entity.foreach(c.giveEntity(_))
+ playerEntity.foreach(c.givePlayer(_))
this.writeToClient(
s"$timeLimit\r\n${this.turnStartInfo(c)}", c
)
+
+ private def writeObservations(): Unit =
+ this.clients.foreach(c =>
+ val observations = c.player.map(_.readAndClearObservations())
+// if observations.filter(_.length > 0).isDefined then
+// println(s"Observations of $c: ```$observations```")
+ observations.foreach(_.foreach((s: String) => this.writeToClient(s"$s\r\n", c)))
+ )
/** Helper function to determine if the next turn can be taken */
private def canExecuteTurns: Boolean =
@@ -121,7 +130,7 @@ class Server(
false
private def turnStartInfo(client: Client): String =
- val clientArea = client.entity.map(_.location)
+ val clientArea = client.player.map(_.location)
val areaDesc = clientArea
.map(_.description)
.getOrElse("You are floating in the middle of a soothing void.")
@@ -131,7 +140,7 @@ class Server(
val items = clientArea
.map(_.getItemNames.mkString(LIST_SEPARATOR))
.getOrElse("")
- val entities = client.entity.map(c =>
+ val entities = client.player.map(c =>
c.location
.getEntityNames
.filter(c.name != _)