Skip to content

Commit

Permalink
Pagination Online Replay Vault
Browse files Browse the repository at this point in the history
Fixes #1764

Co-Authored-By: tarmandan <58087411+tarmandan@users.noreply.github.com>
Co-Authored-By: aclrian <32142988+aclrian@users.noreply.github.com>
Co-Authored-By: Jia Hong Frenki Zhang <64331094+frezha@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 22, 2020
1 parent 501d69d commit c567d6f
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 275 deletions.
114 changes: 67 additions & 47 deletions src/icomoon/Downlord's FAF Client.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/main/java/com/faforever/client/api/FafApiAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.faforever.client.api.dto.TutorialCategory;
import com.faforever.client.game.KnownFeaturedMod;
import com.faforever.client.mod.FeaturedMod;
import com.faforever.client.util.Tuple;
import com.faforever.client.vault.search.SearchController.SearchConfig;
import com.faforever.client.vault.search.SearchController.SortConfig;
import com.faforever.commons.io.ByteCountListener;
Expand Down Expand Up @@ -87,11 +88,11 @@ public interface FafApiAccessor {

List<FeaturedModFile> getFeaturedModFiles(FeaturedMod featuredMod, Integer version);

List<Game> getNewestReplays(int count, int page);
Tuple<List<Game>, java.util.Map<String, ?>> getNewestReplaysWithMeta(int count, int page);

List<Game> getHighestRatedReplays(int count, int page);
Tuple<List<Game>, java.util.Map<String, ?>> getHighestRatedReplaysWithMeta(int count, int page);

List<Game> findReplaysByQuery(String condition, int maxResults, int page, SortConfig sortConfig);
Tuple<List<Game>, java.util.Map<String, ?>> findReplaysByQueryWithMeta(String query, int maxResults, int page, SortConfig sortConfig);

Optional<MapVersion> findMapByFolderName(String folderName);

Expand Down
33 changes: 14 additions & 19 deletions src/main/java/com/faforever/client/api/FafApiAccessorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.faforever.client.mod.FeaturedMod;
import com.faforever.client.user.event.LoggedOutEvent;
import com.faforever.client.user.event.LoginSuccessEvent;
import com.faforever.client.util.Tuple;
import com.faforever.client.vault.search.SearchController.SearchConfig;
import com.faforever.client.vault.search.SearchController.SortConfig;
import com.faforever.commons.io.ByteCountListener;
Expand Down Expand Up @@ -130,23 +131,20 @@ public void onLoginSuccessEvent(LoginSuccessEvent event) {
}

@Override
@SuppressWarnings("unchecked")
public List<PlayerAchievement> getPlayerAchievements(int playerId) {
return getAll("/data/playerAchievement", ImmutableMap.of(
"filter", rsql(qBuilder().intNum("player.id").eq(playerId))
));
}

@Override
@SuppressWarnings("unchecked")
public List<PlayerEvent> getPlayerEvents(int playerId) {
return getAll("/data/playerEvent", ImmutableMap.of(
"filter", rsql(qBuilder().intNum("player.id").eq(playerId))
));
}

@Override
@SuppressWarnings("unchecked")
@Cacheable(CacheNames.ACHIEVEMENTS)
public List<AchievementDefinition> getAchievementDefinitions() {
return getAll("/data/achievement", ImmutableMap.of(
Expand Down Expand Up @@ -176,7 +174,6 @@ public List<com.faforever.client.api.dto.FeaturedMod> getFeaturedMods() {
@Override
@Cacheable(CacheNames.GLOBAL_LEADERBOARD)
@SneakyThrows
@SuppressWarnings("unchecked")
public List<GlobalLeaderboardEntry> getGlobalLeaderboard() {
// This is not an ordinary JSON-API route and thus doesn't support paging, that's why it's called manually
authorizedLatch.await();
Expand All @@ -192,7 +189,6 @@ public List<GlobalLeaderboardEntry> getGlobalLeaderboard() {
@Override
@Cacheable(CacheNames.LADDER_1V1_LEADERBOARD)
@SneakyThrows
@SuppressWarnings("unchecked")
public List<Ladder1v1LeaderboardEntry> getLadder1v1Leaderboard() {
// This is not an ordinary JSON-API route and thus doesn't support paging, that's why it doesn't use getAll()
authorizedLatch.await();
Expand Down Expand Up @@ -312,33 +308,37 @@ public List<FeaturedModFile> getFeaturedModFiles(FeaturedMod featuredMod, Intege
}

@Override
public List<Game> getNewestReplays(int count, int page) {
return getPage("/data/game", count, page, ImmutableMap.of(
public Tuple<List<Game>, java.util.Map<String, ?>> getNewestReplaysWithMeta(int count, int page) {
JSONAPIDocument<List<Game>> jsonApiDoc = getPageWithMeta("/data/game", count, page, ImmutableMap.of(
"sort", "-endTime",
"include", REPLAY_INCLUDES,
"filter", "endTime=isnull=false"
));
return new Tuple<>(jsonApiDoc.get(), jsonApiDoc.getMeta());
}

@Override
public List<Game> getHighestRatedReplays(int count, int page) {
return this.<GameReviewsSummary>getPage("/data/gameReviewsSummary", count, page, ImmutableMap.of(
public Tuple<List<Game>, java.util.Map<String, ?>> getHighestRatedReplaysWithMeta(int count, int page) {
JSONAPIDocument<List<GameReviewsSummary>> pageWithMeta = getPageWithMeta("/data/gameReviewsSummary", count, page, ImmutableMap.of(
"sort", "-lowerBound",
// TODO this was done in a rush, check what is actually needed
"include", "game,game.featuredMod,game.playerStats,game.playerStats.player,game.reviews,game.reviews.player,game.mapVersion,game.mapVersion.map,game.mapVersion.reviews",
"filter", "game.endTime=isnull=false"
)).stream()
));
return new Tuple<>(pageWithMeta.get().stream()
.map(GameReviewsSummary::getGame)
.collect(Collectors.toList());
.collect(Collectors.toList()),
pageWithMeta.getMeta());
}

@Override
public List<Game> findReplaysByQuery(String query, int maxResults, int page, SortConfig sortConfig) {
return getPage("/data/game", maxResults, page, ImmutableMap.of(
public Tuple<List<Game>, java.util.Map<String, ?>> findReplaysByQueryWithMeta(String query, int maxResults, int page, SortConfig sortConfig) {
JSONAPIDocument<List<Game>> jsonApiDoc = getPageWithMeta("/data/game", maxResults, page, ImmutableMap.of(
"filter", "(" + query + ");endTime=isnull=false",
"include", REPLAY_INCLUDES,
"sort", sortConfig.toQuery()
));
return new Tuple<>(jsonApiDoc.get(), jsonApiDoc.getMeta());
}

@Override
Expand Down Expand Up @@ -487,7 +487,7 @@ public Optional<MapVersion> findMapVersionById(String id) {
@Override
@Cacheable(CacheNames.COOP_MAPS)
public List<CoopMission> getCoopMissions() {
return this.getAll("/data/coopMission");
return getAll("/data/coopMission");
}

@Override
Expand All @@ -508,7 +508,6 @@ public List<CoopResult> getCoopLeaderboard(String missionId, int numberOfPlayers

@Override
@SneakyThrows
@SuppressWarnings("unchecked")
public List<Tournament> getAllTournaments() {
return Arrays.asList(restOperations.getForObject(TOURNAMENT_LIST_ENDPOINT, Tournament[].class));
}
Expand Down Expand Up @@ -571,13 +570,11 @@ private void delete(String endpointPath) {
restOperations.delete(endpointPath);
}

@SuppressWarnings("unchecked")
@SneakyThrows
private <T> T getOne(String endpointPath, Class<T> type) {
return restOperations.getForObject(endpointPath, type, Collections.emptyMap());
}

@SuppressWarnings("unchecked")
@SneakyThrows
private <T> T getOne(String endpointPath, Class<T> type, java.util.Map<String, Serializable> params) {
java.util.Map<String, List<String>> multiValues = params.entrySet().stream()
Expand Down Expand Up @@ -626,7 +623,6 @@ private <T> JSONAPIDocument<List<T>> getPageWithMeta(String endpointPath, int pa
return getPageWithMeta(endpointPath, pageSize, page, CollectionUtils.toMultiValueMap(multiValues));
}

@SuppressWarnings("unchecked")
@SneakyThrows
private <T> List<T> getPage(String endpointPath, int pageSize, int page, MultiValueMap<String, String> params) {
UriComponents uriComponents = UriComponentsBuilder.fromPath(endpointPath)
Expand All @@ -639,7 +635,6 @@ private <T> List<T> getPage(String endpointPath, int pageSize, int page, MultiVa
return restOperations.getForObject(uriComponents.toUriString(), List.class);
}

@SuppressWarnings("unchecked")
@SneakyThrows
private <T> JSONAPIDocument<List<T>> getPageWithMeta(String endpointPath, int pageSize, int page, MultiValueMap<String, String> params) {
UriComponents uriComponents = UriComponentsBuilder.fromPath(endpointPath)
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/faforever/client/api/MockFafApiAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.faforever.client.api.dto.TutorialCategory;
import com.faforever.client.game.KnownFeaturedMod;
import com.faforever.client.mod.FeaturedMod;
import com.faforever.client.util.Tuple;
import com.faforever.client.vault.search.SearchController.SearchConfig;
import com.faforever.client.vault.search.SearchController.SortConfig;
import com.faforever.commons.io.ByteCountListener;
Expand Down Expand Up @@ -178,18 +179,18 @@ public List<FeaturedModFile> getFeaturedModFiles(FeaturedMod featuredMod, Intege
}

@Override
public List<Game> getNewestReplays(int count, int page) {
return Collections.emptyList();
public Tuple<List<Game>, java.util.Map<String, ?>> getNewestReplaysWithMeta(int count, int page) {
return new Tuple(Collections.emptyList(), Collections.emptyMap());
}

@Override
public List<Game> getHighestRatedReplays(int count, int page) {
return Collections.emptyList();
public Tuple<List<Game>, java.util.Map<String, ?>> getHighestRatedReplaysWithMeta(int count, int page) {
return new Tuple(Collections.emptyList(), Collections.emptyMap());
}

@Override
public List<Game> findReplaysByQuery(String query, int maxResults, int page, SortConfig sortConfig) {
return Collections.emptyList();
public Tuple<List<Game>, java.util.Map<String, ?>> findReplaysByQueryWithMeta(String query, int maxResults, int page, SortConfig sortConfig) {
return new Tuple(Collections.emptyList(), Collections.emptyMap());
}

@Override
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/faforever/client/remote/FafService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.faforever.client.replay.Replay;
import com.faforever.client.tournament.TournamentBean;
import com.faforever.client.tutorial.TutorialCategory;
import com.faforever.client.util.Tuple;
import com.faforever.client.vault.review.Review;
import com.faforever.client.vault.search.SearchController.SearchConfig;
import com.faforever.client.vault.search.SearchController.SortConfig;
Expand Down Expand Up @@ -75,7 +76,6 @@ public <T extends ServerMessage> void addOnMessageListener(Class<T> type, Consum
fafServerAccessor.addOnMessageListener(type, listener);
}

@SuppressWarnings("unchecked")
public <T extends ServerMessage> void removeOnMessageListener(Class<T> type, Consumer<T> listener) {
fafServerAccessor.removeOnMessageListener(type, listener);
}
Expand Down Expand Up @@ -261,19 +261,23 @@ public CompletableFuture<List<LeaderboardEntry>> getGlobalLeaderboard() {
}

@Async
public CompletableFuture<List<Replay>> getNewestReplays(int topElementCount, int page) {
return CompletableFuture.completedFuture(fafApiAccessor.getNewestReplays(topElementCount, page)
public CompletableFuture<Tuple<List<Replay>, java.util.Map<String, ?>>> getNewestReplaysMeta(int topElementCount, int page) {
Tuple<List<Game>, java.util.Map<String, ?>> tuple = fafApiAccessor.getNewestReplaysWithMeta(topElementCount, page);
return CompletableFuture.completedFuture(new Tuple<>(tuple.getFirst()
.parallelStream()
.map(Replay::fromDto)
.collect(toList()));
.collect(toList()),
tuple.getSecond()));
}

@Async
public CompletableFuture<List<Replay>> getHighestRatedReplays(int topElementCount, int page) {
return CompletableFuture.completedFuture(fafApiAccessor.getHighestRatedReplays(topElementCount, page)
public CompletableFuture<Tuple<List<Replay>, java.util.Map<String, ?>>> getHighestRatedReplaysWithMeta(int topElementCount, int page) {
Tuple<List<Game>, java.util.Map<String, ?>> tuple = fafApiAccessor.getHighestRatedReplaysWithMeta(topElementCount, page);
return CompletableFuture.completedFuture(new Tuple<>(tuple.getFirst()
.parallelStream()
.map(Replay::fromDto)
.collect(toList()));
.collect(toList()),
tuple.getSecond()));
}

public void uploadMod(Path modFile, ByteCountListener byteListener) {
Expand All @@ -296,11 +300,13 @@ public CompletableFuture<AchievementDefinition> getAchievementDefinition(String
}

@Async
public CompletableFuture<List<Replay>> findReplaysByQuery(String query, int maxResults, int page, SortConfig sortConfig) {
return CompletableFuture.completedFuture(fafApiAccessor.findReplaysByQuery(query, maxResults, page, sortConfig)
public CompletableFuture<Tuple<List<Replay>, java.util.Map<String, ?>>> findReplaysByQueryWithMeta(String query, int maxResults, int page, SortConfig sortConfig) {
Tuple<List<Game>, java.util.Map<String, ?>> tuple = fafApiAccessor.findReplaysByQueryWithMeta(query, maxResults, page, sortConfig);
return CompletableFuture.completedFuture(new Tuple<>(tuple.getFirst()
.parallelStream()
.map(Replay::fromDto)
.collect(toList()));
.collect(toList()),
tuple.getSecond()));
}

@Async
Expand Down

0 comments on commit c567d6f

Please sign in to comment.