Skip to content

Commit

Permalink
Top 100 Options. (#86)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Seizoxu authored Nov 11, 2023
1 parent d99a507 commit 2fa2f63
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.seizoxu.zyenyo</groupId>
<artifactId>zyenyo</artifactId>
<version>0.3.01-beta</version>
<version>0.3.02-beta</version>
<name>ZyenyoBot</name>

<properties>
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/asynchronous/typing/TypeTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Document> plays = Database.getTopPlays(userId);
AggregateIterable<Document> plays = Database.getTopPlays(userId, sortBy, isDescending);

EmbedBuilder embed = new EmbedBuilder()
.setTitle("TypeTop for " + messageAuthorName);
Expand Down Expand Up @@ -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
));

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/dataStructures/InfoCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/zyenyo/Database.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -293,14 +294,21 @@ private static double getWeightedTp(long id)
return weightedTp;
}


public static AggregateIterable<Document> 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<Document>
*/
public static AggregateIterable<Document> getTopPlays(String discordId, String sort, Boolean isDescending)
{
AggregateIterable<Document> 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;
Expand Down

0 comments on commit 2fa2f63

Please sign in to comment.