Skip to content

Commit

Permalink
Finish updating attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 2, 2023
1 parent 203c7c6 commit f1db589
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public AttackType getAttack_type() {
@Override
public void load(WarAttack attack) {
super.load(attack);
this.attcas1 = attack.getAtt_soldiers_lost();
this.defcas1 = attack.getDef_soldiers_lost();
this.attcas1 = attack.getAtt_aircraft_lost();
this.defcas1 = attack.getDef_aircraft_lost();
this.defcas2 = attack.getDef_tanks_lost();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public AttackType getAttack_type() {
@Override
public void load(WarAttack attack) {
super.load(attack);
this.attcas1 = attack.getAtt_soldiers_lost();
this.defcas1 = attack.getDef_soldiers_lost();
this.attcas1 = attack.getAtt_aircraft_lost();
this.defcas1 = attack.getDef_aircraft_lost();
this.defcas2 = attack.getDef_tanks_lost();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public AttackType getAttack_type() {
@Override
public void load(WarAttack attack) {
super.load(attack);
this.attcas1 = attack.getAtt_soldiers_lost();
this.defcas1 = attack.getDef_soldiers_lost();
this.attcas1 = attack.getAtt_aircraft_lost();
this.defcas1 = attack.getDef_aircraft_lost();
this.defcas2 = attack.getDef_tanks_lost();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public AttackType getAttack_type() {
@Override
public void load(WarAttack attack) {
super.load(attack);
this.attcas1 = attack.getAtt_soldiers_lost();
this.defcas1 = attack.getDef_soldiers_lost();
this.attcas1 = attack.getAtt_aircraft_lost();
this.defcas1 = attack.getDef_aircraft_lost();
}
@Override
public double getMoney_looted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ public double getLootPercent() {
return loot_percent_cents * 0.01d;
}

@Override
public double getInfra_destroyed_value() {
if (infra_destroyed_value_cents > 0) {
return infra_destroyed_value_cents * 0.01d;
} else if (!city_infra_before_cents.isEmpty() && infra_destroyed_percent_cents > 0) {
double pct = (1 - infra_destroyed_percent_cents * 0.01);
for (Map.Entry<Integer, Integer> entry : city_infra_before_cents.entrySet()) {
int before = entry.getValue();
int after = (int) Math.round(before * pct);
if (after < before) {
double value = PnwUtil.calculateInfra(after * 0.01, before * 0.01);
infra_destroyed_cents += (before - after);
infra_destroyed_value_cents += (value * 100);
}
}
}
return infra_destroyed_value_cents * 0.01;
}

@Override
public void load(DBAttack legacy) {
super.load(legacy);
Expand All @@ -65,7 +84,7 @@ public void load(DBAttack legacy) {
city_infra_before_cents.clear();
this.infra_destroyed_cents = (int) Math.round(legacy.getInfra_destroyed() * 100);
infra_destroyed_percent_cents = (int) Math.round(legacy.infraPercent_cached * 100);
infra_destroyed_value_cents = (int) Math.round(legacy.getInfra_destroyed_value() * 100);
infra_destroyed_value_cents = (long) Math.round(legacy.getInfra_destroyed_value() * 100);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public double profitConverted2(Continent continent, double rads, Predicate<Proje
profit += income;

double basePopulation = getInfra() * 100;
double food = (Math.pow(basePopulation, 2)) / 125000000 + ((basePopulation) * (1 + Math.log(getAge()) / 15d) - basePopulation) / 850;
double food = (Math.pow(basePopulation, 2)) / 125_000_000 + ((basePopulation) * (1 + Math.log(getAge()) / 15d) - basePopulation) / 850;

profit -= PnwUtil.convertedTotalNegative(ResourceType.FOOD, food);

Expand Down Expand Up @@ -820,7 +820,9 @@ public double[] profit(Continent continent, double rads, long date, Predicate<Pr

profitBuffer[ResourceType.MONEY.ordinal()] += income * turns / 12;

profitBuffer[ResourceType.FOOD.ordinal()] -= metrics.population * turns / (1000d * 12);
double basePopulation = getInfra() * 100;
double food = (Math.pow(basePopulation, 2)) / 125_000_000 + ((basePopulation) * (1 + Math.log(getAge()) / 15d) - basePopulation) / 850;
profitBuffer[ResourceType.FOOD.ordinal()] -= food * turns / 12d;

return profitBuffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
String diffStr = TimeUtil.secToTime(TimeUnit.MILLISECONDS, diff);

Map<Integer, Double> totals = new HashMap<>();
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacks(nationMap.keySet(), cutoffMs);
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacksEither(nationMap.keySet(), cutoffMs);
for (AbstractCursor attack : attacks) {
if (attack.getVictor() != 0 && attack.getMoney_looted() != 0) {
if (nationMap.containsKey(attack.getVictor())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m
String title = (damage && net ? "Net " : "Total ") + (typeName == null ? "" : typeName + " ") + (profit ? damage ? "damage" : "profit" : (unitKill != null ? "kills" : unitLoss != null ? "deaths" : "losses")) + " " + (average ? "per" : "of") + " war (%s)";
title = String.format(title, diffStr);

List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacks(nationMap.keySet(), start, end);
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacksEither(nationMap.keySet(), start, end);

GroupedRankBuilder<Integer, AbstractCursor> attackGroup = new RankBuilder<>(attacks)
.group((attack, map) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String onCommand(Guild guild, IMessageIO channel, User author, DBNation m

Map<Integer, Double> totals = new HashMap<>();
Map<Integer, Integer> counters = new HashMap<>();
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacks(nationMap.keySet(), cutoffMs);
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacksEither(nationMap.keySet(), cutoffMs);
for (AbstractCursor attack : attacks) {
if (attack.getVictor() != 0) {
if (nationMap.containsKey(attack.getDefender_id())) {
Expand Down
63 changes: 62 additions & 1 deletion src/main/java/link/locutus/discord/db/WarDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,33 @@ public class WarDB extends DBMainV2 {
private Map<Integer, Map<Integer, DBWar>> warsByNationId;
private final Int2ObjectOpenHashMap<List<byte[]>> attacksByWarId = new Int2ObjectOpenHashMap<>();
public WarDB() throws SQLException {
super(Settings.INSTANCE.DATABASE, "war");
this("war");
}

public WarDB(String name) throws SQLException {
super(Settings.INSTANCE.DATABASE, name);
warsById = new Int2ObjectOpenHashMap<>();
warsByAllianceId = new Int2ObjectOpenHashMap<>();
warsByNationId = new Int2ObjectOpenHashMap<>();
}

public List<DBAttack> getLegacyVictory() {
List<DBAttack> attacks = new ArrayList<>();
try (PreparedStatement stmt= getConnection().prepareStatement("select * FROM `attacks2` WHERE `attack_type` = ? ORDER BY `war_attack_id` ASC", ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY)) {
stmt.setFetchSize(2 << 16);
stmt.setInt(1, AttackType.VICTORY.ordinal());
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DBAttack legacy = createAttack(rs);
attacks.add(legacy);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return attacks;
}

public void testAttackSerializingTime() throws IOException {
Map<AttackType, Integer> countByType = new EnumMap<>(AttackType.class);
int num_attacks = 0;
Expand Down Expand Up @@ -462,6 +483,23 @@ public void load() {
loadAttacks(loadAttackDays);
System.out.println("Loaded attacks in " + (System.currentTimeMillis() - start) + "ms");
System.out.println("Loaded attacks " + attacksByWarId.size());

System.out.println("Updating attacks");

{
// get attacks by id 20071530
// Map<Integer, DBWar> wars = getWars(f -> f.date > System.currentTimeMillis() - TimeUnit.DAYS.toMillis(20));
// List<AbstractCursor> attacks = queryAttacks().withWars(wars).getList();
// //
// attacks.removeIf(f -> f.getDate() > System.currentTimeMillis() - TimeUnit.DAYS.toMillis(18));
// // get last
// AbstractCursor attack = attacks.stream().max(Comparator.comparing(AbstractCursor::getWar_attack_id)).orElse(null);
// if (attack == null) {
// System.out.println("No attacks found");
// } else {
// updateAttacks(attack, false, null, false);
// }
}
}
}

Expand Down Expand Up @@ -660,6 +698,27 @@ public List<AbstractCursor> getAttacks(Set<Integer> nationIds, long start, long
return getAttacksByWars(allWars, start, end);
}

public List<AbstractCursor> getAttacksEither(Set<Integer> nationIds, long cuttoffMs) {
return getAttacksEither(nationIds, cuttoffMs, Long.MAX_VALUE);
}

public List<AbstractCursor> getAttacksEither(Set<Integer> nationIds, long start, long end) {
Set<DBWar> allWars = new LinkedHashSet<>();
long startWithExpire = TimeUtil.getTimeFromTurn(TimeUtil.getTurn(start) - 60);
synchronized (warsByNationId) {
for (int nationId : nationIds) {
Map<Integer, DBWar> natWars = warsByNationId.get(nationId);
if (natWars != null) {
for (DBWar war : natWars.values()) {
if (war.date < startWithExpire || war.date > end) continue;
allWars.add(war);
}
}
}
}
return getAttacksByWars(allWars, start, end);
}

public List<AbstractCursor> getAttacksAny(Set<Integer> nationIds, long cuttoffMs) {
return getAttacksAny(nationIds, cuttoffMs, Long.MAX_VALUE);
}
Expand Down Expand Up @@ -2024,6 +2083,8 @@ public void saveAttacks(Collection<AbstractCursor> values) {
// add to attacks map
synchronized (attacksByWarId) {
for (AbstractCursor attack : values) {
// AttackEntry(int id, int war_id, int attacker_id, int defender_id, long date, byte[] data) {
toSave.add(new AttackEntry(attack.getWar_attack_id(), attack.getWar_id(), attack.getAttacker_id(), attack.getDefender_id(), attack.getDate(), attackCursorFactory.toBytes(attack)));
List<byte[]> attacks = attacksByWarId.get(attack.getWar_id());

Set<Integer> attackIds = attackIdsByWarId.get(attack.getWar_id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public double apply(DBAlliance alliance) {

AttackCost cost = new AttackCost();
long cutoff = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacks(nationIds, cutoff);
List<AbstractCursor> attacks = Locutus.imp().getWarDb().getAttacksEither(nationIds, cutoff);
cost.addCost(attacks, a -> nationIds.contains(a.getAttacker_id()), b -> nationIds.contains(b.getDefender_id()));
return cost.convertedTotal(true);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/link/locutus/discord/util/PnwUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import link.locutus.discord.apiv1.enums.city.JavaCity;
import link.locutus.discord.apiv1.enums.city.project.Projects;
import net.dv8tion.jda.api.entities.Guild;
import org.apache.commons.math3.util.FastMath;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,9 @@ public class NationUpdateProcessor {
public static void updateBlockades() {
long now = System.currentTimeMillis();

// get active wars
Set<DBWar> activeWars = Locutus.imp().getWarDb().getActiveWars();
// Get the ones with navals in them
Map<Integer, DBWar> wars = Locutus.imp().getWarDb().getWarsSince(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(10));

// get wars from those nation ids



Map<Integer, DBWar> wars = Locutus.imp().getWarDb().getWarsSince(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(5));

List<AbstractCursor> attacks = Locutus.imp().getWarDb().queryAttacks().withWars(wars).afterDate(now - TimeUnit.DAYS.toMillis(5)).getList();
List<AbstractCursor> attacks = Locutus.imp().getWarDb().queryAttacks().withWars(wars).withType(AttackType.NAVAL).afterDate(now - TimeUnit.DAYS.toMillis(10)).getList();

Collections.sort(attacks, Comparator.comparingLong(o -> o.getDate()));

Expand Down

0 comments on commit f1db589

Please sign in to comment.