blob: 7eb3d5b5bc459925dd4548495d2bdfc587c76846 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package scalevalapokalypsi.Client
/** `Turn`s represent information the client has got about a turn.
* This class exists essentially so that the client has somewhere
* to store data about turns and something to format that data with.
*/
class RoomState:
/** Description of the area the player controlled by the client is in
* at the end of the turn. */
var areaDescription: String = ""
/** Directions the player controlled by the client can go to. */
var possibleDirections: Array[String] = Array.empty
/** Items the player controlled by the client can see. */
var visibleItems: Array[String] = Array.empty
/** Entities the player controlled by the client can see. */
var visibleEntities: Array[String] = Array.empty
override def toString: String =
val directionDesc = iterableToString(this.possibleDirections)
.map(s => s"Täältä pääsee $s.\n")
.getOrElse("")
val itemDesc = iterableToString(this.visibleItems)
.map(s => s"Täällä on $s.\n")
.getOrElse("")
val entityDesc = iterableToString(this.visibleEntities)
.map(s => s"Seuraavat olennot ovat täällä: $s.\n")
.getOrElse("")
s"$areaDescription\n$directionDesc$itemDesc$entityDesc".dropRight(1)
private def iterableToString(iterable: Iterable[String]): Option[String] =
Some("")
.map(_ + iterable.dropRight(1).mkString(", "))
.map(_ + (if iterable.size >= 2 then " ja " else ""))
.map(_ + iterable.takeRight(1).headOption.getOrElse(""))
.filter(_ != "")
end RoomState
|