From 42ae674e33009fe70964ef24d51c8b32cfc98731 Mon Sep 17 00:00:00 2001 From: xdnw Date: Tue, 8 Aug 2023 09:05:18 +0100 Subject: [PATCH] Update WarDB.java --- .../java/link/locutus/discord/db/WarDB.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main/java/link/locutus/discord/db/WarDB.java b/src/main/java/link/locutus/discord/db/WarDB.java index df8d4770..fc1e22f3 100644 --- a/src/main/java/link/locutus/discord/db/WarDB.java +++ b/src/main/java/link/locutus/discord/db/WarDB.java @@ -515,17 +515,30 @@ public Set getNationsBlockading(int nationId) { public void loadWars(int days) { if (days != 0) { - long date = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(days); + List saveWars = new ArrayList<>(); + + long now = System.currentTimeMillis(); + long date = now - TimeUnit.DAYS.toMillis(days); String whereClause = days > 0 ? " WHERE date > " + date : ""; query("SELECT * FROM wars" + whereClause, f -> { }, (ThrowingConsumer) rs -> { while (rs.next()) { + DBWar war = create(rs); +// if (war.status == WarStatus.EXPIRED && now < war.possibleEndDate()) { +// war.status = WarStatus.ACTIVE; +// saveWars.add(war); +// } setWar(war); } }); + if (!saveWars.isEmpty()) { + saveWars(saveWars); + } + } + List wars = getWarByStatus(WarStatus.ACTIVE, WarStatus.ATTACKER_OFFERED_PEACE, WarStatus.DEFENDER_OFFERED_PEACE); long currentTurn = TimeUtil.getTurn(); @@ -1519,7 +1532,7 @@ public void fetchWarsById(Collection ids, Consumer eventConsumer }); List wars = warsQL.stream().map(DBWar::new).collect(Collectors.toList()); - updateWars(wars, idsSorted, eventConsumer); + updateWars(wars, subList, eventConsumer); } } @@ -1627,33 +1640,34 @@ public boolean updateWars(List dbWars, Collection expectedIds, C long currentTurn = TimeUtil.getTurn(); for (DBWar war : activeWars.getActiveWars().values()) { // Handle deleted wars - if (expectedIds.contains(war.getWarId()) && !idsFetched.contains(war.getWarId())) { + if (expectedIds != null && expectedIds.contains(war.getWarId()) && !idsFetched.contains(war.getWarId())) { prevWars.add(new DBWar(war)); war.status = WarStatus.EXPIRED; newWars.add(war); continue; } - // Expire other wars - if (!newWarIds.add(war.getWarId())) continue; - - long warTurn = TimeUtil.getTurn(war.date); - - if (currentTurn - warTurn >= 60 && false) { // TODO disable - prevWars.add(new DBWar(war)); - war.status = WarStatus.EXPIRED; - newWars.add(war); - - } else if (war.status != WarStatus.EXPIRED){ - DBNation attacker = Locutus.imp().getNationDB().getNation(war.attacker_id); - DBNation defender = Locutus.imp().getNationDB().getNation(war.defender_id); - if ((attacker == null || defender == null) && war.date < System.currentTimeMillis() - TimeUnit.HOURS.toMillis(2)) { - - prevWars.add(new DBWar(war)); - war.status = WarStatus.EXPIRED; - newWars.add(war); - } - } +// // Expire other wars +// if (!newWarIds.add(war.getWarId())) continue; +// +// long warTurn = TimeUtil.getTurn(war.date); +// +// if (currentTurn - warTurn >= 60 && false) { // TODO disable +// prevWars.add(new DBWar(war)); +// war.status = WarStatus.EXPIRED; +// newWars.add(war); +// +//// } else +//// if (war.status != WarStatus.EXPIRED){ +//// DBNation attacker = Locutus.imp().getNationDB().getNation(war.attacker_id); +//// DBNation defender = Locutus.imp().getNationDB().getNation(war.defender_id); +//// if ((attacker == null || defender == null) && war.date < System.currentTimeMillis() - TimeUnit.HOURS.toMillis(2)) { +//// +//// prevWars.add(new DBWar(war)); +//// war.status = WarStatus.EXPIRED; +//// newWars.add(war); +//// } +// } } saveWars(newWars); @@ -1711,8 +1725,6 @@ public void saveWars(Collection values) { } } - System.out.println("Done save war snaptshots"); - String query = "INSERT OR REPLACE INTO `wars`(`id`, `attacker_id`, `defender_id`, `attacker_aa`, `defender_aa`, `war_type`, `status`, `date`) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; ThrowingBiConsumer setStmt = (war, stmt) -> {