RankingFLow: use actual GameResultStates

This commit is contained in:
djmil 2023-09-22 17:23:06 +02:00
parent 5f59260120
commit e1cc9bd9fd

View File

@ -1,7 +1,9 @@
package djmil.cordacheckers.gameresult;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@ -35,11 +37,23 @@ public class RankingFlow implements ClientStartableFlow {
try {
final List<GameResultState> gameStateResutList = getGameResultsList();
final List<Rank> board = new LinkedList<Rank>(List.of(
new Rank("alice", 42, 1),
new Rank("bobik", 25, 78)
));
Map<String, Integer> won = new HashMap<>();
for (GameResultState gs : gameStateResutList) {
final var winner = gs.getWinnerName().getCommonName();
won.put(winner, won.getOrDefault(winner, 0) +1);
}
Map<String, Integer> played = new HashMap<>(won);
for (GameResultState gs : gameStateResutList) {
final var looseer = gs.getLooserName().getCommonName();
played.put(looseer, played.getOrDefault(looseer, 0) +1);
}
List<Rank> board = new LinkedList<Rank>();
for (String name : played.keySet()) {
board.add(new Rank(name, played.getOrDefault(name, 0), won.getOrDefault(name, 0)));
}
return new RankingFlowResponce(board)
.toJsonEncodedString(jsonMarshallingService);
} catch (Exception e) {