|
| 1 | +package vc.commands; |
| 2 | + |
| 3 | +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; |
| 4 | +import discord4j.core.object.entity.Message; |
| 5 | +import discord4j.core.spec.EmbedCreateSpec; |
| 6 | +import discord4j.rest.util.Color; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | +import reactor.core.publisher.Mono; |
| 11 | +import vc.openapi.handler.PlaytimeApi; |
| 12 | +import vc.openapi.model.PlaytimeAllTimeResponse; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@Component |
| 18 | +public class PlaytimeTopCommand implements SlashCommand { |
| 19 | + private static final Logger LOGGER = LoggerFactory.getLogger(PlaytimeTopCommand.class); |
| 20 | + private final PlaytimeApi playtimeApi; |
| 21 | + |
| 22 | + public PlaytimeTopCommand(final PlaytimeApi playtimeApi) { |
| 23 | + this.playtimeApi = playtimeApi; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public String getName() { |
| 28 | + return "playtimetop"; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public Mono<Message> handle(final ChatInputInteractionEvent event) { |
| 33 | + return Mono.defer(() -> { |
| 34 | + PlaytimeAllTimeResponse response = null; |
| 35 | + try { |
| 36 | + response = playtimeApi.playtimeTopAllTime(); |
| 37 | + } catch (final Exception e) { |
| 38 | + LOGGER.error("Failed to get playtime top all time", e); |
| 39 | + } |
| 40 | + if (response == null || response.getPlayers() == null || response.getPlayers().isEmpty()) |
| 41 | + return error(event, "Unable to resolve playtime top data"); |
| 42 | + StringBuilder result = new StringBuilder(); |
| 43 | + for (int i = 0, ptListSize = Math.min(50, response.getPlayers().size()); i < ptListSize; i++) { |
| 44 | + var player = response.getPlayers().get(i); |
| 45 | + var seconds = player.getPlaytimeSeconds(); |
| 46 | + var s = "**" + escape(player.getPlayerName()) + "**: " + formatDuration(seconds); |
| 47 | + if (result.length() + s.length() > 4090) { |
| 48 | + break; |
| 49 | + } |
| 50 | + result.append("*#").append(i + 1).append(":* ").append(s).append("\n"); |
| 51 | + } |
| 52 | + if (!result.isEmpty()) { |
| 53 | + result.deleteCharAt(result.length() - 1); |
| 54 | + } |
| 55 | + return event.createFollowup() |
| 56 | + .withEmbeds(EmbedCreateSpec.builder() |
| 57 | + .title("Top Playtime") |
| 58 | + .color(Color.CYAN) |
| 59 | + .description(result.toString()) |
| 60 | + .build()); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private String formatDuration(long durationInSeconds) { |
| 65 | + var secondsInMinute = 60L; |
| 66 | + var secondsInHour = secondsInMinute * 60L; |
| 67 | + var secondsInDay = secondsInHour * 24L; |
| 68 | + var secondsInMonth = secondsInDay * 30L; // assuming 30 days per month |
| 69 | + var secondsInYear = secondsInMonth * 12L; |
| 70 | + |
| 71 | + var years = durationInSeconds / secondsInYear; |
| 72 | + var months = (durationInSeconds % secondsInYear) / secondsInMonth; |
| 73 | + var days = (durationInSeconds % secondsInMonth) / secondsInDay; |
| 74 | + List<String> entries = new ArrayList<>(3); |
| 75 | + if (years > 0) entries.add(years + " year" + (years != 1 ? "s" : "")); |
| 76 | + if (months > 0) entries.add(months + " month" + (months != 1 ? "s" : "")); |
| 77 | + if (days > 0) entries.add(days + " day" + (days != 1 ? "s" : "")); |
| 78 | + return String.join(", ", entries); |
| 79 | + } |
| 80 | +} |
0 commit comments