diff --git a/CommonCore/SQL/src/de/steamwar/sql/EventFight.java b/CommonCore/SQL/src/de/steamwar/sql/EventFight.java index fe91e3be..c595149b 100644 --- a/CommonCore/SQL/src/de/steamwar/sql/EventFight.java +++ b/CommonCore/SQL/src/de/steamwar/sql/EventFight.java @@ -37,6 +37,7 @@ public class EventFight implements Comparable { private static final Table table = new Table<>(EventFight.class); private static final SelectStatement byId = table.select(Table.PRIMARY); + private static final SelectStatement byGroup = new SelectStatement(table, "SELECT * FROM EventFight WHERE GroupID = ? ORDER BY StartTime ASC"); private static final SelectStatement allComing = new SelectStatement<>(table, "SELECT * FROM EventFight WHERE StartTime > now() ORDER BY StartTime ASC"); private static final SelectStatement 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 { return byId.select(fightID); } + public static List 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 { 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 { @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 getGroup() { + return Optional.ofNullable(groupId).flatMap(EventGroup::get); + } + + public Optional 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); diff --git a/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java b/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java new file mode 100644 index 00000000..3ea62738 --- /dev/null +++ b/CommonCore/SQL/src/de/steamwar/sql/EventGroup.java @@ -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 . + */ + +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 table = new Table<>(EventGroup.class); + + private static final SelectStatement get = table.select(Table.PRIMARY); + private static final SelectStatement byEvent = new SelectStatement<>(table, "SELECT * FROM EventGroup WHERE EventID = ?"); + + public static List get(Event eventID) { + return byEvent.listSelect(eventID); + } + + public static Optional 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 getFights() { + return EventFight.get(this); + } + + public Map calculatePoints() { + Map teams = new HashMap<>(); + Map 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 + } +} diff --git a/CommonCore/SQL/src/de/steamwar/sql/EventRelation.java b/CommonCore/SQL/src/de/steamwar/sql/EventRelation.java new file mode 100644 index 00000000..1c154d56 --- /dev/null +++ b/CommonCore/SQL/src/de/steamwar/sql/EventRelation.java @@ -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 . + */ + +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 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 getFromFight() { + if(fromType == FromType.FIGHT) { + return Optional.of(EventFight.get(fromId)); + } else { + return Optional.empty(); + } + } + + public Optional getFromGroup() { + if(fromType == FromType.GROUP) { + return EventGroup.get(fromId); + } else { + return Optional.empty(); + } + } + + public Optional 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 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 + } +}