Skip to content

Commit

Permalink
Update WarDB.java
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 8, 2023
1 parent 93633bc commit 42ae674
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions src/main/java/link/locutus/discord/db/WarDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,17 +515,30 @@ public Set<Integer> getNationsBlockading(int nationId) {

public void loadWars(int days) {
if (days != 0) {
long date = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(days);
List<DBWar> 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<ResultSet>) 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<DBWar> wars = getWarByStatus(WarStatus.ACTIVE, WarStatus.ATTACKER_OFFERED_PEACE, WarStatus.DEFENDER_OFFERED_PEACE);

long currentTurn = TimeUtil.getTurn();
Expand Down Expand Up @@ -1519,7 +1532,7 @@ public void fetchWarsById(Collection<Integer> ids, Consumer<Event> eventConsumer
});

List<DBWar> wars = warsQL.stream().map(DBWar::new).collect(Collectors.toList());
updateWars(wars, idsSorted, eventConsumer);
updateWars(wars, subList, eventConsumer);
}
}

Expand Down Expand Up @@ -1627,33 +1640,34 @@ public boolean updateWars(List<DBWar> dbWars, Collection<Integer> 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);
Expand Down Expand Up @@ -1711,8 +1725,6 @@ public void saveWars(Collection<DBWar> 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<DBWar, PreparedStatement> setStmt = (war, stmt) -> {
Expand Down

0 comments on commit 42ae674

Please sign in to comment.