Skip to content

Commit

Permalink
Merge pull request #529 from softwaremagico/528-show-a-list-for-you-a…
Browse files Browse the repository at this point in the history
…re-the-worst-nightmare-of

Showing list of users
  • Loading branch information
softwaremagico authored Jan 15, 2025
2 parents 5ae6381 + 73b2f6c commit 0b44a9a
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 147 deletions.
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-657h-blueviolet.svg)]()
[![Time](https://img.shields.io/badge/development-657.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 @@ -28,26 +28,23 @@
import com.softwaremagico.kt.core.converters.models.ParticipantConverterRequest;
import com.softwaremagico.kt.core.exceptions.TokenExpiredException;
import com.softwaremagico.kt.core.exceptions.UserNotFoundException;
import com.softwaremagico.kt.core.providers.DuelProvider;
import com.softwaremagico.kt.core.providers.ParticipantProvider;
import com.softwaremagico.kt.persistence.entities.Participant;
import com.softwaremagico.kt.persistence.repositories.ParticipantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class ParticipantController extends BasicInsertableController<Participant, ParticipantDTO, ParticipantRepository,
ParticipantProvider, ParticipantConverterRequest, ParticipantConverter> {

private final DuelProvider duelProvider;


@Autowired
public ParticipantController(ParticipantProvider provider, ParticipantConverter converter, DuelProvider duelProvider) {
public ParticipantController(ParticipantProvider provider, ParticipantConverter converter) {
super(provider, converter);
this.duelProvider = duelProvider;
}

@Override
Expand Down Expand Up @@ -94,13 +91,13 @@ public Token generateToken(String temporalToken) {
}


public ParticipantDTO getYourWorstNightmare(ParticipantDTO participant) {
return convert(getProvider().getYourWorstNightmare(reverse(participant)));
public List<ParticipantDTO> getYourWorstNightmare(ParticipantDTO participant) {
return convertAll(getProvider().getYourWorstNightmare(reverse(participant)));
}


public ParticipantDTO getYouAreTheWorstNightmareOf(ParticipantDTO participant) {
return convert(getProvider().getYouAreTheWorstNightmareOf(reverse(participant)));
public List<ParticipantDTO> getYouAreTheWorstNightmareOf(ParticipantDTO participant) {
return convertAll(getProvider().getYouAreTheWorstNightmareOf(reverse(participant)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -103,10 +104,6 @@ public List<Participant> get(Club club) {
return getRepository().findByClub(club);
}

public Participant getByIdCard(String idCard) {
return getRepository().findByIdCard(idCard);
}

public TemporalToken generateTemporalToken(Participant participant) {
do {
participant.generateTemporalToken();
Expand Down Expand Up @@ -141,27 +138,35 @@ public Optional<Participant> findByTokenUsername(String tokenUsername) {
return Optional.empty();
}

public Participant getYourWorstNightmare(Participant sourceParticipant) {
public List<Participant> getYourWorstNightmare(Participant sourceParticipant) {
if (sourceParticipant == null) {
return null;
}
final List<Duel> duels = duelRepository.findByParticipant(sourceParticipant);
final Map<Participant, Integer> lostBy = new HashMap<>();
for (Duel duel : duels) {
final Participant winner = duel.getCompetitorWinner();
if (!Objects.equals(winner, sourceParticipant)) {
if (winner != null && !Objects.equals(winner, sourceParticipant)) {
lostBy.put(winner, lostBy.getOrDefault(winner, 0) + 1);
}
}
final List<Map.Entry<Participant, Integer>> sortedLostBy = lostBy.entrySet().stream().sorted(Map.Entry.comparingByValue()).toList();
final List<Map.Entry<Participant, Integer>> sortedLostBy = lostBy.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).toList();
final List<Participant> selected = new ArrayList<>();
if (!sortedLostBy.isEmpty()) {
return sortedLostBy.get(0).getKey();
final int maxScore = sortedLostBy.get(0).getValue();
int count = 0;
while (count < sortedLostBy.size() && sortedLostBy.get(count) != null && sortedLostBy.get(count).getValue() == maxScore) {
selected.add(sortedLostBy.get(count).getKey());
count++;
}
}
return null;

return selected;
}


public Participant getYouAreTheWorstNightmareOf(Participant sourceParticipant) {
public List<Participant> getYouAreTheWorstNightmareOf(Participant sourceParticipant) {
if (sourceParticipant == null) {
return null;
}
Expand All @@ -174,10 +179,18 @@ public Participant getYouAreTheWorstNightmareOf(Participant sourceParticipant) {
lostBy.put(looser, lostBy.getOrDefault(looser, 0) + 1);
}
}
final List<Map.Entry<Participant, Integer>> sortedLostBy = lostBy.entrySet().stream().sorted(Map.Entry.comparingByValue()).toList();
final List<Map.Entry<Participant, Integer>> sortedLostBy = lostBy.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).toList();
final List<Participant> selected = new ArrayList<>();
if (!sortedLostBy.isEmpty()) {
return sortedLostBy.get(0).getKey();
final int maxScore = sortedLostBy.get(0).getValue();
int count = 0;
while (count < sortedLostBy.size() && sortedLostBy.get(count) != null && sortedLostBy.get(count).getValue() == maxScore) {
selected.add(sortedLostBy.get(count).getKey());
count++;
}
}
return null;

return selected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.softwaremagico.kt.core.controller.FightController;
import com.softwaremagico.kt.core.controller.models.FightDTO;
import com.softwaremagico.kt.core.controller.models.TournamentDTO;
import com.softwaremagico.kt.core.converters.ParticipantConverter;
import com.softwaremagico.kt.core.managers.TeamsOrder;
import com.softwaremagico.kt.core.providers.ParticipantProvider;
import com.softwaremagico.kt.persistence.entities.Participant;
Expand Down Expand Up @@ -60,6 +61,9 @@ public class WorstNightmareTest extends TournamentTestUtils {
@Autowired
private ParticipantProvider participantProvider;

@Autowired
private ParticipantConverter participantConverter;

@BeforeClass
public void prepareData() {
addParticipants(MEMBERS, TEAMS, REFEREES, ORGANIZER, VOLUNTEER, PRESS, 0);
Expand Down Expand Up @@ -98,37 +102,37 @@ public void prepareTournament1() {
fightDTOs.get(1).getDuels().get(0).addCompetitor1Score(Score.KOTE);
fightDTOs.get(1).getDuels().get(0).addCompetitor1Score(Score.MEN);
fightDTOs.get(1).getDuels().get(0).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//P7 vs P4
fightDTOs.get(1).getDuels().get(1).addCompetitor1Score(Score.KOTE);
fightDTOs.get(1).getDuels().get(1).addCompetitor1Score(Score.MEN);
fightDTOs.get(1).getDuels().get(1).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//P8 vs P5
fightDTOs.get(1).getDuels().get(2).addCompetitor1Score(Score.KOTE);
fightDTOs.get(1).getDuels().get(2).addCompetitor1Score(Score.MEN);
fightDTOs.get(1).getDuels().get(2).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//P6 vs P0
fightDTOs.get(2).getDuels().get(0).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(0).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(0).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));

//P7 vs P1
fightDTOs.get(2).getDuels().get(1).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(1).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(1).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));

//P8 vs P2
fightDTOs.get(2).getDuels().get(2).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(2).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(2).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));
}

@BeforeClass(dependsOnMethods = "createTournaments")
Expand Down Expand Up @@ -160,54 +164,60 @@ public void prepareTournament2() {
//P7 vs P4
fightDTOs.get(1).getDuels().get(1).addCompetitor2Score(Score.KOTE);
fightDTOs.get(1).getDuels().get(1).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//P8 vs P5
fightDTOs.get(1).getDuels().get(2).addCompetitor1Score(Score.KOTE);
fightDTOs.get(1).getDuels().get(2).addCompetitor1Score(Score.MEN);
fightDTOs.get(1).getDuels().get(2).setFinished(true);
fightDTOs.set(1, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(1, fightController.update(fightDTOs.get(1), null));

//P6 vs P0
fightDTOs.get(2).getDuels().get(0).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(0).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(0).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));

//P7 vs P1
fightDTOs.get(2).getDuels().get(1).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(1).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(1).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));

//P8 vs P2
fightDTOs.get(2).getDuels().get(2).addCompetitor1Score(Score.KOTE);
fightDTOs.get(2).getDuels().get(2).addCompetitor1Score(Score.MEN);
fightDTOs.get(2).getDuels().get(2).setFinished(true);
fightDTOs.set(2, fightController.update(fightDTOs.get(0), null));
fightDTOs.set(2, fightController.update(fightDTOs.get(2), null));
}

@Test
public void checkYourWorstNightmare() {
final Participant participant1 = participantProvider.getByIdCard("0001");
final Participant participant3 = participantProvider.getByIdCard("0003");
final Participant participant4 = participantProvider.getByIdCard("0004");
final Participant participant6 = participantProvider.getByIdCard("0006");
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant3), participant6);
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant4), participant1);
final Participant participant0 = participantConverter.reverse(participantsDTOs.get(0));
final Participant participant1 = participantConverter.reverse(participantsDTOs.get(1));
final Participant participant3 = participantConverter.reverse(participantsDTOs.get(3));
final Participant participant4 = participantConverter.reverse(participantsDTOs.get(4));
//P0 and P6
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant3).size(), 2);
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant3).get(0), participant0);
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant4).size(), 1);
Assert.assertEquals(participantProvider.getYourWorstNightmare(participant4).get(0), participant1);
}

