+ Added first-pass leaderboard command and all requirements
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
package commands;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import core.Command;
|
||||
import core.Config;
|
||||
import core.DatabaseManager;
|
||||
import core.LocStrings;
|
||||
import dataStructures.KittyChannel;
|
||||
@@ -29,7 +28,7 @@ public class CommandDBFlush extends Command
|
||||
KittyEmbed embed = new KittyEmbed();
|
||||
embed.title = "Database queue flushed";
|
||||
embed.descriptionText = "**Dirty:** " + numUpdated;
|
||||
embed.color = new Color(7*16, 8*16, 9*16);
|
||||
embed.color = Config.ColorDefault;
|
||||
|
||||
res.CallEmbed(embed);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package commands;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import core.Command;
|
||||
import core.Config;
|
||||
import core.DatabaseManager;
|
||||
import core.LocStrings;
|
||||
import dataStructures.KittyChannel;
|
||||
@@ -36,7 +36,7 @@ public class CommandDBStats extends Command
|
||||
embed.descriptionText = "**Tracked Items:** " + DatabaseManager.instance.getTrackedObjectsSize();
|
||||
embed.descriptionText += "\n";
|
||||
embed.descriptionText += "**Last Upkeep:** " + dateFormat.format(DatabaseManager.instance.getLastUpkeep()) + " UTC-7";
|
||||
embed.color = new Color(7*16, 8*16, 9*16);
|
||||
embed.color = Config.ColorDefault;
|
||||
|
||||
res.CallEmbed(embed);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import core.Command;
|
||||
import core.Config;
|
||||
import core.DatabaseManager;
|
||||
import core.LocStrings;
|
||||
import core.ObjectBuilderFactory;
|
||||
import dataStructures.KittyChannel;
|
||||
import dataStructures.KittyEmbed;
|
||||
import dataStructures.KittyGuild;
|
||||
import dataStructures.KittyRating;
|
||||
import dataStructures.KittyRole;
|
||||
import dataStructures.KittyUser;
|
||||
import dataStructures.Pair;
|
||||
import dataStructures.Response;
|
||||
import dataStructures.UserInput;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
public class CommandLeaderboard extends Command
|
||||
{
|
||||
public CommandLeaderboard(KittyRole roleLevel, KittyRating contentRating) { super(roleLevel, contentRating); }
|
||||
|
||||
@Override
|
||||
public String HelpText() { return LocStrings.Stub("LeaderboardInfo"); }
|
||||
|
||||
// Called when the command is run!
|
||||
@Override
|
||||
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
||||
{
|
||||
// Start by force-flushing. We need to be up-to-date.
|
||||
GlobalLog.Log("Flushed database for " + DatabaseManager.instance.upkeep() + " items.");
|
||||
|
||||
// Get all users associated with a guild and get their bean count
|
||||
String guildID = guild.uniqueID;
|
||||
List<Pair<Long, String>> users = new ArrayList<Pair<Long, String>>();
|
||||
List<String> out = DatabaseManager.instance.scrapeGlobalForString(guildID);
|
||||
|
||||
for(String item : out)
|
||||
{
|
||||
String val = item.replace("" + guildID, "");
|
||||
if(!val.contains("-") && val.length() > 0)
|
||||
{
|
||||
String userID = val;
|
||||
|
||||
if(userID.length() < 1)
|
||||
continue;
|
||||
|
||||
// Look up in the database the user beans
|
||||
String dbUserData = DatabaseManager.instance.globalGetRemoteValue(guildID + userID);
|
||||
|
||||
// Leverage the fact that the user is automatically read and parsed when constructed.
|
||||
Long parsedBeans = KittyUser.parseBeans(KittyUser.prepareFromString(dbUserData));
|
||||
|
||||
// If we were only using cached users, we would use this. However, we're not.
|
||||
// KittyUser cachedUser = ObjectBuilderFactory.getCachedUser(guildID, userID);
|
||||
users.add(new Pair<Long, String>(parsedBeans, userID));
|
||||
}
|
||||
}
|
||||
|
||||
// Sort users by beans
|
||||
users.sort((pair1, pair2) -> {
|
||||
long difference = pair2.First - pair1.First;
|
||||
|
||||
if(difference < Integer.MIN_VALUE)
|
||||
return Integer.MIN_VALUE;
|
||||
|
||||
if(difference > Integer.MAX_VALUE)
|
||||
return Integer.MAX_VALUE;
|
||||
|
||||
return (int)difference;
|
||||
});
|
||||
|
||||
GlobalLog.Log(LogFilter.Command, "Sorted through " + users.size() + " KittyUsers for leaderboard purposes.");
|
||||
|
||||
// Configure output
|
||||
final int listSize = 10;
|
||||
KittyEmbed embed = new KittyEmbed();
|
||||
embed.color = Config.ColorDefault;
|
||||
embed.title = LocStrings.Stub("LeaderboardTitle");
|
||||
embed.descriptionText = "";
|
||||
|
||||
for(int i = 0; i < listSize && i < users.size(); ++i)
|
||||
{
|
||||
// Only now that we've sorted do we do the full construction and caching of users
|
||||
Pair<Long, String> userPair = users.get(i);
|
||||
KittyUser cachedUser = ObjectBuilderFactory.getKittyUser(guildID, userPair.Second);
|
||||
|
||||
embed.descriptionText += "**" + (i + 1) + ":** " + userPair.First + " - " + cachedUser.name;
|
||||
embed.descriptionText += "\n";
|
||||
}
|
||||
|
||||
// Write out embed result
|
||||
res.CallEmbed(embed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user