aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
commitfdaec6534eb7ee902c75be3e4b732b6970abd859 (patch)
treef323d11dc1a33a93ee44417a58df62ef5f9ad98a
parent12cbf4d451d1002b8872b0028acbe6bd886ca9bd (diff)
downloadscalevalapokalypsi-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.
-rw-r--r--src/main/scala/Model/Action.scala31
-rw-r--r--src/main/scala/Model/Adventure.scala56
-rw-r--r--src/main/scala/Model/Area.scala98
-rw-r--r--src/main/scala/Model/Entity.scala64
-rw-r--r--src/main/scala/Model/Item.scala18
-rw-r--r--src/main/scala/Server/Character.scala4
-rw-r--r--src/main/scala/Server/Client.scala122
-rw-r--r--src/main/scala/Server/Clients.scala70
-rw-r--r--src/main/scala/Server/Server.scala43
-rw-r--r--src/main/scala/Server/constants.scala7
-rw-r--r--src/main/scala/main.scala2
11 files changed, 451 insertions, 64 deletions
diff --git a/src/main/scala/Model/Action.scala b/src/main/scala/Model/Action.scala
new file mode 100644
index 0000000..84704ce
--- /dev/null
+++ b/src/main/scala/Model/Action.scala
@@ -0,0 +1,31 @@
+package o1game.Model
+
+/** The class `Action` represents actions that a player may take in a text adventure game.
+ * `Action` objects are constructed on the basis of textual commands and are, in effect,
+ * parsers for such commands. An action object is immutable after creation.
+ * @param input a textual in-game command such as “go east” or “rest” */
+class Action(input: String):
+
+ private val commandText = input.trim.toLowerCase
+ private val verb = commandText.takeWhile( _ != ' ' )
+ private val modifiers = commandText.drop(verb.length).trim
+
+ /** Causes the given player to take the action represented by this object, assuming
+ * that the command was understood. Returns a description of what happened as a result
+ * of the action (such as “You go west.”). The description is returned in an `Option`
+ * wrapper; if the command was not recognized, `None` is returned. */
+ def execute(actor: Entity): Option[String] =
+ this.verb match
+ case "go" => Some(actor.go(this.modifiers))
+ case "rest" => Some(actor.rest())
+ case "get" => Some(actor.pickUp(this.modifiers))
+ case "drop" => Some(actor.drop(this.modifiers))
+ case "xyzzy" => Some("The grue tastes yummy.")
+ case "quit" => Some(actor.quit())
+ case other => None
+
+ /** Returns a textual description of the action object, for debugging purposes. */
+ override def toString = s"$verb (modifiers: $modifiers)"
+
+end Action
+
diff --git a/src/main/scala/Model/Adventure.scala b/src/main/scala/Model/Adventure.scala
new file mode 100644
index 0000000..c13ff47
--- /dev/null
+++ b/src/main/scala/Model/Adventure.scala
@@ -0,0 +1,56 @@
+package o1game.Model
+
+import scala.collection.mutable.Map
+/** The class `Adventure` represents text adventure games. An adventure consists of a player and
+ * a number of areas that make up the game world. It provides methods for playing the game one
+ * turn at a time and for checking the state of the game.
+ *
+ * N.B. This version of the class has a lot of “hard-coded” information that pertains to a very
+ * specific adventure game that involves a small trip through a twisted forest. All newly created
+ * instances of class `Adventure` are identical to each other. To create other kinds of adventure
+ * games, you will need to modify or replace the source code of this class. */
+class Adventure(val playerNames: Vector[String]):
+
+ /** the name of the game */
+ val title = "A Forest Adventure"
+
+ /*private*/ val middle = Area("Forest", "You are somewhere in the forest. There are a lot of trees here.\nBirds are singing.")
+ /*private*/ val northForest = Area("Forest", "You are somewhere in the forest. A tangle of bushes blocks further passage north.\nBirds are singing.")
+ /*private*/ val southForest = Area("Forest", "The forest just goes on and on.")
+ /*private*/ val clearing = Area("Forest Clearing", "You are at a small clearing in the middle of forest.\nNearly invisible, twisted paths lead in many directions.")
+ /*private*/ val tangle = Area("Tangle of Bushes", "You are in a dense tangle of bushes. It's hard to see exactly where you're going.")
+ /*private*/ val home = Area("Home", "Home sweet home! Now the only thing you need is a working remote control.")
+ /*private*/ val destination = home
+
+ middle.setNeighbors(Vector("north" -> northForest, "east" -> tangle, "south" -> southForest, "west" -> clearing))
+ northForest.setNeighbors(Vector("east" -> tangle, "south" -> middle, "west" -> clearing))
+ southForest.setNeighbors(Vector("north" -> middle, "east" -> tangle, "south" -> southForest, "west" -> clearing))
+ clearing.setNeighbors(Vector("north" -> northForest, "east" -> middle, "south" -> southForest, "west" -> northForest))
+ tangle.setNeighbors(Vector("north" -> northForest, "east" -> home, "south" -> southForest, "west" -> northForest))
+ home.setNeighbors(Vector("west" -> tangle))
+
+ clearing.addItem(Item("battery", "It's a small battery cell. Looks new."))
+ southForest.addItem(Item(
+ "remote",
+ "It's the remote control for your TV.\n" +
+ "What it was doing in the forest, you have no idea.\n" +
+ "Problem is, there's no battery."
+ ))
+
+ val players: Map[String, Entity] = Map()
+ playerNames.foreach(name => players += name -> Entity(name, middle))
+ def getPlayer(name: String): Option[Entity] = this.players.get(name)
+
+ /** Returns a message that is to be displayed to the player at the beginning of the game. */
+ def welcomeMessage = "Generic welcome message"
+
+ /** Plays a turn by executing the given in-game command, such as “go west”. Returns a textual
+ * report of what happened, or an error message if the command was unknown. In the latter
+ * case, no turns elapse. */
+ def playTurnOfPlayer(playerName: String, command: String): Option[String] =
+ val action = Action(command)
+ val actor = this.players.get(playerName)
+ actor.flatMap(action.execute(_))
+
+end Adventure
+
diff --git a/src/main/scala/Model/Area.scala b/src/main/scala/Model/Area.scala
new file mode 100644
index 0000000..97c75bf
--- /dev/null
+++ b/src/main/scala/Model/Area.scala
@@ -0,0 +1,98 @@
+package o1game.Model
+
+import scala.collection.mutable.Map
+
+/** The class `Area` represents locations in a text adventure game world. A game world
+ * consists of areas. In general, an “area” can be pretty much anything: a room, a building,
+ * an acre of forest, or something completely different. What different areas have in
+ * common is that players can be located in them and that they can have exits leading to
+ * other, neighboring areas. An area also has a name and a description.
+ * @param name the name of the area
+ * @param description a basic description of the area (typically not including information about items) */
+class Area(val name: String, var description: String):
+
+ private val neighbors = Map[String, Area]()
+ private val items: Map[String, Item] = Map()
+ private val entities: Map[String, Entity] = Map()
+
+ /** Returns the area that can be reached from this area by moving in the given direction. The result
+ * is returned in an `Option`; `None` is returned if there is no exit in the given direction. */
+ def neighbor(direction: String) = this.neighbors.get(direction)
+
+ /** Adds an exit from this area to the given area. The neighboring area is reached by moving in
+ * the specified direction from this area. */
+ def setNeighbor(direction: String, neighbor: Area) =
+ this.neighbors += direction -> neighbor
+
+ /** Adds exits from this area to the given areas. Calling this method is equivalent to calling
+ * the `setNeighbor` method on each of the given direction–area pairs.
+ * @param exits contains pairs consisting of a direction and the neighboring area in that direction
+ * @see [[setNeighbor]] */
+ def setNeighbors(exits: Vector[(String, Area)]) =
+ this.neighbors ++= exits
+
+ /** Adds the specified item
+ *
+ * @param item the item to add
+ */
+ def addItem(item: Item): Unit = this.items += item.name -> item
+
+ /** Adds multiple items
+ *
+ * @param items a once iterable collection of items to add
+ */
+ def addItems(items: IterableOnce[Item]) =
+ items.iterator.foreach(i => this.items += i.name -> i)
+
+ /** Removes the specified item if it exists.
+ *
+ * @param itemName the name of the item to remove
+ * @return an option containing the removed item
+ */
+ def removeItem(itemName: String): Option[Item] =
+ this.items.remove(itemName)
+
+ /** Adds the specified entity to the area.
+ *
+ * @param entity the entity to add.
+ */
+ def addEntity(entity: Entity): Unit = this.entities += entity.name -> entity
+
+ /** Removes the entity with the name `entityName`.
+ *
+ * @param entityName the name of the entity to remove
+ * @return an option containing the removed entity if it was in the area
+ */
+ def removeEntity(entityName: String): Option[Entity] =
+ this.entities.remove(entityName)
+
+ /** Returns a multi-line description of the area as a player sees it. This includes a basic
+ * description of the area as well as information about exits and items. If there are no
+ * items present, the return value has the form "DESCRIPTION\n\nExits available:
+ * DIRECTIONS SEPARATED BY SPACES". If there are one or more items present, the return
+ * value has the form "DESCRIPTION\nYou see here: ITEMS SEPARATED BY SPACES\n\nExits available:
+ * DIRECTIONS SEPARATED BY SPACES". The items and directions are listed in an arbitrary order. */
+ def fullDescription: String =
+ val exitList = this.neighbors.keys.mkString(" ")
+ val itemList = this.items.keys.mkString(" ")
+ val entityList = this.entities.keys.mkString(" ")
+ val itemDescription =
+ if this.items.nonEmpty then
+ s"\nYou see here: ${itemList}"
+ else ""
+ val entityDescription =
+ if this.entities.nonEmpty then
+ s"\nThere are entities: ${entityList}"
+ else ""
+ (this.description +
+ itemDescription +
+ entityDescription +
+ s"\n\nExits available: $exitList")
+
+
+ /** Returns a single-line description of the area for debugging purposes. */
+ override def toString =
+ this.name + ": " + this.description.replaceAll("\n", " ").take(150)
+
+end Area
+
diff --git a/src/main/scala/Model/Entity.scala b/src/main/scala/Model/Entity.scala
new file mode 100644
index 0000000..eb19606
--- /dev/null
+++ b/src/main/scala/Model/Entity.scala
@@ -0,0 +1,64 @@
+package o1game.Model
+
+import scala.collection.mutable.Map
+
+/** A `Player` object represents a player character controlled by the real-life user
+ * of the program.
+ *
+ * A player object’s state is mutable: the player’s location and possessions can change,
+ * for instance.
+ *
+ * @param startingArea the player’s initial location */
+class Entity(val name: String, initialLocation: Area):
+ private var currentLocation: Area = initialLocation
+ private var quitCommandGiven = false // one-way flag
+ private val inventory: Map[String, Item] = Map()
+
+ /** Determines if the player has indicated a desire to quit the game. */
+ def hasQuit = this.quitCommandGiven // TODO: This is probably unneccessary?
+
+ /** Returns the player’s current location. */
+ def location = this.currentLocation
+
+ /** Attempts to move the player in the given direction. This is successful if there
+ * is an exit from the player’s current location towards the direction name. Returns
+ * a description of the result: "You go DIRECTION." or "You can't go DIRECTION." */
+ def go(direction: String) =
+ val destination = this.location.neighbor(direction)
+ if destination.isDefined then
+ this.currentLocation.removeEntity(this.name)
+ this.currentLocation = destination.getOrElse(this.currentLocation)
+ destination.foreach(_.addEntity(this))
+ s"You go $direction."
+ else
+ "You can't go " + direction + "."
+
+ def pickUp(itemName: String): String =
+ this.currentLocation.removeItem(itemName) match
+ case Some(i) =>
+ this.inventory += i.name -> i
+ s"You pick up the ${i.name}"
+ case None => s"There is no $itemName here to pick up."
+
+ def drop(itemName: String): String =
+ this.inventory.remove(itemName) match
+ case Some(item) =>
+ this.currentLocation.addItem(item)
+ s"You drop the $itemName"
+ case None => "You don't have that!"
+
+ /** Causes the player to rest for a short while (this has no substantial effect in game terms).
+ * Returns a description of what happened. */
+ def rest() =
+ "You rest for a while. Better get a move on, though."
+
+ /** Signals that the player wants to quit the game. Returns a description of what happened within
+ * the game as a result (which is the empty string, in this case). */
+ def quit() =
+ this.quitCommandGiven = true
+ ""
+
+ /** Returns a brief description of the player’s state, for debugging purposes. */
+ override def toString = "Now at: " + this.location.name
+
+end Entity
diff --git a/src/main/scala/Model/Item.scala b/src/main/scala/Model/Item.scala
new file mode 100644
index 0000000..3809561
--- /dev/null
+++ b/src/main/scala/Model/Item.scala
@@ -0,0 +1,18 @@
+package o1game.Model
+
+/** The class `Item` represents items in a text adventure game. Each item has a name
+ * and a longer description. (In later versions of the adventure game, items may
+ * have other features as well.)
+ *
+ * N.B. It is assumed, but not enforced by this class, that items have unique names.
+ * That is, no two items in a game world have the same name.
+ *
+ * @param name the item’s name
+ * @param description the item’s description */
+class Item(val name: String, val description: String):
+
+ /** Returns a short textual representation of the item (its name, that is). */
+ override def toString = this.name
+
+end Item
+
diff --git a/src/main/scala/Server/Character.scala b/src/main/scala/Server/Character.scala
index 082f152..174b7b0 100644
--- a/src/main/scala/Server/Character.scala
+++ b/src/main/scala/Server/Character.scala
@@ -2,6 +2,6 @@ package o1game.Server
import scala.collection.mutable.Buffer
-class GameCharacter(val name: String, initialItems: Iterable[String]):
- private val items: Buffer[String] = Buffer.from(items) // TODO: Item class
+class GameCharacter(val name: String):
+ private val items: Buffer[String] = Buffer.empty // TODO: Item class
// TODO: A lot of other things too
diff --git a/src/main/scala/Server/Client.scala b/src/main/scala/Server/Client.scala
index 3bc4012..1e5dd00 100644
--- a/src/main/scala/Server/Client.scala
+++ b/src/main/scala/Server/Client.scala
@@ -1,23 +1,36 @@
package o1game.Server
import java.net.Socket
-import scala.util.Try
import scala.math.min
import o1game.constants.*
+import ServerProtocolState.*
+import o1game.Model.Entity
+import o1game.Model.Action
-object Client:
- def parseClient(data: String, socket: Socket): Client =
- Client(socket, Some(GameCharacter(data, Vector())))
-
-class Client(val socket: Socket, val character: Option[GameCharacter]):
+class Client(val socket: Socket):
private var incompleteMessage: Array[Byte] =
Array.fill(MAX_MSG_SIZE)(0.toByte)
private var incompleteMessageIndex = 0
+ private var protocolState = WaitingForVersion
+ private var outData: String = ""
+ private var character: Option[Entity] = None
+ private var protocolIsIntact = true
+ private var name: Option[String] = None
/** Calculates the amount of bytes available for future incoming messages
*/
def spaceAvailable: Int = MAX_MSG_SIZE - incompleteMessageIndex
+ def isIntactProtocolWise: Boolean = protocolIsIntact
+ def isReadyForGameStart: Boolean =
+ this.protocolState == WaitingForGameStart
+ def gameStart(): Unit = this.protocolState = InGame
+ def entity: Option[Entity] = this.character
+ def giveEntity(entity: Entity): Unit =
+ println(entity)
+ this.character = Some(entity)
+ def getName: Option[String] = this.name
+
/** Sets `data` as received for the client.
*
* @return false means there was not enough space to receive the message
@@ -30,6 +43,23 @@ class Client(val socket: Socket, val character: Option[GameCharacter]):
min(this.incompleteMessageIndex, MAX_MSG_SIZE)
data.length < spaceAvailable
+ /** Returns data that should be sent to this client.
+ * The data is cleared when calling.
+ */
+ def dataToThisClient(): String =
+ val a = this.outData
+ this.outData = ""
+ a
+
+ /** Specifies that the data should be buffered for
+ * sending to this client
+ *
+ * @param data data to buffer for sending
+ */
+ private def addDataToSend(data: String): Unit =
+ this.outData += s"$data\n"
+
+
/** Returns one line of data if there are any line breaks.
* Removes the parsed data from the message buffering area.
*/
@@ -46,60 +76,42 @@ class Client(val socket: Socket, val character: Option[GameCharacter]):
/** Causes the client to take the actions it has received
*/
- def executeActions(): Unit =
+ def interpretData(): Unit =
LazyList.continually(this.nextLine())
.takeWhile(_.isDefined)
.flatten
- .foreach(s => println(s"`$this` executing `$s`"))
-
-
-class Clients(maxClients: Int):
- private val clients: Array[Option[Client]] = Array.fill(maxClients)(None)
+ .foreach(s => takeAction(s))
- /** Adds `client` to this collection of clients.
+ /** Makes the client execute the action specified by `line`.
+ * If there is a protocol error, the function changes
+ * the variable `protocolIsIntact` to false.
*
- * @param client the Client to add
- * @return true if there was room for the client
- * i.e. fewer clients than `maxClients`, false otherwise
+ * @param line the line to interpret
*/
- def addClient(client: Client): Boolean =
- val i = this.clients.indexOf(None)
- if i == -1 then
- false
- else
- this.clients(i) = Some(client)
- true
+ private def takeAction(line: String): Unit =
+ this.protocolIsIntact = this.protocolState match
+ case WaitingForVersion =>
+ if line == GAME_VERSION then
+ addDataToSend(PROTOCOL_VERSION_GOOD)
+ this.protocolState = WaitingForClientName
+ true
+ else
+ addDataToSend(PROTOCOL_VERSION_BAD)
+ false
+ case WaitingForClientName =>
+ this.name = Some(line)
+ this.protocolState = WaitingForGameStart
+ true
+ case WaitingForGameStart => true
+ case InGame =>
+ println(line)
+ val action = Action(line)
+ this.character.flatMap(action.execute(_)) match
+ case Some(s) => this.addDataToSend(s)
+ case None => this.addDataToSend("You can't do that")
+ this.character.map(_.location.fullDescription) match
+ case Some(s) => this.addDataToSend(s)
+ true
- /** Returns all the clients.
- *
- * @return an iterable of all the clients
- */
- def allClients: Iterable[Client] = clients.toVector.flatten
+end Client
- /** Applies the function `f` to all the clients for its side effects. */
- def foreach(f: Client => Any): Unit = this.clients.flatten.foreach(f)
-
- /** Applies the function `f` to all the clients for its side effects
- * and removes all the clients for which `f([client])` returns false.
- * This is useful for doing IO with the client and removing clients
- * with stale sockets.
- *
- * @param f the function to apply to all the clients and filter them with
- */
- def removeNonSatisfying(f: Client => Boolean): Unit =
- for i <- this.clients.indices do
- this.clients(i) match
- case Some(c) =>
- if !f(c) then
- this.clients(i) = None
- case None =>
-
- /** Applies the function f to all clients for its side effects.
- * If the function throws an exception, the client is removed.
- * Probably a more concise alternative to `removeNonSatisfying`,
- * but might catch exceptions unintentionally.
- *
- * @param f the function to apply for its side effects to each client
- */
- def mapAndRemove(f: Client => Unit): Unit =
- this.removeNonSatisfying(c => Try(f(c)).isSuccess) \ No newline at end of file
diff --git a/src/main/scala/Server/Clients.scala b/src/main/scala/Server/Clients.scala
new file mode 100644
index 0000000..24a245c
--- /dev/null
+++ b/src/main/scala/Server/Clients.scala
@@ -0,0 +1,70 @@
+package o1game.Server
+
+import scala.util.Try
+
+class Clients(maxClients: Int):
+ private val clients: Array[Option[Client]] = Array.fill(maxClients)(None)
+
+ /** Adds `client` to this collection of clients.
+ *
+ * @param client the Client to add
+ * @return true if there was room for the client
+ * i.e. fewer clients than `maxClients`, false otherwise
+ */
+ def addClient(client: Client): Boolean =
+ val i = this.clients.indexOf(None)
+ if i == -1 then
+ false
+ else
+ this.clients(i) = Some(client)
+ true
+
+ /** Returns all the clients.
+ *
+ * @return an iterable of all the clients
+ */
+ def allClients: Iterable[Client] = clients.toVector.flatten
+
+ /** Applies the function `f` to all the clients for its side effects. */
+ def foreach(f: Client => Any): Unit = this.clients.flatten.foreach(f)
+
+ /** Returns true if the predicate `f` stands for all clients,
+ * false otherwise
+ *
+ * @param f the predicate to check for all clients
+ * @return whether `f` stands for all clients
+ */
+ def forall(f: Client => Boolean): Boolean = this.clients.flatten.forall(f)
+
+ def names: Vector[String] = this.clients.flatten.flatMap(_.getName).toVector
+
+ def isEmpty: Boolean = this.clients.flatten.isEmpty
+
+ /** Applies the function `f` to all the clients for its side effects
+ * and removes all the clients for which `f([client])` returns false.
+ * This is useful for doing IO with the client and removing clients
+ * with stale sockets.
+ *
+ * @param f the function to apply to all the clients and filter them with
+ */
+ def removeNonSatisfying(f: Client => Boolean): Unit =
+ for i <- this.clients.indices do
+ this.clients(i) match
+ case Some(c) =>
+ if !f(c) then
+ this.clients(i) = None
+ case None =>
+
+ /** Removes clients that have not behaved according to protocol */
+ def removeNonCompliant(): Unit =
+ this.removeNonSatisfying(_.isIntactProtocolWise)
+
+ /** Applies the function f to all clients for its side effects.
+ * If the function throws an exception, the client is removed.
+ * Probably a more concise alternative to `removeNonSatisfying`,
+ * but might catch exceptions unintentionally.
+ *
+ * @param f the function to apply for its side effects to each client
+ */
+ def mapAndRemove(f: Client => Unit): Unit =
+ this.removeNonSatisfying(c => Try(f(c)).isSuccess)
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 =>
diff --git a/src/main/scala/Server/constants.scala b/src/main/scala/Server/constants.scala
index 4260a60..ddaab00 100644
--- a/src/main/scala/Server/constants.scala
+++ b/src/main/scala/Server/constants.scala
@@ -4,3 +4,10 @@ package o1game.constants
val MAX_MSG_SIZE = 1024 // bytes
val LF: Byte = 10
val POLL_INTERVAL = 100 // millisec.
+val GAME_VERSION = "0.1.0"
+
+val PROTOCOL_VERSION_GOOD = "version ok"
+val PROTOCOL_VERSION_BAD = "version bad"
+
+enum ServerProtocolState:
+ case WaitingForVersion, WaitingForClientName, WaitingForGameStart, InGame
diff --git a/src/main/scala/main.scala b/src/main/scala/main.scala
index 18172e2..da13025 100644
--- a/src/main/scala/main.scala
+++ b/src/main/scala/main.scala
@@ -9,7 +9,7 @@ import scala.io.StdIn.readLine
print("Please choose:\n1) Client.Client\n2) Server\n> ")
readLine().toIntOption match
case Some(1) => Client("127.0.0.1", 2267).startClient()
- case Some(2) => Server(2267, 5).startServer()
+ case Some(2) => Server(2267, 5, 30).startServer()
case _ => println("Invalid input")