aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Server/Client.scala
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 /src/main/scala/Server/Client.scala
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.
Diffstat (limited to 'src/main/scala/Server/Client.scala')
-rw-r--r--src/main/scala/Server/Client.scala122
1 files changed, 67 insertions, 55 deletions
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