Skip to content

Commit 407cc45

Browse files
committed
Add playtime command and show playtime on profiles
1 parent dbe2fbb commit 407cc45

3 files changed

Lines changed: 123 additions & 1 deletion

File tree

‎src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static void initialize() throws LoginException {
116116
new JoinDataCommand(),
117117
new TotalStatsCommand(),
118118
new TokenTopCommand(),
119+
new PlaytimeCommand(),
119120
new HistoryCommand(),
120121
new UuidCommand(),
121122
new PlotLocCommand(),
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.diamondfire.helpbot.bot.command.impl.stats.individualized;
2+
3+
import com.diamondfire.helpbot.bot.command.argument.impl.types.minecraft.Player;
4+
import com.diamondfire.helpbot.bot.command.help.*;
5+
import com.diamondfire.helpbot.bot.command.impl.stats.AbstractPlayerUUIDCommand;
6+
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
7+
import com.diamondfire.helpbot.bot.command.reply.feature.MinecraftUserPreset;
8+
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
9+
import com.diamondfire.helpbot.bot.events.CommandEvent;
10+
import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery;
11+
import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery;
12+
import com.diamondfire.helpbot.util.*;
13+
14+
import java.sql.ResultSet;
15+
import java.util.*;
16+
17+
public class PlaytimeCommand extends AbstractPlayerUUIDCommand {
18+
19+
// How many leaderboard entries to show above and below the player.
20+
private static final int SURROUNDING = 3;
21+
22+
@Override
23+
public String getName() {
24+
return "playtime";
25+
}
26+
27+
@Override
28+
public String[] getAliases() {
29+
return new String[]{"pt"};
30+
}
31+
32+
@Override
33+
public HelpContext getHelpContext() {
34+
return new HelpContext()
35+
.description("Gets a player's network-wide playtime and their position on the playtime leaderboard.")
36+
.category(CommandCategory.PLAYER_STATS)
37+
.addArgument(
38+
new HelpContextArgument()
39+
.name("player|uuid")
40+
.optional()
41+
);
42+
}
43+
44+
@Override
45+
protected void execute(CommandEvent event, Player player) {
46+
String uuid = player.uuidString();
47+
48+
// Leaderboard order is playtime descending, ties broken by uuid, so ranks are stable.
49+
long[] playtimeAndRank = new long[2];
50+
new DatabaseQuery()
51+
.query(new BasicQuery("SELECT me.play_time_ms," +
52+
" (SELECT COUNT(*) FROM player_playtime other" +
53+
" WHERE other.play_time_ms > me.play_time_ms" +
54+
" OR (other.play_time_ms = me.play_time_ms AND other.uuid < me.uuid)) + 1 AS position " +
55+
"FROM player_playtime me WHERE me.uuid = ?", (statement) -> statement.setString(1, uuid)))
56+
.compile()
57+
.run((result) -> {
58+
if (result.isEmpty()) {
59+
return;
60+
}
61+
62+
ResultSet set = result.getResult();
63+
playtimeAndRank[0] = set.getLong("play_time_ms");
64+
playtimeAndRank[1] = set.getLong("position");
65+
});
66+
67+
long playtime = playtimeAndRank[0];
68+
long position = playtimeAndRank[1];
69+
if (position == 0 || playtime == 0) {
70+
event.reply(new PresetBuilder()
71+
.withPreset(
72+
new InformativeReply(InformativeReplyType.ERROR, "This player has no recorded playtime!")
73+
));
74+
return;
75+
}
76+
77+
long startPosition = Math.max(1, position - SURROUNDING);
78+
List<String> lines = new ArrayList<>();
79+
new DatabaseQuery()
80+
.query(new BasicQuery("SELECT player_playtime.uuid, players.name, player_playtime.play_time_ms " +
81+
"FROM player_playtime LEFT JOIN players ON players.uuid = player_playtime.uuid " +
82+
"ORDER BY player_playtime.play_time_ms DESC, player_playtime.uuid " +
83+
"LIMIT ? OFFSET ?", (statement) -> {
84+
statement.setInt(1, SURROUNDING * 2 + 1);
85+
statement.setLong(2, startPosition - 1);
86+
}))
87+
.compile()
88+
.run((result) -> {
89+
long rank = startPosition;
90+
for (ResultSet set : result) {
91+
String rowUuid = set.getString("uuid");
92+
String name = set.getString("name");
93+
String line = String.format("`#%s` %s - %s",
94+
FormatUtil.formatNumber(rank),
95+
StringUtil.display(name == null ? rowUuid : name),
96+
FormatUtil.formatMilliTime(set.getLong("play_time_ms")));
97+
98+
lines.add(rowUuid.equals(uuid) ? "**" + line + "**" : line);
99+
rank++;
100+
}
101+
});
102+
103+
PresetBuilder preset = new PresetBuilder()
104+
.withPreset(
105+
new InformativeReply(InformativeReplyType.INFO, "Playtime", null),
106+
new MinecraftUserPreset(player)
107+
);
108+
preset.getEmbed()
109+
.addField("Playtime", FormatUtil.formatMilliTime(playtime), true)
110+
.addField("Position", "#" + FormatUtil.formatNumber(position), true)
111+
.addField("Leaderboard", String.join("\n", lines), false);
112+
113+
event.reply(preset);
114+
}
115+
116+
}

‎src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/individualized/ProfileCommand.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,23 @@ protected void execute(CommandEvent event, Player player) {
103103
" (SELECT (SELECT date AS owen_join FROM owen.join_log WHERE uuid = ? ORDER BY date DESC LIMIT 1) temp FROM dual) AS owen_join," +
104104
" (SELECT (SELECT COUNT(*) AS votes FROM plot_votes WHERE uuid = ?) temp FROM dual) AS votes," +
105105
" (SELECT (SELECT tokens FROM user_tokens WHERE uuid = ?) temp FROM dual) AS credits," +
106-
" (SELECT (SELECT discord_id FROM linked_accounts WHERE player_uuid = ?) temp FROM dual) AS id", (statement) -> {
106+
" (SELECT (SELECT discord_id FROM linked_accounts WHERE player_uuid = ?) temp FROM dual) AS id," +
107+
" (SELECT (SELECT play_time_ms FROM player_playtime WHERE uuid = ?) temp FROM dual) AS playtime", (statement) -> {
107108
statement.setString(1, playerUUID);
108109
statement.setString(2, playerUUID);
109110
statement.setString(3, playerUUID);
110111
statement.setString(4, playerUUID);
112+
statement.setString(5, playerUUID);
111113
}))
112114
.compile()
113115
.run((stats) -> {
114116
ResultSet statsSet = stats.getResult();
115117

116118
embed.addField("Votes Given", FormatUtil.formatNumber(statsSet.getInt("votes.temp")), false);
117119
embed.addField("Tokens", FormatUtil.formatNumber(statsSet.getInt("credits.temp")), false);
120+
121+
long playtime = statsSet.getLong("playtime.temp");
122+
embed.addField("Playtime", playtime > 0 ? FormatUtil.formatMilliTime(playtime) : "N/A", false);
118123

119124
long discordId = statsSet.getLong("id.temp");
120125
if (discordId != 0L) {

0 commit comments

Comments
 (0)