Skip to content

Commit

Permalink
fix event timers
Browse files Browse the repository at this point in the history
  • Loading branch information
kr45732 committed Jun 30, 2024
1 parent 10da470 commit 434bf3e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 86 deletions.
159 changes: 74 additions & 85 deletions src/main/java/com/skyblockplus/features/event/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,49 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.entities.MessageEmbed;

public class EventHandler {

public static String messageId;
public static final List<Long> eventTimers = new ArrayList<>();

public static void initialize() {
if (eventTimers.size() != 24) {
setEventTimers(Collections.nCopies(24, Instant.now().toEpochMilli()));
}

if (IS_DEV) {
return;
}

scheduler.scheduleAtFixedRate(EventHandler::updateEvents, 30, 30, TimeUnit.SECONDS);
messageId = !IS_DEV ? "960675388441387058" : "960687190990520400";
}

public static void setEventTimers(List<Long> eventTimers) {
EventHandler.eventTimers.clear();
EventHandler.eventTimers.addAll(eventTimers);
}

public static void updateEvents() {
try {
if (IS_DEV) {
return;
}

jda
.getGuildById(PRIMARY_GUILD_ID)
.getTextChannelById("959829695686381658")
.retrieveMessageById(messageId)
.queue(m -> {
String[] times = m.getContentRaw().split("\n");
ZonedDateTime nowDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
Map<String, MessageEmbed> ebs = new HashMap<>();
getEventEmbeds(nowDateTime, 0);
ebs.putAll(getEventEmbeds(nowDateTime, 5));

ZonedDateTime nowDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
Map<String, MessageEmbed> ebs = new HashMap<>();
getEventEmbeds(times, nowDateTime, 0);
ebs.putAll(getEventEmbeds(times, nowDateTime, 5));

if (!ebs.isEmpty()) {
for (AutomaticGuild guild : guildMap.values()) {
guild.onEventNotif(ebs);
}

m.editMessage(String.join("\n", times)).queue();
}
});
if (!ebs.isEmpty()) {
for (AutomaticGuild guild : guildMap.values()) {
guild.onEventNotif(ebs);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDateTime nowDateTime, int timeBefore) {
public static Map<String, MessageEmbed> getEventEmbeds(ZonedDateTime nowDateTime, int timeBefore) {
// 0 - bingo start
// 1 - bingo end
// 2 - zoo early summer
Expand All @@ -92,7 +89,7 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
// 11 - bank interest

nowDateTime = nowDateTime.plusMinutes(timeBefore);
int index = timeBefore == 0 ? 0 : times.length / 2;
int index = timeBefore == 0 ? 0 : eventTimers.size() / 2;
Map<String, MessageEmbed> ebs = new HashMap<>();

long nowEpoch = nowDateTime.toInstant().toEpochMilli();
Expand All @@ -103,15 +100,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
startOfBingo.toEpochMilli() <= nowEpoch &&
nowEpoch <= startOfBingo.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < startOfBingo.toEpochMilli()
eventTimers.get(index) < startOfBingo.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"bingo_start",
defaultEmbed("Bingo Start").setDescription("Bingo starts " + getRelativeTimestamp(startOfBingo)).build()
);
} else if (nowEpoch > startOfBingo.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < startOfBingo.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > startOfBingo.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < startOfBingo.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"bingo_start",
defaultEmbed("Bingo Start").setDescription("Bingo starts " + getRelativeTimestamp(startOfBingo)).build()
Expand All @@ -123,12 +120,12 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
endOfBingo.toEpochMilli() <= nowEpoch &&
nowEpoch <= endOfBingo.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < endOfBingo.toEpochMilli()
eventTimers.get(index) < endOfBingo.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put("bingo_end", defaultEmbed("Bingo End").setDescription("Bingo ends " + getRelativeTimestamp(endOfBingo)).build());
} else if (nowEpoch > endOfBingo.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < endOfBingo.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > endOfBingo.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < endOfBingo.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put("bingo_end", defaultEmbed("Bingo End").setDescription("Bingo ends " + getRelativeTimestamp(endOfBingo)).build());
}

Expand All @@ -148,9 +145,9 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
zooEarlySummer.toEpochMilli() <= nowEpoch &&
nowEpoch <= zooEarlySummer.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < zooEarlySummer.toEpochMilli()
eventTimers.get(index) < zooEarlySummer.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"zoo",
defaultEmbed("Traveling Zoo")
Expand All @@ -164,10 +161,8 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
)
.build()
);
} else if (
nowEpoch > zooEarlySummer.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < zooEarlySummer.toEpochMilli()
) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > zooEarlySummer.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < zooEarlySummer.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"zoo",
defaultEmbed("Traveling Zoo")
Expand All @@ -192,9 +187,9 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
zooEarlyWinter.toEpochMilli() <= nowEpoch &&
nowEpoch <= zooEarlyWinter.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < zooEarlyWinter.toEpochMilli()
eventTimers.get(index) < zooEarlyWinter.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"zoo",
defaultEmbed("Traveling Zoo")
Expand All @@ -208,10 +203,8 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
)
.build()
);
} else if (
nowEpoch > zooEarlyWinter.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < zooEarlyWinter.toEpochMilli()
) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > zooEarlyWinter.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < zooEarlyWinter.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"zoo",
defaultEmbed("Traveling Zoo")
Expand All @@ -236,17 +229,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
jerryIslandOpen.toEpochMilli() <= nowEpoch &&
nowEpoch <= jerryIslandOpen.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < jerryIslandOpen.toEpochMilli()
eventTimers.get(index) < jerryIslandOpen.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"winter_island",
defaultEmbed("Winter Island").setDescription("Winter island opens " + getRelativeTimestamp(jerryIslandOpen)).build()
);
} else if (
nowEpoch > jerryIslandOpen.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < jerryIslandOpen.toEpochMilli()
) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > jerryIslandOpen.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < jerryIslandOpen.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"winter_island",
defaultEmbed("Winter Island").setDescription("Winter island opens " + getRelativeTimestamp(jerryIslandOpen)).build()
Expand All @@ -258,17 +249,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
darkAuctionOpen.toEpochMilli() <= nowEpoch &&
nowEpoch <= darkAuctionOpen.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < darkAuctionOpen.toEpochMilli()
eventTimers.get(index) < darkAuctionOpen.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"dark_auction",
defaultEmbed("Dark Auction").setDescription("Dark auction opens " + getRelativeTimestamp(darkAuctionOpen)).build()
);
} else if (
nowEpoch > darkAuctionOpen.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < darkAuctionOpen.toEpochMilli()
) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > darkAuctionOpen.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < darkAuctionOpen.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"dark_auction",
defaultEmbed("Dark Auction").setDescription("Dark auction opens " + getRelativeTimestamp(darkAuctionOpen)).build()
Expand All @@ -285,17 +274,17 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
newYearEvent.toEpochMilli() <= nowEpoch &&
nowEpoch <= newYearEvent.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < newYearEvent.toEpochMilli()
eventTimers.get(index) < newYearEvent.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"new_year",
defaultEmbed("New Year Celebration")
.setDescription("New year celebration starts " + getRelativeTimestamp(newYearEvent))
.build()
);
} else if (nowEpoch > newYearEvent.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < newYearEvent.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > newYearEvent.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < newYearEvent.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"new_year",
defaultEmbed("New Year Celebration")
Expand All @@ -316,15 +305,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
spookyFishing.toEpochMilli() <= nowEpoch &&
nowEpoch <= spookyFishing.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < spookyFishing.toEpochMilli()
eventTimers.get(index) < spookyFishing.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"spooky_fishing",
defaultEmbed("Spooky Fishing").setDescription("Spooky fishing starts " + getRelativeTimestamp(spookyFishing)).build()
);
} else if (nowEpoch > spookyFishing.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < spookyFishing.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > spookyFishing.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < spookyFishing.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"spooky_fishing",
defaultEmbed("Spooky Fishing").setDescription("Spooky fishing starts " + getRelativeTimestamp(spookyFishing)).build()
Expand All @@ -341,15 +330,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
spookyEvent.toEpochMilli() <= nowEpoch &&
nowEpoch <= spookyEvent.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < spookyEvent.toEpochMilli()
eventTimers.get(index) < spookyEvent.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"spooky",
defaultEmbed("Spooky Festival").setDescription("Spooky festival starts " + getRelativeTimestamp(spookyEvent)).build()
);
} else if (nowEpoch > spookyEvent.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < spookyEvent.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > spookyEvent.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < spookyEvent.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"spooky",
defaultEmbed("Spooky Festival").setDescription("Spooky festival starts " + getRelativeTimestamp(spookyEvent)).build()
Expand All @@ -370,19 +359,19 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
fishingFestival.toEpochMilli() <= nowEpoch &&
nowEpoch <= fishingFestival.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < fishingFestival.toEpochMilli()
eventTimers.get(index) < fishingFestival.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"fishing_festival",
defaultEmbed("Fishing Festival")
.setDescription("Fishing festival starts " + getRelativeTimestamp(fishingFestival))
.build()
);
} else if (
nowEpoch > fishingFestival.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < fishingFestival.toEpochMilli()
nowEpoch > fishingFestival.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < fishingFestival.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"fishing_festival",
defaultEmbed("Fishing Festival")
Expand Down Expand Up @@ -416,17 +405,17 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
fallenStar.toEpochMilli() <= nowEpoch &&
nowEpoch <= fallenStar.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < fallenStar.toEpochMilli()
eventTimers.get(index) < fallenStar.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"fallen_star",
defaultEmbed("Cult Of Fallen Star")
.setDescription("Cult of fallen star arrives " + getRelativeTimestamp(fallenStar))
.build()
);
} else if (nowEpoch > fallenStar.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < fallenStar.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > fallenStar.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < fallenStar.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"fallen_star",
defaultEmbed("Cult Of Fallen Star")
Expand Down Expand Up @@ -455,15 +444,15 @@ public static Map<String, MessageEmbed> getEventEmbeds(String[] times, ZonedDate
if (
bankStart.toEpochMilli() <= nowEpoch &&
nowEpoch <= bankStart.plusSeconds(60).toEpochMilli() &&
Long.parseLong(times[index]) < bankStart.toEpochMilli()
eventTimers.get(index) < bankStart.toEpochMilli()
) {
times[index] = "" + nowEpoch;
eventTimers.set(index, nowEpoch);
ebs.put(
"bank_interest",
defaultEmbed("Bank Interest").setDescription("Bank interest is deposited " + getRelativeTimestamp(spookyEvent)).build()
);
} else if (nowEpoch > spookyEvent.plusSeconds(60).toEpochMilli() && Long.parseLong(times[index]) < spookyEvent.toEpochMilli()) {
times[index] = "" + nowEpoch;
} else if (nowEpoch > spookyEvent.plusSeconds(60).toEpochMilli() && eventTimers.get(index) < spookyEvent.toEpochMilli()) {
eventTimers.set(index, nowEpoch);
ebs.put(
"bank_interest",
defaultEmbed("Bank Interest").setDescription("Bank interest is deposited " + getRelativeTimestamp(spookyEvent)).build()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/skyblockplus/utils/ApiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static void initialize() {
leaderboardDatabase.initializeJacob();
leaderboardDatabase.initializeTokens();
leaderboardDatabase.initializeParties();
leaderboardDatabase.initializeEventTimers();
scheduler.scheduleWithFixedDelay(leaderboardDatabase::updateJsonCache, 60, 60, TimeUnit.SECONDS);
scheduler.scheduleWithFixedDelay(ApiHandler::updateCaches, 60, 60, TimeUnit.MINUTES);
if (!IS_DEV) {
Expand All @@ -84,6 +85,7 @@ public static void updateCaches() {
leaderboardDatabase.cacheAuctionTracker();
leaderboardDatabase.cacheTokens();
leaderboardDatabase.cacheParties();
leaderboardDatabase.cacheEventTimers();
} catch (Exception e) {
log.error("Exception when interval caching", e);
}
Expand Down
Loading

0 comments on commit 434bf3e

Please sign in to comment.