Skip to content

Commit

Permalink
legacy attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 1, 2023
1 parent b68631c commit 99652d9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ public Map<ResourceType, Double> getLosses(boolean attacker, boolean units, bool
if (loot != null) {
Map<ResourceType, Double> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public double getLootPercent() {
return loot_percent_cents * 0.0001d;
}

@Override
public Map<ResourceType, Double> getLosses(boolean attacker, boolean units, boolean infra, boolean consumption, boolean includeLoot, boolean includeBuildings) {
return null;
}

@Override
public AttackType getAttack_type() {
return AttackType.A_LOOT;
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/link/locutus/discord/db/NationDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ public void updateTreasures(Consumer<Event> eventConsumer) {
saveTreasures(treasuresToSave);
}

public void saveTreasures(Collection<DBTreasure> treasures) {
public synchronized void saveTreasures(Collection<DBTreasure> 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)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2364,7 +2364,7 @@ public Map<Continent, Double> 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));
Expand Down Expand Up @@ -2965,7 +2965,9 @@ private void importLegacyNationLoot(boolean fromAttacks) throws SQLException {
}
}

getDb().drop("NATION_LOOT");
synchronized (this) {
getDb().drop("NATION_LOOT");
}
}

if (fromAttacks) {
Expand Down Expand Up @@ -3924,7 +3926,7 @@ public void deleteNations(Set<Integer> ids, Consumer<Event> eventConsumer) {
deleteNationsInDB(ids);
}

private void deleteNationsInDB(Set<Integer> ids) {
private synchronized void deleteNationsInDB(Set<Integer> ids) {
if (ids.size() == 1) {
int id = ids.iterator().next();
executeStmt("DELETE FROM NATIONS2 WHERE nation_id = " + id);
Expand All @@ -3937,7 +3939,7 @@ private void deleteNationsInDB(Set<Integer> ids) {
}
}

private void deleteTreatiesInDB(Set<Integer> ids) {
private synchronized void deleteTreatiesInDB(Set<Integer> ids) {
if (ids.size() == 1) {
int id = ids.iterator().next();
executeStmt("DELETE FROM TREATIES2 WHERE id = " + id);
Expand Down
79 changes: 32 additions & 47 deletions src/main/java/link/locutus/discord/db/WarDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttackType, Integer> countByType = new EnumMap<>(AttackType.class);
int num_attacks = 0;
Expand Down Expand Up @@ -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<AbstractCursor, PreparedStatement>() {
@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);
Expand All @@ -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`");
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -533,6 +516,7 @@ public List<AbstractCursor> getAttacks(Map<Integer, DBWar> wars, Predicate<Attac

for (byte[] data : attacks) {
AbstractCursor cursor = loader.apply(war, data);
if (cursor == null) continue;
attackAdder.accept(cursor);
}
}
Expand Down Expand Up @@ -837,7 +821,7 @@ public void createTables() {
// create index for war_id, attacker_nation_id, defender_nation_id, date

String attacksTable = "CREATE TABLE IF NOT EXISTS `ATTACKS3` (" +
"`id` INTEGER PRIMARY KEY AUTOINCREMENT, " +
"`id` INTEGER PRIMARY KEY, " +
"`war_id` INT NOT NULL, " +
"`attacker_nation_id` INT NOT NULL, " +
"`defender_nation_id` INT NOT NULL, " +
Expand Down Expand Up @@ -2041,21 +2025,22 @@ public void saveAttacks(Collection<AbstractCursor> 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<AttackEntry, PreparedStatement>() {
@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());
}
});
}
Expand Down Expand Up @@ -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<List<byte[]>> attacksByWarId (is a field for this class)
AttackCursorFactory factory = new AttackCursorFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ public void addCost(AbstractCursor attack, Function<AbstractCursor, Boolean> isP
Map<ResourceType, Double> attConsume = attack.getLosses(true, false, false, true, false, false);
Map<ResourceType, Double> 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<Building, Integer> defBuild = attack.getBuildingsDestroyed();

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}

0 comments on commit 99652d9

Please sign in to comment.