Skip to content

Commit

Permalink
playtime database
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Oct 28, 2024
1 parent 34f3792 commit 00053b6
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 209 deletions.
137 changes: 74 additions & 63 deletions src/main/java/com/zenith/command/impl/DatabaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public CommandUsage commandUsage() {
"deathMessages on/off",
"restarts on/off",
"playerCount on/off",
"tablist on/off"
"tablist on/off",
"playtime on/off"
),
asList("db")
);
Expand All @@ -53,89 +54,99 @@ public LiteralArgumentBuilder<CommandContext> register() {
}))
.then(literal("queueWait")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.queueWait.enabled = getToggle(c, "toggle");
if (CONFIG.database.queueWait.enabled) DATABASE.startQueueWaitDatabase();
else DATABASE.stopQueueWaitDatabase();
c.getSource().getEmbed()
.title("Queue Wait Database " + toggleStrCaps(CONFIG.database.queueWait.enabled));
return OK;
CONFIG.database.queueWaitEnabled = getToggle(c, "toggle");
if (CONFIG.database.queueWaitEnabled) DATABASE.startQueueWaitDatabase();
else DATABASE.stopQueueWaitDatabase();
c.getSource().getEmbed()
.title("Queue Wait Database " + toggleStrCaps(CONFIG.database.queueWaitEnabled));
return OK;
})))
.then(literal("queueLength")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.queueLength.enabled = getToggle(c, "toggle");
if (CONFIG.database.queueLength.enabled) DATABASE.startQueueLengthDatabase();
else DATABASE.stopQueueLengthDatabase();
c.getSource().getEmbed()
.title("Queue Length Database " + toggleStrCaps(CONFIG.database.queueLength.enabled));
return OK;
CONFIG.database.queueLengthEnabled = getToggle(c, "toggle");
if (CONFIG.database.queueLengthEnabled) DATABASE.startQueueLengthDatabase();
else DATABASE.stopQueueLengthDatabase();
c.getSource().getEmbed()
.title("Queue Length Database " + toggleStrCaps(CONFIG.database.queueLengthEnabled));
return OK;
})))
.then(literal("publicChat")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.chats.enabled = getToggle(c, "toggle");
if (CONFIG.database.chats.enabled) DATABASE.startChatsDatabase();
else DATABASE.stopChatsDatabase();
c.getSource().getEmbed()
.title("Public Chat Database " + toggleStrCaps(CONFIG.database.chats.enabled));
return OK;
CONFIG.database.chatsEnabled = getToggle(c, "toggle");
if (CONFIG.database.chatsEnabled) DATABASE.startChatsDatabase();
else DATABASE.stopChatsDatabase();
c.getSource().getEmbed()
.title("Public Chat Database " + toggleStrCaps(CONFIG.database.chatsEnabled));
return OK;
})))
.then(literal("joinLeave")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.connections.enabled = getToggle(c, "toggle");
if (CONFIG.database.connections.enabled) DATABASE.startConnectionsDatabase();
else DATABASE.stopConnectionsDatabase();
c.getSource().getEmbed()
.title("Connections Database " + toggleStrCaps(CONFIG.database.connections.enabled));
return OK;
CONFIG.database.connectionsEnabled = getToggle(c, "toggle");
if (CONFIG.database.connectionsEnabled) DATABASE.startConnectionsDatabase();
else DATABASE.stopConnectionsDatabase();
c.getSource().getEmbed()
.title("Connections Database " + toggleStrCaps(CONFIG.database.connectionsEnabled));
return OK;
})))
.then(literal("deathMessages")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.deaths.enabled = getToggle(c, "toggle");
if (CONFIG.database.deaths.enabled) DATABASE.startDeathsDatabase();
else DATABASE.stopDeathsDatabase();
c.getSource().getEmbed()
.title("Death Messages Database " + toggleStrCaps(CONFIG.database.deaths.enabled));
return OK;
CONFIG.database.deathsEnabled = getToggle(c, "toggle");
if (CONFIG.database.deathsEnabled) DATABASE.startDeathsDatabase();
else DATABASE.stopDeathsDatabase();
c.getSource().getEmbed()
.title("Death Messages Database " + toggleStrCaps(CONFIG.database.deathsEnabled));
return OK;
})))
.then(literal("restarts")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.restarts.enabled = getToggle(c, "toggle");
if (CONFIG.database.restarts.enabled) DATABASE.startRestartsDatabase();
else DATABASE.stopRestartsDatabase();
c.getSource().getEmbed()
.title("Restarts Database " + toggleStrCaps(CONFIG.database.restarts.enabled));
return OK;
})))
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.restartsEnabled = getToggle(c, "toggle");
if (CONFIG.database.restartsEnabled) DATABASE.startRestartsDatabase();
else DATABASE.stopRestartsDatabase();
c.getSource().getEmbed()
.title("Restarts Database " + toggleStrCaps(CONFIG.database.restartsEnabled));
return OK;
})))
.then(literal("playerCount")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.playerCount.enabled = getToggle(c, "toggle");
if (CONFIG.database.playerCount.enabled) DATABASE.startPlayerCountDatabase();
else DATABASE.stopPlayerCountDatabase();
c.getSource().getEmbed()
.title("Player Count Database " + toggleStrCaps(CONFIG.database.playerCount.enabled));
return OK;
})))
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.playerCountEnabled = getToggle(c, "toggle");
if (CONFIG.database.playerCountEnabled) DATABASE.startPlayerCountDatabase();
else DATABASE.stopPlayerCountDatabase();
c.getSource().getEmbed()
.title("Player Count Database " + toggleStrCaps(CONFIG.database.playerCountEnabled));
return OK;
})))
.then(literal("tablist")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.tablist.enabled = getToggle(c, "toggle");
if (CONFIG.database.tablist.enabled) DATABASE.startTablistDatabase();
else DATABASE.stopTablistDatabase();
c.getSource().getEmbed()
.title("Tablist Database " + toggleStrCaps(CONFIG.database.tablist.enabled));
return OK;
})));
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.tablistEnabled = getToggle(c, "toggle");
if (CONFIG.database.tablistEnabled) DATABASE.startTablistDatabase();
else DATABASE.stopTablistDatabase();
c.getSource().getEmbed()
.title("Tablist Database " + toggleStrCaps(CONFIG.database.tablistEnabled));
return OK;
})))
.then(literal("playtime")
.then(argument("toggle", toggle()).executes(c -> {
CONFIG.database.playtimeEnabled = getToggle(c, "toggle");
if (CONFIG.database.playtimeEnabled) DATABASE.startPlaytimeDatabase();
else DATABASE.stopPlaytimeDatabase();
c.getSource().getEmbed()
.title("Playtime Database " + toggleStrCaps(CONFIG.database.playtimeEnabled));
return OK;
})));
}

