|
| 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 | +} |
0 commit comments