Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xdnw/locutus
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Aug 6, 2024
2 parents fc19ebb + 0d38c16 commit 86dd319
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public DBWar getWar() {
@Override
public double[] getLosses(double[] buffer, boolean attacker, boolean units, boolean infra, boolean consumption, boolean includeLoot, boolean includeBuildings) {
if (units) {
double[] unitLosses = getUnitLossCost(buffer, attacker);
getUnitLossCost(buffer, attacker);
}
if (includeLoot) {
double[] loot = getLoot();
Expand All @@ -148,11 +148,10 @@ public double[] getLosses(double[] buffer, boolean attacker, boolean units, bool
}
}
else if (getMoney_looted() != 0) {
int sign = (getVictor() == (attacker ? getAttacker_id() : getDefender_id())) ? -1 : 1;
buffer[ResourceType.MONEY.ordinal()] += getMoney_looted() * sign;
buffer[ResourceType.MONEY.ordinal()] += attacker ? -getMoney_looted() : getMoney_looted();
}
}
if (attacker ? getVictor() == getDefender_id() : getVictor() == getAttacker_id()) {
if (attacker) {
if (infra && getInfra_destroyed_value() != 0) {
buffer[ResourceType.MONEY.ordinal()] += getInfra_destroyed_value();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ public CompletableFuture<IMessageBuilder> send(IMessageBuilder builder) {
CompletableFuture<Message> future = RateLimitUtil.queue(channel.sendMessage(message));
msgFuture = future.thenApply(f -> new DiscordMessageBuilder(this, f));
}


}
if (sendFiles && (!discMsg.files.isEmpty() || !discMsg.images.isEmpty())) {
Map<String, byte[]> allFiles = new HashMap<>(discMsg.files);
allFiles.putAll(discMsg.images);
if (sendFiles && (!discMsg.files.isEmpty() || !discMsg.images.isEmpty() || !discMsg.tables.isEmpty())) {
List<Map.Entry<String, byte[]>> allFiles = new ArrayList<>();
allFiles.addAll(discMsg.files.entrySet());
allFiles.addAll(discMsg.images.entrySet());
allFiles.addAll(discMsg.buildTables());
Message result = null;
for (Map.Entry<String, byte[]> entry : allFiles.entrySet()) {
for (Map.Entry<String, byte[]> entry : allFiles) {
result = RateLimitUtil.complete(channel.sendFiles(FileUpload.fromData(entry.getValue(), entry.getKey())));
}
if (result != null && msgFuture == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.api.utils.messages.MessageEditData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class DiscordHookIO implements IMessageIO {
Expand Down Expand Up @@ -120,11 +117,13 @@ public CompletableFuture<IMessageBuilder> send(IMessageBuilder builder) {
msgFuture = future.thenApply(f -> new DiscordMessageBuilder(this, f));
}
}
if (sendFiles && (!discMsg.files.isEmpty() || !discMsg.images.isEmpty())) {
Map<String, byte[]> allFiles = new HashMap<>(discMsg.files);
allFiles.putAll(discMsg.images);
if (sendFiles && (!discMsg.files.isEmpty() || !discMsg.images.isEmpty() || !discMsg.tables.isEmpty())) {
List<Map.Entry<String, byte[]>> allFiles = new ArrayList<>();
allFiles.addAll(discMsg.files.entrySet());
allFiles.addAll(discMsg.images.entrySet());
allFiles.addAll(discMsg.buildTables());
Message result = null;
for (Map.Entry<String, byte[]> entry : allFiles.entrySet()) {
for (Map.Entry<String, byte[]> entry : allFiles) {
result = RateLimitUtil.complete(hook.sendFiles(FileUpload.fromData(entry.getValue(), entry.getKey())));
}
if (result != null && msgFuture == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void sendWhenFree() {
RateLimitUtil.queueMessage(getParent(), new Function<IMessageBuilder, Boolean>() {
@Override
public Boolean apply(IMessageBuilder msg) {
if (embeds.isEmpty() && images.isEmpty() && files.isEmpty() && buttons.isEmpty() && content.isEmpty()) return false;
if (embeds.isEmpty() && images.isEmpty() && tables.isEmpty() && files.isEmpty() && buttons.isEmpty() && content.isEmpty()) return false;
writeTo(msg);
return true;
}
Expand Down Expand Up @@ -223,14 +223,9 @@ public MessageCreateData build(boolean includeContent) {
for (Map.Entry<String, byte[]> entry : images.entrySet()) {
upload.add(FileUpload.fromData(entry.getValue(), entry.getKey()));
}
for (GraphMessageInfo gmi : tables) {
try {
byte[] imgData = gmi.table().write(gmi.timeFormat(), gmi.numberFormat());
// String name = gmi.table().getName();
upload.add(FileUpload.fromData(imgData, "img.png"));
} catch (IOException e) {
e.printStackTrace();
}
List<Map.Entry<String, byte[]>> tableData = buildTables();
for (Map.Entry<String, byte[]> entry : tableData) {
upload.add(FileUpload.fromData(entry.getValue(), entry.getKey()));
}
if (!upload.isEmpty()) {
discBuilder.setFiles(upload);
Expand All @@ -241,6 +236,20 @@ public MessageCreateData build(boolean includeContent) {
return discBuilder.build();
}

public List<Map.Entry<String, byte[]>> buildTables() {
List<Map.Entry<String, byte[]>> tables = new ArrayList<>();
for (GraphMessageInfo gmi : this.tables) {
try {
byte[] imgData = gmi.table().write(gmi.timeFormat(), gmi.numberFormat());
String fileName = gmi.table().getName().replaceAll("[^a-zA-Z0-9.-]", "") + ".png";
tables.add(new AbstractMap.SimpleEntry<>(fileName, imgData));
} catch (IOException e) {
e.printStackTrace();
}
}
return tables;
}

@Override
public IMessageBuilder clearButtons() {
this.buttons.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static Map<String, String> parseArguments(Set<String> params, String inpu
public CommandManager2 registerDefaults() {
// this.commands.registerMethod(new TestCommands(), List.of("test"), "test", "test");
getCommands().registerMethod(new GrantCommands(), List.of("grant"), "costBulk", "cost");
getCommands().registerMethod(new GrantCommands(), List.of("alliance", "stats"), "militarizationTime", "militarization_time");
getCommands().registerMethod(new StatCommands(), List.of("alliance", "stats"), "militarizationTime", "militarization_time");

getCommands().registerMethod(new WarCommands(), List.of("sheets_ia"), "ActivitySheetDate", "activity_date");
getCommands().registerMethod(new WarCommands(), List.of("sheets_ia"), "WarDecSheetDate", "declares_date");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ public String militarizationTime(@Me IMessageIO channel, DBAlliance alliance, @D
List<AllianceMetric> metrics = new ArrayList<>(Arrays.asList(AllianceMetric.SOLDIER_PCT, AllianceMetric.TANK_PCT, AllianceMetric.AIRCRAFT_PCT, AllianceMetric.SHIP_PCT));
TimeNumericTable table = AllianceMetric.generateTable(metrics, startTurn, endTurn, alliance.getName(), Collections.singleton(alliance));
table.write(channel, TimeFormat.TURN_TO_DATE, TableNumberFormat.PERCENTAGE_ONE, start_time, attach_json, attach_csv);
return "Done!";
return null;
}

@Command(desc = "Graph an alliance metric over time for two coalitions")
Expand Down
36 changes: 10 additions & 26 deletions src/main/java/link/locutus/discord/db/conflict/CoalitionSide.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,33 +315,12 @@ public void updateWar(DBWar previous, DBWar current, boolean isAttacker) {
}
}

public void updateAttack(DBWar war, AbstractCursor attack, boolean isAttacker, AttackTypeSubCategory subCategory) {
int attackerAA, attackerId, cities;
long day = TimeUtil.getDay(attack.getDate());
if (isAttacker) {
attackerId = attack.getAttacker_id();
if (attack.getAttacker_id() == war.getAttacker_id()) {
attackerAA = war.getAttacker_aa();
cities = war.getAttCities();
} else {
attackerAA = war.getDefender_aa();
cities = war.getDefCities();
}
} else {
attackerId = attack.getDefender_id();
if (attack.getDefender_id() == war.getDefender_id()) {
attackerAA = war.getDefender_aa();
cities = war.getDefCities();
} else {
attackerAA = war.getAttacker_aa();
cities = war.getAttCities();
}
}
allianceIdByNation.putIfAbsent(attackerId, attackerAA);
public void updateAttack(DBWar war, AbstractCursor attack, int attackerAA, int nationId, int cities, long day, boolean isAttacker, AttackTypeSubCategory subCategory) {
allianceIdByNation.putIfAbsent(nationId, attackerAA);

Map.Entry<DamageStatGroup, DamageStatGroup> aaDamage = damageByAlliance.computeIfAbsent(attackerAA,
k -> Map.entry(new DamageStatGroup(), new DamageStatGroup()));
Map.Entry<DamageStatGroup, DamageStatGroup> nationDamage = damageByNation.computeIfAbsent(attackerId,
Map.Entry<DamageStatGroup, DamageStatGroup> nationDamage = damageByNation.computeIfAbsent(nationId,
k -> Map.entry(new DamageStatGroup(), new DamageStatGroup()));

Map.Entry<DamageStatGroup, DamageStatGroup> tierDamage = getAllianceDamageStatsByDayPair(attackerAA, cities, day);
Expand All @@ -351,14 +330,17 @@ public void updateAttack(DBWar war, AbstractCursor attack, boolean isAttacker, A

lossesAndDefensiveStats.apply(attack, true);
inflictedAndOffensiveStats.apply(attack, false);

aaDamage.getKey().apply(attack, true);
aaDamage.getValue().apply(attack, false);

nationDamage.getKey().apply(attack, true);
nationDamage.getValue().apply(attack, false);

tierDamage.getKey().apply(attack, true);
tierDamage.getValue().apply(attack, false);

applyAttackerStats(attackerAA, attackerId, cities, day, p -> p.newAttack(war, attack, subCategory));
applyAttackerStats(attackerAA, nationId, cities, day, p -> p.newAttack(war, attack, subCategory));
} else {
lossesAndDefensiveStats.newAttack(war, attack, subCategory);

Expand All @@ -367,12 +349,14 @@ public void updateAttack(DBWar war, AbstractCursor attack, boolean isAttacker, A

aaDamage.getKey().apply(attack, false);
aaDamage.getValue().apply(attack, true);

nationDamage.getKey().apply(attack, false);
nationDamage.getValue().apply(attack, true);

tierDamage.getKey().apply(attack, false);
tierDamage.getValue().apply(attack, true);

applyDefenderStats(attackerAA, attackerId, cities, day, p -> p.newAttack(war, attack, subCategory));
applyDefenderStats(attackerAA, nationId, cities, day, p -> p.newAttack(war, attack, subCategory));
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/link/locutus/discord/db/conflict/Conflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,29 @@ public boolean updateWar(DBWar previous, DBWar current, long turn) {
}

public void updateAttack(DBWar war, AbstractCursor attack, long turn, Function<IAttack, AttackTypeSubCategory> getCached) {
int attackerAA, defenderAA;
int attackerAA, defenderAA, attCities, defCities;
if (attack.getAttacker_id() == war.getAttacker_id()) {
attackerAA = war.getAttacker_aa();
defenderAA = war.getDefender_aa();
attCities = war.getAttCities();
defCities = war.getDefCities();
} else {
attackerAA = war.getDefender_aa();
defenderAA = war.getAttacker_aa();
attCities = war.getDefCities();
defCities = war.getAttCities();
}
CoalitionSide side = getCoalition(attackerAA, defenderAA, turn);
if (side == null) return;

long day = TimeUtil.getDay(attack.getDate());

CoalitionSide otherSide = side.getOther();
AttackTypeSubCategory subCategory = getCached.apply(attack);
side.updateAttack(war, attack, true, subCategory);
otherSide.updateAttack(war, attack, false, subCategory);

side.updateAttack(war, attack, attackerAA, attack.getAttacker_id(), attCities, day, true, subCategory);
otherSide.updateAttack(war, attack, defenderAA, attack.getDefender_id(), defCities, day, false, subCategory);

getWarWebEntry(attackerAA, defenderAA).newAttack(war, attack, null);
getWarWebEntry(attackerAA, defenderAA).apply(attack, true);
getWarWebEntry(defenderAA, attackerAA).apply(attack, false);
Expand Down

0 comments on commit 86dd319

Please sign in to comment.