Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@



import net.tfminecraft.simplefactions.war.history.WarHistoryEvents;
import net.tfminecraft.simplefactions.Cache;
import net.tfminecraft.simplefactions.managers.WarManager;
import net.tfminecraft.simplefactions.war.core.War;
Expand Down Expand Up @@ -122,6 +123,36 @@ public static WarScheduleAdminResult lockDay(War war, String dayArg, Instant now
+ AvailabilityCalendar.timeLockAt(day) + " unless you set it with sethour.");
}

/**
* Moves the first date a war in its buildup can be fought on, earlier or later. Players can mark
* dates from it on; the date's day lock must still be ahead, and no battle day may come before it.
*/
public static WarScheduleAdminResult setEarliestDay(War war, String dayArg, Instant now) {
WarScheduleAdminResult invalid = requireScheduling(war);
if (invalid != null) {
return invalid;
}
if (!war.isBuildupFrozen()) {
return WarScheduleAdminResult.error("War " + war.getId() + " is not in its buildup.");
}
LocalDate day = parseDay(dayArg);
if (day == null) {
return WarScheduleAdminResult.error(DAY_FORMAT);
}
if (!now.isBefore(AvailabilityCalendar.dayLockAt(day))) {
return WarScheduleAdminResult.error("That day's vote has already closed.");
}
for (LockedBattleDay locked : war.getAvailability().battleDays()) {
if (locked.date().isBefore(day)) {
return WarScheduleAdminResult.error("Battle day " + locked.date() + " is before that. Unlock it first"
+ " with /war admin schedule " + war.getId() + " unlockday " + locked.date() + ".");
}
}
war.getAvailability().setEarliestBattleDay(day);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
WarHistoryEvents.votesChanged(war);
return WarScheduleAdminResult.ok("War " + war.getId() + " can now be fought from " + day + ".");
}

