From 2fa2f630b0fe22c2f94c4225d1c3d74c36025ef4 Mon Sep 17 00:00:00 2001
From: Seizoxu <66660680+Seizoxu@users.noreply.github.com>
Date: Fri, 10 Nov 2023 20:46:50 -0600
Subject: [PATCH] Top 100 Options. (#86)
* Added ability to sort tops based on WPM, Acc, TP, and date, along with a reverse option.
* Appended entry in InfoCard
* Incremented version.
* Fixed footer.
---
pom.xml | 2 +-
.../java/asynchronous/typing/TypeTop.java | 27 +++++++++++++++----
src/main/java/dataStructures/InfoCard.java | 14 +++++-----
src/main/java/zyenyo/Database.java | 16 ++++++++---
4 files changed, 41 insertions(+), 18 deletions(-)
diff --git a/pom.xml b/pom.xml
index 57dc836..e466dfe 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
4.0.0
com.seizoxu.zyenyo
zyenyo
- 0.3.01-beta
+ 0.3.02-beta
ZyenyoBot
diff --git a/src/main/java/asynchronous/typing/TypeTop.java b/src/main/java/asynchronous/typing/TypeTop.java
index cb2ec73..b089996 100644
--- a/src/main/java/asynchronous/typing/TypeTop.java
+++ b/src/main/java/asynchronous/typing/TypeTop.java
@@ -36,10 +36,13 @@ public TypeTop(MessageReceivedEvent event, String[] args)
@Override
public void run()
{
- // One page = 5 plays.
- int playsPage = 1;
String userId = event.getAuthor().getId();
String messageAuthorName = event.getAuthor().getName();
+
+ // One page = 5 plays.
+ int playsPage = 1;
+ String sortBy = "tp";
+ boolean isDescending = true;
for (String cmd : args)
{
@@ -56,11 +59,25 @@ public void run()
case "-p":
playsPage = getPageArg(args, cmd);
break;
- //TODO: More sort options in the future.
+ case "-tp":
+ sortBy = "tp";
+ break;
+ case "-wpm":
+ sortBy = "wpm";
+ break;
+ case "-accuracy": case "-acc":
+ sortBy = "accuracy";
+ break;
+ case "-date":
+ sortBy = "date";
+ break;
+ case "-reverse": case "-rev":
+ isDescending = false;
+ break;
}
}
- AggregateIterable plays = Database.getTopPlays(userId);
+ AggregateIterable plays = Database.getTopPlays(userId, sortBy, isDescending);
EmbedBuilder embed = new EmbedBuilder()
.setTitle("TypeTop for " + messageAuthorName);
@@ -94,7 +111,7 @@ public void run()
embed.setFooter(
String.format("Showing test %d to %d on page %d.",
(playsPage-1) * 5 + index - 4,
- (playsPage-1) * 5 + index + 1,
+ (playsPage-1) * 5 + index,
playsPage
));
diff --git a/src/main/java/dataStructures/InfoCard.java b/src/main/java/dataStructures/InfoCard.java
index b558168..c40f873 100644
--- a/src/main/java/dataStructures/InfoCard.java
+++ b/src/main/java/dataStructures/InfoCard.java
@@ -172,14 +172,12 @@ public static EmbedBuilder commandNotFound(String command)
.addField("Aliases","`ttop`", false)
.addField("Syntax", "`\\typetop [Options]`", false)
.addField("Options",
-// "`-tp :` Sorts by TP.\n"
-// + "`-wpm :` Sorts by WPM.\n"
-// + "`-acc :` Sorts by accuracy.\n"
-// + "`-best :` Sorts by global (overall) stats.\n"
-// + "`-avg :` Sorts by average (recent 10 tests) stats.\n"
-// + "`-sum :` Sorts by cumulative TP.\n"
-// + "`-tests:` Sorts by number of tests.\n"
- "`-p :` Specifies a page number (Eg: `-p 2`).\n", false);
+ "`-tp :` Sorts by TP.\n"
+ + "`-wpm :` Sorts by WPM.\n"
+ + "`-acc :` Sorts by accuracy.\n"
+ + "`-date :` Sorts by date.\n"
+ + "`-rev :` Reverses the sort order to ascending.\n"
+ + "`-p :` Specifies a page number (Eg: `-p 2`).\n", false);
public static final EmbedBuilder CHART =
new EmbedBuilder()
diff --git a/src/main/java/zyenyo/Database.java b/src/main/java/zyenyo/Database.java
index 00d5a0f..1db5921 100644
--- a/src/main/java/zyenyo/Database.java
+++ b/src/main/java/zyenyo/Database.java
@@ -1,6 +1,7 @@
package zyenyo;
import static com.mongodb.client.model.Sorts.descending;
+import static com.mongodb.client.model.Sorts.ascending;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -293,14 +294,21 @@ private static double getWeightedTp(long id)
return weightedTp;
}
-
- public static AggregateIterable getTopPlays(String discordId)
+ /**
+ * Filters through the testsV2 collection, and initially orders tests by TP,
+ * the top 100 of which are sorted by a specified parameter.
+ * @param discordId
+ * @param sort
+ * @param isDescending
+ * @return AggregateIterable
+ */
+ public static AggregateIterable getTopPlays(String discordId, String sort, Boolean isDescending)
{
AggregateIterable playsList = testsV2.aggregate(Arrays.asList(
Aggregates.match(Filters.eq("discordId", discordId)),
Aggregates.match(Filters.exists("tp")),
- Aggregates.sort(descending("tp")),
- Aggregates.limit(100)
+ Aggregates.limit(100), // Limit before sort, since we want to sort the top 100, not everything.
+ (isDescending) ? Aggregates.sort(descending(sort)) : Aggregates.sort(ascending(sort))
));
return playsList;