@Test
public void checkWorstNightmareOf() {
final Participant participant1 = participantProvider.getByIdCard("0001");
final Participant participant3 = participantProvider.getByIdCard("0003");
final Participant participant4 = participantProvider.getByIdCard("0004");
final Participant participant6 = participantProvider.getByIdCard("0006");
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant6), participant3);
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant1), participant4);
final Participant participant0 = participantConverter.reverse(participantsDTOs.get(0));
final Participant participant1 = participantConverter.reverse(participantsDTOs.get(1));
final Participant participant4 = participantConverter.reverse(participantsDTOs.get(4));
final Participant participant6 = participantConverter.reverse(participantsDTOs.get(6));
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant6).size(), 1);
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant6).get(0), participant0);
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant1).size(), 1);
Assert.assertEquals(participantProvider.getYouAreTheWorstNightmareOf(participant1).get(0), participant4);
}

@AfterClass(alwaysRun = true)
@Override
public void wipeOut() {
super.wipeOut();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ List<Participant> findParticipantsWithRoleNotInTournaments(@Param("tournament")

List<Participant> findByClub(Club club);

Participant findByIdCard(String idCard);

long countByTemporalToken(String temporalToken);

Optional<Participant> findByTemporalToken(String temporalToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,16 @@ public ParticipantStatisticsDTO getStatisticsFromParticipant(@Parameter(descript
@PreAuthorize("hasAnyRole('ROLE_VIEWER', 'ROLE_EDITOR', 'ROLE_ADMIN', 'ROLE_PARTICIPANT')")
@Operation(summary = "Gets participant worst nightmare.", security = @SecurityRequirement(name = "bearerAuth"))
@GetMapping(value = "/participants/your-worst-nightmare/{participantId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ParticipantDTO getYourWorstNightmareFromParticipant(@Parameter(description = "Id of an existing participant", required = true)
@PathVariable("participantId") Integer participantId,
Authentication authentication,
HttpServletRequest request) {
public List<ParticipantDTO> getYourWorstNightmareFromParticipant(@Parameter(description = "Id of an existing participant", required = true)
@PathVariable("participantId") Integer participantId,
Authentication authentication,
HttpServletRequest request) {
//If is a participant guest, only its own statistics can see.
if (authentication != null) {
final Optional<Participant> participant = participantProvider.findByTokenUsername(authentication.getName());
if (participant.isPresent()) {
if (!Objects.equals(participant.get().getId(), participantId)) {
throw new InvalidRequestException(this.getClass(), "User '" + authentication.getName()
+ "' is trying to access to statistics from user '" + participantId + "'.");
}
if (participant.isPresent() && !Objects.equals(participant.get().getId(), participantId)) {
throw new InvalidRequestException(this.getClass(), "User '" + authentication.getName()
+ "' is trying to access to statistics from user '" + participantId + "'.");
}
}
return participantController.getYourWorstNightmare(participantController.get(participantId));
Expand All @@ -180,10 +178,10 @@ public ParticipantDTO getYourWorstNightmareFromParticipant(@Parameter(descriptio
@PreAuthorize("hasAnyRole('ROLE_VIEWER', 'ROLE_EDITOR', 'ROLE_ADMIN', 'ROLE_PARTICIPANT')")
@Operation(summary = "Gets participant worst nightmare.", security = @SecurityRequirement(name = "bearerAuth"))
@GetMapping(value = "/participants/worst-nightmare-of/{participantId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ParticipantDTO getWorstNightmareOf(@Parameter(description = "Id of an existing participant", required = true)
@PathVariable("participantId") Integer participantId,
Authentication authentication,
HttpServletRequest request) {
public List<ParticipantDTO> getWorstNightmareOf(@Parameter(description = "Id of an existing participant", required = true)
@PathVariable("participantId") Integer participantId,
Authentication authentication,
HttpServletRequest request) {
//If is a participant guest, only its own statistics can see.
if (authentication != null) {
final Optional<Participant> participant = participantProvider.findByTokenUsername(authentication.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class TournamentBracketsEditorComponent implements OnInit, OnDestroy {
const pdf: jsPDF = new jsPDF(jsPdfOptions);
pdf.addImage(result, 'PNG', 25, 25, widthMM * ratio, heightMM * ratio);
pdf.save(this.tournament.name + '.pdf');
}).catch(error => {
}).catch((): void => {
});
}

Expand Down
Loading

0 comments on commit 0b44a9a

Please sign in to comment.