@Override
public void postPopulate(final Embed builder) {
builder
.addField("Queue Wait", toggleStr(CONFIG.database.queueWait.enabled), false)
.addField("Queue Length", toggleStr(CONFIG.database.queueLength.enabled), false)
.addField("Public Chat", toggleStr(CONFIG.database.chats.enabled), false)
.addField("Join/Leave", toggleStr(CONFIG.database.connections.enabled), false)
.addField("Death Messages", toggleStr(CONFIG.database.deaths.enabled), false)
.addField("Restarts", toggleStr(CONFIG.database.restarts.enabled), false)
.addField("Player Count", toggleStr(CONFIG.database.playerCount.enabled), false)
.addField("Tablist", toggleStr(CONFIG.database.tablist.enabled), false)
.addField("Queue Wait", toggleStr(CONFIG.database.queueWaitEnabled), false)
.addField("Queue Length", toggleStr(CONFIG.database.queueLengthEnabled), false)
.addField("Public Chat", toggleStr(CONFIG.database.chatsEnabled), false)
.addField("Join/Leave", toggleStr(CONFIG.database.connectionsEnabled), false)
.addField("Death Messages", toggleStr(CONFIG.database.deathsEnabled), false)
.addField("Restarts", toggleStr(CONFIG.database.restartsEnabled), false)
.addField("Player Count", toggleStr(CONFIG.database.playerCountEnabled), false)
.addField("Tablist", toggleStr(CONFIG.database.tablistEnabled), false)
.addField("Playtime", toggleStr(CONFIG.database.playtimeEnabled), false)
.primaryColor();
}
}
42 changes: 31 additions & 11 deletions src/main/java/com/zenith/database/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DatabaseManager {
private RestartsDatabase restartsDatabase;
private PlayerCountDatabase playerCountDatabase;
private TablistDatabase tablistDatabase;
private PlaytimeDatabase playtimeDatabase;
private QueryExecutor queryExecutor;
private RedisClient redisClient;
private ScheduledFuture<?> databaseTickFuture;
Expand All @@ -36,38 +37,41 @@ public DatabaseManager() {
public void start() {
try {
this.queryExecutor = new QueryExecutor(getJdbi());
if (CONFIG.database.queueWait.enabled) {
if (CONFIG.database.queueWaitEnabled) {
startQueueWaitDatabase();
}
if (CONFIG.database.connections.enabled) {
if (CONFIG.database.connectionsEnabled) {
startConnectionsDatabase();
}
if (CONFIG.database.chats.enabled) {
if (CONFIG.database.chatsEnabled) {
startChatsDatabase();
}
if (CONFIG.database.deaths.enabled) {
if (CONFIG.database.deathsEnabled) {
startDeathsDatabase();
}
if (CONFIG.database.queueLength.enabled) {
if (CONFIG.database.queueLengthEnabled) {
startQueueLengthDatabase();
}
if (CONFIG.database.restarts.enabled) {
if (CONFIG.database.restartsEnabled) {
startRestartsDatabase();
}
if (CONFIG.database.playerCount.enabled) {
if (CONFIG.database.playerCountEnabled) {
startPlayerCountDatabase();
}
if (CONFIG.database.tablist.enabled) {
if (CONFIG.database.tablistEnabled) {
startTablistDatabase();
}
if (CONFIG.database.playtimeEnabled) {
startPlaytimeDatabase();
}
if (databaseTickFuture != null) {
databaseTickFuture.cancel(false);
}
databaseTickFuture = EXECUTOR
.scheduleAtFixedRate(this::postDatabaseTick,
1L,
5L,
TimeUnit.MINUTES);
DatabaseTickEvent.TICK_INTERVAL_SECONDS,
DatabaseTickEvent.TICK_INTERVAL_SECONDS,
TimeUnit.SECONDS);
} catch (final Exception e) {
DATABASE_LOG.error("Failed starting databases", e);
}
Expand All @@ -84,6 +88,7 @@ public void stop() {
stopRestartsDatabase();
stopPlayerCountDatabase();
stopTablistDatabase();
stopPlaytimeDatabase();
} catch (final Exception e) {
DATABASE_LOG.error("Failed stopping databases", e);
}
Expand Down Expand Up @@ -220,6 +225,21 @@ public void stopTablistDatabase() {
}
}

public void startPlaytimeDatabase() {
if (nonNull(this.playtimeDatabase)) {
this.playtimeDatabase.start();
} else {
this.playtimeDatabase = new PlaytimeDatabase(queryExecutor, getRedisClient());
this.playtimeDatabase.start();
}
}

public void stopPlaytimeDatabase() {
if (nonNull(this.playtimeDatabase)) {
this.playtimeDatabase.stop();
}
}

private synchronized Jdbi getJdbi() {
if (isNull(this.jdbi)) {
this.jdbi = Jdbi.create(new HikariConnectionFactory(new ConnectionPool()));
Expand Down
Loading

0 comments on commit 00053b6

Please sign in to comment.