Dieser Commit ist enthalten in:
Chaoscaot 2023-01-21 19:45:04 +01:00
Ursprung ee8bfbffd4
Commit ec2a74de1f
4 geänderte Dateien mit 85 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -37,4 +37,5 @@ dependencies {
implementation("com.mysql:mysql-connector-j:8.0.31")
implementation(project(":CommonCore"))
implementation("org.bspfsystems:yamlconfiguration:1.3.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-cbor:1.4.1")
}

Datei anzeigen

@ -0,0 +1,56 @@
package de.steamwar.data
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
@Serializable
data class GroupsData(val groups: MutableList<GroupData>)
@Serializable
data class GroupData(val name: String, val fights: MutableList<Int>)
@OptIn(ExperimentalSerializationApi::class)
class Groups {
companion object {
private var groups: GroupsData = Cbor.decodeFromByteArray(kGroupsFile.readBytes())
fun getGroup(name: String): GroupData? {
return groups.groups.find { it.name == name }
}
fun getGroup(fight: Int): GroupData? {
return groups.groups.find { it.fights.contains(fight) }
}
fun getOrCreateGroup(name: String): GroupData {
val group = getGroup(name)
if (group != null) {
return group
}
val newGroup = GroupData(name, mutableListOf())
groups.groups.add(newGroup)
return newGroup
}
fun resetGroup(fight: Int, save: Boolean = false) {
val oldGroup = getGroup(fight)
oldGroup?.fights?.remove(fight)
if(oldGroup?.fights?.isEmpty() == true) {
groups.groups.remove(oldGroup)
}
if(save) {
kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
}
}
fun setGroup(fight: Int, group: String) {
resetGroup(fight)
val newGroup = getOrCreateGroup(group)
newGroup.fights.add(fight)
kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
}
}
}

Datei anzeigen

@ -0,0 +1,11 @@
package de.steamwar.data
import java.io.File
const val kDataFolder: String = "data"
const val kGroupsName: String = "groups.cbor"
val kGroupsFile: File = File(kDataFolder, kGroupsName)
const val kRelationsName = "relations.cbor"
val kRelationsFile: File = File(kDataFolder, kRelationsName)

Datei anzeigen

@ -1,6 +1,7 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.data.Groups
import de.steamwar.sql.EventFight
import de.steamwar.sql.SteamwarUser
import de.steamwar.sql.Team
@ -15,10 +16,10 @@ import java.time.Instant
@Serializable
data class ResponseEventFight(val id: Int, val spielmodus: String, val map: String, val blueTeam: ResponseTeam, val redTeam: ResponseTeam, val kampfleiter: ResponseUser, val start: Long, val ergebnis: Int) {
data class ResponseEventFight(val id: Int, val spielmodus: String, val map: String, val blueTeam: ResponseTeam, val redTeam: ResponseTeam, val kampfleiter: ResponseUser, val start: Long, val ergebnis: Int, val group: String?) {
constructor(eventFight: EventFight): this(eventFight.fightID, eventFight.spielModus, eventFight.map, ResponseTeam(
Team.get(eventFight.teamBlue)), ResponseTeam(Team.get(eventFight.teamRed)),
ResponseUser(SteamwarUser.get(eventFight.kampfleiter)), eventFight.startTime.time, eventFight.ergebnis)
ResponseUser(SteamwarUser.get(eventFight.kampfleiter)), eventFight.startTime.time, eventFight.ergebnis, Groups.getGroup(eventFight.fightID)?.name)
}
@Serializable
@ -27,10 +28,10 @@ data class ResponseTeam(val id: Int, val name: String, val kuerzel: String, val
}
@Serializable
data class UpdateEventFight(val blueTeam: Int? = null, val redTeam: Int? = null, val kampfleiter: Int? = null, val start: Long? = null, val spielmodus: String? = null, val map: String? = null)
data class UpdateEventFight(val blueTeam: Int? = null, val redTeam: Int? = null, val kampfleiter: Int? = null, val start: Long? = null, val spielmodus: String? = null, val map: String? = null, val group: String? = null)
@Serializable
data class CreateEventFight(val event: Int, val spielmodus: String, val map: String, val blueTeam: Int, val redTeam: Int, val start: Long, val kampfleiter: Int? = null)
data class CreateEventFight(val event: Int, val spielmodus: String, val map: String, val blueTeam: Int, val redTeam: Int, val start: Long, val kampfleiter: Int? = null, val group: String? = null)
fun Routing.configureEventFightRoutes() {
route("/fights") {
@ -41,6 +42,11 @@ fun Routing.configureEventFightRoutes() {
return@post
}
val eventFight = EventFight.create(fight.event, Timestamp.from(Instant.ofEpochMilli(fight.start)), fight.spielmodus, fight.map, fight.blueTeam, fight.redTeam, fight.kampfleiter ?: 0)
if(fight.group != null) {
if(fight.group != "null") {
Groups.setGroup(eventFight.fightID, fight.group)
}
}
call.respond(HttpStatusCode.Created, ResponseEventFight(eventFight))
}
route("/{fight}") {
@ -78,6 +84,13 @@ fun Routing.configureEventFightRoutes() {
if(updateFight.spielmodus != null) {
fight.spielModus = updateFight.spielmodus
}
if(updateFight.group != null) {
if(updateFight.group == "null") {
Groups.resetGroup(fightId, true)
} else {
Groups.setGroup(fightId, updateFight.group)
}
}
fight.update()
call.respond(HttpStatusCode.OK, ResponseEventFight(fight))
}