From 333815e42e183ec3a0f6497ef8c94b77e64f2c5f Mon Sep 17 00:00:00 2001 From: Boyan Zlatanov Date: Tue, 24 Dec 2024 11:14:53 +0000 Subject: [PATCH] Remove stats animation --- .../tourdetails/TournamentDetailsBuilder.java | 63 +------------- .../TournamentStatisticsBuilder.java | 86 +++++++++++++++++++ .../resources/templates/component/stats.html | 6 +- 3 files changed, 89 insertions(+), 66 deletions(-) create mode 100644 src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentStatisticsBuilder.java diff --git a/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentDetailsBuilder.java b/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentDetailsBuilder.java index f681be3..61dbd82 100644 --- a/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentDetailsBuilder.java +++ b/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentDetailsBuilder.java @@ -1,12 +1,8 @@ package xyz.zlatanov.ravenscore.web.service.builder.tourdetails; -import static java.math.BigDecimal.ZERO; -import static java.math.RoundingMode.HALF_UP; import static java.math.RoundingMode.UP; import static java.util.Comparator.comparing; import static xyz.zlatanov.ravenscore.Utils.*; -import static xyz.zlatanov.ravenscore.domain.domain.House.BOLTON; -import static xyz.zlatanov.ravenscore.domain.domain.House.STARK; import java.math.BigDecimal; import java.util.*; @@ -16,9 +12,6 @@ import lombok.RequiredArgsConstructor; import lombok.val; import xyz.zlatanov.ravenscore.domain.domain.*; -import xyz.zlatanov.ravenscore.web.model.statistics.Dataset; -import xyz.zlatanov.ravenscore.web.model.statistics.HouseData; -import xyz.zlatanov.ravenscore.web.model.statistics.TournamentStatistics; import xyz.zlatanov.ravenscore.web.model.tourdetails.*; @RequiredArgsConstructor @@ -49,7 +42,7 @@ public TournamentDetailsModel build() { .winnerParticipantId(getWinnerId(tournamentStageModelList)) .substituteModelList(getSubstitutes()) .tournamentStageModelList(tournamentStageModelList) - .tournamentStatistics(buildStatistics(tournamentStageModelList)); + .tournamentStatistics(new TournamentStatisticsBuilder(tournamentStageModelList, playerList, gameList).buildStatistics()); } private List getSubstitutes() { @@ -233,60 +226,6 @@ private String getWinnerId(List tournamentStageModelList) return lastStage.participantModelList().getFirst().id(); } - private TournamentStatistics buildStatistics(List tournamentStageModelList) { - val houseDataList = calculateHouseData(tournamentStageModelList); - val stats = new TournamentStatistics().datasets(List.of( - new Dataset().label("Win percentage"), - new Dataset().label("Average points"))); - for (val hd : houseDataList) { - stats.labels().add(hd.label()); - stats.datasets().get(0).data().add(hd.winPercent()); - stats.datasets().get(1).data().add(hd.averagePoints()); - } - return stats; - } - - private List calculateHouseData(List tournamentStageModelList) { - val houseDataList = new ArrayList(); - playerList.stream() - .map(Player::house) - .filter(h -> h != BOLTON) // include in Stark statistics - .distinct() // each house participating in the tournament game types - .forEach(house -> { - val winsCount = tournamentStageModelList.stream() - .flatMap(tsm -> tsm.participantModelList().stream()) - .map(ParticipantModel::wins) - .flatMap(Collection::stream) - .filter(gw -> gw.house() == house || (gw.house() == BOLTON && house == STARK)) - .count(); - val pointsList = gameList.stream() - .filter(Game::completed) - .map(g -> playerList.stream() - .filter(p -> p.gameId().equals(g.id()) - && (p.house() == house || (p.house() == BOLTON && house == STARK))) - .findFirst() - .map(Player::points) - .orElse(null)) - .filter(Objects::nonNull) - .toList(); - val pointsSum = pointsList.stream().reduce(Integer::sum).map(BigDecimal::valueOf).orElse(ZERO); - val gamesCount = BigDecimal.valueOf(pointsList.size()); - val houseData = new HouseData().label(house != STARK ? house.label() : STARK.label() + " / " + BOLTON.label()); - if (gamesCount.compareTo(ZERO) > 0) { - houseData.winPercent(BigDecimal.valueOf(winsCount).divide(gamesCount, 2, HALF_UP)); - houseData.averagePoints(pointsSum.divide(gamesCount, 2, UP)); - } else { - houseData.winPercent(ZERO); - houseData.averagePoints(ZERO); - } - houseDataList.add(houseData); - }); - return houseDataList.stream() - .sorted(comparing(HouseData::averagePoints)) - .toList() - .reversed(); - } - @RequiredArgsConstructor public static class PlayerPointsCalculator { diff --git a/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentStatisticsBuilder.java b/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentStatisticsBuilder.java new file mode 100644 index 0000000..45da62e --- /dev/null +++ b/src/main/java/xyz/zlatanov/ravenscore/web/service/builder/tourdetails/TournamentStatisticsBuilder.java @@ -0,0 +1,86 @@ +package xyz.zlatanov.ravenscore.web.service.builder.tourdetails; + +import static java.math.BigDecimal.ZERO; +import static java.math.RoundingMode.HALF_UP; +import static java.math.RoundingMode.UP; +import static java.util.Comparator.comparing; +import static xyz.zlatanov.ravenscore.domain.domain.House.BOLTON; +import static xyz.zlatanov.ravenscore.domain.domain.House.STARK; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +import lombok.RequiredArgsConstructor; +import lombok.val; +import xyz.zlatanov.ravenscore.domain.domain.Game; +import xyz.zlatanov.ravenscore.domain.domain.Player; +import xyz.zlatanov.ravenscore.web.model.statistics.Dataset; +import xyz.zlatanov.ravenscore.web.model.statistics.HouseData; +import xyz.zlatanov.ravenscore.web.model.statistics.TournamentStatistics; +import xyz.zlatanov.ravenscore.web.model.tourdetails.ParticipantModel; +import xyz.zlatanov.ravenscore.web.model.tourdetails.TournamentStageModel; + +@RequiredArgsConstructor +public class TournamentStatisticsBuilder { + + private final List tournamentStageModelList; + private final List playerList; + private final List gameList; + + public TournamentStatistics buildStatistics() { + val houseDataList = calculateHouseData(tournamentStageModelList); + val stats = new TournamentStatistics().datasets(List.of( + new Dataset().label("Win percentage"), + new Dataset().label("Average points"))); + for (val hd : houseDataList) { + stats.labels().add(hd.label()); + stats.datasets().get(0).data().add(hd.winPercent()); + stats.datasets().get(1).data().add(hd.averagePoints()); + } + return stats; + } + + private List calculateHouseData(List tournamentStageModelList) { + val houseDataList = new ArrayList(); + playerList.stream() + .map(Player::house) + .filter(h -> h != BOLTON) // include in Stark statistics + .distinct() // each house participating in the tournament game types + .forEach(house -> { + val winsCount = tournamentStageModelList.stream() + .flatMap(tsm -> tsm.participantModelList().stream()) + .map(ParticipantModel::wins) + .flatMap(Collection::stream) + .filter(gw -> gw.house() == house || (gw.house() == BOLTON && house == STARK)) + .count(); + val pointsList = gameList.stream() + .filter(Game::completed) + .map(g -> playerList.stream() + .filter(p -> p.gameId().equals(g.id()) + && (p.house() == house || (p.house() == BOLTON && house == STARK))) + .findFirst() + .map(Player::points) + .orElse(null)) + .filter(Objects::nonNull) + .toList(); + val pointsSum = pointsList.stream().reduce(Integer::sum).map(BigDecimal::valueOf).orElse(ZERO); + val gamesCount = BigDecimal.valueOf(pointsList.size()); + val houseData = new HouseData().label(house != STARK ? house.label() : STARK.label() + " / " + BOLTON.label()); + if (gamesCount.compareTo(ZERO) > 0) { + houseData.winPercent(BigDecimal.valueOf(winsCount).divide(gamesCount, 2, HALF_UP)); + houseData.averagePoints(pointsSum.divide(gamesCount, 2, UP)); + } else { + houseData.winPercent(ZERO); + houseData.averagePoints(ZERO); + } + houseDataList.add(houseData); + }); + return houseDataList.stream() + .sorted(comparing(HouseData::averagePoints)) + .toList() + .reversed(); + } +} diff --git a/src/main/resources/templates/component/stats.html b/src/main/resources/templates/component/stats.html index 3d21358..ebef6e7 100644 --- a/src/main/resources/templates/component/stats.html +++ b/src/main/resources/templates/component/stats.html @@ -85,16 +85,14 @@

Tournament Statistics

anchor: "end", align: "end", color: "#A0A0A0", - font: { - //weight: 'bold', - }, formatter: function (value, context) { return context.datasetIndex === 0 ? (value * 100) + "%" : value; } } - } + }, + animation: false }, }); }