From fe2543627bcec1ea0f7a429bede20ca293458ba9 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 18 Nov 2024 02:01:12 +0200 Subject: 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. --- src/scalevalapokalypsi/Server/Server.scala | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/scalevalapokalypsi/Server/Server.scala') 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 -- cgit v1.2.3