Refactor page routing and point calculation logic

Streamlined the `page` routing structure by optimizing branch and file handling, introducing a reusable `filesInDirectory` method, and cleaning up redundancies. Enhanced `EventGroup` point calculation with incremental updates, new helper methods (`getTeams`, `getTeamsId`), and better handling of unfinished fights.
This commit is contained in:
2025-05-28 23:57:29 +02:00
parent 8768fd7d81
commit a5bb62590c
3 changed files with 130 additions and 105 deletions
@@ -23,10 +23,9 @@ import de.steamwar.sql.internal.*;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Getter
@Setter
@@ -94,6 +93,15 @@ public class EventGroup {
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);
}
@@ -104,13 +112,16 @@ public class EventGroup {
public Map<Team, Integer> calculatePoints() {
if (points == null) {
Map<Integer, Team> teams = new HashMap<>();
points = new HashMap<>();
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;
@@ -128,19 +139,18 @@ public class EventGroup {
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);
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(id, name, type, pointsPerWin, pointsPerLoss, pointsPerDraw);
update.update(name, type, pointsPerWin, pointsPerLoss, pointsPerDraw, id);
this.name = name;
this.type = type;
this.pointsPerWin = pointsPerWin;