From 99652d954bdadd802b53b1b00dd10221ab78361e Mon Sep 17 00:00:00 2001 From: xdnw Date: Tue, 1 Aug 2023 07:41:07 +0100 Subject: [PATCH] legacy attacks --- .../subdomains/attack/v3/AbstractCursor.java | 4 +- .../attack/v3/cursors/ALootCursor.java | 5 -- .../link/locutus/discord/db/NationDB.java | 14 ++-- .../java/link/locutus/discord/db/WarDB.java | 79 ++++++++----------- .../discord/db/entities/AttackCost.java | 4 +- .../discord/db/entities/AttackEntry.java | 2 +- 6 files changed, 45 insertions(+), 63 deletions(-) diff --git a/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/AbstractCursor.java b/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/AbstractCursor.java index 53656dcd..8882101c 100644 --- a/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/AbstractCursor.java +++ b/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/AbstractCursor.java @@ -152,9 +152,9 @@ public Map getLosses(boolean attacker, boolean units, bool if (loot != null) { Map lootDouble = PnwUtil.resourcesToMap(loot); if (attacker) { - losses = PnwUtil.subResourcesToA(losses, lootDouble); + PnwUtil.subResourcesToA(losses, lootDouble); } else { - losses = PnwUtil.addResourcesToA(losses, lootDouble); + PnwUtil.addResourcesToA(losses, lootDouble); } } else if (getMoney_looted() != 0) { diff --git a/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/cursors/ALootCursor.java b/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/cursors/ALootCursor.java index 75f7bcf3..d332c1c8 100644 --- a/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/cursors/ALootCursor.java +++ b/src/main/java/link/locutus/discord/apiv1/domains/subdomains/attack/v3/cursors/ALootCursor.java @@ -55,11 +55,6 @@ public double getLootPercent() { return loot_percent_cents * 0.0001d; } - @Override - public Map getLosses(boolean attacker, boolean units, boolean infra, boolean consumption, boolean includeLoot, boolean includeBuildings) { - return null; - } - @Override public AttackType getAttack_type() { return AttackType.A_LOOT; diff --git a/src/main/java/link/locutus/discord/db/NationDB.java b/src/main/java/link/locutus/discord/db/NationDB.java index 9bfc0b7c..edf29698 100644 --- a/src/main/java/link/locutus/discord/db/NationDB.java +++ b/src/main/java/link/locutus/discord/db/NationDB.java @@ -2217,7 +2217,7 @@ public void updateTreasures(Consumer eventConsumer) { saveTreasures(treasuresToSave); } - public void saveTreasures(Collection treasures) { + public synchronized void saveTreasures(Collection treasures) { String insert = "INSERT OR REPLACE INTO TREASURES4 (id, name, color, continent, bonus, spawn_date, nation_id, respawn_alert) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; for (DBTreasure treasure : treasures) { try (PreparedStatement stmt = getConnection().prepareStatement(insert, Statement.RETURN_GENERATED_KEYS)) { @@ -2292,7 +2292,7 @@ public void loadTreasures() { } } - public void addDescription(int id, String description) { + public synchronized void addDescription(int id, String description) { String query = "INSERT INTO NATION_DESCRIPTIONS (id, description) VALUES (?, ?)"; try (PreparedStatement stmt = getConnection().prepareStatement(query)) { stmt.setInt(1, id); @@ -2364,7 +2364,7 @@ public Map getRadiationByTurn(long turn) { throw new RuntimeException(e); } } - public void addRadiationByTurn(Continent continent, long turn, double radiation) { + public synchronized void addRadiationByTurn(Continent continent, long turn, double radiation) { try (PreparedStatement stmt = getConnection().prepareStatement("INSERT OR IGNORE INTO RADIATION_BY_TURN (continent, radiation, turn) VALUES (?, ?, ?)")) { stmt.setInt(1, continent.ordinal()); stmt.setInt(2, (int) (radiation * 100)); @@ -2965,7 +2965,9 @@ private void importLegacyNationLoot(boolean fromAttacks) throws SQLException { } } - getDb().drop("NATION_LOOT"); + synchronized (this) { + getDb().drop("NATION_LOOT"); + } } if (fromAttacks) { @@ -3924,7 +3926,7 @@ public void deleteNations(Set ids, Consumer eventConsumer) { deleteNationsInDB(ids); } - private void deleteNationsInDB(Set ids) { + private synchronized void deleteNationsInDB(Set ids) { if (ids.size() == 1) { int id = ids.iterator().next(); executeStmt("DELETE FROM NATIONS2 WHERE nation_id = " + id); @@ -3937,7 +3939,7 @@ private void deleteNationsInDB(Set ids) { } } - private void deleteTreatiesInDB(Set ids) { + private synchronized void deleteTreatiesInDB(Set ids) { if (ids.size() == 1) { int id = ids.iterator().next(); executeStmt("DELETE FROM TREATIES2 WHERE id = " + id); diff --git a/src/main/java/link/locutus/discord/db/WarDB.java b/src/main/java/link/locutus/discord/db/WarDB.java index c72ea4c1..e20e292d 100644 --- a/src/main/java/link/locutus/discord/db/WarDB.java +++ b/src/main/java/link/locutus/discord/db/WarDB.java @@ -75,23 +75,6 @@ public WarDB() throws SQLException { warsByNationId = new Int2ObjectOpenHashMap<>(); } - public static void main(String[] args) throws SQLException, IOException { - Settings.INSTANCE.reload(Settings.INSTANCE.getDefaultFile()); - WarDB warDb = new WarDB(); - - Settings.INSTANCE.TASKS.UNLOAD_ATTACKS_AFTER_DAYS = 5; - -// warDb.testLoadAttacks2(); - warDb.loadAttacks(1); - warDb.loadWars(-1); - warDb.testAttackSerializingTime(); - - System.exit(0); - -// warDb.loadWars(); -// warDb.testLoadAttacks(); - } - public void testAttackSerializingTime() throws IOException { Map countByType = new EnumMap<>(AttackType.class); int num_attacks = 0; @@ -376,22 +359,23 @@ public void importLegacyAttacks() { e.printStackTrace(); } - String query = "INSERT OR IGNORE INTO `ATTACKS3` (`war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data`) VALUES (?, ?, ?, ?, ?)"; + String query = "INSERT OR IGNORE INTO `ATTACKS3` (`id`, `war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data`) VALUES (?, ?, ?, ?, ?, ?)"; executeBatch(attacks, query, new ThrowingBiConsumer() { @Override public void acceptThrows(AbstractCursor attack, PreparedStatement stmt) throws SQLException { - stmt.setInt(1, attack.getWar_id()); - stmt.setInt(2, attack.getAttacker_id()); - stmt.setInt(3, attack.getDefender_id()); - stmt.setLong(4, attack.getDate()); + stmt.setInt(1, attack.getWar_attack_id()); + stmt.setInt(2, attack.getWar_id()); + stmt.setInt(3, attack.getAttacker_id()); + stmt.setInt(4, attack.getDefender_id()); + stmt.setLong(5, attack.getDate()); byte[] data = cursorManager.toBytes(attack); - stmt.setBytes(5, data); + stmt.setBytes(6, data); } }); // if table sizes match, drop attacks2 int countRows = 0; - try (PreparedStatement stmt = getConnection().prepareStatement("SELECT COUNT(*) FROM `attacks2`")) { + try (PreparedStatement stmt = getConnection().prepareStatement("SELECT COUNT(*) FROM `attacks3`")) { try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { countRows = rs.getInt(1); @@ -400,7 +384,7 @@ public void acceptThrows(AbstractCursor attack, PreparedStatement stmt) throws S } catch (SQLException e) { throw new RuntimeException(e); } - if (countRows == attacks.size()) { + if (countRows >= attacks.size() && countRows > 0) { executeStmt("DROP TABLE `attacks2`"); } } @@ -465,19 +449,18 @@ private void setWar(DBWar war) { } public void load() { - long start = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(6); - Locutus.imp().getExecutor().submit(new Runnable() { - @Override - public void run() { - int loadWarsDays = Settings.INSTANCE.TASKS.UNLOAD_WARS_AFTER_DAYS < 0 ? -1 : Math.max(Settings.INSTANCE.TASKS.UNLOAD_WARS_AFTER_DAYS, 6); - loadWars(loadWarsDays); - int loadAttackDays = Settings.INSTANCE.TASKS.UNLOAD_ATTACKS_AFTER_DAYS < 0 ? -1 : Math.min(Settings.INSTANCE.TASKS.UNLOAD_ATTACKS_AFTER_DAYS, Math.max(0, loadWarsDays)); - if (loadAttackDays != 0) { - importLegacyAttacks(); - loadAttacks(loadAttackDays); - } - } - }); + System.out.println("Loading wars and attacks"); + int loadWarsDays = Settings.INSTANCE.TASKS.UNLOAD_WARS_AFTER_DAYS < 0 ? -1 : Math.max(Settings.INSTANCE.TASKS.UNLOAD_WARS_AFTER_DAYS, 6); + loadWars(loadWarsDays); + System.out.println("Loaded wars"); + int loadAttackDays = Settings.INSTANCE.TASKS.UNLOAD_ATTACKS_AFTER_DAYS < 0 ? -1 : Math.min(Settings.INSTANCE.TASKS.UNLOAD_ATTACKS_AFTER_DAYS, Math.max(0, loadWarsDays)); + if (loadAttackDays != 0) { + System.out.println("Loading attacks"); + importLegacyAttacks(); + System.out.println("Loaded legacy attacks"); + loadAttacks(loadAttackDays); + System.out.println("Loaded attacks " + attacksByWarId.size()); + } } public void loadWars(int days) { @@ -533,6 +516,7 @@ public List getAttacks(Map wars, Predicate values) { byte[] data = attackCursorFactory.toBytes(attack); if (attacks.isEmpty() || attacks.stream().noneMatch(f -> Arrays.equals(f, data))) { attacks.add(data); - toSave.add(new AttackEntry(attack.getWar_id(), attack.getAttacker_id(), attack.getDefender_id(), attack.getDate(), data)); + toSave.add(new AttackEntry(attack.getWar_attack_id(), attack.getWar_id(), attack.getAttacker_id(), attack.getDefender_id(), attack.getDate(), data)); } } } // String query = "INSERT OR IGNORE INTO `ATTACKS3` (`war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data`) VALUES (?, ?, ?, ?, ?)"; - String query = "INSERT OR REPLACE INTO `ATTACKS3` (`war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data`) VALUES (?, ?, ?, ?, ?)"; + String query = "INSERT OR REPLACE INTO `ATTACKS3` (`id`, `war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data`) VALUES (?, ?, ?, ?, ?, ?)"; executeBatch(toSave, query, new ThrowingBiConsumer() { @Override public void acceptThrows(AttackEntry attack, PreparedStatement stmt) throws SQLException { - stmt.setInt(1, attack.war_id()); - stmt.setInt(2, attack.attacker_id()); - stmt.setInt(3, attack.defender_id()); - stmt.setLong(4, attack.date()); - stmt.setBytes(5, attack.data()); + stmt.setInt(1, attack.id()); + stmt.setInt(2, attack.war_id()); + stmt.setInt(3, attack.attacker_id()); + stmt.setInt(4, attack.defender_id()); + stmt.setLong(5, attack.date()); + stmt.setBytes(6, attack.data()); } }); } @@ -2251,7 +2236,7 @@ public void loadAttacks(int days) { } else { whereClause = ""; } - String query = "SELECT * FROM `attacks3` " + whereClause + " ORDER BY `war_attack_id` ASC"; + String query = "SELECT * FROM `attacks3` " + whereClause + " ORDER BY `id` ASC"; // `war_id`, `attacker_nation_id`, `defender_nation_id`, `date`, `data` // Int2ObjectOpenHashMap> attacksByWarId (is a field for this class) AttackCursorFactory factory = new AttackCursorFactory(); diff --git a/src/main/java/link/locutus/discord/db/entities/AttackCost.java b/src/main/java/link/locutus/discord/db/entities/AttackCost.java index 048da4b2..28699a42 100644 --- a/src/main/java/link/locutus/discord/db/entities/AttackCost.java +++ b/src/main/java/link/locutus/discord/db/entities/AttackCost.java @@ -200,8 +200,8 @@ public void addCost(AbstractCursor attack, Function isP Map attConsume = attack.getLosses(true, false, false, true, false, false); Map defConsume = attack.getLosses(false, false, false, true, false, false); - double attInfra = PnwUtil.convertedTotal(attack.getLosses(true, false, true, false, false, false)); - double defInfra = PnwUtil.convertedTotal(attack.getLosses(false, false, true, false, false, false)); + double attInfra = 0; + double defInfra = attack.getInfra_destroyed_value(); Map defBuild = attack.getBuildingsDestroyed(); diff --git a/src/main/java/link/locutus/discord/db/entities/AttackEntry.java b/src/main/java/link/locutus/discord/db/entities/AttackEntry.java index 8b939b0e..61280ef6 100644 --- a/src/main/java/link/locutus/discord/db/entities/AttackEntry.java +++ b/src/main/java/link/locutus/discord/db/entities/AttackEntry.java @@ -1,4 +1,4 @@ package link.locutus.discord.db.entities; -public record AttackEntry(int war_id, int attacker_id, int defender_id, long date, byte[] data) { +public record AttackEntry(int id, int war_id, int attacker_id, int defender_id, long date, byte[] data) { }