Revert "Add EventCache for efficient retrieval of event groups and teams"

This reverts commit 8b4f864f99.

Revert "Refactor EventFights and Fight classes to improve player initialization and retrieval"

This reverts commit 8d705e7a84.

Revert "Refactor EventFights and Fight classes to improve player initialization and retrieval"

This reverts commit 78352a3e67.
This commit is contained in:
2025-11-14 23:24:48 +01:00
parent 78352a3e67
commit f923a007a7
6 changed files with 12 additions and 93 deletions
+1 -1
View File
@@ -141,7 +141,7 @@ class Fight(id: EntityID<Int>) : IntEntity(id) {
lateinit var bluePlayers: List<FightPlayer>
lateinit var redPlayers: List<FightPlayer>
fun initPlayers(fightPlayers: List<FightPlayer>) {
private fun initPlayers(fightPlayers: List<FightPlayer>) {
val blue = mutableListOf<FightPlayer>()
val red = mutableListOf<FightPlayer>()
@@ -23,7 +23,6 @@ import de.steamwar.sql.internal.useDb
import org.jetbrains.exposed.v1.core.dao.id.CompositeID
import org.jetbrains.exposed.v1.core.dao.id.CompositeIdTable
import org.jetbrains.exposed.v1.core.dao.id.EntityID
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.inList
import org.jetbrains.exposed.v1.dao.CompositeEntity
import org.jetbrains.exposed.v1.dao.CompositeEntityClass
@@ -68,11 +67,6 @@ class FightPlayer(id: EntityID<CompositeID>) : CompositeEntity(id) {
fun batchGet(fightIds: List<Int>) = useDb {
find { FightPlayerTable.fightId inList fightIds.toList() }.toList()
}
@JvmStatic
fun getFight(fightId: Int) = useDb {
find { FightPlayerTable.fightId eq fightId }.toList()
}
}
val fightID by FightPlayerTable.fightId.transform({ EntityID(it, FightTable) }, { it.value })
+2 -2
View File
@@ -43,10 +43,10 @@ class Team(id: EntityID<Int>) : IntEntity(id) {
private val teamCache = mutableMapOf<Int, Team>()
@JvmStatic
fun clear() = synchronized(teamCache) { teamCache.clear() }
fun clear() = teamCache.clear()
@JvmStatic
fun byId(id: Int) = synchronized(teamCache) { teamCache.computeIfAbsent(id) { useDb { Team[id] } } }
fun byId(id: Int) = teamCache.computeIfAbsent(id) { useDb { Team[id] } }
@JvmStatic
fun get(name: String) = useDb { find { TeamTable.name.lowerCase() eq name.lowercase() or (TeamTable.kuerzel.lowerCase() eq name.lowercase()) }.firstOrNull() }
@@ -1,36 +0,0 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data
import de.steamwar.routes.ResponseGroups
import de.steamwar.routes.ResponseTeam
import de.steamwar.sql.EventGroup
import de.steamwar.sql.Team
class EventCache {
private val groupCache = mutableMapOf<Int, ResponseGroups>()
private val teamCache = mutableMapOf<Int, ResponseTeam>()
fun getGroup(id: Int): ResponseGroups = groupCache.getOrPut(id) { ResponseGroups(EventGroup.byId(id).get()) }
fun getTeam(id: Int): ResponseTeam = teamCache.getOrPut(id) { ResponseTeam(Team.byId(id)) }
fun convertGroup(group: EventGroup) = ResponseGroups(group).also { groupCache[group.getId()] = it }
fun convertTeam(team: Team) = ResponseTeam(team).also { teamCache[team.teamId] = it }
}
@@ -20,9 +20,7 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.data.EventCache
import de.steamwar.sql.*
import de.steamwar.sql.internal.useDb
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
@@ -43,55 +41,19 @@ data class ResponseEventFight(
val ergebnis: Int,
val spectatePort: Int?,
val group: ResponseGroups?,
val hasFinished: Boolean,
val fightData: FightData? = null
val hasFinished: Boolean
) {
constructor(eventFight: EventFight, cache: EventCache = EventCache()) : this(
constructor(eventFight: EventFight) : this(
eventFight.fightID,
eventFight.spielmodus,
eventFight.map,
cache.getTeam(eventFight.teamBlue),
cache.getTeam(eventFight.teamRed),
ResponseTeam(Team.byId(eventFight.teamBlue)),
ResponseTeam(Team.byId(eventFight.teamRed)),
eventFight.startTime.time,
eventFight.ergebnis,
eventFight.spectatePort,
eventFight.groupId?.let { cache.getGroup(it.value) },
eventFight.hasFinished(),
eventFight.fight?.let { FightData(Fight.getById(it)) }
)
}
@Serializable
data class FightData(
val id: Int,
val gamemode: String,
val server: String,
val start: Long,
val duration: Int,
val blueLeader: ResponseUser,
val redLeader: ResponseUser,
val blueSchem: String?,
val redSchem: String?,
val win: String,
val winCondition: String,
val bluePlayers: List<ResponseUser>,
val redPlayers: List<ResponseUser>,
) {
constructor(fight: Fight, players: List<FightPlayer> = FightPlayer.getFight(fight.fightID)) : this(
fight.id.value,
fight.gameMode,
fight.server,
fight.startTime.time,
fight.duration,
ResponseUser.get(fight.blueLeader),
ResponseUser.get(fight.redLeader),
fight.blueSchem?.let { SchematicNode.getSchematicNode(it.value)?.name },
fight.redSchem?.let { SchematicNode.getSchematicNode(it.value)?.name },
fight.winner.name,
fight.winCondition,
players.filter { it.team == 1 }.map { ResponseUser.get(it.userID) },
players.filter { it.team == 2 }.map { ResponseUser.get(it.userID) },
eventFight.group.orElse(null)?.let { ResponseGroups(it, short = true) },
eventFight.hasFinished()
)
}
@@ -20,7 +20,6 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.data.EventCache
import de.steamwar.plugins.SWPermissionCheck
import de.steamwar.sql.*
import de.steamwar.sql.EventGroup.EventGroupType
@@ -114,10 +113,10 @@ data class ExtendedResponseEvent(
val referees: List<ResponseUser>,
val relations: List<ResponseRelation>
) {
constructor(event: Event, cache: EventCache = EventCache()) : this(
constructor(event: Event) : this(
ResponseEvent(event),
TeamTeilnahme.getTeams(event.eventID).map { cache.convertTeam(it) },
EventGroup.get(event).map { cache.convertGroup(it) },
TeamTeilnahme.getTeams(event.eventID).map { ResponseTeam(it) },
EventGroup.get(event).map { ResponseGroups(it) },
EventFight.getEvent(event.eventID).map { ResponseEventFight(it) },
Referee.get(event.eventID).map { ResponseUser.get(SteamwarUser.byId(it)!!) },
EventRelation.get(event).map { ResponseRelation(it) }