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 Jul 24, 2024
2 parents 14c299f + eddc597 commit 78b8c00
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,9 @@ public static NationOrAllianceOrGuildOrTaxid nationOrAllianceOrGuildOrTaxId(Stri
return selfDb;
}
}
boolean allowDeleted = data != null && data.getAnnotation(AllowDeleted.class) != null;
try {
return nationOrAlliance(input, data);
return nationOrAlliance(data, input, allowDeleted);
} catch (IllegalArgumentException ignore) {
if (includeTaxId && !input.startsWith("#") && input.contains("tax_id")) {
int taxId = PW.parseTaxId(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2655,7 +2655,7 @@ public void largest(Map<ResourceType, Double> total, Map<ResourceType, Double> l

@Command(desc = "Get a sheet of in-game transfers for nations")
@RolePermission(value = Roles.ECON)
public String getIngameNationTransfers(@Me IMessageIO channel, @Me GuildDB db, Set<NationOrAlliance> senders, Set<NationOrAlliance> receivers, @Arg("Only transfers after timeframe") @Default("%epoch%") @Timestamp long timeframe, @Switch("s") SpreadSheet sheet) throws IOException, GeneralSecurityException {
public String getIngameNationTransfers(@Me IMessageIO channel, @Me GuildDB db, @AllowDeleted Set<NationOrAlliance> senders, @AllowDeleted Set<NationOrAlliance> receivers, @Arg("Only transfers after timeframe") @Default("%epoch%") @Timestamp long timeframe, @Switch("s") SpreadSheet sheet) throws IOException, GeneralSecurityException {
if (sheet == null) sheet = SpreadSheet.create(db, SheetKey.BANK_TRANSACTION_SHEET);
Set<Long> senderIds = senders.stream().map(NationOrAllianceOrGuild::getIdLong).collect(Collectors.toSet());
Set<Long> receiverIds = receivers.stream().map(NationOrAllianceOrGuild::getIdLong).collect(Collectors.toSet());
Expand Down Expand Up @@ -2800,7 +2800,7 @@ public String convertNegativeDeposits(@Me IMessageIO channel, @Me GuildDB db, @M
})
@RolePermission(value = Roles.ECON)
public String getNationsInternalTransfers(@Me IMessageIO channel, @Me GuildDB db,
Set<DBNation> nations,
@AllowDeleted Set<DBNation> nations,
@Arg(value = "Only list transfers after this time", group = 0)
@Timestamp @Default Long startTime,
@Arg(value = "Only list transfers before this time", group = 0)
Expand All @@ -2826,7 +2826,11 @@ public String getNationsInternalTransfers(@Me IMessageIO channel, @Me GuildDB db

@Command(desc = "Get a sheet of transfers")
@RolePermission(value = Roles.ECON, root = true)
public String getIngameTransactions(@Me IMessageIO channel, @Me GuildDB db, @Default NationOrAlliance sender, @Default NationOrAlliance receiver, @Default NationOrAlliance banker, @Default("%epoch%") @Timestamp long timeframe, @Switch("s") SpreadSheet sheet) throws GeneralSecurityException, IOException {
public String getIngameTransactions(@Me IMessageIO channel, @Me GuildDB db,
@AllowDeleted @Default NationOrAlliance sender,
@AllowDeleted @Default NationOrAlliance receiver,
@AllowDeleted @Default NationOrAlliance banker,
@Default("%epoch%") @Timestamp long timeframe, @Switch("s") SpreadSheet sheet) throws GeneralSecurityException, IOException {
if (sheet == null) sheet = SpreadSheet.create(db, SheetKey.BANK_TRANSACTION_SHEET);
List<Transaction2> transactions = Locutus.imp().getBankDB().getAllTransactions(sender, receiver, banker, timeframe, null);
if (transactions.size() > 10000) return "Timeframe is too large, please use a shorter period";
Expand All @@ -2841,7 +2845,7 @@ public String getIngameTransactions(@Me IMessageIO channel, @Me GuildDB db, @Def
})
@RolePermission(value = Roles.ECON)
public String transactions(@Me IMessageIO channel, @Me GuildDB db, @Me User user,
NationOrAllianceOrGuildOrTaxid nationOrAllianceOrGuild,
@AllowDeleted NationOrAllianceOrGuildOrTaxid nationOrAllianceOrGuild,
@Arg(value = "Only show transactions after this time", group = 0)
@Default("%epoch%") @Timestamp long timeframe,
@Arg(value = "Do NOT include the tax record resources below the internal tax rate\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public String title(@Me User user, @Me IMessageIO io, @Me Guild guild, Message d
return null;
}

@Command(desc = "Set the description of an embed from this bot")
@Command(desc = "Set the description of an embed from this bot\n" +
"Use backslash n for newline and `{description}` to include the existing description")
@RolePermission(Roles.INTERNAL_AFFAIRS)
public String description(@Me User user, @Me IMessageIO io, @Me Guild guild, Message discMessage, String description) {
checkMessagePerms(user, guild, discMessage);
Expand All @@ -113,7 +114,12 @@ public String description(@Me User user, @Me IMessageIO io, @Me Guild guild, Mes
MessageEmbed embed = embeds.get(0);

EmbedBuilder builder = new EmbedBuilder(embed);
builder.setDescription(description.replace("\\n", "\n"));
description = description.replace("\\n", "\n");
String existing = embed.getDescription();
if (existing != null && description.contains("{description}")) {
description = description.replace("{description}", existing);
}
builder.setDescription(description);

message.clearEmbeds();
message.embed(builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public String renameInterviewChannels(@Me GuildDB db, @Me Guild guild, @Me User
} else if (!allow_vm && nation.getVm_turns() > 0) {
warnings.put(channel, "Nation is a VM: " + nation.getMarkdownUrl() + " (ignore with `allow_vm: True`");
}
if (channel.getTopic().isEmpty()) {
String topic = channel.getTopic();
if (topic == null || channel.getTopic().isEmpty()) {
String newTopic = nation.getUrl();
RateLimitUtil.queue(channel.getManager().setTopic(newTopic));
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/link/locutus/discord/db/NationDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import link.locutus.discord.Locutus;
import link.locutus.discord.apiv1.domains.subdomains.SNationContainer;
import link.locutus.discord.apiv1.enums.city.building.MilitaryBuilding;
import link.locutus.discord.apiv3.csv.DataDumpParser;
import link.locutus.discord.apiv3.csv.file.CitiesFile;
import link.locutus.discord.apiv3.csv.file.NationsFile;
Expand Down Expand Up @@ -1840,8 +1841,23 @@ public void markDirtyIncorrectNations(boolean score, boolean cities) {
for (DBNation nation : getNationsMatching(f -> f.getVm_turns() == 0)) {
if (score && Math.round(100 * (nation.estimateScore() - nation.getScore())) != 0) {
dirtyNations.add(nation.getNation_id());
} else if (cities && nation.getCities() != getCitiesV3(nation.getNation_id()).size()) {
dirtyNations.add(nation.getNation_id());
} else {
Map<Integer, DBCity> cityMap = getCitiesV3(nation.getNation_id());
if (cities && nation.getCities() != cityMap.size()) {
dirtyNations.add(nation.getNation_id());
} else {
for (MilitaryUnit unit : new MilitaryUnit[]{MilitaryUnit.SOLDIER, MilitaryUnit.TANK, MilitaryUnit.AIRCRAFT, MilitaryUnit.SHIP}) {
MilitaryBuilding building = unit.getBuilding();
int unitCap = 0;
for (DBCity city : cityMap.values()) {
unitCap += city.getBuilding(building) * building.getUnitCap() ;
}
if (nation.getUnits(unit) > unitCap) {
dirtyNations.add(nation.getNation_id());
break;
}
}
}
}
}
int added = dirtyNations.size() - originalSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public String getCommandString() {
expiryOrZero == 0 ? null : TimeUtil.secToTime(TimeUnit.MILLISECONDS, expiryOrZero),
decayOrZero == 0 ? null : TimeUtil.secToTime(TimeUnit.MILLISECONDS, decayOrZero),
allowIgnore ? "true" : null,
repeatable_time < 0 ? null : repeatable_time == 0 ? "0" : TimeUtil.secToTime(TimeUnit.MILLISECONDS, repeatable_time));
repeatable_time <= 0 ? null : TimeUtil.secToTime(TimeUnit.MILLISECONDS, repeatable_time));
}

public abstract String getCommandString(String name, String allowedRecipients, String econRole, String selfRole, String bracket, String useReceiverBracket, String maxTotal, String maxDay, String maxGranterDay, String maxGranterTotal, String allowExpire, String allowDecay, String allowIgnore, String repeatable);
Expand Down Expand Up @@ -407,17 +407,19 @@ public Boolean apply(DBNation nation) {
}
}));
}

if (template == null || template.repeatable_time <= 0) {
// check nation not received grant already
list.add(new Grant.Requirement("Nation must NOT receive a grant template twice (when `repeatable: 0`)", false, new Function<DBNation, Boolean>() {
@Override
public Boolean apply(DBNation nation) {
if (template == null) return true;
return db.getGrantTemplateManager().getRecordsByReceiver(nation.getId(), template.getName()).isEmpty();
list.add(new Grant.Requirement("Nation must NOT receive a grant template twice (when `repeatable: false`)", false, new Function<DBNation, Boolean>() {
@Override
public Boolean apply(DBNation nation) {
if (template == null) return true;
List<GrantTemplateManager.GrantSendRecord> records = db.getGrantTemplateManager().getRecordsByReceiver(nation.getId(), template.getName());
if (template.repeatable_time <= 0) {
return records.isEmpty();
}
}));
}
long cutoff = System.currentTimeMillis() - template.repeatable_time;
records.removeIf(f -> f.date <= cutoff);
return records.isEmpty();
}
}));

// errors.computeIfAbsent(nation, f -> new ArrayList<>()).add("Nation was not found in guild");
list.add(new Grant.Requirement("Nation must be verified: " + CM.register.cmd.toSlashMention(), false, new Function<DBNation, Boolean>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import link.locutus.discord.db.entities.DBCity;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.db.entities.MMRInt;
import link.locutus.discord.db.entities.Transaction2;
import link.locutus.discord.pnw.json.CityBuild;
import link.locutus.discord.util.TimeUtil;
import link.locutus.discord.util.offshore.Grant;
Expand Down Expand Up @@ -307,10 +308,29 @@ public Boolean apply(DBNation receiver) {
list.add(new Grant.Requirement("Nation must NOT have received a new city build grant (when `repeatable: False`)", false, new Function<DBNation, Boolean>() {
@Override
public Boolean apply(DBNation receiver) {
if (template == null || template.getRepeatable() >= 0) return true;
if (template.allow_switch_after_offensive || template.allow_switch_after_infra || template.allow_switch_after_land_or_project) return true;

if (template == null || db == null) return true;
// if (template.allow_switch_after_offensive || template.allow_switch_after_infra || template.allow_switch_after_land_or_project) return true;
List<GrantTemplateManager.GrantSendRecord> records = db.getGrantTemplateManager().getRecordsByReceiver(receiver.getId());
long repeatable = template.getRepeatable();
if (repeatable > 0) {
long cutoff = System.currentTimeMillis() - repeatable;
records.removeIf(f -> f.date < cutoff);
}
long lastLand = 0;
long lastProject = 0;
long lastInfra = 0;
// get transfers
List<Map.Entry<Integer, Transaction2>> transactions = receiver.getTransactions(db, null, false, false, false, -1, 0, true);
for (Map.Entry<Integer, Transaction2> entry : transactions) {
Transaction2 tx = entry.getValue();
if (tx.receiver_id != receiver.getId()) continue;
String note = tx.note;
if (note == null || note.isEmpty()) continue;
if (note.contains("#project")) lastProject = Math.max(lastProject, tx.tx_datetime);
if (note.contains("#land")) lastLand = Math.max(lastLand, tx.tx_datetime);
if (note.contains("#infra")) lastInfra = Math.max(lastInfra, tx.tx_datetime);
}
long lastLandOrProject = Math.max(lastLand, lastProject);

for(GrantTemplateManager.GrantSendRecord record : records) {
if (template.allow_switch_after_days > 0 && record.date < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(template.allow_switch_after_days)) {
Expand All @@ -319,6 +339,12 @@ public Boolean apply(DBNation receiver) {
if (receiver.getCitiesSince(record.date) == 0 && record.grant_type == TemplateTypes.BUILD) {
return false;
}
if (template.allow_switch_after_infra && record.grant_type == TemplateTypes.INFRA && record.date > lastInfra) {
return false;
}
if (template.allow_switch_after_land_or_project && (record.grant_type == TemplateTypes.LAND || record.grant_type == TemplateTypes.PROJECT) && record.date > lastLandOrProject) {
return false;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CityTemplate(GuildDB db, boolean isEnabled, String name, NationFilter nat

// create new constructor with typed parameters instead of resultset
public CityTemplate(GuildDB db, boolean isEnabled, String name, NationFilter nationFilter, long econRole, long selfRole, int fromBracket, boolean useReceiverBracket, int maxTotal, int maxDay, int maxGranterDay, int maxGranterTotal, long dateCreated, int min_city, int max_city, long expiryOrZero, long decayOrZero, boolean allowIgnore) {
super(db, isEnabled, name, nationFilter, econRole, selfRole, fromBracket, useReceiverBracket, maxTotal, maxDay, maxGranterDay, maxGranterTotal, dateCreated, expiryOrZero, decayOrZero, allowIgnore, 0);
super(db, isEnabled, name, nationFilter, econRole, selfRole, fromBracket, useReceiverBracket, maxTotal, maxDay, maxGranterDay, maxGranterTotal, dateCreated, expiryOrZero, decayOrZero, allowIgnore, -1);
this.min_city = min_city;
this.max_city = max_city;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ProjectTemplate(GuildDB db, boolean isEnabled, String name, NationFilter

// create new constructor with typed parameters instead of resultset
public ProjectTemplate(GuildDB db, boolean isEnabled, String name, NationFilter nationFilter, long econRole, long selfRole, int fromBracket, boolean useReceiverBracket, int maxTotal, int maxDay, int maxGranterDay, int maxGranterTotal, long dateCreated, Project project, long expiryOrZero, long decayOrZero, boolean allowIgnore) {
super(db, isEnabled, name, nationFilter, econRole, selfRole, fromBracket, useReceiverBracket, maxTotal, maxDay, maxGranterDay, maxGranterTotal, dateCreated, expiryOrZero, decayOrZero, allowIgnore, 0);
super(db, isEnabled, name, nationFilter, econRole, selfRole, fromBracket, useReceiverBracket, maxTotal, maxDay, maxGranterDay, maxGranterTotal, dateCreated, expiryOrZero, decayOrZero, allowIgnore, -1);
this.project = project;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String getCommandString(String name, String allowedRecipients, String eco
allowExpire).decayTime(
allowDecay).allowIgnore(
allowIgnore).nonRepeatable(
getRepeatable() < 0 ? null : getRepeatable() == 0 ? "0" : TimeUtil.secToTime(TimeUnit.MILLISECONDS, getRepeatable())).toString();
getRepeatable() <= 0 ? null : TimeUtil.secToTime(TimeUnit.MILLISECONDS, getRepeatable())).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String getCommandString(String name, String allowedRecipients, String eco
allowExpire).decayTime(
allowDecay).allowIgnore(
allowIgnore).nonRepeatable(
getRepeatable() < 0 ? null : getRepeatable() == 0 ? "0" : TimeUtil.secToTime(TimeUnit.MILLISECONDS, getRepeatable())).toString();
getRepeatable() <= 0 ? null : TimeUtil.secToTime(TimeUnit.MILLISECONDS, getRepeatable())).toString();
}

@Override
Expand Down

0 comments on commit 78b8c00

Please sign in to comment.