Skip to content

Commit

Permalink
City bu;ds
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jul 29, 2023
1 parent fa84777 commit 1155109
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ public void accept(NationResponseProjection projection) {

projection.wars_won();
projection.wars_lost();

projection.spies();
}
}, f -> PoliticsAndWarV3.ErrorResponse.THROW, nationResults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ public String onCommand(IMessageIO io, Guild guild, User author, DBNation me, Li

checkup(io, me, cityId, origin); // show help

if (days == null) {
// if (days == null)
{
origin.zeroNonMilitary();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,36 @@ default IMessageBuilder paginate(String title, JSONObject command, Integer page,
@CheckReturnValue
default IMessageBuilder writeTable(String title, List<List<String>> tableList, boolean embed, String footer) {
if (tableList.size() == 0) return this;

AsciiTable at = new AsciiTable(new AT_Context().setWidth(36).setLineSeparator("\n"));
at.addRow(tableList.get(0).toArray(new Object[0]));
at.addRule();
for (int i = 1; i < tableList.size(); i++) {
at.addRow(tableList.get(i).toArray(new Object[0]));
if (i != tableList.size() - 1) {
at.addRule();
String tableStr;
try {
AsciiTable at = new AsciiTable(new AT_Context().setWidth(36).setLineSeparator("\n"));
at.addRow(tableList.get(0).toArray(new Object[0]));
at.addRule();
for (int i = 1; i < tableList.size(); i++) {
at.addRow(tableList.get(i).toArray(new Object[0]));
if (i != tableList.size() - 1) {
at.addRule();
}
}
}
if (footer == null) footer = "";
else footer = "\n" + footer;
if (footer == null) footer = "";
else footer = "\n" + footer;

tableStr = at.render();
} catch (Throwable e) {
AsciiTable at = new AsciiTable(new AT_Context().setLineSeparator("\n"));
at.addRow(tableList.get(0).toArray(new Object[0]));
at.addRule();
for (int i = 1; i < tableList.size(); i++) {
at.addRow(tableList.get(i).toArray(new Object[0]));
if (i != tableList.size() - 1) {
at.addRule();
}
}
if (footer == null) footer = "";
else footer = "\n" + footer;

String tableStr = at.render();
tableStr = at.render();
}
tableStr = tableStr.lines().map(f -> f.substring(1, f.length() - 1)).collect(Collectors.joining("\n"));
if (embed) {
return embed(title, "```\n" + tableStr + "\n```" + footer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public Map<NationFilter, Integer> taxIdMap(@Me GuildDB db, String input) {
public CityBuild city(@Default @Me DBNation nation, @TextArea String input) {
// {city X Nation}
int index = input.indexOf('{');
Integer cityId = null;
String json;
if (index == -1) {
json = null;
Expand All @@ -268,12 +269,14 @@ public CityBuild city(@Default @Me DBNation nation, @TextArea String input) {
index = input.indexOf('}');
if (index == -1) throw new IllegalArgumentException("No closing bracket found");
// parse number 1234
int cityId = input.contains(" ") ? Integer.parseInt(input.substring(6, index)) - 1 : 0;
int cityIndex = input.contains(" ") ? Integer.parseInt(input.substring(6, index)) - 1 : 0;
Set<Map.Entry<Integer, JavaCity>> cities = nation.getCityMap(true, false).entrySet();
int i = 0;
for (Map.Entry<Integer, JavaCity> entry : cities) {
if (++i == index) {
return entry.getValue().toCityBuild();
if (++i == cityIndex) {
CityBuild build = entry.getValue().toCityBuild();
build.setCity_id(entry.getKey());
return build;
}
}
throw new IllegalArgumentException("City not found: " + index + " for natiion " + nation.getName());
Expand All @@ -283,7 +286,7 @@ public CityBuild city(@Default @Me DBNation nation, @TextArea String input) {
}
CityBuild build = null;
if (input.contains("city/id=")) {
int cityId = Integer.parseInt(input.split("=")[1]);
cityId = Integer.parseInt(input.split("=")[1]);
Map.Entry<Integer, DBCity> cityEntry = Locutus.imp().getNationDB().getCitiesV3ByCityId(cityId);
if (cityEntry == null) throw new IllegalArgumentException("No city found in cache for " + cityId);
int nationId = cityEntry.getKey();
Expand All @@ -297,8 +300,13 @@ public CityBuild city(@Default @Me DBNation nation, @TextArea String input) {
if (build != null) {
json = build2.toString().replace("}", "") + "," + build.toString().replace("{", "");
build = CityBuild.of(json, true);
} else {
build = build2;
}
}
if (build != null && cityId != null) {
build.setCity_id(cityId);
}
return build;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public String depositResources(@Me User author, @Me DBNation me, @Me Member memb
throw new IllegalArgumentException("rawsDays must be > 1 turns (1/12 days)");
}
allianceList.updateCities();
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> funds = allianceList.calculateDisburse(nations, rawsDays, true, false, true, rawsNoDailyCash, rawsNoCash, false);
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> funds = allianceList.calculateDisburse(nations, null, rawsDays, false, false, true, rawsNoDailyCash, rawsNoCash, false);
for (Map.Entry<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> entry : funds.entrySet()) {
DBNation nation = entry.getKey();
OffshoreInstance.TransferStatus status = entry.getValue().getKey();
Expand All @@ -267,7 +267,7 @@ public String depositResources(@Me User author, @Me DBNation me, @Me Member memb
}
}

Map<DBNation, Map<ResourceType, Double>> stockpiles = allianceList.getMemberStockpile();
Map<DBNation, Map<ResourceType, Double>> stockpiles = allianceList.getMemberStockpile(remainingNations::contains);
for (DBNation nation : remainingNations) {
Map<ResourceType, Double> stockpile = stockpiles.get(nation);
if (stockpile == null) {
Expand Down Expand Up @@ -338,9 +338,9 @@ public String depositResources(@Me User author, @Me DBNation me, @Me Member memb
nation.getName(),
nation.getNationUrl(),
entry.getValue().name(),
"",
"{}",
stockpileStr,
"",
"null",
"false",
"false",
"false"
Expand All @@ -359,7 +359,9 @@ public void consume(IMessageBuilder msg, List<List<String>> errors, String title
String content = errors.stream().map(f -> StringMan.join(f, "\t")).collect(Collectors.joining("\n"));
msg.file(title + ".txt", content);
} else {
msg.writeTable(title, errors, false, null);
String content = errors.stream().map(f -> StringMan.join(f, "\t")).collect(Collectors.joining("\n"));
msg.append("### " + title + "\n");
msg.append("```\n" + content + "\n```");
}
}
}
Expand Down Expand Up @@ -950,7 +952,7 @@ else if (!force) {
return null;
}

Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> funds = allianceList.calculateDisburse(nations, daysDefault, true, false, true, noDailyCash, noCash, force);
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> funds = allianceList.calculateDisburse(nations, null, daysDefault, true, false, true, noDailyCash, noCash, force);
Map<DBNation, Map<ResourceType, Double>> fundsToSendNations = new LinkedHashMap<>();
for (Map.Entry<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> entry : funds.entrySet()) {
DBNation nation = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,13 @@ public String optimalBuild(@Me JSONObject command, @Me IMessageIO io, @Me Guild
List<String> cmd = new ArrayList<>();
Set<Character> flags = new HashSet<>();
if (days != null) cmd.add(days + " ");

if (build.getCity_id() != null) {
JavaCity jc = new JavaCity(build);
jc.zeroNonMilitary();
build = jc.toCityBuild();
}

cmd.add(build.toString());
if (buildMMR != null) cmd.add("mmr=" + buildMMR);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public SyncWars() {

@Override
public String onCommand(Guild guild, IMessageIO channel, User author, DBNation me, String fullCommandRaw, List<String> args, Set<Character> flags) throws Exception {
Locutus.imp().getWarDb().updateAllWarsV2(Event::post);
Locutus.imp().getWarDb().updateAllWars(Event::post);
return "Done!";
}
}
2 changes: 1 addition & 1 deletion src/main/java/link/locutus/discord/db/WarDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ public boolean updateWars(List<DBWar> dbWars, Consumer<Event> eventConsumer) {
for (DBWar war : dbWars) {
DBWar existing = warsById.get(war.warId);

if ((existing == null && !war.isActive()) || (existing != null && war.status == existing.status)) continue;
if ((existing == null && !war.isActive()) || (existing != null && (war.status == existing.status || !existing.isActive()))) continue;

prevWars.add(existing);
newWars.add(war);
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/link/locutus/discord/db/entities/DBAlliance.java
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,18 @@ public ApiKeyPool getApiKeys(AlliancePermission... permissions) {
}

public Map<DBNation, Map<ResourceType, Double>> getMemberStockpile() throws IOException {
return getMemberStockpile(f -> true);
}

public Map<DBNation, Map<ResourceType, Double>> getMemberStockpile(Predicate<DBNation> fetchNations) throws IOException {
PoliticsAndWarV3 api = getApiOrThrow(AlliancePermission.SEE_SPIES);
List<Integer> ids = getNations().stream()
.filter(f -> f.getVm_turns() == 0 && f.getPositionEnum().id > Rank.APPLICANT.id)
.filter(f -> f.getVm_turns() == 0 && f.getPositionEnum().id > Rank.APPLICANT.id && fetchNations.test(f))
.map(f -> f.getNation_id()).collect(Collectors.toList());
ids.sort(Comparator.comparingInt(a -> a));
if (ids.isEmpty()) {
return new HashMap<>();
}
Map<Integer, double[]> stockPile = api.getStockPile(f -> f.setId(ids));
Map<DBNation, Map<ResourceType, Double>> result = new HashMap<>();
for (Map.Entry<Integer, double[]> entry : stockPile.entrySet()) {
Expand Down Expand Up @@ -883,7 +891,12 @@ public Map<ResourceType, Double> getStockpile() {
}

public void updateCities() throws IOException, ParseException {
Set<Integer> nationIds = getNations(false, 0, true).stream().map(f -> f.getId()).collect(Collectors.toSet());
updateCities(f -> true);
}

public void updateCities(Predicate<DBNation> fetchNation) throws IOException, ParseException {
Set<Integer> nationIds = getNations(false, 0, true).stream().filter(fetchNation).map(DBNation::getId).collect(Collectors.toSet());
if (nationIds.isEmpty()) return;
Locutus.imp().getNationDB().updateCitiesOfNations(nationIds, true, Event::post);
}

Expand Down Expand Up @@ -1106,10 +1119,11 @@ public List<BankDB.TaxDeposit> updateTaxes(Long afterDate) {
return taxes;
}

public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> getResourcesNeeded(Collection<DBNation> nations, double daysDefault, boolean useExisting, boolean force) throws IOException {
Map<DBNation, Map<ResourceType, Double>> existing;
public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> getResourcesNeeded(Collection<DBNation> nations, Map<DBNation, Map<ResourceType, Double>> existing, double daysDefault, boolean useExisting, boolean force) throws IOException {
if (useExisting) {
existing = getMemberStockpile();
if (existing == null) {
existing = getMemberStockpile(f -> nations.contains(f));
}
} else {
existing = new HashMap<>();
for (DBNation nation : nations) {
Expand Down Expand Up @@ -1188,9 +1202,9 @@ public int updateTaxesLegacy(Long latestDate) {
return count;
}

public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> calculateDisburse(Collection<DBNation> nations, double daysDefault, boolean useExisting, boolean ignoreInactives, boolean allowBeige, boolean noDailyCash, boolean noCash, boolean force) throws IOException, ExecutionException, InterruptedException {
public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> calculateDisburse(Collection<DBNation> nations, Map<DBNation, Map<ResourceType, Double>> cachedStockpilesorNull, double daysDefault, boolean useExisting, boolean ignoreInactives, boolean allowBeige, boolean noDailyCash, boolean noCash, boolean force) throws IOException, ExecutionException, InterruptedException {
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> nationResourcesNeed;
nationResourcesNeed = getResourcesNeeded(nations, daysDefault, useExisting, force);
nationResourcesNeed = getResourcesNeeded(nations, cachedStockpilesorNull, daysDefault, useExisting, force);

Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> toSend = new HashMap<>();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/link/locutus/discord/db/guild/GuildKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ public String DELEGATE_SERVER(@Me GuildDB db, @Me User user, Guild guild) {
@Override
public Map.Entry<Integer, Long> validate(GuildDB db, Map.Entry<Integer, Long> ids) {
if (db.getOrNull(ALLIANCE_ID) != null) {
throw new IllegalArgumentException("You cannot delegate a server with an alliance id set");
throw new IllegalArgumentException("You cannot set a delegate a server when this server has `ALLIANCE_ID` set. Remove `DELEGATE_SERVER` or `ALLIANCE_ID` first");
}
Guild guild = Locutus.imp().getDiscordApi().getGuildById(ids.getValue());
if (guild == null)
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/link/locutus/discord/pnw/AllianceList.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,30 @@ public AllianceList subList(Collection<DBNation> nations) {
}

public Map<DBNation, Map<ResourceType, Double>> getMemberStockpile() throws IOException {
return getMemberStockpile(f -> true);
}

public Map<DBNation, Map<ResourceType, Double>> getMemberStockpile(Predicate<DBNation> fetchNation) throws IOException {
Map<DBNation, Map<ResourceType, Double>> result = new LinkedHashMap<>();
for (DBAlliance alliance : getAlliances()) {
result.putAll(alliance.getMemberStockpile());
result.putAll(alliance.getMemberStockpile(fetchNation));
}
return result;
}

public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> getResourcesNeeded(Collection<DBNation> nations, double daysDefault, boolean useExisting, boolean force) throws IOException {
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> result = new LinkedHashMap<>();
for (DBAlliance alliance : getAlliances()) {
result.putAll(alliance.getResourcesNeeded(nations, daysDefault, useExisting, force));
result.putAll(alliance.getResourcesNeeded(nations, null, daysDefault, useExisting, force));
}
return result;
}

public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> calculateDisburse(Collection<DBNation> nations, double daysDefault, boolean useExisting, boolean ignoreInactives, boolean allowBeige, boolean noDailyCash, boolean noCash, boolean force) throws IOException, ExecutionException, InterruptedException {
public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> calculateDisburse(Collection<DBNation> nations, Map<DBNation, Map<ResourceType, Double>> cachedStockpilesorNull, double daysDefault, boolean useExisting, boolean ignoreInactives, boolean allowBeige, boolean noDailyCash, boolean noCash, boolean force) throws IOException, ExecutionException, InterruptedException {
Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> result = new LinkedHashMap<>();
for (DBAlliance alliance : getAlliances()) {
try {
alliance.updateCities();
alliance.updateCities(nations::contains);
} catch (ParseException e) {
for (DBNation nation : nations) {
if (nation.getAlliance_id() == alliance.getAlliance_id()) {
Expand All @@ -169,7 +173,7 @@ public Map<DBNation, Map.Entry<OffshoreInstance.TransferStatus, double[]>> calcu

continue;
}
result.putAll(alliance.calculateDisburse(nations, daysDefault, useExisting, ignoreInactives, allowBeige, noDailyCash, noCash, force));
result.putAll(alliance.calculateDisburse(nations, cachedStockpilesorNull, daysDefault, useExisting, ignoreInactives, allowBeige, noDailyCash, noCash, force));
}
return result;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/link/locutus/discord/pnw/json/CityBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public class CityBuild {
private transient Double commerce;
private transient Boolean powered;

private transient Integer city_id;

public void setCity_id(Integer city_id) {
this.city_id = city_id;
}

public Integer getCity_id() {
return city_id;
}

public CityBuild() {
infraNeeded = 0;
impTotal = 0;
Expand Down

0 comments on commit 1155109

Please sign in to comment.