Skip to content

Commit

Permalink
FAForever#2319 Adjusted API interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ETFreeman committed Mar 14, 2024
1 parent b7d9dc4 commit 7516c06
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/faforever/client/api/FafApiAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class FafApiAccessor implements InitializingBean {
java.util.Map.entry(MapVersion.class, List.of("map", "map.reviewsSummary", "map.author")),
java.util.Map.entry(MapReviewsSummary.class, List.of("map.latestVersion", "map.author", "map.reviewsSummary")),
java.util.Map.entry(Map.class, List.of("latestVersion", "author", "reviewsSummary")),
java.util.Map.entry(MapPoolAssignment.class, List.of("mapVersion", "mapVersion.map", "mapVersion.map.author", "mapVersion.map.reviewsSummary")),
java.util.Map.entry(MapPoolAssignment.class, List.of("mapVersion", "mapVersion.map", "mapVersion.map.author", "mapVersion.map.reviewsSummary", "mapPool", "mapPool.matchmakerQueueMapPool")),
java.util.Map.entry(ModVersion.class, List.of("mod", "mod.latestVersion", "mod.reviewsSummary", "mod.uploader")),
java.util.Map.entry(ModReviewsSummary.class, List.of("mod.latestVersion", "mod.reviewsSummary", "mod.uploader")),
java.util.Map.entry(Mod.class, List.of("latestVersion", "reviewsSummary", "uploader")),
Expand Down
41 changes: 13 additions & 28 deletions src/main/java/com/faforever/client/map/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.faforever.client.domain.api.Map;
import com.faforever.client.domain.api.MapType;
import com.faforever.client.domain.api.MapVersion;
import com.faforever.client.domain.api.MatchmakerQueueMapPool;
import com.faforever.client.domain.server.MatchmakerQueueInfo;
import com.faforever.client.domain.server.PlayerInfo;
import com.faforever.client.exception.AssetLoadException;
Expand All @@ -16,6 +17,7 @@
import com.faforever.client.i18n.I18n;
import com.faforever.client.map.generator.MapGeneratorService;
import com.faforever.client.mapstruct.MapMapper;
import com.faforever.client.mapstruct.MatchmakerMapper;
import com.faforever.client.notification.NotificationService;
import com.faforever.client.player.PlayerService;
import com.faforever.client.preferences.ForgedAlliancePrefs;
Expand Down Expand Up @@ -87,6 +89,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.faforever.client.util.LuaUtil.loadFile;
Expand Down Expand Up @@ -117,6 +120,7 @@ public class MapService implements InitializingBean, DisposableBean {
private final MapGeneratorService mapGeneratorService;
private final PlayerService playerService;
private final MapMapper mapMapper;
private final MatchmakerMapper matchmakerMapper;
private final FileSizeReader fileSizeReader;
private final ClientProperties clientProperties;
private final ForgedAlliancePrefs forgedAlliancePrefs;
Expand Down Expand Up @@ -627,37 +631,18 @@ public Mono<Void> downloadAllMatchmakerMaps(MatchmakerQueueInfo matchmakerQueue)

@Cacheable(value = CacheNames.MATCHMAKER_POOLS, sync = true)
@SuppressWarnings({"rawtypes", "unchecked"})
public Mono<Tuple2<List<MapVersion>, Integer>> getMatchmakerMapsWithPageCount(MatchmakerQueueInfo matchmakerQueue,
int count, int page) {
PlayerInfo player = playerService.getCurrentPlayer();
double rating = Optional.ofNullable(player.getLeaderboardRatings())
.map(ratings -> ratings.get(matchmakerQueue.getLeaderboard().technicalName()))
.map(ratingBean -> ratingBean.mean() - 3 * ratingBean.deviation())
.orElse(0d);
public Mono <java.util.Map<MatchmakerQueueMapPool, List<MapVersion>>> getMatchmakerBrackets(MatchmakerQueueInfo matchmakerQueue) {
ElideNavigatorOnCollection<MapPoolAssignment> navigator = ElideNavigator.of(MapPoolAssignment.class).collection();
List<Condition<?>> conditions = new ArrayList<>();
conditions.add(qBuilder().intNum("mapPool.matchmakerQueueMapPool.matchmakerQueue.id").eq(matchmakerQueue.getId()));
conditions.add(qBuilder().doubleNum("mapPool.matchmakerQueueMapPool.minRating")
.lte(rating)
.or()
.floatNum("mapPool.matchmakerQueueMapPool.minRating")
.ne(null));
// The api doesn't support the ne operation so we manually replace it with isnull which rsql does not support
String customFilter = ((String) new QBuilder().and(conditions).query(new RSQLVisitor())).replace("ex", "isnull");
Flux<MapVersion> matchmakerMapsFlux = fafApiAccessor.getMany(navigator, customFilter)
.map(mapMapper::mapFromPoolAssignment)
.distinct()
.sort(
Comparator.nullsLast(Comparator.comparing(MapVersion::size))
.thenComparing(Comparator.nullsLast(
Comparator.comparing(MapVersion::map,
Comparator.nullsLast(
Comparator.comparing(
Map::displayName,
Comparator.nullsLast(
String.CASE_INSENSITIVE_ORDER)))))));
return Mono.zip(matchmakerMapsFlux.skip((long) (page - 1) * count).take(count).collectList(),
matchmakerMapsFlux.count().map(size -> (int) (size - 1) / count + 1));

String customFilter = ((String) new QBuilder().and(conditions).query(new RSQLVisitor()));

return fafApiAccessor.getMany(navigator, customFilter)
.map(matchmakerMapper::map)
.collect(Collectors.groupingBy(assignment -> assignment.mapPool().mapPool(),
Collectors.mapping(assignment -> assignment.mapVersion(), Collectors.toList())));

}

public Mono<Boolean> hasPlayedMap(PlayerInfo player, MapVersion mapVersion) {
Expand Down
44 changes: 3 additions & 41 deletions src/test/java/com/faforever/client/map/MapServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void setUp() throws Exception {
}).when(taskService).submitTask(any());

instance = new MapService(notificationService, taskService, fafApiAccessor, assetService, i18n,
themeService, mapGeneratorService, playerService, mapMapper, fileSizeReader,
themeService, mapGeneratorService, playerService, mapMapper, matchmakerMapper, fileSizeReader,
clientProperties, forgedAlliancePrefs, preferences, mapUploadTaskFactory,
downloadMapTaskFactory, uninstallMapTaskFactory, fxApplicationThreadExecutor);
instance.officialMaps = Set.of();
Expand Down Expand Up @@ -443,54 +443,16 @@ public void testGetMatchMakerMaps() throws Exception {
Flux<ElideEntity> resultFlux = Flux.fromIterable(
matchmakerMapper.mapAssignmentBeans(List.of(mapPoolAssignment1, mapPoolAssignment2)));
when(fafApiAccessor.getMany(any(), anyString())).thenReturn(resultFlux);
when(playerService.getCurrentPlayer()).thenReturn(PlayerInfoBuilder.create().defaultValues().get());

MatchmakerQueueInfo matchmakerQueue = MatchmakerQueueInfoBuilder.create().defaultValues().get();
StepVerifier.create(instance.getMatchmakerMapsWithPageCount(matchmakerQueue, 10, 1)).assertNext(results -> {
assertThat(results.getT1(), hasSize(2));
assertThat(results.getT2(), is(1));
StepVerifier.create(instance.getMatchmakerBrackets(matchmakerQueue)).assertNext(results -> {
assertThat(results.entrySet(), hasSize(2));
}).verifyComplete();

verify(fafApiAccessor).getMany(
argThat(ElideMatchers.hasDtoClass(com.faforever.commons.api.dto.MapPoolAssignment.class)), anyString());
}

@Test
public void testGetMatchMakerMapsWithPagination() throws Exception {
MapPoolAssignment mapPoolAssignment1 = Instancio.of(MapPoolAssignment.class)
.set(field(MapVersion::size).within(scope(MapVersion.class)),
new MapSize(512, 512))
.set(field(Map::displayName).within(scope(Map.class)),
"a")
.create();
MapPoolAssignment mapPoolAssignment2 = Instancio.of(MapPoolAssignment.class)
.set(field(MapVersion::size).within(scope(MapVersion.class)),
new MapSize(512, 512))
.set(field(Map::displayName).within(scope(Map.class)),
"b")
.create();
MapPoolAssignment mapPoolAssignment3 = Instancio.of(MapPoolAssignment.class)
.set(field(MapVersion::size).within(scope(MapVersion.class)),
new MapSize(1024, 1024))
.set(field(Map::displayName).within(scope(Map.class)),
"c")
.create();

Flux<ElideEntity> resultFlux = Flux.fromIterable(
matchmakerMapper.mapAssignmentBeans(List.of(mapPoolAssignment1, mapPoolAssignment2, mapPoolAssignment3)));
when(fafApiAccessor.getMany(any(), anyString())).thenReturn(resultFlux);
when(playerService.getCurrentPlayer()).thenReturn(PlayerInfoBuilder.create().defaultValues().get());

MatchmakerQueueInfo matchmakerQueue = MatchmakerQueueInfoBuilder.create().defaultValues().get();
StepVerifier.create(instance.getMatchmakerMapsWithPageCount(matchmakerQueue, 1, 2)).assertNext(results -> {
assertThat(results.getT1(), hasSize(1));
assertThat(results.getT1().getFirst().id(), is(mapPoolAssignment2.mapVersion().id()));
assertThat(results.getT2(), is(3));
}).verifyComplete();

verify(fafApiAccessor).getMany(
argThat(ElideMatchers.hasDtoClass(com.faforever.commons.api.dto.MapPoolAssignment.class)), anyString());
}

@Test
public void testHasPlayedMap() throws Exception {
Expand Down

0 comments on commit 7516c06

Please sign in to comment.