Skip to content

Commit

Permalink
check V3 rate limit duration
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Sep 4, 2024
1 parent 4e905cf commit 04730ed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
13 changes: 10 additions & 3 deletions src/main/java/link/locutus/discord/apiv3/PoliticsAndWarV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,16 @@ public <T extends Serializable> List<T> readSnapshot(PagePriority priority, Clas
JsonObject obj = JsonParser.parseString(body).getAsJsonObject();
String errorRaw = obj.get("error").getAsString();
if (errorRaw.equalsIgnoreCase("rate-limited")) {
long amt = 30_500;
setRateLimit(amt);
Thread.sleep(amt);
long sleepMs = 30_500;

JsonObject duration = obj.getAsJsonObject("duration");
if (duration != null && duration.has("nanos")) {
long nanos = duration.get("nanos").getAsLong();
sleepMs = (nanos + 999_999) / 1_000_000;
}

setRateLimit(sleepMs);
Thread.sleep(sleepMs);
continue;
} else {
errorMsg = errorRaw;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/link/locutus/discord/db/WarDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -1888,9 +1888,9 @@ public boolean updateWars(List<DBWar> dbWars, Collection<Integer> expectedIds, C
}
}

long currentTurn = TimeUtil.getTurn();
long cannotExpireWithin5m = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5);
for (DBWar war : activeWars.getActiveWars()) {
// Handle deleted wars
if (war.getDate() >= cannotExpireWithin5m) continue;
if (expectedIdsSet != null && expectedIdsSet.contains(war.getWarId()) && !idsFetched.contains(war.getWarId())) {
prevWars.add(new DBWar(war));
war.setStatus(WarStatus.EXPIRED);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/link/locutus/discord/db/entities/DBWar.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package link.locutus.discord.db.entities;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.politicsandwar.graphql.model.War;
import link.locutus.discord.Locutus;
import link.locutus.discord.apiv1.domains.subdomains.attack.v3.AbstractCursor;
import link.locutus.discord.apiv1.enums.AttackType;
import link.locutus.discord.apiv1.enums.MilitaryUnit;
import link.locutus.discord.apiv1.enums.SuccessType;
import link.locutus.discord.config.Settings;
import link.locutus.discord.db.NationDB;
import link.locutus.discord.util.MarkupUtil;
import link.locutus.discord.util.MathMan;
import link.locutus.discord.util.PW;
Expand All @@ -16,6 +21,7 @@
import link.locutus.discord.util.task.war.WarCard;
import link.locutus.discord.apiv1.domains.subdomains.SWarContainer;
import link.locutus.discord.apiv1.enums.WarType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -161,7 +167,11 @@ public DBWar(DBWar other) {
}

private static int getCities(int nationId) {
DBNation nation = Locutus.imp().getNationDB().getNation(nationId);
Locutus lc = Locutus.imp();
if (lc == null) return 0;
NationDB natDb = lc.getNationDB();
if (natDb == null) return 0;
DBNation nation = natDb.getNation(nationId);
return nation == null ? 0 : nation.getCities();
}

Expand Down
32 changes: 0 additions & 32 deletions src/main/java/link/locutus/discord/db/handlers/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,6 @@ private void loadSyncDates() {
}
}

public static void main(String[] args) {

String input = "AA:Singularity";
List<String> splitAnd = StringMan.split(input, ',');
for (String andGroup : splitAnd) {
List<String> splitXor = StringMan.split(andGroup, '^');
if (splitXor.isEmpty()) {
throw new IllegalArgumentException("Invalid group: `" + andGroup + "`: Empty group");
}

for (String xorGroup : splitXor) {
List<String> splitOr = StringMan.split(xorGroup, '|');
if (splitOr.isEmpty()) {
throw new IllegalArgumentException("Invalid group: `" + xorGroup + "`: Empty group");
}

for (String elem : splitOr) {
if (elem.isEmpty()) {
if (xorGroup.isEmpty()) {
if (andGroup.isEmpty()) {
throw new IllegalArgumentException("Invalid group: `" + input + "`: Empty group");
}
throw new IllegalArgumentException("Invalid group: `" + andGroup + "`: Empty group");
}
throw new IllegalArgumentException("Invalid group: `" + xorGroup + "`: Empty group");
}
char char0 = elem.charAt(0);
}
}
}
}

private void writeSyncDates() {
File file = new File("syncdates.dat");
try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file))) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/link/locutus/discord/gpt/pw/PWGPTHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ public void registerDefaults() {
providerManager.registerDefaults();
}

public static void main(String[] args) throws IOException {
//
}

// public String generateSolution(ValueStore store, GuildDB db, User user, String userInput) {
// // check moderation
// List<ModerationResult> modResult = handler.getModerator().moderate(userInput);
Expand Down

0 comments on commit 04730ed

Please sign in to comment.