/** Takes a battle day (yyyy-MM-dd) back. */
public static WarScheduleAdminResult unlockDay(War war, String dayArg) {
WarScheduleAdminResult invalid = requireScheduling(war);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static String describe(String[] args) {
case "truce" -> truce(lower(args[2]));
case "history" -> lower(args[2]).equals("upload-battles") ? WarHistoryAdmin.UPLOAD_RISK : null;
case "forceinstallation" -> WarForceInstallation.risk(args);
case "goal" -> args.length >= 4 && knownWar(args[2])
? "replaces the war's main goal and rebuilds its campaign route; both sides are told." : null;
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import net.tfminecraft.simplefactions.war.history.WarHistoryAdmin;
import net.tfminecraft.simplefactions.war.declare.WarDeclareRequest;
import net.tfminecraft.simplefactions.war.declare.WarDemandRules;
import net.tfminecraft.simplefactions.war.declare.WarGoalChangeService;
import net.tfminecraft.simplefactions.war.campaign.runtime.BattleSideMembers;
import net.tfminecraft.simplefactions.war.notice.WarNotices;
import net.tfminecraft.simplefactions.war.declare.WarValidationResult;

public class WarCommandManager implements CommandExecutor {
Expand All @@ -54,23 +57,24 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (args.length < 1) {
player.sendMessage("§eUsage: §a/war list§e, §a/war vote <war id>§e, §a/war installations <war id>§e, §a/war calls§e, §a/war call accept|decline <war id>");
if (Permissions.isAdmin(sender)) {
player.sendMessage("§7Staff: §e/war admin declare|demand|end|win|status|path|time|schedule|buildup|devmode|raid|reparations|truce|history|factions|forceinstallation §7...");
player.sendMessage("§7Staff: §e/war admin declare|demand|goal|end|win|status|path|time|schedule|buildup|devmode|raid|reparations|truce|history|factions|forceinstallation §7...");
}
return true;
}
return handle(player, args);
}

/** The console may declare wars, set second demands and read a buildup; other subcommands need a player. */
/** The console may declare wars, set demands and goals, and run a buildup; other subcommands need a player. */
private boolean handleConsole(CommandSender sender, String[] args) {
String sub = args.length >= 2 && args[0].equalsIgnoreCase("admin")
? args[1].toLowerCase(java.util.Locale.ROOT) : "";
return switch (sub) {
case "declare" -> handleDeclare(sender, args);
case "demand" -> handleDemand(sender, args);
case "goal" -> handleGoal(sender, args);
case "buildup" -> handleBuildup(sender, args);
default -> {
sender.sendMessage("§cFrom the console: /war admin declare|demand|buildup ...");
sender.sendMessage("§cFrom the console: /war admin declare|demand|goal|buildup ...");
yield true;
}
};
Expand Down Expand Up @@ -151,13 +155,14 @@ private boolean handleAdmin(Player player, String[] rawArgs) {
return true;
}
if (args.length < 2) {
player.sendMessage("§cUsage: /war admin declare|demand|end|win|status|path|time|schedule|buildup|devmode|raid|reparations|truce|history|factions|forceinstallation|losses|army|wounds ...");
player.sendMessage("§cUsage: /war admin declare|demand|goal|end|win|status|path|time|schedule|buildup|devmode|raid|reparations|truce|history|factions|forceinstallation|losses|army|wounds ...");
return true;
}
String subcommand = args[1].toLowerCase(java.util.Locale.ROOT);
return switch (subcommand) {
case "declare" -> handleDeclare(player, args);
case "demand" -> handleDemand(player, args);
case "goal" -> handleGoal(player, args);
case "end" -> handleEnd(player, args);
case "win" -> handleWin(player, args);
case "status" -> handleStatus(player, args);
Expand Down Expand Up @@ -191,7 +196,7 @@ private boolean handleAdmin(Player player, String[] rawArgs) {
yield true;
}
default -> {
player.sendMessage("§cUnknown admin subcommand. Use: declare, demand, end, win, status, path, time, schedule, devmode, raid, reparations, truce, history, factions, forceinstallation, losses, army, wounds");
player.sendMessage("§cUnknown admin subcommand. Use: declare, demand, goal, end, win, status, path, time, schedule, devmode, raid, reparations, truce, history, factions, forceinstallation, losses, army, wounds");
yield true;
}
};
Expand All @@ -202,6 +207,8 @@ private boolean handleAdmin(Player player, String[] rawArgs) {
+ "pillage <settlementId>, change_government <governmentLaw> <leadershipLaw>";
private static final String DECLARE_USAGE =
"§cUsage: /war admin declare <attacker> <defender> [goal] [goal args] " + GOAL_ARGS;
static final String GOAL_USAGE =
"§cUsage: /war admin goal <warId> <goal> [goal args] " + GOAL_ARGS;
static final String DEMAND_USAGE =
"§cUsage: /war admin demand <warId> attacker|defender <goal> [goal args] " + GOAL_ARGS;

Expand Down Expand Up @@ -362,6 +369,41 @@ private boolean handleDemand(CommandSender sender, String[] args) {
return true;
}

/**
* Swaps a war's main goal as staff or from the console, before its first battle. The attacker
* leader demands it of the defender leader under the goal's own rules.
*/
private boolean handleGoal(CommandSender sender, String[] args) {
if (args.length < 4) {
sender.sendMessage(GOAL_USAGE);
return true;
}
War war = WarCommandHelper.parseWarId(args[2]).map(WarManager::getById).orElse(null);
if (war == null || !war.isActive()) {
sender.sendMessage("§cNo active war by that id");
return true;
}
Faction attacker = FactionManager.getByString(war.getAttackerLeaderId());
Faction defender = FactionManager.getByString(war.getDefenderLeaderId());
if (attacker == null || defender == null) {
sender.sendMessage("§cThat war has no leading nation on both sides.");
return true;
}
WarDeclareRequest request = parseGoal(sender, args, 3, attacker, defender, null, GOAL_USAGE);
if (request == null) {
return true;
}
String refusal = WarGoalChangeService.change(war, request);
if (refusal != null) {
sender.sendMessage(refusal);
return true;
}
LogManager.war("ADMIN_GOAL warId=%d by=%s goal=%s", war.getId(), sender.getName(), request.getGoal().toJson());
sender.sendMessage("§aWar §e#" + war.getId() + "§a now has the goal §f"
+ WarDemand.fromRequest(request).describeGoal() + "§a. Its campaign route was rebuilt.");
return true;
}

private boolean handleEnd(Player player, String[] args) {
if (args.length != 3) {
player.sendMessage("§cUsage: /war admin end <warId>");
Expand Down Expand Up @@ -718,7 +760,8 @@ private static String factionName(String factionId) {
"skipday", "sethour", "setscheduled", "battlecreate", "battlestart", "winbattle", "choice", "battlechoice",
"defenderchoice", "pushchoice", "holdchoice");

private static final String BUILDUP_USAGE = "§cUsage: /war admin buildup <warId> status|end|setday <yyyy-MM-dd>";
private static final String BUILDUP_USAGE =
"§cUsage: /war admin buildup <warId> status|end|setday <yyyy-MM-dd>|earliest <yyyy-MM-dd>";

private boolean handleBuildup(CommandSender player, String[] args) {
if (args.length < 4) {
Expand Down Expand Up @@ -754,6 +797,25 @@ private boolean handleBuildup(CommandSender player, String[] args) {
return true;
}
}
case "earliest" -> {
if (args.length < 5) {
player.sendMessage(BUILDUP_USAGE);
return true;
}
WarScheduleAdminResult result = WarScheduleAdminService.setEarliestDay(w, args[4], now);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
player.sendMessage((result.success() ? "§a" : "§c") + result.message());
if (!result.success()) {
return true;
}
String news = "§eWar §f#" + w.getId() + " §ecan now be fought from §f" + args[4].trim()
+ "§e. Mark the days and times you can fight with §f/war vote " + w.getId() + "§e.";
for (String member : BattleSideMembers.collectEligibleMemberNames(w.getAttackers())) {
WarNotices.send(member, news);
}
for (String member : BattleSideMembers.collectEligibleMemberNames(w.getDefenders())) {
WarNotices.send(member, news);
}
}
default -> {
player.sendMessage(BUILDUP_USAGE);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
List<String> completions = new ArrayList<>();
addIfPrefix(completions, args[1], "declare");
addIfPrefix(completions, args[1], "demand");
addIfPrefix(completions, args[1], "goal");
addIfPrefix(completions, args[1], "end");
addIfPrefix(completions, args[1], "win");
addIfPrefix(completions, args[1], "status");
Expand Down Expand Up @@ -107,6 +108,9 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
if (args[1].equalsIgnoreCase("demand")) {
return demandCompletions(args);
}
if (args[1].equalsIgnoreCase("goal")) {
return goalCompletions(args);
}
if (args.length == 4 && args[1].equalsIgnoreCase("reparations")) {
return factionIds(args[3]);
}
Expand Down Expand Up @@ -178,6 +182,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
addIfPrefix(completions, args[3], "status");
addIfPrefix(completions, args[3], "end");
addIfPrefix(completions, args[3], "setday");
addIfPrefix(completions, args[3], "earliest");
return completions;
}
if (args.length == 4 && args[1].equalsIgnoreCase("win")) {
Expand Down Expand Up @@ -254,7 +259,8 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
if (args.length == 5 && args[1].equalsIgnoreCase("schedule") && isDayArgument(args[3])) {
return upcomingDays(args[4]);
}
if (args.length == 5 && args[1].equalsIgnoreCase("buildup") && args[3].equalsIgnoreCase("setday")) {
if (args.length == 5 && args[1].equalsIgnoreCase("buildup")
&& (args[3].equalsIgnoreCase("setday") || args[3].equalsIgnoreCase("earliest"))) {
return upcomingDays(args[4]);
}
if (args.length == 5 && args[1].equalsIgnoreCase("schedule") && args[3].equalsIgnoreCase("sethour")) {
Expand Down Expand Up @@ -321,6 +327,25 @@ private static List<String> declareCompletions(String[] args) {
return List.of();
}

private static List<String> goalCompletions(String[] args) {
if (args.length == 3) {
return activeWarIds(args[2]);
}
if (args.length == 4) {
List<String> completions = new ArrayList<>();
for (WarGoalType goal : WarGoalType.values()) {
if (!goal.isMovementOrigin()) {
addIfPrefix(completions, args[3], goal.toJson());
}
}
return completions;
}
if (args.length == 5 && WarGoalType.fromJson(args[3]) == WarGoalType.TRANSFER_SUBJECT) {
return factionIds(args[4]);
}
return List.of();
}

private static List<String> demandCompletions(String[] args) {
if (args.length == 3) {
return activeWarIds(args[2]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package net.tfminecraft.simplefactions.war.declare;

import net.tfminecraft.simplefactions.managers.LogManager;
import net.tfminecraft.simplefactions.managers.WarManager;
import net.tfminecraft.simplefactions.war.battle.engine.core.BattleManager;
import net.tfminecraft.simplefactions.war.campaign.runtime.BattleSideMembers;
import net.tfminecraft.simplefactions.war.core.War;
import net.tfminecraft.simplefactions.war.core.WarDeclareHelper;
import net.tfminecraft.simplefactions.war.core.WarDemand;
import net.tfminecraft.simplefactions.war.enums.WarGoalType;
import net.tfminecraft.simplefactions.war.enums.WarType;
import net.tfminecraft.simplefactions.war.history.WarHistoryEvents;
import net.tfminecraft.simplefactions.war.notice.WarNotices;

/**
* Staff swap the attacker's main goal of a war before it has a battle day. The new goal passes the
* same goal rules as a declaration of it, the campaign route is rebuilt for it, and both sides are
* told. The buildup and the players' availability marks stay.
*/
public final class WarGoalChangeService {
static final String NOT_ACTIVE = "§cNo active war by that id";
static final String NOT_CHANGEABLE = "§cThis war's goal cannot be changed: raids and civil wars keep theirs.";
static final String LOCKED = "§cA war's goal is locked once its first battle begins.";
static final String HAS_BATTLE_DAY = "§cThis war already has a battle day. Unlock it before changing the goal.";
static final String SAME_AS_SECOND = "§cThat is already the attacker's second demand. Change the second demand first.";
static final String NO_ROUTE = "§cNo campaign route fits that goal, so the war keeps its goal.";

private WarGoalChangeService() {}

/**
* Gives {@code war} the goal in {@code request}, whose attacker and defender are the war's side
* leaders. Returns why it cannot, or null once the goal is changed.
*/
public static String change(War war, WarDeclareRequest request) {
if (war == null || !war.isActive()) {
return NOT_ACTIVE;
}
if (war.getWarType() == WarType.RAID || war.getMovementId() != null || WarDemandRules.isCivilWar(war)) {
return NOT_CHANGEABLE;
}
if (war.hasFirstBattleStarted()) {
return LOCKED;
}
// The route decides where a prepared battle is fought, so a battle day already set keeps the goal.
if (!war.getAvailability().battleDays().isEmpty() || BattleManager.getByWarId(war.getId()) != null) {
return HAS_BATTLE_DAY;
}
// Inside a war both sides already fight, so only the goal's own rules apply, as for a demand.
WarValidationResult validation = new WarGoalValidator().validate(request.asDemand());
if (!validation.isValid()) {
return validation.getMessage();
}
WarDemand goal = WarDemand.fromRequest(request);
if (WarDemandRules.conflicts(goal, war.getSecondDemand(true))) {
return SAME_AS_SECOND;
}
Goal before = Goal.of(war);
Goal.of(request).applyTo(war);
if (!WarManager.regenerateCampaign(war)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// A route that cannot be built leaves the campaign untouched; only the goal goes back.
before.applyTo(war);
return NO_ROUTE;
}
LogManager.war("GOAL_CHANGE warId=%d from=%s to=%s", war.getId(), before.goal().toJson(),
goal.getGoal().toJson());
String news = "§eThe goal of war §f#" + war.getId() + " §e(" + war.getName() + "§e) is now §f"
+ goal.describeGoal() + "§e.";
for (String member : BattleSideMembers.collectEligibleMemberNames(war.getAttackers())) {
WarNotices.send(member, news);
}
for (String member : BattleSideMembers.collectEligibleMemberNames(war.getDefenders())) {
WarNotices.send(member, news);
}
WarHistoryEvents.votesChanged(war);
return null;
}

/** The fields a war keeps for its main goal. */
private record Goal(WarGoalType goal, String titleId, String subjectFactionId, String relationTypeId,
String governmentLawId, String leadershipLawId, String settlementId) {
static Goal of(War war) {
return new Goal(war.getGoal(), war.getTargetTitleId(), war.getSubjectFactionId(), war.getRelationTypeId(),
war.getGovernmentLawId(), war.getLeadershipLawId(), war.getTargetSettlementId());
}

static Goal of(WarDeclareRequest request) {
WarGoalType goal = request.getGoal();
return new Goal(goal,
goal == WarGoalType.DE_JURE_ANNEX ? request.getTargetTitleId() : null,
goal == WarGoalType.TRANSFER_SUBJECT ? request.getSubjectFactionId() : null,
goal == WarGoalType.SUBJUGATE ? request.getRelationTypeId() : null,
goal == WarGoalType.CHANGE_GOVERNMENT ? request.getGovernmentLawId() : null,
goal == WarGoalType.CHANGE_GOVERNMENT ? request.getLeadershipLawId() : null,
goal == WarGoalType.PILLAGE ? request.getTargetSettlementId() : null);
}

void applyTo(War war) {
war.setGoal(goal);
war.setWarType(WarDeclareHelper.warTypeForGoal(goal));
war.setTargetTitleId(titleId);
war.setSubjectFactionId(subjectFactionId);
war.setRelationTypeId(relationTypeId);
war.setGovernmentLawId(governmentLawId);
war.setLeadershipLawId(leadershipLawId);
war.setTargetSettlementId(settlementId);
}
}
}
Loading