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 Feb 28, 2025
2 parents c9ed487 + 2cb9482 commit ecd2458
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class AProject implements Project {
private final int maxCities;
private String name;
private int index;
private boolean disabled;

public AProject(int id, String apiName, String imageName, Map<ResourceType, Double> cost, ResourceType output, int requiredCities, int maxCities, Supplier<Project[]> reqProjects, Predicate<DBNation> otherRequirements) {
this.id = id;
Expand All @@ -36,6 +37,11 @@ public AProject(int id, String apiName, String imageName, Map<ResourceType, Doub
this.otherRequirements = otherRequirements;
}

public AProject disable() {
this.disabled = true;
return this;
}

@Override
public Set<Project> requiredProjects() {
return reqProjects == null ? Collections.emptySet() : new LinkedHashSet<>(Arrays.asList(reqProjects.get()));
Expand Down Expand Up @@ -113,4 +119,9 @@ public boolean hasLegacy(long bitMask) {
public int getLegacyIndex() {
return index;
}

@Override
public boolean isDisabled() {
return disabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,6 @@ default String getImageUrl() {
String name = getImageName();
return "" + Settings.INSTANCE.PNW_URL() + "/img/projects/" + name + (name.contains(".") ? "" : ".jpg");
}

boolean isDisabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Projects {
.cost(FOOD, 500000)
.cost(URANIUM, 24000)
.requiredCities(16)
.build();
.build().disable();

public static final Project ARMS_STOCKPILE = new Builder("arms_stockpile", 2)
.image("armsstockpile.png")
Expand Down Expand Up @@ -64,7 +64,7 @@ public class Projects {
.cost(FOOD, 250000)
.cost(URANIUM, 12000)
.requiredCities(11)
.build();
.build().disable();

public static final Project EMERGENCY_GASOLINE_RESERVE = new Builder("emergency_gasoline_reserve", 3)
.image("emergencygasolinereserve.png")
Expand Down Expand Up @@ -297,7 +297,7 @@ public Project[] get() {
.cost(URANIUM, 36000)
.requiredCities(21)
.requiredProjects(() -> new Project[]{URBAN_PLANNING, ADVANCED_URBAN_PLANNING})
.build();
.build().disable();

public static final Project MILITARY_SALVAGE = new Builder("military_salvage", 31)
.cost(MONEY, 20000000)
Expand Down Expand Up @@ -463,7 +463,7 @@ public Builder output(ResourceType output) {
return this;
}

public Project build() {
public AProject build() {
return new AProject(id, apiName, imageName, resources, output, requiredCities, maxCities, requiredProjects, otherRequirements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ public static Map<String, String> parseArguments(Set<String> params, String inpu
}

public CommandManager2 registerDefaults() {
getCommands().registerMethod(new AdminCommands(), List.of("admin", "sync2"), "syncCityRefund", "city_refund");

getCommands().registerMethod(new ConflictCommands(), List.of("conflict", "edit"), "addManualWars", "add_none_war");
getCommands().registerMethod(new SettingCommands(), List.of("bank"), "importTransactions", "import_transfers");
getCommands().registerMethod(new AppMenuCommands(), List.of("menu"), "info", "info");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.unimi.dsi.fastutil.objects.*;
import link.locutus.discord.Logg;
import link.locutus.discord.apiv1.enums.Continent;
import link.locutus.discord.apiv3.csv.DataDumpParser;
import link.locutus.discord.commands.manager.v2.binding.Key;
import link.locutus.discord.commands.manager.v2.binding.LocalValueStore;
import link.locutus.discord.commands.manager.v2.binding.ValueStore;
Expand All @@ -20,6 +21,7 @@
import link.locutus.discord.commands.war.WarRoomUtil;
import link.locutus.discord.db.*;
import link.locutus.discord.db.entities.announce.AnnounceType;
import link.locutus.discord.db.entities.nation.DBNationSnapshot;
import link.locutus.discord.gpt.GPTUtil;
import link.locutus.discord.util.*;
import link.locutus.discord.util.task.mail.AlertMailTask;
Expand Down Expand Up @@ -112,6 +114,30 @@
import static link.locutus.discord.commands.manager.v2.binding.annotation.Kw.*;

public class AdminCommands {
@Command(desc = "Sync city refund data")
@RolePermission(Roles.ADMIN)
public String syncCityRefund() throws IOException, ParseException {
Set<DBNation> toSave = new HashSet<>();

DataDumpParser snapshot = Locutus.imp().getDataDumper(true);
for (Map.Entry<Integer, DBNationSnapshot> entry : snapshot.getNations(TimeUtil.getDay()).entrySet()) {
DBNationSnapshot nation = entry.getValue();
double reduction = nation._costReduction();
if (reduction <= 0) continue;
DBNation real = DBNation.getById(entry.getKey());
if (real == null) continue;
if (real.getCities() > nation.getCities()) {
reduction -= PW.City.cityCost(real, nation.getCities(), real.getCities());
}
if (reduction <= 0) continue;
real.edit().setCostReduction(reduction);
toSave.add(real);
}
System.out.println("Saving " + toSave.size());
Locutus.imp().getNationDB().saveNations(toSave);
return "Updated and saved " + toSave.size() + " nations";

}

@Command(desc = "Sync and debug war rooms",
keywords = {WAR, ROOM, SYNC, CHANNEL, UPDATE, WARCAT, CATEGORY})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,7 @@ public String ProjectSheet(@Me IMessageIO io, @Me GuildDB db, NationList nations

for (int i = 0; i < Projects.values.length; i++) {
Project project = Projects.values[i];
if (project.isDisabled()) continue;
header.set(5 + i, nation.hasProject(project) + "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,15 @@ public String auto(ValueStore store, @Me GuildDB db, SpreadSheet sheet, @Switch(
selection = SheetBindings.selectionAlias(true, manager, store, tabName);
} catch (IllegalArgumentException e) {
int index = tabName.indexOf(":");
int barIndex = tabName.indexOf(" | ");
if (index == -1) {
errors.add(TAB_TYPE.replace("{tab_name}", tabName) + " (1)");
continue;
}
String typeStr = tabName.substring(0, index);
if (barIndex < index && barIndex != -1) {
typeStr = typeStr.substring(barIndex + 3);
}
String typeModifier = null;
if (typeStr.contains("(")) {
int end = typeStr.indexOf(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ public String grantCity(
@Arg(value = "The mode for escrowing funds (e.g. if the receiver is blockaded)\nDefaults to never", group = 3) @Switch("em") EscrowMode escrow_mode,

@Arg(value = "Apply the specified domestic policy for determining cost", group = 4) @Switch("md") Boolean manifest_destiny,
@Arg(value = "Apply the specified project for determining cost", group = 4) @Switch("up") Boolean urban_planning,
@Arg(value = "Apply the specified project for determining cost", group = 4) @Switch("aup") Boolean advanced_urban_planning,
@Arg(value = "Apply the specified project for determining cost", group = 4) @Switch("mp") Boolean metropolitan_planning,
@Arg(value = "Apply the specified project for determining cost", group = 4) @Switch("gsa") Boolean gov_support_agency,
@Arg(value = "Apply the specified project for determining cost", group = 4) @Switch("bda") Boolean domestic_affairs,
@Switch("er") boolean exclude_city_refund,

@Switch("b") boolean bypass_checks,
@Switch("f") boolean force
Expand All @@ -107,14 +105,23 @@ public String grantCity(
}
DepositType.DepositTypeInfo note = DepositType.CITY.withAmount(currentCity + numBuy);
double cost = PW.City.cityCost(currentCity, currentCity + numBuy, manifest_destiny != null ? manifest_destiny : receiver.getDomesticPolicy() == DomesticPolicy.MANIFEST_DESTINY,
urban_planning != null ? urban_planning : receiver.hasProject(Projects.URBAN_PLANNING),
advanced_urban_planning != null ? advanced_urban_planning : receiver.hasProject(Projects.ADVANCED_URBAN_PLANNING),
metropolitan_planning != null ? metropolitan_planning : receiver.hasProject(Projects.METROPOLITAN_PLANNING),
receiver.hasProject(Projects.URBAN_PLANNING),
receiver.hasProject(Projects.ADVANCED_URBAN_PLANNING),
receiver.hasProject(Projects.METROPOLITAN_PLANNING),
gov_support_agency != null ? gov_support_agency : receiver.hasProject(Projects.GOVERNMENT_SUPPORT_AGENCY),
domestic_affairs != null ? domestic_affairs : receiver.hasProject(Projects.BUREAU_OF_DOMESTIC_AFFAIRS)
);

String append = "";
if (!exclude_city_refund) {
double refund = receiver.getCityRefund();
if (refund > 0) {
cost = Math.max(0, cost - refund);
append = " (using $" + MathMan.format(refund) + " from your city project refund)";
}
}
double[] resources = ResourceType.MONEY.toArray(cost);
grant.setInstructions("Go to <" + Settings.INSTANCE.PNW_URL() + "/city/create/> and purchase " + numBuy + " cities");
grant.setInstructions("Go to <" + Settings.INSTANCE.PNW_URL() + "/city/create/> and purchase " + numBuy + " cities" + append);
grant.setCost(f -> resources).setType(note);
return null;
}, DepositType.CITY, receiver -> {
Expand Down Expand Up @@ -3024,7 +3031,6 @@ public String costBulk(@Me GuildDB db, @Me IMessageIO io,
@Switch("l") Integer land_level,
@Switch("d") @Arg("Force the use of the provided policies for cost reduction") Set<DomesticPolicy> force_policy,
@Switch("fp") @Arg("These projects are not purchased but are included for cost reduction calculations") Set<Project> force_projects,
@Switch("nc") @Arg("Use the new city cost formula") boolean new_city_formula,
@Switch("er") boolean exclude_city_refund,
@Switch("s") SpreadSheet sheet
) throws GeneralSecurityException, IOException {
Expand Down Expand Up @@ -3067,7 +3073,7 @@ public String costBulk(@Me GuildDB db, @Me IMessageIO io,
for (DBNation nation : receivers) {
int citiesPurchased = 0;
double cityCost = 0;
double costReduction = new_city_formula ? PW.City.getCostReduction(nation::hasProject) : 0d;
double costReduction = nation.getCityRefund();

if (cities != null) {
citiesPurchased = cities_up_to ? Math.max(0, cities - nation.getCities()) : cities;
Expand All @@ -3081,11 +3087,7 @@ public String costBulk(@Me GuildDB db, @Me IMessageIO io,
boolean metPlanning = nation.hasProject(Projects.METROPOLITAN_PLANNING) || (force_projects.contains(Projects.METROPOLITAN_PLANNING) && city >= Projects.METROPOLITAN_PLANNING.requiredCities());
boolean govSupportAgency = nation.hasProject(Projects.GOVERNMENT_SUPPORT_AGENCY) || force_projects.contains(Projects.GOVERNMENT_SUPPORT_AGENCY);
boolean domesticAffairs = nation.hasProject(Projects.BUREAU_OF_DOMESTIC_AFFAIRS) || force_projects.contains(Projects.BUREAU_OF_DOMESTIC_AFFAIRS);
if (new_city_formula) {
cityCost += PW.City.newNextCityCost(city, manifestDestiny, cityPlanning, advCityPlanning, metPlanning, govSupportAgency, domesticAffairs);
} else {
cityCost += PW.City.nextCityCost(city, manifestDestiny, cityPlanning, advCityPlanning, metPlanning, govSupportAgency, domesticAffairs);
}
cityCost += PW.City.nextCityCost(city, manifestDestiny, cityPlanning, advCityPlanning, metPlanning, govSupportAgency, domesticAffairs);
}
}
}
Expand Down Expand Up @@ -3149,8 +3151,8 @@ public String costBulk(@Me GuildDB db, @Me IMessageIO io,

if (!exclude_city_refund) {
double tmp = cityCost;
cityCost = Math.max(0, cityCost - costReduction);
costReduction = Math.max(0, tmp - cityCost);
cityCost = Math.max(1, cityCost - costReduction);
costReduction = Math.max(0, costReduction - tmp);
}

headers.set(0, MarkupUtil.sheetUrl(nation.getNation(), nation.getUrl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,30 +828,20 @@ public void accept(DBWar dbWar, GroupedRankBuilder<Integer, DBWar> builder) {

@Command(desc = "Calculate the costs of purchasing cities (from current to max)", aliases = {"citycost", "citycosts"}, viewable = true)
public String CityCost(@Range(min=1, max=100) int currentCity, @Range(min=1, max=100) int maxCity,
@Default("false") boolean manifestDestiny, @Default("false") boolean urbanPlanning, @Default("false") boolean advancedUrbanPlanning, @Default("false") boolean metropolitanPlanning,
@Default("false") boolean manifestDestiny,
@Default("false") boolean governmentSupportAgency,
@Default("false") boolean domestic_affairs,
@Switch("nc") @Arg("Use the new city cost formula") boolean new_city_formula) {
@Default("false") boolean domestic_affairs) {
if (maxCity > 1000) throw new IllegalArgumentException("Max cities 1000");

double total = 0;

for (int i = Math.max(1, currentCity); i < maxCity; i++) {
if (new_city_formula) {
total += PW.City.newNextCityCost(i, manifestDestiny,
urbanPlanning && i >= Projects.URBAN_PLANNING.requiredCities(),
advancedUrbanPlanning && i >= Projects.ADVANCED_URBAN_PLANNING.requiredCities(),
metropolitanPlanning && i >= Projects.METROPOLITAN_PLANNING.requiredCities(),
governmentSupportAgency,
domestic_affairs);
} else {
total += PW.City.nextCityCost(i, manifestDestiny,
urbanPlanning && i >= Projects.URBAN_PLANNING.requiredCities(),
advancedUrbanPlanning && i >= Projects.ADVANCED_URBAN_PLANNING.requiredCities(),
metropolitanPlanning && i >= Projects.METROPOLITAN_PLANNING.requiredCities(),
governmentSupportAgency,
domestic_affairs);
}
total += PW.City.nextCityCost(i, manifestDestiny,
false,
false,
false,
governmentSupportAgency,
domestic_affairs);
}

return "$" + MathMan.format(total);
Expand Down Expand Up @@ -2560,7 +2550,6 @@ public static String vmHistory(@Me IMessageIO io, @Me GuildDB db, Set<DBNation>
public String allianceCost(@Me IMessageIO channel, @Me GuildDB db,
NationList nations, @Switch("u") boolean update,
@Switch("p") @Arg("Only include the cost of specific projects") Set<Project> includeProjects,
@Switch("nc") @Arg("Use the new city cost formula") boolean new_city_formula,
@Switch("s") @Timestamp Long snapshotDate) {
Set<DBNation> nationSet = PW.getNationsSnapshot(nations.getNations(), nations.getFilter(), snapshotDate, db.getGuild());
double infraCost = 0;
Expand All @@ -2574,11 +2563,10 @@ public String allianceCost(@Me IMessageIO channel, @Me GuildDB db,
cityProjectRefund += PW.City.getCostReduction(nation::hasProject);
Set<Project> projects = nation.getProjects();
for (Project project : projects) {
if (new_city_formula) {
if (project == Projects.URBAN_PLANNING) continue;
if (project == Projects.ADVANCED_URBAN_PLANNING) continue;
if (project == Projects.METROPOLITAN_PLANNING) continue;
}
if (project == Projects.URBAN_PLANNING) continue;
if (project == Projects.ADVANCED_URBAN_PLANNING) continue;
if (project == Projects.METROPOLITAN_PLANNING) continue;

if (includeProjects != null && !includeProjects.contains(project)) continue;
projectCost = ResourceType.addResourcesToA(projectCost, project.cost());
}
Expand All @@ -2595,11 +2583,7 @@ public String allianceCost(@Me IMessageIO channel, @Me GuildDB db,
boolean mp = i > Projects.METROPOLITAN_PLANNING.requiredCities() && projects.contains(Projects.METROPOLITAN_PLANNING);
boolean gsa = projects.contains(Projects.GOVERNMENT_SUPPORT_AGENCY);
boolean bda = projects.contains(Projects.BUREAU_OF_DOMESTIC_AFFAIRS);
if (new_city_formula) {
nationCityCost += PW.City.newNextCityCost(i, manifest, cp, acp, mp, gsa, bda);
} else {
nationCityCost += PW.City.nextCityCost(i, manifest, cp, acp, mp, gsa, bda);
}
nationCityCost += PW.City.nextCityCost(i, manifest, cp, acp, mp, gsa, bda);
}

cityCost += nationCityCost;
Expand Down
Loading

0 comments on commit ecd2458

Please sign in to comment.