|
| 1 | +package eu.solven.kumite.app.player; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.UUID; |
| 7 | + |
| 8 | +import eu.solven.kumite.app.server.IKumiteServer; |
| 9 | +import eu.solven.kumite.contest.ContestSearchParameters; |
| 10 | +import eu.solven.kumite.contest.ContestView; |
| 11 | +import eu.solven.kumite.game.GameSearchParameters; |
| 12 | +import eu.solven.kumite.game.IGameMetadataConstants; |
| 13 | +import eu.solven.kumite.player.PlayerRawMovesHolder; |
| 14 | +import lombok.AllArgsConstructor; |
| 15 | +import lombok.NonNull; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +@AllArgsConstructor |
| 20 | +@Slf4j |
| 21 | +public class KumitePlayer implements IKumitePlayer { |
| 22 | + final IKumiteServer kumiteServer; |
| 23 | + |
| 24 | + /** |
| 25 | + * Optimization games are the simplest one in term of integration: one just have to publish one solution to get on |
| 26 | + * the leaderboard |
| 27 | + * |
| 28 | + * @param kumiteServer |
| 29 | + * @param playerId |
| 30 | + */ |
| 31 | + @Override |
| 32 | + public void playOptimizationGames(UUID playerId) { |
| 33 | + GameSearchParameters optimizationsGameSearch = |
| 34 | + GameSearchParameters.builder().requiredTag(IGameMetadataConstants.TAG_OPTIMIZATION).build(); |
| 35 | + |
| 36 | + // Given a list of human-friendly game title |
| 37 | + // Flux.fromStream(Stream.of("Travelling Salesman Problem", "Tic-Tac-Toe")) |
| 38 | + // Build a SearchGames query |
| 39 | + // .map(gameTitle -> )) |
| 40 | + Mono.just(optimizationsGameSearch) |
| 41 | + // Search the games |
| 42 | + .flatMapMany(gameSearch -> { |
| 43 | + log.info("Looking for games matching `{}`", gameSearch); |
| 44 | + return kumiteServer.searchGames(gameSearch); |
| 45 | + }) |
| 46 | + // Search for contests for given game |
| 47 | + .flatMap(game -> kumiteServer.searchContests( |
| 48 | + ContestSearchParameters.builder().gameId(Optional.of(game.getGameId())).build())) |
| 49 | + // Load the board for given contest |
| 50 | + .flatMap(contest -> kumiteServer.loadBoard(playerId, contest.getContestId())) |
| 51 | + // Filter interesting boards |
| 52 | + .filter(c -> !c.getDynamicMetadata().isGameOver()) |
| 53 | + .filter(c -> c.getDynamicMetadata().isAcceptingPlayers()) |
| 54 | + // Process each contest |
| 55 | + .flatMap(contestView -> { |
| 56 | + UUID contestId = contestView.getContestId(); |
| 57 | + |
| 58 | + if (contestView.getPlayingPlayer().isPlayerHasJoined()) { |
| 59 | + log.info("Received board for already joined contestId={}", contestId); |
| 60 | + return Mono.empty(); |
| 61 | + } else if (contestView.getPlayingPlayer().isPlayerCanJoin()) { |
| 62 | + log.info("Received board for joinable contestId={}", contestId); |
| 63 | + return kumiteServer.joinContest(playerId, contestId) |
| 64 | + // We load the board again once we are signed-up |
| 65 | + .flatMap(playingPlayer -> kumiteServer.loadBoard(contestId, playerId)); |
| 66 | + } else { |
| 67 | + log.info("We can not join contest={}", contestId); |
| 68 | + return Mono.empty(); |
| 69 | + } |
| 70 | + }) |
| 71 | + .flatMap(joinedContestView -> { |
| 72 | + UUID contestId = joinedContestView.getContestId(); |
| 73 | + |
| 74 | + if (joinedContestView.getDynamicMetadata().isGameOver()) { |
| 75 | + log.info("contestId={} is gameOver", contestId); |
| 76 | + return Mono.empty(); |
| 77 | + } |
| 78 | + |
| 79 | + Mono<PlayerRawMovesHolder> exampleMoves = |
| 80 | + kumiteServer.getExampleMoves(joinedContestView.getPlayingPlayer().getPlayerId(), contestId); |
| 81 | + |
| 82 | + return exampleMoves.flatMap(moves -> { |
| 83 | + Optional<Map<String, ?>> selectedMove = selectMove(joinedContestView.getBoard(), moves); |
| 84 | + |
| 85 | + if (selectedMove.isEmpty()) { |
| 86 | + log.info("No move. We quit the game. contestId=", contestId); |
| 87 | + return Mono.empty(); |
| 88 | + } |
| 89 | + |
| 90 | + return kumiteServer.playMove(playerId, joinedContestView.getContestId(), selectedMove.get()); |
| 91 | + }); |
| 92 | + }) |
| 93 | + .flatMap(contestView -> { |
| 94 | + return kumiteServer.loadLeaderboard(contestView.getContestId()).doOnNext(leaderboard -> { |
| 95 | + log.info("contestid={} leaderbord={}", contestView.getContestId(), leaderboard); |
| 96 | + }); |
| 97 | + }) |
| 98 | + .subscribe(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 1v1 games needs sone sort of `do-while` loop, to play moves until the game is over. |
| 103 | + * |
| 104 | + * @param kumiteServer |
| 105 | + * @param playerId |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public void play1v1(UUID playerId) { |
| 109 | + GameSearchParameters optimizationsGameSearch = |
| 110 | + GameSearchParameters.builder().requiredTag(IGameMetadataConstants.TAG_1V1).build(); |
| 111 | + |
| 112 | + Mono.just(optimizationsGameSearch) |
| 113 | + // Search the games |
| 114 | + .flatMapMany(gameSearch -> { |
| 115 | + log.info("Looking for games matching `{}`", gameSearch); |
| 116 | + return kumiteServer.searchGames(gameSearch); |
| 117 | + }) |
| 118 | + // Search for contests for given game |
| 119 | + .flatMap(game -> kumiteServer.searchContests( |
| 120 | + ContestSearchParameters.builder().gameId(Optional.of(game.getGameId())).build())) |
| 121 | + // Load the board for given contest |
| 122 | + .flatMap(contest -> kumiteServer.loadBoard(playerId, contest.getContestId())) |
| 123 | + // Filter interesting boards |
| 124 | + .filter(c -> !c.getDynamicMetadata().isGameOver()) |
| 125 | + .filter(c -> c.getDynamicMetadata().isAcceptingPlayers()) |
| 126 | + // Process each contest |
| 127 | + .flatMap(contestView -> { |
| 128 | + UUID contestId = contestView.getContestId(); |
| 129 | + |
| 130 | + if (contestView.getPlayingPlayer().isPlayerHasJoined()) { |
| 131 | + log.info("Received board for already joined contestId={}", contestId); |
| 132 | + return Mono.empty(); |
| 133 | + } else if (contestView.getPlayingPlayer().isPlayerCanJoin()) { |
| 134 | + log.info("Received board for joinable contestId={}", contestId); |
| 135 | + return kumiteServer.joinContest(playerId, contestId) |
| 136 | + .flatMap(playingPlayer -> kumiteServer.loadBoard(contestId, playerId)); |
| 137 | + } else { |
| 138 | + log.info("We can not join contest={}", contestId); |
| 139 | + return Mono.empty(); |
| 140 | + } |
| 141 | + }) |
| 142 | + // This acts like a `do-while` loop: we loop by playing moves until the game is over |
| 143 | + // https://codersee.com/project-reactor-expand/ |
| 144 | + .expand(joinedContestView -> { |
| 145 | + UUID contestId = joinedContestView.getContestId(); |
| 146 | + |
| 147 | + if (joinedContestView.getDynamicMetadata().isGameOver()) { |
| 148 | + log.info("contestId={} is gameOver", contestId); |
| 149 | + return Mono.empty(); |
| 150 | + } |
| 151 | + |
| 152 | + Mono<PlayerRawMovesHolder> exampleMoves = |
| 153 | + kumiteServer.getExampleMoves(joinedContestView.getPlayingPlayer().getPlayerId(), contestId); |
| 154 | + Mono<ContestView> monoContestViewPostMove = exampleMoves.flatMap(moves -> { |
| 155 | + Optional<Map<String, ?>> optSelectedMove = selectMove(joinedContestView.getBoard(), moves); |
| 156 | + |
| 157 | + if (optSelectedMove.isEmpty()) { |
| 158 | + // There is no available move: wait until gameOver |
| 159 | + Duration delay = Duration.ofSeconds(5); |
| 160 | + log.info("No move. We wait {} for {}", delay, contestId); |
| 161 | + return Mono.just(joinedContestView).delayElement(delay); |
| 162 | + } |
| 163 | + |
| 164 | + Map<String, ?> selectedMove = optSelectedMove.get(); |
| 165 | + log.info("We playMove `{}`for {}", selectedMove); |
| 166 | + return kumiteServer.playMove(playerId, joinedContestView.getContestId(), selectedMove); |
| 167 | + }); |
| 168 | + return monoContestViewPostMove; |
| 169 | + }) |
| 170 | + .flatMap(contestView -> { |
| 171 | + return kumiteServer.loadLeaderboard(contestView.getContestId()).doOnNext(leaderboard -> { |
| 172 | + log.info("contestid={} leaderbord={}", contestView.getContestId(), leaderboard); |
| 173 | + }); |
| 174 | + }) |
| 175 | + .subscribe(); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Select one move amongst the examples moves. |
| 180 | + * |
| 181 | + * @param board |
| 182 | + * @param moves |
| 183 | + * @return the first suggested move. |
| 184 | + */ |
| 185 | + private Optional<Map<String, ?>> selectMove(@NonNull Map<String, ?> board, PlayerRawMovesHolder moves) { |
| 186 | + // WaitForPlayersMove and WaitForSignups would have a `wait:true` flag |
| 187 | + return moves.getMoves().values().stream().filter(m -> !Boolean.TRUE.equals(m.get("wait"))).findAny(); |
| 188 | + } |
| 189 | +} |
0 commit comments