Add event grouping

This commit is contained in:
2025-05-07 13:51:13 +02:00
parent 2c644eef26
commit f387805b40
3 changed files with 273 additions and 0 deletions
@@ -37,6 +37,7 @@ 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> 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 Statement reschedule = table.update(Table.PRIMARY, "StartTime");
@@ -54,6 +55,10 @@ public class EventFight implements Comparable<EventFight> {
return byId.select(fightID);
}
public static List<EventFight> get(EventGroup group) {
return byGroup.listSelect(group.getId());
}
public static void loadAllComingFights() {
fights.clear();
fights.addAll(allComing.listSelect());
@@ -75,6 +80,10 @@ public class EventFight implements Comparable<EventFight> {
private final int fightID;
@Getter
@Setter
@Field(nullable = true, def = "null")
private Integer groupId;
@Getter
@Setter
@Field
private Timestamp startTime;
@Getter
@@ -98,11 +107,25 @@ public class EventFight implements Comparable<EventFight> {
@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 void setErgebnis(int winner) {
this.ergebnis = winner;
setResult.update(winner, fightID);
@@ -0,0 +1,117 @@
/*
* 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.SqlTypeMapper;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@AllArgsConstructor
@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 = ?");
public static List<EventGroup> get(Event eventID) {
return byEvent.listSelect(eventID);
}
public static Optional<EventGroup> get(int id) {
return Optional.ofNullable(get.select(id));
}
@Field(keys = Table.PRIMARY)
private final int id;
@Field
private String name;
@Field
private EventGroupType type;
@Field
private int pointsPerWin;
@Field
private int pointsPerLoss;
@Field
private int pointsPerDraw;
public List<EventFight> getFights() {
return EventFight.get(this);
}
public Map<Team, Integer> calculatePoints() {
Map<Integer, Team> teams = new HashMap<>();
Map<Team, Integer> points = new HashMap<>();
for(EventFight fight : getFights()) {
int blueTeamAdd = 0;
int redTeamAdd = 0;
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;
}
Team blueTeam = teams.computeIfAbsent(fight.getTeamBlue(), Team::get);
Team redTeam = teams.computeIfAbsent(fight.getTeamRed(), Team::get);
points.put(blueTeam, points.getOrDefault(blueTeam, 0) + blueTeamAdd);
points.put(redTeam, points.getOrDefault(redTeam, 0) + redTeamAdd);
}
return points;
}
public static enum EventGroupType {
GROUP_STAGE,
ELIMINATION_STAGE
}
}
@@ -0,0 +1,133 @@
/*
* 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.SqlTypeMapper;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
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);
@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 Optional<Team> getAdvancingTeam() {
if (fromType == FromType.FIGHT) {
return getFromFight().flatMap(EventFight::getWinner);
} else if (fromType == FromType.GROUP) {
return getFromGroup().map(EventGroup::calculatePoints)
.flatMap(points -> points.entrySet().stream()
.max(Map.Entry.comparingByValue())
.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(),
team.get(),
fight.getTeamBlue(),
fight.getSpectatePort()
);
} else {
fight.update(
fight.getStartTime(),
fight.getSpielmodus(),
fight.getMap(),
fight.getTeamRed(),
team.get(),
fight.getSpectatePort()
);
}
return true;
}
public static enum FightTeam {
RED,
BLUE
}
public static enum FromType {
FIGHT,
GROUP
}
}