Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search flags for /sponsor maps command #48

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<path>
<groupId>cloud.commandframework</groupId>
<artifactId>cloud-annotations</artifactId>
<version>1.7.1</version>
<version>1.8.2</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static tc.oc.pgm.util.Assert.assertNotNull;
import static tc.oc.pgm.util.player.PlayerComponent.player;
import static tc.oc.pgm.util.text.TemporalComponent.duration;

import com.google.common.collect.Sets;
import dev.pgm.community.Community;
import dev.pgm.community.CommunityCommand;
import dev.pgm.community.requests.RequestConfig;
Expand All @@ -20,9 +20,16 @@
import dev.pgm.community.utils.PGMUtils;
import dev.pgm.community.utils.PaginatedComponentResults;
import dev.pgm.community.utils.VisibilityUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
Expand All @@ -33,12 +40,19 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.map.Contributor;
import tc.oc.pgm.api.map.MapInfo;
import tc.oc.pgm.api.map.MapTag;
import tc.oc.pgm.api.map.Phase;
import tc.oc.pgm.lib.cloud.commandframework.annotations.Argument;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandDescription;
import tc.oc.pgm.lib.cloud.commandframework.annotations.CommandMethod;
import tc.oc.pgm.lib.cloud.commandframework.annotations.Flag;
import tc.oc.pgm.lib.cloud.commandframework.annotations.specifier.Greedy;
import tc.oc.pgm.lib.cloud.commandframework.annotations.suggestions.Suggestions;
import tc.oc.pgm.lib.cloud.commandframework.context.CommandContext;
import tc.oc.pgm.util.LiquidMetal;
import tc.oc.pgm.util.StringUtils;
import tc.oc.pgm.util.named.MapNameStyle;
import tc.oc.pgm.util.named.NameStyle;
import tc.oc.pgm.util.text.TextFormatter;
Expand Down Expand Up @@ -216,13 +230,41 @@ public void menu(CommandAudience audience, Player sender) {
@CommandMethod("maps [page]")
@CommandDescription("View a list of maps which can be sponsored")
public void viewMapList(
CommandAudience audience, @Argument(value = "page", defaultValue = "1") int page) {
Set<MapInfo> maps =
Sets.newHashSet(PGM.get().getMapLibrary().getMaps()).stream()
CommandAudience audience,
@Argument(value = "page", defaultValue = "1") int page,
@Flag(value = "tags", aliases = "t", repeatable = true, suggestions = "maptags")
List<String> tags,
@Flag(value = "author", aliases = "a") String author,
@Flag(value = "name", aliases = "n") String name) {
Stream<MapInfo> search =
PGM.get()
.getMapLibrary()
.getMaps(name)
.filter(PGMUtils::isMapSizeAllowed)
.filter(m -> m.getPhase() != Phase.DEVELOPMENT)
.filter(m -> !requests.hasMapCooldown(m))
.collect(Collectors.toSet());
.filter(m -> !requests.hasMapCooldown(m));

if (!tags.isEmpty()) {
final Map<Boolean, Set<String>> tagSet =
tags.stream()
.flatMap(t -> Arrays.stream(t.split(",")))
.map(String::toLowerCase)
.map(String::trim)
.collect(
Collectors.partitioningBy(
s -> s.startsWith("!"),
Collectors.mapping(
(String s) -> s.startsWith("!") ? s.substring(1) : s,
Collectors.toSet())));
search = search.filter(map -> matchesTags(map, tagSet.get(false), tagSet.get(true)));
}

if (author != null) {
String query = StringUtils.normalize(author);
search = search.filter(map -> matchesAuthor(map, query));
}

Set<MapInfo> maps = search.collect(Collectors.toCollection(TreeSet::new));

int resultsPerPage = 8;
int pages = (maps.size() + resultsPerPage - 1) / resultsPerPage;
Expand Down Expand Up @@ -300,6 +342,43 @@ public Component formatEmpty() {
}
}

@Suggestions("maptags")
public List<String> suggestMapTags(CommandContext<CommandSender> sender, String input) {
int commaIdx = input.lastIndexOf(',');

final String prefix = input.substring(0, commaIdx == -1 ? 0 : commaIdx + 1);
final String toComplete =
input.substring(commaIdx + 1).toLowerCase(Locale.ROOT).replace("!", "");

return MapTag.getAllTagIds().stream()
.filter(mt -> LiquidMetal.match(mt, toComplete))
.flatMap(tag -> Stream.of(prefix + tag, prefix + "!" + tag))
.collect(Collectors.toList());
}

private static boolean matchesTags(
MapInfo map, Collection<String> posTags, Collection<String> negTags) {
int matches = 0;
for (MapTag tag : assertNotNull(map).getTags()) {
if (negTags != null && negTags.contains(tag.getId())) {
return false;
}
if (posTags != null && posTags.contains(tag.getId())) {
matches++;
}
}
return posTags == null || matches == posTags.size();
}

private static boolean matchesAuthor(MapInfo map, String query) {
for (Contributor contributor : map.getAuthors()) {
if (StringUtils.normalize(contributor.getNameLegacy()).contains(query)) {
return true;
}
}
return false;
}

@CommandMethod("queue [page]")
@CommandDescription("View the sponsored maps queue")
public void viewQueue(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/pgm/community/utils/PGMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static boolean isMapSizeAllowed(MapInfo map) {

int max = map.getMaxPlayers().stream().reduce(0, Integer::sum);
int lowerBound = participants;
int upperBound = total + (int) (total * 0.35);
int upperBound = Math.max(5, total + (int) (total * 0.35));

return max >= lowerBound && max <= upperBound;
}
Expand Down