-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateParser.kt
101 lines (86 loc) · 3.68 KB
/
TemplateParser.kt
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package deprecated
import com.soywiz.korio.file.std.resourcesVfs
import com.soywiz.korio.lang.Charsets
import components.*
import deprecated.Symbols
import steps.posititioning.LineConnection
@Deprecated("use JSON serialization for pasing")
class TemplateParser {
suspend fun parse(template: String): List<MutableList<out Any>> {
var mapInfo = StringBuilder(resourcesVfs[template].readLines(Charsets.UTF8).reduce { acc, s -> acc + s }
.filter { it != Symbols.WHITESPACE_1.char && it != Symbols.WHITESPACE_2.char })
// remove comments
while (mapInfo.contains(Symbols.COMMENT.char)) {
val start = mapInfo.indexOf(Symbols.COMMENT.char)
mapInfo = mapInfo.deleteRange(start, mapInfo.indexOf(Symbols.COMMENT.char, startIndex = start + 1) + 1)
}
val (connectionsString, zonesAndPlayersString) = mapInfo
.split(Symbols.ZONE_SEPARATOR.char)
.filter { it.isNotEmpty() }
.partition { it.contains(Regex("[-~^]")) }
val (zonesString, playersString) = zonesAndPlayersString
.partition { it.first().isDigit() }
val zones = mutableListOf<Zone>()
// for (i in zonesString.indices)
// zones.add(parseZone(zonesString[i], i))
val connections = parseConnections(connectionsString[0], zones)
// val players = mutableListOf<Player>()
// for (i in playersString.indices)
// players.add(parsePlayer(playersString[i], zones, i))
//
return listOf(zones, connections)
}
// private fun parseZone(zone: String, index: Int): Zone {
// val fields = zone.split(Symbols.SEPARATOR.char)
//// if (fields.size == 2)
//// return Zone(
//// Surface.valueOf(fields[1].uppercase()), fields[0].toInt(), mutableListOf(),
//// listOf(Mine(Resource.ORE, null), Mine(Resource.WOOD, null)), mutableListOf(), index
//// )
//// if (fields.size == 3)
//// return Zone(
//// Surface.valueOf(fields[1].uppercase()), fields[0].toInt(), mutableListOf(),
//// listOf(Mine(Resource.ORE, null), Mine(Resource.WOOD, null)), mutableListOf(), index
//// )
//// return Zone(
//// Surface.valueOf(fields[1].uppercase()),
//// fields[0].toInt(),
//// mutableListOf(),
//// mutableListOf(),
//// mutableListOf(),
//// index
//// )
// }
// private fun parsePlayer(player: String, zones: List<Zone>, index: Int): Player {
// val fields = player.split(Symbols.SEPARATOR.char)
// return Player(
// Fraction.valueOf(fields[0].uppercase()),
// index,
// zones[fields[1].toInt()],
// if (fields.size == 3) fields[2].toInt() else 0
// )
// }
private fun parseConnections(connectionsString: String, zones: List<Zone>): MutableList<LineConnection> {
val connections = mutableListOf<LineConnection>()
for (i in connectionsString.split(Symbols.SEPARATOR.char)) {
val z1 = zones[i[0].code - '0'.code]
val z2 = zones[i[2].code - '0'.code]
var connectionType = ConnectionType.REGULAR
for (v in Symbols.values())
if (v.char == i[1]) {
connectionType = v.convertToConnectionType()
break
}
// connections.add(
// LineConnection(
// z1, z2, connectionType, 0
// )
// )
//
// z1.connections.add(connections.last())
// z2.connections.add(connections.last())
// }
}
return connections
}
}