Skip to content

Commit 905a98e

Browse files
committed
feat: kotlin dsl
1 parent 16f91aa commit 905a98e

File tree

11 files changed

+623
-0
lines changed

11 files changed

+623
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package app.simplecloud.controller.api.dsl.builders
2+
3+
import app.simplecloud.controller.api.dsl.markers.GroupDsl
4+
import app.simplecloud.controller.shared.group.Group
5+
import build.buf.gen.simplecloud.controller.v1.ServerType
6+
7+
@GroupDsl
8+
class GroupCreateBuilder {
9+
var name: String = ""
10+
var type: ServerType = ServerType.UNKNOWN_SERVER
11+
var minMemory: Long = 0
12+
var maxMemory: Long = 0
13+
var startPort: Long = 0
14+
var minOnlineCount: Long = 0
15+
var maxOnlineCount: Long = 0
16+
var maxPlayers: Long = 0
17+
var newServerPlayerRatio: Long = -1
18+
private val properties = mutableMapOf<String, String>()
19+
20+
operator fun String.unaryPlus() = this
21+
22+
operator fun Pair<String, String>.unaryPlus() {
23+
properties[first] = second
24+
}
25+
26+
infix fun String.to(value: String) {
27+
properties[this] = value
28+
}
29+
30+
internal fun build() = Group(
31+
name = name,
32+
type = type,
33+
minMemory = minMemory,
34+
maxMemory = maxMemory,
35+
startPort = startPort,
36+
minOnlineCount = minOnlineCount,
37+
maxOnlineCount = maxOnlineCount,
38+
maxPlayers = maxPlayers,
39+
newServerPlayerRatio = newServerPlayerRatio,
40+
properties = properties
41+
)
42+
}
43+
44+
@GroupDsl
45+
class GroupUpdateBuilder {
46+
private var group: Group? = null
47+
private val properties = mutableMapOf<String, String>()
48+
49+
fun fromGroup(existingGroup: Group) {
50+
group = existingGroup
51+
properties.putAll(existingGroup.properties)
52+
}
53+
54+
operator fun String.unaryPlus() = this
55+
56+
operator fun Pair<String, String>.unaryPlus() {
57+
properties[first] = second
58+
}
59+
60+
infix fun String.to(value: String) {
61+
properties[this] = value
62+
}
63+
64+
internal fun build(): Group {
65+
requireNotNull(group) { "Group must be set using fromGroup()" }
66+
return group!!.copy(properties = properties)
67+
}
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package app.simplecloud.controller.api.dsl.builders
2+
3+
import app.simplecloud.controller.api.dsl.markers.PropertyDsl
4+
5+
@PropertyDsl
6+
class PropertyBuilder {
7+
private val properties = mutableMapOf<String, String>()
8+
9+
operator fun String.unaryPlus() = this
10+
11+
operator fun Pair<String, String>.unaryPlus() {
12+
properties[first] = second
13+
}
14+
15+
infix fun String.to(value: String) {
16+
properties[this] = value
17+
}
18+
19+
fun property(key: String, value: String) {
20+
properties[key] = value
21+
}
22+
23+
fun properties(vararg pairs: Pair<String, String>) {
24+
properties.putAll(pairs)
25+
}
26+
27+
internal fun build(): Map<String, String> = properties.toMap()
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package app.simplecloud.controller.api.dsl.builders
2+
3+
import app.simplecloud.controller.api.dsl.markers.ServerDsl
4+
import build.buf.gen.simplecloud.controller.v1.ServerStartCause
5+
import build.buf.gen.simplecloud.controller.v1.ServerState
6+
import build.buf.gen.simplecloud.controller.v1.ServerStopCause
7+
8+
@ServerDsl
9+
class ServerStartBuilder {
10+
var startCause: ServerStartCause = ServerStartCause.API_START
11+
private val properties = mutableMapOf<String, String>()
12+
13+
operator fun String.unaryPlus() = this
14+
15+
operator fun Pair<String, String>.unaryPlus() {
16+
properties[first] = second
17+
}
18+
19+
infix fun String.to(value: String) {
20+
properties[this] = value
21+
}
22+
23+
fun getProperties(): Map<String, String> = properties.toMap()
24+
}
25+
26+
@ServerDsl
27+
class ServerStopBuilder {
28+
var stopCause: ServerStopCause = ServerStopCause.API_STOP
29+
}
30+
31+
@ServerDsl
32+
class ServerStateBuilder {
33+
var state: ServerState = ServerState.UNKNOWN_STATE
34+
}
35+
36+
@ServerDsl
37+
class ServerPropertyBuilder {
38+
private val properties = mutableMapOf<String, String>()
39+
40+
operator fun String.unaryPlus() = this
41+
42+
operator fun Pair<String, String>.unaryPlus() {
43+
properties[first] = second
44+
}
45+
46+
infix fun String.to(value: String) {
47+
properties[this] = value
48+
}
49+
50+
internal fun build(): Map<String, String> = properties.toMap()
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package app.simplecloud.controller.api.dsl.extensions
2+
3+
import app.simplecloud.controller.api.ControllerApi
4+
import app.simplecloud.controller.api.dsl.markers.ControllerDsl
5+
import app.simplecloud.controller.api.dsl.scopes.GroupScope
6+
import app.simplecloud.controller.api.dsl.scopes.ServerScope
7+
import kotlinx.coroutines.coroutineScope
8+
9+
@ControllerDsl
10+
class ControllerApiScope(private val api: ControllerApi.Coroutine) {
11+
suspend fun groups(block: suspend GroupScope.() -> Unit) {
12+
GroupScope(api.getGroups()).block()
13+
}
14+
15+
suspend fun servers(block: suspend ServerScope.() -> Unit) {
16+
ServerScope(api.getServers()).block()
17+
}
18+
}
19+
20+
suspend fun controllerApiScope(block: suspend ControllerApiScope.() -> Unit) {
21+
val api = ControllerApi.createCoroutineApi()
22+
controllerApiScope(api, block)
23+
}
24+
25+
suspend fun controllerApiScope(
26+
api: ControllerApi.Coroutine,
27+
block: suspend ControllerApiScope.() -> Unit
28+
) {
29+
coroutineScope {
30+
ControllerApiScope(api).block()
31+
}
32+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package app.simplecloud.controller.api.dsl.extensions
2+
3+
import app.simplecloud.controller.api.GroupApi
4+
import app.simplecloud.controller.api.dsl.markers.GroupDsl
5+
import app.simplecloud.controller.shared.group.Group
6+
import build.buf.gen.simplecloud.controller.v1.ServerType
7+
8+
@GroupDsl
9+
class GroupCreateBuilder {
10+
var name: String = ""
11+
var type: ServerType = ServerType.UNKNOWN_SERVER
12+
var minMemory: Long = 0
13+
var maxMemory: Long = 0
14+
var startPort: Long = 0
15+
var minOnlineCount: Long = 0
16+
var maxOnlineCount: Long = 0
17+
var maxPlayers: Long = 0
18+
var newServerPlayerRatio: Long = -1
19+
private val properties = mutableMapOf<String, String>()
20+
21+
fun properties(block: ServerPropertyBuilder.() -> Unit) {
22+
val builder = ServerPropertyBuilder().apply(block)
23+
properties.putAll(builder.build())
24+
}
25+
26+
internal fun build() = Group(
27+
name = name,
28+
type = type,
29+
minMemory = minMemory,
30+
maxMemory = maxMemory,
31+
startPort = startPort,
32+
minOnlineCount = minOnlineCount,
33+
maxOnlineCount = maxOnlineCount,
34+
maxPlayers = maxPlayers,
35+
newServerPlayerRatio = newServerPlayerRatio,
36+
properties = properties
37+
)
38+
}
39+
40+
@GroupDsl
41+
class GroupUpdateBuilder {
42+
private var group: Group? = null
43+
private val properties = mutableMapOf<String, String>()
44+
45+
fun fromGroup(existingGroup: Group) {
46+
group = existingGroup
47+
properties.putAll(existingGroup.properties)
48+
}
49+
50+
fun properties(block: ServerPropertyBuilder.() -> Unit) {
51+
val builder = ServerPropertyBuilder().apply(block)
52+
properties.putAll(builder.build())
53+
}
54+
55+
internal fun build(): Group {
56+
requireNotNull(group) { "Group must be set using fromGroup()" }
57+
return group!!.copy(properties = properties)
58+
}
59+
}
60+
61+
suspend fun GroupApi.Coroutine.create(block: GroupCreateBuilder.() -> Unit): Group {
62+
val builder = GroupCreateBuilder().apply(block)
63+
return createGroup(builder.build())
64+
}
65+
66+
suspend fun GroupApi.Coroutine.update(block: GroupUpdateBuilder.() -> Unit): Group {
67+
val builder = GroupUpdateBuilder().apply(block)
68+
return updateGroup(builder.build())
69+
}
70+
71+
suspend fun GroupApi.Coroutine.delete(
72+
name: String,
73+
block: (Group) -> Unit = {}
74+
) {
75+
block(deleteGroup(name))
76+
}
77+
78+
suspend fun GroupApi.Coroutine.getGroup(
79+
name: String,
80+
block: (Group) -> Unit = {}
81+
) {
82+
block(getGroupByName(name))
83+
}
84+
85+
suspend fun GroupApi.Coroutine.getAllGroups(
86+
block: (List<Group>) -> Unit = {}
87+
) {
88+
block(getAllGroups())
89+
}
90+
91+
suspend fun GroupApi.Coroutine.getGroupsByType(
92+
type: ServerType,
93+
block: (List<Group>) -> Unit = {}
94+
) {
95+
block(getGroupsByType(type))
96+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package app.simplecloud.controller.api.dsl.extensions
2+
3+
import app.simplecloud.controller.api.GroupApi
4+
import app.simplecloud.controller.shared.group.Group
5+
6+
suspend fun GroupApi.Coroutine.createGroup(
7+
block: GroupCreateBuilder.() -> Unit
8+
): Group {
9+
val builder = GroupCreateBuilder().apply(block)
10+
return createGroup(builder.build())
11+
}
12+
13+
suspend fun GroupApi.Coroutine.updateGroup(
14+
block: GroupUpdateBuilder.() -> Unit
15+
): Group {
16+
val builder = GroupUpdateBuilder().apply(block)
17+
return updateGroup(builder.build())
18+
}

0 commit comments

Comments
 (0)