Skip to content

Commit

Permalink
Kan 51 bug duels curr (#41)
Browse files Browse the repository at this point in the history
* first try

* trivial "/duels/curr"

* done

* prytify
  • Loading branch information
c-i-a-s-t-e-k authored Jun 11, 2024
1 parent 68f3d51 commit 319a825
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.rikishi.rikishi.service.FightService;
import com.rikishi.rikishi.service.UserService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -72,6 +74,7 @@ public void patchDuel(@RequestBody Map<String, String> fields, @PathVariable Lon

@GetMapping("/duels/curr")
public Duels getCurrentDuels() {
fightService.tryReload();
return new Duels(
fightService.getCurrFights().map(Fight::toJson).toList()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
public class FightService implements AutoCloseable {
private final FightRepository fightRepository;
private final Map<WeightClass, MatchingSystem_II> tournaments = new HashMap<>();
private final ResConfigProvider resConfigProvider;
private final UserService userService;


public FightService(FightRepository fr, UserService us, ResConfigProvider rcf) {
this.fightRepository = fr;
this.resConfigProvider = rcf;
this.userService = us;

for (WeightClass weightClass : rcf.getWeightClasses()) {
try {
Expand All @@ -29,6 +33,7 @@ public FightService(FightRepository fr, UserService us, ResConfigProvider rcf) {
matchingSystem.loadPlayers(players);
fightRepository.addAll(matchingSystem.getAllFights());
tournaments.put(weightClass, matchingSystem);
System.out.println("\u001B[32m" + weightClass.name() + "\u001B[0m");
} catch (IllegalArgumentException e) {
System.err.println(weightClass.name() + "\t\t\t" + e.getMessage());
}
Expand All @@ -41,6 +46,23 @@ public void updateFight(Fight fight) {
fightRepository.add(fight);
}

public void tryReload() {
for (WeightClass weightClass : resConfigProvider.getWeightClasses()) {
if (tournaments.containsKey(weightClass) && !tournaments.get(weightClass).playersLoaded()) {
try {
Collection<User> players = userService.getUsers().filter(weightClass::isValid).toList();
MatchingSystem_II matchingSystem = SystemPicker.pickSystem(players.size());
matchingSystem.loadPlayers(players);
fightRepository.addAll(matchingSystem.getAllFights());
tournaments.put(weightClass, matchingSystem);
System.out.println("\u001B[32m" + weightClass.name() + " added\u001B[0m");
} catch (IllegalArgumentException e) {
System.err.println(weightClass.name() + "\t\t\tunabled to reload");
}
}
}
}

public Stream<Fight> getAllCategoryFights(WeightClass weightClass) {
if (!tournaments.containsKey(weightClass))
return Stream.empty();
Expand All @@ -51,6 +73,7 @@ public Stream<Fight> getCurrFights() {
return tournaments.values().stream()
.flatMap(system -> system.getCurrentFights().stream());
}

public Stream<Fight> getAllFights() {
return tournaments.values().stream()
.flatMap(system -> system.getAllFights().stream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public interface MatchingSystem_II {
void loadPlayers(Collection<User> players);

boolean playersLoaded();

void updateFight(Fight fight);

List<Fight> getAllFights();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public void loadPlayers(Collection<User> players) {
maxEntry.ifPresent(entry -> weightCategory = entry.getKey());
}

@Override
public boolean playersLoaded() {
return (!arrayTree.isEmpty());
}

public void printBracket() {
if (arrayTree.isEmpty()) {
System.out.println("Empty Bracket.");
Expand Down

0 comments on commit 319a825

Please sign in to comment.