Repository navigation
Let staff swap a war's goal and move its earliest battle day #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/java/net/tfminecraft/simplefactions/war/declare/WarGoalChangeService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) { | ||
|
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); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.