diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 01:17:58 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 01:17:58 +0200 |
commit | fdaec6534eb7ee902c75be3e4b732b6970abd859 (patch) | |
tree | f323d11dc1a33a93ee44417a58df62ef5f9ad98a /src/main/scala/Server/Server.scala | |
parent | 12cbf4d451d1002b8872b0028acbe6bd886ca9bd (diff) | |
download | scalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.tar.gz scalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.zip |
Made the server work with the adventure model
* The model was imported from the wrong version, so that needs to be fixed.
* The client side doesn't work at all right now. Use netcat for testing.
* There are inconveniences and bugs in the model (eg. it lists the player among entities)
* Players can just see each other, not interact in any way
But it's a good base.
Diffstat (limited to 'src/main/scala/Server/Server.scala')
-rw-r--r-- | src/main/scala/Server/Server.scala | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/src/main/scala/Server/Server.scala b/src/main/scala/Server/Server.scala index 1f0cbfc..c0a76ca 100644 --- a/src/main/scala/Server/Server.scala +++ b/src/main/scala/Server/Server.scala @@ -7,34 +7,56 @@ import java.io.IOException import java.lang.Thread.sleep import java.net.{ServerSocket, Socket} import o1game.constants.* +import o1game.Model.Adventure +import o1game.Model.Entity +import scala.collection.mutable.Map + +def stringToByteArray(str: String): Array[Byte] = + str.toVector.map(_.toByte).toArray /** `Server` exists to initialize a server for the game * and run it with its method `startServer`. */ -class Server(port: Int, maxClients: Int): +class Server(port: Int, maxClients: Int, val timeLimit: Int): private val socket = ServerSocket(port) private val clientGetter = ConnectionGetter(socket) private val clients: Clients = Clients(maxClients) private val buffer: Array[Byte] = Array.ofDim(1024) private var bufferIndex = 0 + private var adventure: Option[Adventure] = None /** Starts the server. Won't terminate under normal circumstances. */ def startServer(): Unit = while true do sleep(POLL_INTERVAL) this.receiveNewClient() - this.writeToAll("Test message.") this.readFromAll() - clients.foreach(_.executeActions()) + this.writeClientDataToClients() + this.clients.removeNonCompliant() + this.clients.foreach(_.interpretData()) + if this.adventure.isEmpty && !this.clients.isEmpty && this.clients.forall(_.isReadyForGameStart) then + this.adventure = Some(Adventure(this.clients.names)) + this.clients.foreach( c => + c.gameStart() + val name = c.getName + val entity: Option[Entity] = name match + case Some(n) => this.adventure match + case Some(a) => a.getPlayer(n) + case None => None + case None => None + entity.map(c.giveEntity(_)) + ) + this.writeToAll(s"$timeLimit") + /** Receives a new client and stores it in `clients`. * * @return describes if a client was added */ private def receiveNewClient(): Boolean = - clientGetter.newClient() match + this.clientGetter.newClient() match case Some(c) => - clients.addClient(Client(c, None)) + clients.addClient(Client(c)) true case None => false @@ -44,12 +66,21 @@ class Server(port: Int, maxClients: Int): * @param message the message to send */ private def writeToAll(message: String): Unit = - clients.mapAndRemove(c => + this.clients.mapAndRemove(c => val output = c.socket.getOutputStream output.write(message.toVector.map(_.toByte).toArray) output.flush() ) + /** Sends every client's `dataToThisClient` to the client */ + private def writeClientDataToClients(): Unit = + this.clients.mapAndRemove(c => + val output = c.socket.getOutputStream + val data = c.dataToThisClient() + output.write(data.toVector.map(_.toByte).toArray) + output.flush() + ) + /** Reads data sent by clients and stores it in the `Client`s of `clients` */ private def readFromAll(): Unit = clients.mapAndRemove(c => |