Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

478 senbatsu shiai #487

Merged
merged 8 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![GitHub commit activity](https://img.shields.io/github/commit-activity/y/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![GitHub last commit](https://img.shields.io/github/last-commit/softwaremagico/KendoTournamentManager)](https://github.com/softwaremagico/KendoTournamentManager)
[![CircleCI](https://circleci.com/gh/softwaremagico/KendoTournamentManager.svg?style=shield)](https://circleci.com/gh/softwaremagico/KendoTournamentManager)
[![Time](https://img.shields.io/badge/development-639h-blueviolet.svg)]()
[![Time](https://img.shields.io/badge/development-642.5h-blueviolet.svg)]()

[![Powered by](https://img.shields.io/badge/powered%20by%20java-orange.svg?logo=OpenJDK&logoColor=white)]()
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=kendo-tournament-backend&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=kendo-tournament-backend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ && getScoresByParticipant().get(participant).contains(Score.HANSOKU)
}

/**
* When somebody loose a combat only by Hansokus.
* When somebody loses a combat only by Hansokus.
*
* @param tournament The tournament to check.
* @return a list of new achievements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public FightController(FightProvider provider, FightConverter converter, Tournam
TournamentProvider tournamentProvider, TournamentHandlerSelector tournamentHandlerSelector,
TournamentExtraPropertyProvider tournamentExtraPropertyProvider,
ParticipantProvider participantProvider, GroupProvider groupProvider,
TeamConverter teamConverter) {
TeamConverter teamConverter, FightProvider fightProvider) {
super(provider, converter);
this.tournamentConverter = tournamentConverter;
this.tournamentProvider = tournamentProvider;
Expand Down Expand Up @@ -197,6 +197,9 @@ public List<FightDTO> createFights(Integer tournamentId, TeamsOrder teamsOrder,
final Tournament tournament = (tournamentProvider.get(tournamentId)
.orElseThrow(() -> new TournamentNotFoundException(getClass(), "No tournament found with id '" + tournamentId + "',",
ExceptionType.INFO)));
//Delete any deeper level. If a group changes, the inner levels are invalid.
getProvider().delete(tournament, level + 1);
groupProvider.delete(tournament, level + 1);
final ITournamentManager selectedManager = tournamentHandlerSelector.selectManager(tournament.getType());
if (selectedManager != null) {
final List<Fight> createdFights = getProvider().saveAll(selectedManager.createFights(tournament, teamsOrder, level, createdBy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@
import com.softwaremagico.kt.core.converters.models.TeamConverterRequest;
import com.softwaremagico.kt.core.exceptions.ClubNotFoundException;
import com.softwaremagico.kt.core.exceptions.GroupNotFoundException;
import com.softwaremagico.kt.core.exceptions.TournamentNotFoundException;
import com.softwaremagico.kt.core.providers.ClubProvider;
import com.softwaremagico.kt.core.providers.GroupProvider;
import com.softwaremagico.kt.core.providers.ParticipantProvider;
import com.softwaremagico.kt.core.providers.RankingProvider;
import com.softwaremagico.kt.core.providers.RoleProvider;
import com.softwaremagico.kt.core.providers.TournamentProvider;
import com.softwaremagico.kt.core.score.CompetitorRanking;
import com.softwaremagico.kt.core.score.ScoreOfCompetitor;
import com.softwaremagico.kt.core.tournaments.BubbleSortTournamentHandler;
import com.softwaremagico.kt.persistence.entities.Club;
import com.softwaremagico.kt.persistence.entities.Group;
import com.softwaremagico.kt.persistence.entities.Participant;
import com.softwaremagico.kt.persistence.entities.Role;
import com.softwaremagico.kt.persistence.entities.Team;
import com.softwaremagico.kt.persistence.entities.Tournament;
import com.softwaremagico.kt.persistence.values.RoleType;
import com.softwaremagico.kt.persistence.values.ScoreType;
import com.softwaremagico.kt.persistence.values.TournamentType;
Expand All @@ -69,12 +73,14 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -112,6 +118,10 @@ public class RankingController {

private final RoleProvider roleProvider;

private final TournamentProvider tournamentProvider;

private final BubbleSortTournamentHandler bubbleSortTournamentHandler;

public RankingController(GroupProvider groupProvider, GroupConverter groupConverter,
TournamentConverter tournamentConverter, FightConverter fightConverter,
TeamConverter teamConverter, DuelConverter duelConverter,
Expand All @@ -120,7 +130,8 @@ public RankingController(GroupProvider groupProvider, GroupConverter groupConver
ScoreOfCompetitorConverter scoreOfCompetitorConverter,
ScoreOfTeamConverter scoreOfTeamConverter,
ClubProvider clubProvider, ParticipantProvider participantProvider, ClubConverter clubConverter,
RoleProvider roleProvider) {
RoleProvider roleProvider, TournamentProvider tournamentProvider,
BubbleSortTournamentHandler bubbleSortTournamentHandler) {
this.groupProvider = groupProvider;
this.groupConverter = groupConverter;
this.tournamentConverter = tournamentConverter;
Expand All @@ -136,6 +147,8 @@ public RankingController(GroupProvider groupProvider, GroupConverter groupConver
this.participantProvider = participantProvider;
this.clubConverter = clubConverter;
this.roleProvider = roleProvider;
this.tournamentProvider = tournamentProvider;
this.bubbleSortTournamentHandler = bubbleSortTournamentHandler;
}

private static Set<ParticipantDTO> getParticipants(List<TeamDTO> teams) {
Expand Down Expand Up @@ -164,8 +177,27 @@ public List<ScoreOfTeamDTO> getTeamsScoreRankingFromGroup(Integer groupId) {
}

public List<ScoreOfTeamDTO> getTeamsScoreRankingFromTournament(Integer tournamentId) {
return scoreOfTeamConverter.convertAll(rankingProvider.getTeamsScoreRankingFromTournament(tournamentId)
final Tournament tournament = tournamentProvider.get(tournamentId).orElseThrow(
() -> new TournamentNotFoundException(this.getClass(), "No tournamnet exists with id '" + tournamentId + "'."));
final List<ScoreOfTeamDTO> ranking = scoreOfTeamConverter.convertAll(rankingProvider.getTeamsScoreRankingFromTournament(tournamentId)
.stream().map(ScoreOfTeamConverterRequest::new).toList());
//Bubble sort is different. The winner is only on the first group.
if (tournament.getType().equals(TournamentType.BUBBLE_SORT)) {
final List<Team> sortedTeams = bubbleSortTournamentHandler.getTeamsOrderedByRanks(tournament);
Collections.reverse(sortedTeams);
final List<ScoreOfTeamDTO> scoreOfTeamDTOS = new ArrayList<>();
final AtomicInteger counter = new AtomicInteger(0);
sortedTeams.forEach(team -> {
final ScoreOfTeamDTO score = ranking.stream().filter(rankingElement ->
Objects.equals(rankingElement.getTeam().getId(), team.getId())).findFirst().orElse(null);
if (score != null) {
score.setSortingIndex(counter.getAndIncrement());
scoreOfTeamDTOS.add(score);
}
});
return scoreOfTeamDTOS;
}
return ranking;
}

public List<ScoreOfTeamDTO> getTeamsScoreRanking(GroupDTO groupDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

import com.softwaremagico.kt.persistence.values.TournamentExtraPropertyKey;

import java.io.Serial;

public class TournamentExtraPropertyDTO extends ElementDTO {

@Serial
private static final long serialVersionUID = -4825047701201067422L;

private TournamentDTO tournament;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.softwaremagico.kt.core.exceptions;

/*-
* #%L
* Kendo Tournament Manager (Core)
* %%
* Copyright (C) 2021 - 2024 Softwaremagico
* %%
* 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 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 <http://www.gnu.org/licenses/>.
* #L%
*/

import com.softwaremagico.kt.logger.ExceptionType;
import com.softwaremagico.kt.logger.LoggedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.Serial;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class InvalidExtraPropertyException extends LoggedException {

@Serial
private static final long serialVersionUID = 2621573668990271417L;

public InvalidExtraPropertyException(Class<?> clazz, String message, ExceptionType type) {
super(clazz, message, type, HttpStatus.BAD_REQUEST);
}

public InvalidExtraPropertyException(Class<?> clazz, String message) {
super(clazz, message, ExceptionType.SEVERE, HttpStatus.BAD_REQUEST);
}

public InvalidExtraPropertyException(Class<?> clazz) {
this(clazz, "Invalid Group");
}

public InvalidExtraPropertyException(Class<?> clazz, Throwable e) {
super(clazz, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.softwaremagico.kt.core.managers;

/*-
* #%L
* Kendo Tournament Manager (Core)
* %%
* Copyright (C) 2021 - 2024 Softwaremagico
* %%
* 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 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 <http://www.gnu.org/licenses/>.
* #L%
*/

import org.springframework.stereotype.Service;

@Service
public class BubbleSortTournamentManager extends KingOfTheMountainFightManager {


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,27 @@
public class KingOfTheMountainFightManager {

public List<Fight> createFights(Tournament tournament, List<Team> teams, Integer level, String createdBy) {
return createOneFightsForEachTeam(tournament, teams, level, createdBy);
return createFirstFight(tournament, teams, level, createdBy);
}

private Fight createFight(Tournament tournament, Team team1, Team team2, Integer shiaijo, Integer level, String createdBy) {
return new Fight(tournament, team1, team2, shiaijo, level, createdBy);
}

private List<Fight> createOneFightsForEachTeam(Tournament tournament, List<Team> teams, int level, String createdBy) {
/**
* Only the first two teams are used, as only first fight can be defined.
*/
private List<Fight> createFirstFight(Tournament tournament, List<Team> teams, int level, String createdBy) {
if (teams == null || tournament == null || teams.size() < 2) {
return new ArrayList<>();
}
final List<Fight> fights = new ArrayList<>();

// If only exists two teams, there are only one fight. If no, as many
// fights as teams
for (int i = 0; i < (teams.size() > 2 ? teams.size() : 1); i++) {
final Team team1 = teams.get(i);
final Team team2 = teams.get((i + 1) % teams.size());

if (fights.size() % 2 == 0) {
fights.add(createFight(tournament, team1, team2, 0, level, createdBy));
} else {
fights.add(createFight(tournament, team2, team1, 0, level, createdBy));
}
}
final Team team1 = teams.get(0);
final Team team2 = teams.get(1);
fights.add(createFight(tournament, team1, team2, 0, level, createdBy));
return fights;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public void delete(Tournament tournament) {
getRepository().deleteByTournament(tournament);
}

public void delete(Tournament tournament, int level) {
groupRepository.findByTournamentAndLevelIsGreaterThanEqual(tournament, level).forEach(group -> {
group.setFights(new ArrayList<>());
groupRepository.save(group);
});
getRepository().deleteByTournamentAndLevelGreaterThanEqual(tournament, level);
}

public long count(Tournament tournament) {
return getRepository().countByTournament(tournament);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* #L%
*/

import com.softwaremagico.kt.core.exceptions.InvalidExtraPropertyException;
import com.softwaremagico.kt.persistence.entities.Tournament;
import com.softwaremagico.kt.persistence.entities.TournamentExtraProperty;
import com.softwaremagico.kt.persistence.repositories.TournamentExtraPropertyRepository;
Expand Down Expand Up @@ -77,6 +78,10 @@ public void deleteByTournamentAndProperty(Tournament tournament, TournamentExtra

@Override
public TournamentExtraProperty save(TournamentExtraProperty entity) {
if (!entity.getPropertyKey().getAllowedTournaments().contains(entity.getTournament().getType())) {
throw new InvalidExtraPropertyException(this.getClass(), "Tournament '" + entity.getTournament()
+ "' cannot have property '" + entity.getPropertyKey() + "'");
}
deleteByTournamentAndProperty(entity.getTournament(), entity.getPropertyKey());
return getRepository().save(entity);
}
Expand Down
Loading
Loading