forked from SteamWar/SteamWar
Remove Event-related SQL classes and update relevant references across modules
Signed-off-by: Chaoscaot <max@maxsp.de>
This commit is contained in:
@@ -1,216 +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.sql;
|
||||
|
||||
import de.steamwar.sql.internal.Field;
|
||||
import de.steamwar.sql.internal.SelectStatement;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
import de.steamwar.sql.internal.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.*;
|
||||
|
||||
import static java.time.temporal.ChronoUnit.SECONDS;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class EventFight implements Comparable<EventFight> {
|
||||
|
||||
private static final Table<EventFight> table = new Table<>(EventFight.class);
|
||||
private static final SelectStatement<EventFight> byId = table.select(Table.PRIMARY);
|
||||
private static final SelectStatement<EventFight> byGroup = new SelectStatement<EventFight>(table, "SELECT * FROM EventFight WHERE GroupID = ? ORDER BY StartTime ASC");
|
||||
private static final SelectStatement<EventFight> byGroupLast = new SelectStatement<EventFight>(table, "SELECT * FROM EventFight WHERE GroupID = ? ORDER BY StartTime DESC LIMIT 1");
|
||||
private static final SelectStatement<EventFight> allComing = new SelectStatement<>(table, "SELECT * FROM EventFight WHERE StartTime > now() ORDER BY StartTime ASC");
|
||||
private static final SelectStatement<EventFight> event = new SelectStatement<>(table, "SELECT * FROM EventFight WHERE EventID = ? ORDER BY StartTime ASC");
|
||||
private static final SelectStatement<EventFight> activeFights = new SelectStatement<>(table, "SELECT * FROM EventFight WHERE EventID IN (SELECT EventID FROM Event WHERE Start < now() and End > now()) AND Fight IS NULL AND StartTime < now()");
|
||||
private static final Statement reschedule = table.update(Table.PRIMARY, "StartTime");
|
||||
private static final Statement setResult = table.update(Table.PRIMARY, "Ergebnis");
|
||||
private static final Statement setFight = table.update(Table.PRIMARY, "Fight");
|
||||
|
||||
private static final Statement create = table.insertFields(true, "eventID", "startTime", "spielmodus", "map", "teamBlue", "teamRed", "spectatePort");
|
||||
private static final Statement update = table.update(Table.PRIMARY, "startTime", "spielModus", "map", "teamBlue", "teamRed", "spectatePort");
|
||||
private static final Statement setGroup = table.update(Table.PRIMARY, "GroupID");
|
||||
private static final Statement delete = table.delete(Table.PRIMARY);
|
||||
|
||||
@Getter
|
||||
private static final Queue<EventFight> fights = new PriorityQueue<>();
|
||||
|
||||
public static EventFight get(int fightID) {
|
||||
return byId.select(fightID);
|
||||
}
|
||||
|
||||
public static List<EventFight> get(EventGroup group) {
|
||||
return byGroup.listSelect(group.getId());
|
||||
}
|
||||
|
||||
public static Optional<EventFight> getLast(EventGroup group) {
|
||||
return Optional.ofNullable(byGroupLast.select(group.getId()));
|
||||
}
|
||||
|
||||
public static void loadAllComingFights() {
|
||||
fights.clear();
|
||||
fights.addAll(allComing.listSelect());
|
||||
}
|
||||
|
||||
public static List<EventFight> getEvent(int eventID) {
|
||||
return event.listSelect(eventID);
|
||||
}
|
||||
|
||||
private static List<EventFight> activeFightsCache = null;
|
||||
|
||||
public static void clearActiveFightsCache() {
|
||||
activeFightsCache = null;
|
||||
}
|
||||
|
||||
public static List<EventFight> getActiveFights() {
|
||||
if (activeFightsCache == null) {
|
||||
activeFightsCache = activeFights.listSelect();
|
||||
}
|
||||
return activeFightsCache;
|
||||
}
|
||||
|
||||
public static EventFight create(int event, Timestamp from, String spielmodus, String map, int blueTeam, int redTeam, Integer spectatePort) {
|
||||
return get(create.insertGetKey(event, from, spielmodus, map, blueTeam, redTeam, spectatePort));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Field
|
||||
private final int eventID;
|
||||
@Getter
|
||||
@Field(keys = {Table.PRIMARY}, autoincrement = true)
|
||||
private final int fightID;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field(nullable = true, def = "null")
|
||||
private Integer groupId;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field
|
||||
private Timestamp startTime;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field
|
||||
private String spielmodus;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field
|
||||
private String map;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field
|
||||
private int teamBlue;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field
|
||||
private int teamRed;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field(nullable = true)
|
||||
private Integer spectatePort;
|
||||
@Getter
|
||||
@Setter
|
||||
@Field(def = "1")
|
||||
private int bestOf;
|
||||
@Getter
|
||||
@Field(def = "0")
|
||||
private int ergebnis;
|
||||
@Field(nullable = true)
|
||||
private int fight;
|
||||
|
||||
public Optional<EventGroup> getGroup() {
|
||||
return Optional.ofNullable(groupId).flatMap(EventGroup::get);
|
||||
}
|
||||
|
||||
public Optional<Team> getWinner() {
|
||||
if(ergebnis == 0)
|
||||
return Optional.empty();
|
||||
return Optional.ofNullable(ergebnis == 1 ? Team.get(teamBlue) : Team.get(teamRed));
|
||||
}
|
||||
|
||||
public Optional<Team> getLosser() {
|
||||
if(ergebnis == 0)
|
||||
return Optional.empty();
|
||||
return Optional.ofNullable(ergebnis == 1 ? Team.get(teamRed) : Team.get(teamBlue));
|
||||
}
|
||||
|
||||
public List<EventRelation> getDependents() {
|
||||
return EventRelation.getFightRelations(this);
|
||||
}
|
||||
|
||||
public void setErgebnis(int winner) {
|
||||
this.ergebnis = winner;
|
||||
setResult.update(winner, fightID);
|
||||
}
|
||||
|
||||
public void setFight(int fight) {
|
||||
//Fight.FightID, not EventFight.FightID
|
||||
this.fight = fight;
|
||||
setFight.update(fight, fightID);
|
||||
}
|
||||
|
||||
public void setGroup(Integer group) {
|
||||
setGroup.update(group, fightID);
|
||||
this.groupId = group;
|
||||
}
|
||||
|
||||
public boolean hasFinished() {
|
||||
return fight != 0 || ergebnis != 0;
|
||||
}
|
||||
|
||||
public void reschedule() {
|
||||
startTime = Timestamp.from(new Date().toInstant().plus(30, SECONDS));
|
||||
reschedule.update(startTime, fightID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return fightID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if(o == null)
|
||||
return false;
|
||||
if(!(o instanceof EventFight))
|
||||
return false;
|
||||
return fightID == ((EventFight) o).fightID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(EventFight o) {
|
||||
return startTime.compareTo(o.startTime);
|
||||
}
|
||||
|
||||
public void update(Timestamp startTime, String spielmodus, String map, int teamBlue, int teamRed, Integer spectatePort) {
|
||||
update.update(startTime, spielmodus, map, teamBlue, teamRed, spectatePort, fightID);
|
||||
this.startTime = startTime;
|
||||
this.spielmodus = spielmodus;
|
||||
this.map = map;
|
||||
this.teamBlue = teamBlue;
|
||||
this.teamRed = teamRed;
|
||||
this.spectatePort = spectatePort;
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
delete.update(fightID);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* 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.sql
|
||||
|
||||
import de.steamwar.sql.internal.useDb
|
||||
import org.jetbrains.exposed.v1.core.SortOrder
|
||||
import org.jetbrains.exposed.v1.core.and
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.core.greater
|
||||
import org.jetbrains.exposed.v1.core.greaterEq
|
||||
import org.jetbrains.exposed.v1.core.inList
|
||||
import org.jetbrains.exposed.v1.core.inSubQuery
|
||||
import org.jetbrains.exposed.v1.core.isNull
|
||||
import org.jetbrains.exposed.v1.core.less
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.javatime.timestamp
|
||||
import org.jetbrains.exposed.v1.jdbc.insertAndGetId
|
||||
import org.jetbrains.exposed.v1.jdbc.select
|
||||
import java.sql.Timestamp
|
||||
import java.time.Instant
|
||||
import java.util.Optional
|
||||
import java.util.PriorityQueue
|
||||
import java.util.Queue
|
||||
|
||||
object EventFightTable : IntIdTable("EventFight", "FightID") {
|
||||
val eventId = reference("EventID", EventTable)
|
||||
val startTime = timestamp("StartTime")
|
||||
val gamemode = text("Spielmodus")
|
||||
val map = text("Map")
|
||||
val groupId = optReference("GroupId", EventGroupTable)
|
||||
val teamBlue = reference("TeamBlue", TeamTable)
|
||||
val teamRed = reference("TeamRed", TeamTable)
|
||||
val spectatePort = integer("SpectatePort").nullable()
|
||||
val bestOf = integer("BestOf")
|
||||
val ergebnis = integer("Ergebnis")
|
||||
val fight = integer("Fight").entityId().nullable()
|
||||
}
|
||||
|
||||
class EventFight(id: EntityID<Int>) : IntEntity(id), Comparable<EventFight> {
|
||||
companion object : IntEntityClass<EventFight>(EventFightTable) {
|
||||
val fights: Queue<EventFight> = PriorityQueue()
|
||||
@JvmStatic get
|
||||
|
||||
@JvmStatic
|
||||
fun byId(fightId: Int) = useDb { findById(fightId) }
|
||||
|
||||
@JvmStatic
|
||||
fun byId(group: EventGroup) = useDb {
|
||||
find { EventFightTable.groupId eq group.id }.orderBy(EventFightTable.startTime to SortOrder.DESC).toList()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getLast(group: EventGroup) = useDb {
|
||||
Optional.ofNullable(
|
||||
find { EventFightTable.groupId eq group.id }.orderBy(EventFightTable.startTime to SortOrder.DESC)
|
||||
.firstOrNull()
|
||||
)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun loadAllComingFights() = useDb {
|
||||
fights.clear()
|
||||
fights.addAll(find { EventFightTable.startTime greaterEq Instant.now() }.orderBy(EventFightTable.startTime to SortOrder.ASC))
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getEvent(eventId: Int) = useDb {
|
||||
find { EventFightTable.eventId eq eventId }.orderBy(EventFightTable.startTime to SortOrder.ASC).toList()
|
||||
}
|
||||
|
||||
private var activeFightsCache: List<EventFight>? = null
|
||||
|
||||
@JvmStatic
|
||||
fun clearActiveFightsCache() {
|
||||
activeFightsCache = null
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getActiveFights(): List<EventFight> {
|
||||
if (activeFightsCache == null) {
|
||||
activeFightsCache = useDb {
|
||||
find {
|
||||
EventFightTable.fight.isNull() and (EventFightTable.startTime less Instant.now()) and (EventFightTable.eventId.inSubQuery(
|
||||
EventTable.select(
|
||||
EventTable.id
|
||||
)
|
||||
.where { (EventTable.start less Instant.now()) and (EventTable.end greater Instant.now()) }))
|
||||
}.orderBy(EventFightTable.startTime to SortOrder.ASC).toList()
|
||||
}
|
||||
}
|
||||
|
||||
return activeFightsCache!!
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun create(
|
||||
event: Int,
|
||||
from: Timestamp,
|
||||
spielmodus: String,
|
||||
map: String,
|
||||
blueTeam: Int,
|
||||
redTeam: Int,
|
||||
spectatePort: Int?
|
||||
) = useDb {
|
||||
get(
|
||||
EventFightTable.insertAndGetId {
|
||||
it[eventId] = EntityID(event, EventTable)
|
||||
it[startTime] = from.toInstant()
|
||||
it[gamemode] = spielmodus
|
||||
it[EventFightTable.map] = map
|
||||
it[teamBlue] = EntityID(blueTeam, TeamTable)
|
||||
it[teamRed] = EntityID(redTeam, TeamTable)
|
||||
it[EventFightTable.spectatePort] = spectatePort
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val fightID by EventFightTable.id.transform({ EntityID(it, EventFightTable) }, { it.value })
|
||||
var teamBlue by EventFightTable.teamBlue.transform({ EntityID(it, TeamTable) }, { it.value })
|
||||
var teamRed by EventFightTable.teamRed.transform({ EntityID(it, TeamTable) }, { it.value })
|
||||
private var fightErgebnis by EventFightTable.ergebnis
|
||||
var ergebnis: Int
|
||||
get() = fightErgebnis
|
||||
set(value) = useDb {
|
||||
fightErgebnis = value
|
||||
}
|
||||
var eventID by EventFightTable.eventId.transform({ EntityID(it, EventTable) }, { it.value })
|
||||
var startTime by EventFightTable.startTime.transform({ it.toInstant() }, { Timestamp.from(it) })
|
||||
var spielmodus by EventFightTable.gamemode
|
||||
var map by EventFightTable.map
|
||||
var groupId by EventFightTable.groupId
|
||||
val group by lazy { Optional.ofNullable(groupId).map { EventGroup[it] } }
|
||||
var spectatePort by EventFightTable.spectatePort
|
||||
private var fightStat by EventFightTable.fight.transform({ it?.let { EntityID(it, EventFightTable) } }, { it?.value })
|
||||
var fight: Int?
|
||||
get() = fightStat
|
||||
set(value) = useDb {
|
||||
fightStat = value
|
||||
}
|
||||
val dependents by lazy { useDb { EventRelation.getFightRelations(this@EventFight) } }
|
||||
|
||||
val winner: Team?
|
||||
get() = if (ergebnis == 1) Team[teamBlue] else if (ergebnis == 2) Team[teamRed] else null
|
||||
val losser: Team?
|
||||
get() = if (ergebnis == 1) Team[teamRed] else if (ergebnis == 2) Team[teamBlue] else null
|
||||
|
||||
fun setGroup(group: Int?) = useDb { groupId = group?.let { EntityID(it, EventGroupTable) } }
|
||||
fun hasFinished() = fight != null || ergebnis != 0
|
||||
|
||||
fun reschedule() = useDb {
|
||||
startTime = Timestamp.from(Instant.now().plusSeconds(30))
|
||||
}
|
||||
|
||||
override fun hashCode() = fightID
|
||||
override fun equals(other: Any?) = other is EventFight && other.fightID == fightID
|
||||
override fun compareTo(other: EventFight): Int = startTime.compareTo(other.startTime)
|
||||
|
||||
fun update(
|
||||
startTime: Timestamp,
|
||||
spielmodus: String,
|
||||
map: String,
|
||||
teamBlue: Int,
|
||||
teamRed: Int,
|
||||
spectatePort: Int?
|
||||
) = useDb {
|
||||
this@EventFight.startTime = startTime
|
||||
this@EventFight.spielmodus = spielmodus
|
||||
this@EventFight.map = map
|
||||
this@EventFight.teamBlue = teamBlue
|
||||
this@EventFight.teamRed = teamRed
|
||||
this@EventFight.spectatePort = spectatePort
|
||||
}
|
||||
}
|
||||
@@ -1,173 +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.sql;
|
||||
|
||||
import de.steamwar.sql.internal.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class EventGroup {
|
||||
static {
|
||||
SqlTypeMapper.ordinalEnumMapper(EventGroupType.class);
|
||||
}
|
||||
|
||||
private static final Table<EventGroup> table = new Table<>(EventGroup.class);
|
||||
|
||||
private static final SelectStatement<EventGroup> get = table.select(Table.PRIMARY);
|
||||
private static final SelectStatement<EventGroup> byEvent = new SelectStatement<>(table, "SELECT * FROM EventGroup WHERE EventID = ?");
|
||||
|
||||
private static final Statement insert = table.insertFields(true, "EventID", "Name", "Type");
|
||||
private static final Statement update = table.update(Table.PRIMARY, "Name", "Type", "PointsPerWin", "PointsPerLoss", "PointsPerDraw");
|
||||
private static final Statement delete = table.delete(Table.PRIMARY);
|
||||
|
||||
public static List<EventGroup> get(Event eventID) {
|
||||
return byEvent.listSelect(eventID.getEventID());
|
||||
}
|
||||
|
||||
public static EventGroup create(Event event, String name, EventGroupType type) {
|
||||
int key = insert.insertGetKey(event.getEventID(), name, type);
|
||||
return EventGroup.get(key).get();
|
||||
}
|
||||
|
||||
public static Optional<EventGroup> get(int id) {
|
||||
return Optional.ofNullable(get.select(id));
|
||||
}
|
||||
|
||||
@Field(keys = Table.PRIMARY)
|
||||
private final int id;
|
||||
|
||||
@Field(keys = "EVENT_NAME")
|
||||
private int eventID;
|
||||
|
||||
@Field(keys = "EVENT_NAME")
|
||||
private String name;
|
||||
|
||||
@Field
|
||||
private EventGroupType type;
|
||||
|
||||
@Field
|
||||
private int pointsPerWin;
|
||||
|
||||
@Field
|
||||
private int pointsPerLoss;
|
||||
|
||||
@Field
|
||||
private int pointsPerDraw;
|
||||
|
||||
public EventGroup(int id, int eventID, String name, EventGroupType type, int pointsPerWin, int pointsPerLoss, int pointsPerDraw) {
|
||||
this.id = id;
|
||||
this.eventID = eventID;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.pointsPerWin = pointsPerWin;
|
||||
this.pointsPerLoss = pointsPerLoss;
|
||||
this.pointsPerDraw = pointsPerDraw;
|
||||
}
|
||||
|
||||
private Map<Team, Integer> points;
|
||||
|
||||
public List<EventFight> getFights() {
|
||||
return EventFight.get(this);
|
||||
}
|
||||
|
||||
public Set<Integer> getTeamsId() {
|
||||
return getFights().stream().flatMap(fight -> Stream.of(fight.getTeamBlue(), fight.getTeamRed()))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Set<Team> getTeams() {
|
||||
return getTeamsId().stream().map(Team::get).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Optional<EventFight> getLastFight() {
|
||||
return EventFight.getLast(this);
|
||||
}
|
||||
|
||||
public List<EventRelation> getDependents() {
|
||||
return EventRelation.getGroupRelations(this);
|
||||
}
|
||||
|
||||
public Map<Team, Integer> calculatePoints() {
|
||||
if (points == null) {
|
||||
Map<Integer, Integer> p = getTeamsId().stream().collect(Collectors.toMap(team -> team, team -> 0));
|
||||
|
||||
for (EventFight fight : getFights()) {
|
||||
int blueTeamAdd = 0;
|
||||
int redTeamAdd = 0;
|
||||
|
||||
if (!fight.hasFinished()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (fight.getErgebnis()) {
|
||||
case 1:
|
||||
blueTeamAdd += pointsPerWin;
|
||||
redTeamAdd += pointsPerLoss;
|
||||
break;
|
||||
case 2:
|
||||
blueTeamAdd += pointsPerLoss;
|
||||
redTeamAdd += pointsPerWin;
|
||||
break;
|
||||
case 0:
|
||||
if (fight.getFightID() != 0) {
|
||||
blueTeamAdd += pointsPerDraw;
|
||||
redTeamAdd += pointsPerDraw;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
p.put(fight.getTeamBlue(), p.get(fight.getTeamBlue()) + blueTeamAdd);
|
||||
p.put(fight.getTeamRed(), p.get(fight.getTeamRed()) + redTeamAdd);
|
||||
}
|
||||
|
||||
points = p.entrySet().stream().collect(Collectors.toMap(integerIntegerEntry -> Team.get(integerIntegerEntry.getKey()), Map.Entry::getValue));
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
public void update(String name, EventGroupType type, int pointsPerWin, int pointsPerLoss, int pointsPerDraw) {
|
||||
update.update(name, type, pointsPerWin, pointsPerLoss, pointsPerDraw, id);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.pointsPerWin = pointsPerWin;
|
||||
this.pointsPerLoss = pointsPerLoss;
|
||||
this.pointsPerDraw = pointsPerDraw;
|
||||
}
|
||||
|
||||
public boolean needsTieBreak() {
|
||||
return calculatePoints().values().stream().sorted().limit(2).distinct().count() < 2;
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
delete.update(id);
|
||||
}
|
||||
|
||||
public static enum EventGroupType {
|
||||
GROUP_STAGE,
|
||||
ELIMINATION_STAGE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* 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.sql
|
||||
|
||||
import de.steamwar.sql.internal.useDb
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import java.util.Optional
|
||||
|
||||
object EventGroupTable : IntIdTable("EventGroup", "Id") {
|
||||
val event = reference("EventID", EventTable)
|
||||
val name = varchar("Name", 64)
|
||||
val type = enumeration("Type", EventGroup.EventGroupType::class)
|
||||
val pointsPerWin = integer("PointsPerWin")
|
||||
val pointsPerLoss = integer("PointsPerLoss")
|
||||
val pointsPerDraw = integer("PointsPerDraw")
|
||||
}
|
||||
|
||||
class EventGroup(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<EventGroup>(EventGroupTable) {
|
||||
|
||||
@JvmStatic
|
||||
fun get(event: Event) = useDb { find { EventGroupTable.event eq event.id }.toList() }
|
||||
|
||||
@JvmStatic
|
||||
fun byId(groupId: Int) = useDb { Optional.ofNullable(findById(groupId)) }
|
||||
|
||||
@JvmStatic
|
||||
fun create(event: Event, name: String, type: EventGroupType) = useDb {
|
||||
new {
|
||||
this.eventID = event.id.value
|
||||
this.groupName = name
|
||||
this.groupType = type
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var eventID by EventGroupTable.event.transform({ EntityID(it, EventTable) }, { it.value })
|
||||
private set
|
||||
private var groupName by EventGroupTable.name
|
||||
private var groupType by EventGroupTable.type
|
||||
private var groupPointsPerWin by EventGroupTable.pointsPerWin
|
||||
private var groupPointsPerLoss by EventGroupTable.pointsPerLoss
|
||||
private var groupPointsPerDraw by EventGroupTable.pointsPerDraw
|
||||
val fights by lazy { useDb { EventFight.find { EventFightTable.groupId eq id }.toList() } }
|
||||
val teamsId by lazy { fights.flatMap { listOf(it.teamBlue, it.teamRed) }.toSet() }
|
||||
|
||||
var name: String
|
||||
get() = groupName
|
||||
set(value) {
|
||||
groupName = value
|
||||
}
|
||||
var type: EventGroupType
|
||||
get() = groupType
|
||||
set(value) {
|
||||
groupType = value
|
||||
}
|
||||
var pointsPerWin: Int
|
||||
get() = groupPointsPerWin
|
||||
set(value) {
|
||||
groupPointsPerWin = value
|
||||
}
|
||||
var pointsPerLoss: Int
|
||||
get() = groupPointsPerLoss
|
||||
set(value) {
|
||||
groupPointsPerLoss = value
|
||||
}
|
||||
var pointsPerDraw: Int
|
||||
get() = groupPointsPerDraw
|
||||
set(value) {
|
||||
groupPointsPerDraw = value
|
||||
}
|
||||
val dependents by lazy { EventRelation.getGroupRelations(this) }
|
||||
val lastFight by lazy { Optional.ofNullable(fights.maxByOrNull { it.startTime }) }
|
||||
|
||||
fun getId() = id.value
|
||||
|
||||
private var points: Map<Team, Int>? = null
|
||||
|
||||
fun calculatePoints(): Map<Team, Int> {
|
||||
if (points == null) {
|
||||
val p: MutableMap<Int, Int> = teamsId.associateWith { 0 }.toMutableMap()
|
||||
|
||||
for (fight in fights) {
|
||||
var blueTeamAdd = 0
|
||||
var redTeamAdd = 0
|
||||
|
||||
if (!fight.hasFinished()) {
|
||||
continue
|
||||
}
|
||||
|
||||
when (fight.ergebnis) {
|
||||
1 -> {
|
||||
blueTeamAdd += pointsPerWin
|
||||
redTeamAdd += pointsPerLoss
|
||||
}
|
||||
2 -> {
|
||||
blueTeamAdd += pointsPerLoss
|
||||
redTeamAdd += pointsPerWin
|
||||
}
|
||||
0 -> if (fight.fight != null) {
|
||||
blueTeamAdd += pointsPerDraw
|
||||
redTeamAdd += pointsPerDraw
|
||||
}
|
||||
}
|
||||
|
||||
p[fight.teamBlue] = p[fight.teamBlue]?.plus(blueTeamAdd) ?: blueTeamAdd
|
||||
p[fight.teamRed] = p[fight.teamRed]?.plus(redTeamAdd) ?: redTeamAdd
|
||||
}
|
||||
|
||||
return p.mapKeys { Team.byId(it.key) }.also { points = it }
|
||||
} else {
|
||||
return points!!
|
||||
}
|
||||
}
|
||||
|
||||
fun needsTieBreak() = calculatePoints().values.let { it.size == it.toSet().size }
|
||||
|
||||
fun update(name: String, type: EventGroupType, pointsPerWin: Int, pointsPerLoss: Int, pointsPerDraw: Int) = useDb {
|
||||
this@EventGroup.name = name
|
||||
this@EventGroup.type = type
|
||||
this@EventGroup.pointsPerWin = pointsPerWin
|
||||
this@EventGroup.pointsPerLoss = pointsPerLoss
|
||||
this@EventGroup.pointsPerDraw = pointsPerDraw
|
||||
}
|
||||
|
||||
enum class EventGroupType {
|
||||
GROUP_STAGE,
|
||||
ELIMINATION_STAGE
|
||||
}
|
||||
}
|
||||
@@ -1,192 +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.sql;
|
||||
|
||||
import de.steamwar.sql.internal.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class EventRelation {
|
||||
|
||||
static {
|
||||
SqlTypeMapper.ordinalEnumMapper(FightTeam.class);
|
||||
SqlTypeMapper.ordinalEnumMapper(FromType.class);
|
||||
}
|
||||
|
||||
private static final Table<EventRelation> table = new Table<>(EventRelation.class);
|
||||
|
||||
private static final SelectStatement<EventRelation> get = new SelectStatement<>(table, "SELECT * FROM EventRelation WHERE FromType = ? AND FromId = ?");
|
||||
private static final SelectStatement<EventRelation> byId = new SelectStatement<>(table, "SELECT * FROM EventRelation WHERE id = ?");
|
||||
private static final SelectStatement<EventRelation> byEvent = new SelectStatement<>(table, "SELECT ER.* FROM EventRelation ER JOIN EventFight EF ON EF.fightId = ER.fightId WHERE EF.EventID = ?");
|
||||
private static final Statement insert = table.insertFields(true, "fightId", "fightTeam", "fromType", "fromId", "fromPlace");
|
||||
private static final Statement update = table.update(Table.PRIMARY, "fromType", "fromId", "fromPlace");
|
||||
private static final Statement updateTeam = table.update(Table.PRIMARY, "fightTeam");
|
||||
private static final Statement delete = table.delete(Table.PRIMARY);
|
||||
|
||||
public static List<EventRelation> get(Event event) {
|
||||
return byEvent.listSelect(event.getEventID());
|
||||
}
|
||||
|
||||
public static EventRelation get(int id) {
|
||||
return byId.select(id);
|
||||
}
|
||||
|
||||
public static List<EventRelation> getFightRelations(EventFight fight) {
|
||||
return get.listSelect(FromType.FIGHT, fight.getFightID());
|
||||
}
|
||||
|
||||
public static List<EventRelation> getGroupRelations(EventGroup group) {
|
||||
return get.listSelect(FromType.GROUP, group.getId());
|
||||
}
|
||||
|
||||
public static EventRelation create(EventFight fight, FightTeam fightTeam, FromType fromType, int fromId, int fromPlace) {
|
||||
int id = insert.insertGetKey(fight.getFightID(), fightTeam, fromType, fromId, fromPlace);
|
||||
return get(id);
|
||||
}
|
||||
|
||||
@Field(keys = Table.PRIMARY)
|
||||
private final int id;
|
||||
|
||||
@Field
|
||||
private int fightId;
|
||||
|
||||
@Field
|
||||
private FightTeam fightTeam;
|
||||
|
||||
@Field
|
||||
private FromType fromType;
|
||||
|
||||
@Field
|
||||
private int fromId;
|
||||
|
||||
@Field
|
||||
private int fromPlace;
|
||||
|
||||
public EventFight getFight() {
|
||||
return EventFight.get(fightId);
|
||||
}
|
||||
|
||||
public Optional<EventFight> getFromFight() {
|
||||
if(fromType == FromType.FIGHT) {
|
||||
return Optional.of(EventFight.get(fromId));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<EventGroup> getFromGroup() {
|
||||
if(fromType == FromType.GROUP) {
|
||||
return EventGroup.get(fromId);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
delete.update(id);
|
||||
}
|
||||
|
||||
public void setUpdateTeam(FightTeam team) {
|
||||
updateTeam.update(id, team);
|
||||
this.fightTeam = team;
|
||||
}
|
||||
|
||||
public void setFromFight(EventFight fight, int place) {
|
||||
setFrom(fight.getFightID(), place, FromType.FIGHT);
|
||||
}
|
||||
|
||||
public void setFromGroup(EventGroup group, int place) {
|
||||
setFrom(group.getId(), place, FromType.GROUP);
|
||||
}
|
||||
|
||||
private void setFrom(int fightId, int place, FromType type) {
|
||||
update.update(type, fightId, place, id);
|
||||
this.fromType = type;
|
||||
this.fromId = fightId;
|
||||
this.fromPlace = place;
|
||||
}
|
||||
|
||||
public Optional<Team> getAdvancingTeam() {
|
||||
if (fromType == FromType.FIGHT) {
|
||||
if (fromPlace == 0) {
|
||||
return getFromFight().flatMap(EventFight::getWinner);
|
||||
} else {
|
||||
return getFromFight().flatMap(EventFight::getLosser);
|
||||
}
|
||||
} else if (fromType == FromType.GROUP) {
|
||||
return getFromGroup().map(EventGroup::calculatePoints)
|
||||
.flatMap(points -> points.entrySet().stream()
|
||||
.sorted(Map.Entry.<Team, Integer>comparingByValue().reversed())
|
||||
.skip(fromPlace)
|
||||
.findFirst()
|
||||
.map(Map.Entry::getKey));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean apply() {
|
||||
Optional<Integer> team = getAdvancingTeam().map(Team::getTeamId);
|
||||
if(!team.isPresent())
|
||||
return false;
|
||||
|
||||
EventFight fight = getFight();
|
||||
if(fightTeam == FightTeam.RED) {
|
||||
fight.update(
|
||||
fight.getStartTime(),
|
||||
fight.getSpielmodus(),
|
||||
fight.getMap(),
|
||||
fight.getTeamBlue(),
|
||||
team.get(),
|
||||
fight.getSpectatePort()
|
||||
);
|
||||
} else {
|
||||
fight.update(
|
||||
fight.getStartTime(),
|
||||
fight.getSpielmodus(),
|
||||
fight.getMap(),
|
||||
team.get(),
|
||||
fight.getTeamRed(),
|
||||
fight.getSpectatePort()
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static enum FightTeam {
|
||||
BLUE,
|
||||
RED
|
||||
}
|
||||
|
||||
public static enum FromType {
|
||||
FIGHT,
|
||||
GROUP
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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.sql
|
||||
|
||||
import de.steamwar.sql.internal.useDb
|
||||
import org.jetbrains.exposed.v1.core.and
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.jdbc.select
|
||||
import java.util.Optional
|
||||
|
||||
object EventRelationTable : IntIdTable("EventRelation") {
|
||||
val fightId = reference("FightId", EventFightTable)
|
||||
val fightTeam = enumeration("FightTeam", EventRelation.FightTeam::class)
|
||||
val fromType = enumeration("FromType", EventRelation.FromType::class)
|
||||
val fromId = integer("FromId")
|
||||
val fromPlace = integer("FromPlace")
|
||||
}
|
||||
|
||||
class EventRelation(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<EventRelation>(EventRelationTable) {
|
||||
@JvmStatic
|
||||
fun get(event: Event) = useDb {
|
||||
EventRelationTable.innerJoin(EventFightTable)
|
||||
.select(EventRelationTable.columns)
|
||||
.where { EventFightTable.eventId eq event.id }
|
||||
.map { wrapRow(it) }
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun byId(id: Int) = useDb { findById(id) }
|
||||
|
||||
@JvmStatic
|
||||
fun getFightRelations(fight: EventFight) =
|
||||
useDb { find { (EventRelationTable.fromId eq fight.id.value) and (EventRelationTable.fromType eq FromType.FIGHT) } }
|
||||
|
||||
@JvmStatic
|
||||
fun getGroupRelations(group: EventGroup) =
|
||||
useDb { find { (EventRelationTable.fromId eq group.id.value) and (EventRelationTable.fromType eq FromType.GROUP) } }
|
||||
|
||||
@JvmStatic
|
||||
fun create(fight: EventFight, fightTeam: FightTeam, fromType: FromType, fromId: Int, fromPlace: Int) = useDb {
|
||||
new {
|
||||
this.fightEntityId = fight.id
|
||||
this.fightTeam = fightTeam
|
||||
this.fromType = fromType
|
||||
this.fromId = fromId
|
||||
this.fromPlace = fromPlace
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var fightEntityId by EventRelationTable.fightId
|
||||
var fightId: Int
|
||||
get() = fightEntityId.value
|
||||
set(value) = useDb { fightEntityId = EntityID(value, EventFightTable) }
|
||||
val fight by lazy { useDb { EventFight[fightEntityId] } }
|
||||
var fightTeam by EventRelationTable.fightTeam
|
||||
private set
|
||||
var fromType by EventRelationTable.fromType
|
||||
private set
|
||||
var fromId by EventRelationTable.fromId
|
||||
private set
|
||||
|
||||
var fromFightId: Int?
|
||||
get() = if (fromType == FromType.FIGHT) fromId else null
|
||||
set(value) = useDb {
|
||||
fromType = FromType.FIGHT
|
||||
fromId = value!!
|
||||
}
|
||||
val fromFight: EventFight?
|
||||
get() = fromFightId?.let { EventFight[it] }
|
||||
var fromGroupId: Int?
|
||||
get() = if (fromType == FromType.GROUP) fromId else null
|
||||
set(value) = useDb {
|
||||
fromType = FromType.GROUP
|
||||
fromId = value!!
|
||||
}
|
||||
val fromGroup: EventGroup?
|
||||
get() = fromGroupId?.let { EventGroup[it] }
|
||||
var fromPlace by EventRelationTable.fromPlace
|
||||
private set
|
||||
|
||||
fun getId() = id.value
|
||||
fun setUpdateTeam(team: FightTeam) = useDb {
|
||||
fightTeam = team
|
||||
}
|
||||
|
||||
fun setFromFight(fight: EventFight, place: Int) = useDb {
|
||||
fromType = FromType.FIGHT
|
||||
fromId = fight.id.value
|
||||
fromPlace = place
|
||||
}
|
||||
|
||||
fun setFromGroup(group: EventGroup, place: Int) = useDb {
|
||||
fromType = FromType.GROUP
|
||||
fromId = group.id.value
|
||||
fromPlace = place
|
||||
}
|
||||
|
||||
fun getAdvancingTeam(): Team? = when(fromType) {
|
||||
FromType.FIGHT -> if (fromPlace == 0) fromFight?.winner else fromFight?.losser
|
||||
FromType.GROUP -> fromGroup?.calculatePoints()?.toList()?.sortedBy { (_, v) -> v }?.reversed()?.elementAt(fromPlace)?.first
|
||||
}
|
||||
|
||||
fun apply(): Boolean {
|
||||
val team = getAdvancingTeam() ?: return false
|
||||
|
||||
useDb {
|
||||
when(fightTeam) {
|
||||
FightTeam.BLUE -> fight.teamBlue = team.teamId
|
||||
FightTeam.RED -> fight.teamRed = team.teamId
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
enum class FightTeam {
|
||||
BLUE, RED
|
||||
}
|
||||
|
||||
enum class FromType {
|
||||
FIGHT, GROUP
|
||||
}
|
||||
}
|
||||
@@ -1,140 +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.sql;
|
||||
|
||||
import de.steamwar.sql.internal.Field;
|
||||
import de.steamwar.sql.internal.SelectStatement;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
import de.steamwar.sql.internal.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class Team {
|
||||
|
||||
private static final Map<Integer, Team> teamCache = new HashMap<>();
|
||||
|
||||
public static void clear() {
|
||||
teamCache.clear();
|
||||
}
|
||||
|
||||
private static final Table<Team> table = new Table<>(Team.class);
|
||||
private static final SelectStatement<Team> byId = table.select(Table.PRIMARY);
|
||||
private static final SelectStatement<Team> byName = new SelectStatement<>(table, "SELECT * FROM Team WHERE (lower(TeamName) = ? OR lower(TeamKuerzel) = ?) AND NOT TeamDeleted");
|
||||
private static final SelectStatement<Team> all = table.selectFields("TeamDeleted");
|
||||
private static final Statement insert = table.insertFields("TeamKuerzel", "TeamName");
|
||||
private static final Statement update = table.update(Table.PRIMARY, "TeamKuerzel", "TeamName", "TeamColor", "Address", "Port");
|
||||
private static final Statement delete = table.update(Table.PRIMARY, "TeamDeleted");
|
||||
private static final Statement getSize = new Statement("SELECT COUNT(id) FROM UserData WHERE Team = ?");
|
||||
|
||||
@Field(keys = {Table.PRIMARY}, autoincrement = true)
|
||||
@Getter
|
||||
private final int teamId;
|
||||
@Field
|
||||
@Getter
|
||||
private String teamKuerzel;
|
||||
@Field
|
||||
@Getter
|
||||
private String teamName;
|
||||
@Field(def = "'8'")
|
||||
@Getter
|
||||
private String teamColor;
|
||||
@Field(nullable = true)
|
||||
@Getter
|
||||
private String address;
|
||||
@Field(def = "'25565'")
|
||||
@Getter
|
||||
private int port;
|
||||
@Field(def = "0")
|
||||
private boolean teamDeleted;
|
||||
|
||||
public static void create(String kuerzel, String name){
|
||||
insert.update(kuerzel, name);
|
||||
}
|
||||
|
||||
public static Team get(int id) {
|
||||
return teamCache.computeIfAbsent(id, byId::select);
|
||||
}
|
||||
|
||||
public static Team get(String name){
|
||||
// No cache lookup due to low frequency use
|
||||
name = name.toLowerCase();
|
||||
return byName.select(name, name);
|
||||
}
|
||||
|
||||
public static List<Team> getAll(){
|
||||
clear();
|
||||
List<Team> teams = all.listSelect(false);
|
||||
teams.forEach(team -> teamCache.put(team.getTeamId(), team));
|
||||
return teams;
|
||||
}
|
||||
|
||||
public List<Integer> getMembers(){
|
||||
return SteamwarUser.getTeam(teamId).stream().map(SteamwarUser::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return getSize.select(rs -> {
|
||||
rs.next();
|
||||
return rs.getInt("COUNT(id)");
|
||||
}, teamId);
|
||||
}
|
||||
|
||||
public void disband(SteamwarUser user){
|
||||
user.setLeader(false);
|
||||
delete.update(true, teamId);
|
||||
teamCache.remove(teamId);
|
||||
TeamTeilnahme.deleteFuture(teamId);
|
||||
}
|
||||
|
||||
public void setTeamKuerzel(String teamKuerzel) {
|
||||
this.teamKuerzel = teamKuerzel;
|
||||
updateDB();
|
||||
}
|
||||
|
||||
public void setTeamName(String teamName) {
|
||||
this.teamName = teamName;
|
||||
updateDB();
|
||||
}
|
||||
|
||||
public void setTeamColor(String teamColor) {
|
||||
this.teamColor = teamColor;
|
||||
updateDB();
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
updateDB();
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
updateDB();
|
||||
}
|
||||
|
||||
private void updateDB(){
|
||||
update.update(teamKuerzel, teamName, teamColor, address, port, teamId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.sql
|
||||
|
||||
import de.steamwar.sql.internal.useDb
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.core.lowerCase
|
||||
import org.jetbrains.exposed.v1.dao.IntEntity
|
||||
import org.jetbrains.exposed.v1.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.v1.jdbc.select
|
||||
|
||||
object TeamTable : IntIdTable("Team", "TeamID") {
|
||||
val kuerzel = varchar("TeamKuerzel", 10)
|
||||
val color = char("TeamColor", 1)
|
||||
val name = varchar("TeamName", 16)
|
||||
val deleted = bool("TeamDeleted")
|
||||
val address = text("Address")
|
||||
val port = ushort("Port")
|
||||
}
|
||||
|
||||
class Team(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<Team>(TeamTable) {
|
||||
private val teamCache = mutableMapOf<Int, Team>()
|
||||
|
||||
@JvmStatic
|
||||
fun clear() = teamCache.clear()
|
||||
|
||||
@JvmStatic
|
||||
fun byId(id: Int) = teamCache.computeIfAbsent(id) { useDb { Team[id] } }
|
||||
|
||||
@JvmStatic
|
||||
fun get(name: String) = useDb { find { TeamTable.name.lowerCase() eq name.lowercase() }.firstOrNull() }
|
||||
|
||||
@JvmStatic
|
||||
fun getAll() = useDb { all().toList() }
|
||||
|
||||
@JvmStatic
|
||||
fun create(kuerzel: String, name: String) = useDb {
|
||||
new {
|
||||
this.kuerzel = kuerzel
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val teamId by TeamTable.id.transform({ EntityID(it, TeamTable) }, { it.value })
|
||||
private var kuerzel by TeamTable.kuerzel
|
||||
private var color by TeamTable.color
|
||||
private var name by TeamTable.name
|
||||
var deleted by TeamTable.deleted
|
||||
private set
|
||||
private var teamAddress by TeamTable.address
|
||||
private var teamPort by TeamTable.port
|
||||
val members by lazy { SteamwarUserTable.select(SteamwarUserTable.id).where { SteamwarUserTable.team eq teamId }.map { it[SteamwarUserTable.id].value } }
|
||||
|
||||
fun size() = useDb { SteamwarUser.find { SteamwarUserTable.team eq teamId }.count().toInt() }
|
||||
fun disband(user: SteamwarUser) = useDb {
|
||||
user.team = 0
|
||||
deleted = true
|
||||
teamCache.remove(teamId)
|
||||
TeamTeilnahme.deleteFuture(teamId)
|
||||
}
|
||||
|
||||
var teamKuerzel: String
|
||||
get() = kuerzel
|
||||
set(value) = useDb {
|
||||
kuerzel = value
|
||||
}
|
||||
|
||||
var teamColor: String
|
||||
get() = color
|
||||
set(value) = useDb {
|
||||
color = value
|
||||
}
|
||||
|
||||
var teamName: String
|
||||
get() = name
|
||||
set(value) = useDb {
|
||||
name = value
|
||||
}
|
||||
|
||||
var address: String
|
||||
get() = teamAddress
|
||||
set(value) = useDb {
|
||||
teamAddress = value
|
||||
}
|
||||
|
||||
var port: Int
|
||||
get() = teamPort.toInt()
|
||||
set(value) = useDb {
|
||||
teamPort = value.toUShort()
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class TeamTeilnahme {
|
||||
}
|
||||
|
||||
public static Set<Team> getTeams(int eventID){
|
||||
return selectTeams.listSelect(eventID).stream().map(tt -> Team.get(tt.teamId)).collect(Collectors.toSet()); // suboptimal performance (O(n) database queries)
|
||||
return selectTeams.listSelect(eventID).stream().map(tt -> Team.byId(tt.teamId)).collect(Collectors.toSet()); // suboptimal performance (O(n) database queries)
|
||||
}
|
||||
|
||||
public static Set<Event> getEvents(int teamID){
|
||||
|
||||
@@ -211,15 +211,15 @@ public class Config {
|
||||
|
||||
int eventKampfID = Integer.parseInt(System.getProperty("fightID", "0"));
|
||||
if(eventKampfID >= 1){
|
||||
EventKampf = EventFight.get(eventKampfID);
|
||||
EventKampf = EventFight.byId(eventKampfID);
|
||||
if(EventKampf == null){
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight");
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
|
||||
assert EventKampf != null;
|
||||
Team team1 = Team.get(EventKampf.getTeamBlue());
|
||||
Team team2 = Team.get(EventKampf.getTeamRed());
|
||||
Team team1 = Team.byId(EventKampf.getTeamBlue());
|
||||
Team team2 = Team.byId(EventKampf.getTeamRed());
|
||||
|
||||
if(team1 == null || team2 == null){
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Failed to load Team");
|
||||
|
||||
+1
-1
@@ -549,7 +549,7 @@ public class PacketProcessor implements Listener {
|
||||
}
|
||||
|
||||
private void pasteForTeam(int teamId, FightTeam fightTeam){
|
||||
Team team = Team.get(teamId);
|
||||
Team team = Team.byId(teamId);
|
||||
fightTeam.setPrefixAndName("§" + team.getTeamColor(), team.getTeamKuerzel());
|
||||
fightTeam.pasteTeamName();
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public interface ParticleRequirement {
|
||||
|
||||
static ParticleRequirement specificTeam(int teamId) {
|
||||
if (teamId == 0) return NO_REQUIRMENT;
|
||||
String teamKuerzel = Team.get(teamId).getTeamKuerzel();
|
||||
String teamKuerzel = Team.byId(teamId).getTeamKuerzel();
|
||||
return new ParticleRequirement() {
|
||||
@Override
|
||||
public String getRequirementName(Player player) {
|
||||
@@ -216,7 +216,7 @@ public interface ParticleRequirement {
|
||||
|
||||
static ParticleRequirement easterEventSpecificTeam(int teamId) {
|
||||
if (teamId == 0) return NO_REQUIRMENT;
|
||||
String teamKuerzel = Team.get(teamId).getTeamKuerzel();
|
||||
String teamKuerzel = Team.byId(teamId).getTeamKuerzel();
|
||||
return new ParticleRequirement() {
|
||||
@Override
|
||||
public String getRequirementName(Player player) {
|
||||
|
||||
@@ -124,15 +124,15 @@ public class Config {
|
||||
|
||||
EventKampfID = Integer.parseInt(System.getProperty("fightID", "0"));
|
||||
if (EventKampfID >= 1) {
|
||||
EventKampf = EventFight.get(EventKampfID);
|
||||
EventKampf = EventFight.byId(EventKampfID);
|
||||
if (EventKampf == null) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight");
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
assert EventKampf != null;
|
||||
|
||||
Team team1 = Team.get(EventKampf.getTeamBlue());
|
||||
Team team2 = Team.get(EventKampf.getTeamRed());
|
||||
Team team1 = Team.byId(EventKampf.getTeamBlue());
|
||||
Team team2 = Team.byId(EventKampf.getTeamRed());
|
||||
|
||||
if (team1 == null || team2 == null) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Failed to load Team");
|
||||
|
||||
+2
-2
@@ -117,7 +117,7 @@ public class MemberPart extends SWCommand {
|
||||
@Register("addteam")
|
||||
public void addTeam(Player player, @Validator("isOwnerValidator") SchematicNode node) {
|
||||
SteamwarUser user = getUser(player);
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
if (team == null || team.getTeamId() == 0) {
|
||||
SchematicSystem.MESSAGE.send("COMMAND_ADD_TEAM_NOT_IN_TEAM", player);
|
||||
return;
|
||||
@@ -129,7 +129,7 @@ public class MemberPart extends SWCommand {
|
||||
@Register("delteam")
|
||||
public void remTeam(Player player, @Validator("isOwnerValidator") SchematicNode node) {
|
||||
SteamwarUser user = getUser(player);
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
if (team == null || team.getTeamId() == 0) {
|
||||
SchematicSystem.MESSAGE.send("COMMAND_DEL_TEAM_NOT_IN_TEAM", player);
|
||||
return;
|
||||
|
||||
@@ -53,12 +53,12 @@ data class TNTLeagueConfig(
|
||||
|
||||
init {
|
||||
if (eventFightId != null && eventFightId > 0) {
|
||||
eventFight = EventFight.get(eventFightId) ?: throw IllegalArgumentException("EventFight with ID $eventFightId not found")
|
||||
eventFight = EventFight.byId(eventFightId) ?: throw IllegalArgumentException("EventFight with ID $eventFightId not found")
|
||||
|
||||
event = Event.byId(eventFight.eventID)!!
|
||||
|
||||
eventTeamBlue = Team.get(eventFight.teamBlue)
|
||||
eventTeamRed = Team.get(eventFight.teamRed)
|
||||
eventTeamBlue = Team.byId(eventFight.teamBlue)
|
||||
eventTeamRed = Team.byId(eventFight.teamRed)
|
||||
|
||||
blueTeam = TeamConfig(TNTLeagueWorldConfig.blueTeam, SubMessage("PLAIN_STRING", "§${eventTeamBlue.teamColor}${eventTeamBlue.teamName}"), eventTeamBlue.teamColor[0])
|
||||
redTeam = TeamConfig(TNTLeagueWorldConfig.redTeam, SubMessage("PLAIN_STRING", "§${eventTeamRed.teamColor}${eventTeamRed.teamName}"), eventTeamRed.teamColor[0])
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Config {
|
||||
EVENT_KAMPF_ID = Integer.parseInt(System.getProperty("fightID", "0"));
|
||||
|
||||
if (event()) {
|
||||
EVENT_FIGHT = EventFight.get(EVENT_KAMPF_ID);
|
||||
EVENT_FIGHT = EventFight.byId(EVENT_KAMPF_ID);
|
||||
|
||||
if (EVENT_FIGHT == null) {
|
||||
Bukkit.shutdown();
|
||||
|
||||
@@ -64,8 +64,8 @@ public class EventStarter {
|
||||
|
||||
EventFight next;
|
||||
while((next = nextFight(fights)) != null){
|
||||
Team blue = Team.get(next.getTeamBlue());
|
||||
Team red = Team.get(next.getTeamRed());
|
||||
Team blue = Team.byId(next.getTeamBlue());
|
||||
Team red = Team.byId(next.getTeamRed());
|
||||
|
||||
//Don't start EventServer if not the event bungee
|
||||
String command;
|
||||
|
||||
@@ -110,7 +110,7 @@ public class ServerStarter {
|
||||
fightMap = eventFight.getMap();
|
||||
gameMode = eventFight.getSpielmodus();
|
||||
|
||||
String serverName = Team.get(eventFight.getTeamBlue()).getTeamKuerzel() + "_vs_" + Team.get(eventFight.getTeamRed()).getTeamKuerzel();
|
||||
String serverName = Team.byId(eventFight.getTeamBlue()).getTeamKuerzel() + "_vs_" + Team.byId(eventFight.getTeamRed()).getTeamKuerzel();
|
||||
serverNameProvider = port -> serverName;
|
||||
worldName = serverToWorldName(serverName + eventFight.getStartTime().toLocalDateTime().format(DateTimeFormatter.ISO_TIME));
|
||||
return this;
|
||||
|
||||
@@ -87,8 +87,8 @@ public class EventCommand extends SWCommand {
|
||||
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern(sender.parseToPlain("EVENT_TIME_FORMAT"));
|
||||
for (EventFight fight : EventFight.getEvent(currentEvent.getEventID())) {
|
||||
Team blue = Team.get(fight.getTeamBlue());
|
||||
Team red = Team.get(fight.getTeamRed());
|
||||
Team blue = Team.byId(fight.getTeamBlue());
|
||||
Team red = Team.byId(fight.getTeamRed());
|
||||
StringBuilder fline = new StringBuilder(sender.parseToLegacy("EVENT_CURRENT_FIGHT", fight.getStartTime().toLocalDateTime().format(format), blue.getTeamColor(), blue.getTeamKuerzel(), red.getTeamColor(), red.getTeamKuerzel()));
|
||||
|
||||
if (fight.hasFinished()) {
|
||||
@@ -125,7 +125,7 @@ public class EventCommand extends SWCommand {
|
||||
@Override
|
||||
public Team map(Chatter sender, PreviousArguments previousArguments, String s) {
|
||||
if ("PUB".equalsIgnoreCase(s) || "public".equalsIgnoreCase(s)) {
|
||||
return Team.get(0);
|
||||
return Team.byId(0);
|
||||
}
|
||||
return Team.get(s);
|
||||
}
|
||||
@@ -137,7 +137,7 @@ public class EventCommand extends SWCommand {
|
||||
for (EventFight fight : EventFight.getEvent(currentEvent.getEventID())) {
|
||||
if (fight.hasFinished()) continue;
|
||||
teamMapper.apply(fight).forEach(integer -> {
|
||||
Team team = Team.get(integer);
|
||||
Team team = Team.byId(integer);
|
||||
teams.add(team.getTeamName());
|
||||
teams.add(team.getTeamKuerzel());
|
||||
});
|
||||
|
||||
@@ -83,7 +83,7 @@ public class TeamCommand extends SWCommand {
|
||||
@Register(value = "create", description = "TEAM_CREATE_USAGE")
|
||||
public void create(@Validator("isNotInTeam") Chatter sender, @Length(min = 2, max = 4) @ErrorMessage("TEAM_KUERZEL_LENGTH") String kuerzel, @Length(min = 4, max = 15) @ErrorMessage("TEAM_NAME_LENGTH") String name){
|
||||
SteamwarUser user = sender.user();
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
|
||||
if(checkTeamKuerzel(sender, team, kuerzel) || checkTeamName(sender, team, name))
|
||||
return;
|
||||
@@ -116,7 +116,7 @@ public class TeamCommand extends SWCommand {
|
||||
sender.system("TEAM_JOIN_USAGE");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int inv : invs){
|
||||
Team team = Team.get(inv);
|
||||
Team team = Team.byId(inv);
|
||||
sb.append(team.getTeamName()).append(" ");
|
||||
}
|
||||
sender.system("TEAM_JOIN_INVITED", sb.toString());
|
||||
@@ -124,7 +124,7 @@ public class TeamCommand extends SWCommand {
|
||||
}
|
||||
|
||||
for(int inv : invs){
|
||||
Team team = Team.get(inv);
|
||||
Team team = Team.byId(inv);
|
||||
if(team.getTeamKuerzel().equalsIgnoreCase(args[0]) || team.getTeamName().equalsIgnoreCase(args[0])){
|
||||
t = inv;
|
||||
break;
|
||||
@@ -139,13 +139,13 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
user.setTeam(t);
|
||||
teamInvitations.remove(user.getId());
|
||||
sender.system("TEAM_JOIN_JOINED", Team.get(t).getTeamName());
|
||||
sender.system("TEAM_JOIN_JOINED", Team.byId(t).getTeamName());
|
||||
}
|
||||
|
||||
@Register("stepback")
|
||||
public void stepBack(@Validator("isLeader") Chatter sender) {
|
||||
SteamwarUser user = sender.user();
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
|
||||
if(noRemainingLeaders(team, user)){
|
||||
sender.system("TEAM_OTHER_LEADER_REQUIRED");
|
||||
@@ -159,7 +159,7 @@ public class TeamCommand extends SWCommand {
|
||||
@Register("leave")
|
||||
public void leave(@Validator("isInTeam") Chatter sender) {
|
||||
SteamwarUser user = sender.user();
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
|
||||
int teamSize = team.size();
|
||||
if(teamSize > 1 && user.isLeader() && noRemainingLeaders(team, user)) {
|
||||
@@ -182,7 +182,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register(value = "invite", description = "TEAM_INVITE_USAGE")
|
||||
public void invite(@Validator("isLeader") Chatter sender, @ErrorMessage("TEAM_INVITE_NO_PLAYER") SteamwarUser target){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if(notDuringEvent(sender))
|
||||
return;
|
||||
@@ -205,7 +205,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register(value = "remove", description = "TEAM_REMOVE_USAGE")
|
||||
public void remove(@Validator("isLeader") Chatter sender, @ErrorMessage("TEAM_REMOVE_NOT_PLAYER") @Mapper("memberList") SteamwarUser target){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if (target.isLeader()) {
|
||||
sender.system("TEAM_REMOVE_NOT_LEADER");
|
||||
@@ -239,7 +239,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Override
|
||||
public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
|
||||
return Team.get(sender.user().getTeam()).getMembers().stream()
|
||||
return Team.byId(sender.user().getTeam()).getMembers().stream()
|
||||
.map(SteamwarUser::byId)
|
||||
.map(SteamwarUser::getUserName)
|
||||
.toList();
|
||||
@@ -249,7 +249,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register(value = "changekurzel", description = "TEAM_KUERZEL_USAGE")
|
||||
public void changekuerzel(@Validator("isLeader") Chatter sender, @Length(min = 2, max = 4) @ErrorMessage("TEAM_KUERZEL_LENGTH") String kuerzel){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if(notDuringEvent(sender))
|
||||
return;
|
||||
@@ -263,7 +263,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register(value = "changename", description = "TEAM_NAME_USAGE")
|
||||
public void changename(@Validator("isLeader") Chatter sender, @Length(min = 4, max = 15) @ErrorMessage("TEAM_NAME_LENGTH") String name){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if(notDuringEvent(sender))
|
||||
return;
|
||||
@@ -291,7 +291,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register("info")
|
||||
public void info(@Validator("isInTeam") Chatter sender){
|
||||
info(sender, Team.get(sender.user().getTeam()));
|
||||
info(sender, Team.byId(sender.user().getTeam()));
|
||||
}
|
||||
|
||||
@Register(value = "info", description = "TEAM_INFO_USAGE")
|
||||
@@ -371,7 +371,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register("event")
|
||||
public void event(@Validator("isInTeam") Chatter sender) {
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
sender.system("TEAM_EVENT_USAGE");
|
||||
Set<Event> events = TeamTeilnahme.getEvents(team.getTeamId());
|
||||
@@ -385,7 +385,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register("event")
|
||||
public void event(@Validator("isLeader") Chatter sender, Event event){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if(notDuringEvent(sender))
|
||||
return;
|
||||
@@ -409,7 +409,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register("tp")
|
||||
public void tp(@Validator("isInTeam") PlayerChatter sender) {
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
tp(sender, team);
|
||||
}
|
||||
|
||||
@@ -450,7 +450,7 @@ public class TeamCommand extends SWCommand {
|
||||
|
||||
@Register(value = "server", description = "TEAM_SERVER_USAGE")
|
||||
public void server(@Validator("isLeader") Chatter sender, String server, @Min(intValue = 1) @Max(intValue = 65535) @ErrorMessage("TEAM_SERVER_PORT_INVALID") int port){
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
if (PunishmentCommand.isPunishedWithMessage(sender, Punishment.PunishmentType.NoTeamServer)) {
|
||||
return;
|
||||
}
|
||||
@@ -508,7 +508,7 @@ public class TeamCommand extends SWCommand {
|
||||
@Register("color")
|
||||
@Register("changecolor")
|
||||
public void changeColor(@Validator("isLeader") PlayerChatter sender) {
|
||||
Team team = Team.get(sender.user().getTeam());
|
||||
Team team = Team.byId(sender.user().getTeam());
|
||||
|
||||
if(notDuringEvent(sender))
|
||||
return;
|
||||
|
||||
@@ -87,7 +87,7 @@ public class TpCommand extends SWCommand {
|
||||
}
|
||||
if (Event.get() != null) {
|
||||
EventStarter.getEventServer().keySet().forEach(teamId -> {
|
||||
Team team = Team.get(teamId);
|
||||
Team team = Team.byId(teamId);
|
||||
list.add(team.getTeamName());
|
||||
list.add(team.getTeamKuerzel());
|
||||
});
|
||||
|
||||
@@ -63,7 +63,7 @@ public class WhoisCommand extends SWCommand {
|
||||
public void whois(Chatter sender, SteamwarUser user, WhoisParameterTypes... parameters) {
|
||||
EnumSet<WhoisParameterTypes> parameterTypes = parameters.length == 0 ? EnumSet.noneOf(WhoisParameterTypes.class) : EnumSet.copyOf(Arrays.asList(parameters));
|
||||
|
||||
Team team = Team.get(user.getTeam());
|
||||
Team team = Team.byId(user.getTeam());
|
||||
|
||||
sender.system("WHOIS_USERNAME", user.getUserName());
|
||||
sender.system("WHOIS_PREFIX", user.prefix().getColorCode() + user.prefix().getChatPrefix());
|
||||
|
||||
@@ -96,8 +96,8 @@ public class EventChannel {
|
||||
|
||||
Instant now = Instant.now();
|
||||
EventFight.getEvent(event.getEventID()).forEach(eventFight -> {
|
||||
Team teamBlue = Team.get(eventFight.getTeamBlue());
|
||||
Team teamRed = Team.get(eventFight.getTeamRed());
|
||||
Team teamBlue = Team.byId(eventFight.getTeamBlue());
|
||||
Team teamRed = Team.byId(eventFight.getTeamRed());
|
||||
|
||||
if (eventFight.getStartTime().toLocalDateTime().getDayOfYear() != LocalDate.now().getDayOfYear()) {
|
||||
return;
|
||||
|
||||
@@ -247,7 +247,7 @@ public class ChatListener extends BasicListener {
|
||||
sender,
|
||||
msgReceiver == null ? receiver : msgReceiver,
|
||||
highlightMentions(message, chatColorCode, receiver),
|
||||
sender.getTeam() == 0 ? "" : "§" + Team.get(sender.getTeam()).getTeamColor() + Team.get(sender.getTeam()).getTeamKuerzel() + " ",
|
||||
sender.getTeam() == 0 ? "" : "§" + Team.byId(sender.getTeam()).getTeamColor() + Team.byId(sender.getTeam()).getTeamKuerzel() + " ",
|
||||
UserElo.getEmblem(sender, rankedModes),
|
||||
prefix.getColorCode(),
|
||||
prefix.getChatPrefix().length() == 0 ? "§f" : prefix.getChatPrefix() + " ",
|
||||
|
||||
@@ -44,11 +44,7 @@ data class ResponseSchematicType(val name: String, val db: String)
|
||||
|
||||
@Serializable
|
||||
data class ResponseUser(val name: String, val uuid: String, val prefix: String, val perms: List<String>) {
|
||||
constructor(user: SteamwarUser) : this(user.userName, user.uuid.toString(), user.prefix().chatPrefix, user.perms().filter { !it.name.startsWith("PREFIX_") }.map { it.name }) {
|
||||
synchronized(cache) {
|
||||
cache[user.getId()] = this
|
||||
}
|
||||
}
|
||||
private constructor(user: SteamwarUser) : this(user.userName, user.uuid.toString(), user.prefix().chatPrefix, user.perms().filter { !it.name.startsWith("PREFIX_") }.map { it.name })
|
||||
|
||||
companion object {
|
||||
private val cache = mutableMapOf<Int, ResponseUser>()
|
||||
@@ -59,6 +55,10 @@ data class ResponseUser(val name: String, val uuid: String, val prefix: String,
|
||||
}
|
||||
}
|
||||
|
||||
fun get(user: SteamwarUser): ResponseUser = synchronized(cache) {
|
||||
return cache[user.getId()] ?: ResponseUser(user).also { cache[user.getId()] = it }
|
||||
}
|
||||
|
||||
fun clearCache() {
|
||||
synchronized(cache) {
|
||||
cache.clear()
|
||||
@@ -75,7 +75,7 @@ fun Route.configureDataRoutes() {
|
||||
permission = UserPerm.MODERATION
|
||||
}
|
||||
get("/users") {
|
||||
call.respond(SteamwarUser.all().map { ResponseUser(it) })
|
||||
call.respond(SteamwarUser.all().map { ResponseUser.get(it) })
|
||||
}
|
||||
get("/teams") {
|
||||
call.respond(Team.getAll().map { ResponseTeam(it) })
|
||||
@@ -118,7 +118,7 @@ fun Route.configureDataRoutes() {
|
||||
listOf(UserPerm.PREFIX_ADMIN, UserPerm.PREFIX_DEVELOPER, UserPerm.PREFIX_MODERATOR, UserPerm.PREFIX_SUPPORTER, UserPerm.PREFIX_BUILDER)
|
||||
.associateWith { SteamwarUser.getUsersWithPerm(it) }
|
||||
.mapKeys { UserPerm.prefixes[it.key]!!.chatPrefix }
|
||||
.mapValues { it.value.map { ResponseUser(it) } }
|
||||
.mapValues { it.value.map { ResponseUser.get(it) } }
|
||||
)
|
||||
}
|
||||
get("/skin/{uuid}") {
|
||||
@@ -137,7 +137,7 @@ fun Route.configureDataRoutes() {
|
||||
route("/me") {
|
||||
install(SWPermissionCheck)
|
||||
get {
|
||||
call.respond(ResponseUser(call.principal<SWAuthPrincipal>()!!.user))
|
||||
call.respond(ResponseUser.get(call.principal<SWAuthPrincipal>()!!.user))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ data class ResponseEventFight(
|
||||
eventFight.fightID,
|
||||
eventFight.spielmodus,
|
||||
eventFight.map,
|
||||
ResponseTeam(Team.get(eventFight.teamBlue)),
|
||||
ResponseTeam(Team.get(eventFight.teamRed)),
|
||||
ResponseTeam(Team.byId(eventFight.teamBlue)),
|
||||
ResponseTeam(Team.byId(eventFight.teamRed)),
|
||||
eventFight.startTime.time,
|
||||
eventFight.ergebnis,
|
||||
eventFight.spectatePort,
|
||||
@@ -162,7 +162,7 @@ suspend fun ApplicationCall.receiveFight(fieldName: String = "fight"): EventFigh
|
||||
return null
|
||||
}
|
||||
|
||||
val fight = EventFight.get(fightId)
|
||||
val fight = EventFight.byId(fightId)
|
||||
if (fight == null) {
|
||||
respond(HttpStatusCode.NotFound, ResponseError("Fight not found"))
|
||||
return null
|
||||
|
||||
@@ -66,7 +66,7 @@ fun Route.configureEventGroups() {
|
||||
val pointsPerLoss = updateEventGroup.pointsPerLoss ?: group.pointsPerLoss
|
||||
val pointsPerDraw = updateEventGroup.pointsPerDraw ?: group.pointsPerDraw
|
||||
group.update(name, type, pointsPerWin, pointsPerLoss, pointsPerDraw)
|
||||
call.respond(ResponseGroups(EventGroup.get(group.id).orElse(null) ?: return@put))
|
||||
call.respond(ResponseGroups(EventGroup.byId(group.getId()).orElse(null) ?: return@put))
|
||||
}
|
||||
delete {
|
||||
val group = call.receiveEventGroup() ?: return@delete
|
||||
@@ -84,7 +84,7 @@ suspend fun ApplicationCall.receiveEventGroup(): EventGroup? {
|
||||
return null
|
||||
}
|
||||
|
||||
val group = EventGroup.get(groupId).orElse(null)
|
||||
val group = EventGroup.byId(groupId).orElse(null)
|
||||
if (group == null) {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
|
||||
@@ -31,7 +31,7 @@ fun Route.configureEventRefereesRouting() {
|
||||
route("/referees") {
|
||||
get {
|
||||
val event = call.receiveEvent() ?: return@get
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser(SteamwarUser.byId(it)!!) })
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser.get(SteamwarUser.byId(it)!!) })
|
||||
}
|
||||
put {
|
||||
val event = call.receiveEvent() ?: return@put
|
||||
@@ -39,7 +39,7 @@ fun Route.configureEventRefereesRouting() {
|
||||
referees.forEach {
|
||||
Referee.add(event.eventID, SteamwarUser.get(UUID.fromString(it))!!.getId())
|
||||
}
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser(SteamwarUser.byId(it)!!) })
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser.get(SteamwarUser.byId(it)!!) })
|
||||
}
|
||||
delete {
|
||||
val event = call.receiveEvent() ?: return@delete
|
||||
@@ -47,7 +47,7 @@ fun Route.configureEventRefereesRouting() {
|
||||
referees.forEach {
|
||||
Referee.remove(event.eventID, SteamwarUser.get(UUID.fromString(it))!!.getId())
|
||||
}
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser(SteamwarUser.byId(it)!!) })
|
||||
call.respond(Referee.get(event.eventID).map { ResponseUser.get(SteamwarUser.byId(it)!!) })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,11 +48,11 @@ fun Route.configureEventRelations() {
|
||||
post {
|
||||
val create = call.receive<CreateEventRelation>()
|
||||
|
||||
val fight = EventFight.get(create.fightId) ?: return@post call.respond(HttpStatusCode.NotFound)
|
||||
val fight = EventFight.byId(create.fightId) ?: return@post call.respond(HttpStatusCode.NotFound)
|
||||
|
||||
when (create.fromType) {
|
||||
EventRelation.FromType.FIGHT -> EventFight.get(create.fromId) ?: return@post call.respond(HttpStatusCode.BadRequest)
|
||||
EventRelation.FromType.GROUP -> EventGroup.get(create.fromId) ?: return@post call.respond(HttpStatusCode.BadRequest)
|
||||
EventRelation.FromType.FIGHT -> EventFight.byId(create.fromId) ?: return@post call.respond(HttpStatusCode.BadRequest)
|
||||
EventRelation.FromType.GROUP -> EventGroup.byId(create.fromId) ?: return@post call.respond(HttpStatusCode.BadRequest)
|
||||
}
|
||||
|
||||
val relation = EventRelation.create(fight, create.team, create.fromType, create.fromId, create.fromPlace)
|
||||
@@ -70,10 +70,10 @@ fun Route.configureEventRelations() {
|
||||
|
||||
update.from?.let {
|
||||
when(it.fromType) {
|
||||
EventRelation.FromType.FIGHT -> relation.setFromFight(EventFight.get(it.fromId) ?: return@put call.respond(HttpStatusCode.BadRequest),
|
||||
EventRelation.FromType.FIGHT -> relation.setFromFight(EventFight.byId(it.fromId) ?: return@put call.respond(HttpStatusCode.BadRequest),
|
||||
it.fromPlace
|
||||
)
|
||||
EventRelation.FromType.GROUP -> relation.setFromGroup(EventGroup.get(it.fromId).orElse(null) ?: return@put call.respond(HttpStatusCode.BadRequest),
|
||||
EventRelation.FromType.GROUP -> relation.setFromGroup(EventGroup.byId(it.fromId).orElse(null) ?: return@put call.respond(HttpStatusCode.BadRequest),
|
||||
it.fromPlace
|
||||
)
|
||||
}
|
||||
@@ -99,7 +99,7 @@ suspend fun ApplicationCall.receiveEventRelation(): EventRelation? {
|
||||
return null
|
||||
}
|
||||
|
||||
val relation = EventRelation.get(relationId)
|
||||
val relation = EventRelation.byId(relationId)
|
||||
if (relation == null) {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
|
||||
@@ -51,7 +51,7 @@ data class ResponseGroups(
|
||||
val points: Map<Int, Int>
|
||||
) {
|
||||
constructor(group: EventGroup, short: Boolean = false) : this(
|
||||
group.id,
|
||||
group.getId(),
|
||||
group.name,
|
||||
group.pointsPerWin,
|
||||
group.pointsPerLoss,
|
||||
@@ -71,12 +71,12 @@ data class ResponseRelation(
|
||||
val fromPlace: Int
|
||||
) {
|
||||
constructor(relation: EventRelation) : this(
|
||||
relation.id,
|
||||
relation.getId(),
|
||||
relation.fightId,
|
||||
relation.fightTeam,
|
||||
relation.fromType,
|
||||
relation.fromFight.map { ResponseEventFight(it) }.orElse(null),
|
||||
relation.fromGroup.map { ResponseGroups(it) }.orElse(null),
|
||||
relation.fromFight?.let { ResponseEventFight(it) },
|
||||
relation.fromGroup?.let { ResponseGroups(it) },
|
||||
relation.fromPlace
|
||||
)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ data class ExtendedResponseEvent(
|
||||
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(SteamwarUser.byId(it)!!) },
|
||||
Referee.get(event.eventID).map { ResponseUser.get(SteamwarUser.byId(it)!!) },
|
||||
EventRelation.get(event).map { ResponseRelation(it) }
|
||||
)
|
||||
}
|
||||
@@ -176,8 +176,8 @@ fun Route.configureEventsRoute() {
|
||||
csv.append(arrayOf("Start", "BlueTeam", "RedTeam", "WinnerTeam", "Group").joinToString(","))
|
||||
fights.forEach {
|
||||
csv.appendLine()
|
||||
val blue = Team.get(it.teamBlue)
|
||||
val red = Team.get(it.teamRed)
|
||||
val blue = Team.byId(it.teamBlue)
|
||||
val red = Team.byId(it.teamRed)
|
||||
val winner = when (it.ergebnis) {
|
||||
1 -> blue.teamName
|
||||
2 -> red.teamName
|
||||
@@ -187,8 +187,8 @@ fun Route.configureEventsRoute() {
|
||||
csv.append(
|
||||
arrayOf(
|
||||
it.startTime.toString(),
|
||||
Team.get(it.teamBlue).teamName,
|
||||
Team.get(it.teamRed).teamName,
|
||||
Team.byId(it.teamBlue).teamName,
|
||||
Team.byId(it.teamRed).teamName,
|
||||
winner,
|
||||
it.group.map { it.name }.orElse("Ungrouped")
|
||||
).joinToString(",")
|
||||
|
||||
Reference in New Issue
Block a user