Skip to content

Commit

Permalink
Merge pull request #253 from TownyAdvanced/hotfix/revolt_plunder
Browse files Browse the repository at this point in the history
Fixes plunder under REVOLT siege scenarios
  • Loading branch information
Goosius1 authored Apr 25, 2021
2 parents 7f21a61 + 65520b7 commit 1580960
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/gmail/goosius/siegewar/SiegeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void saveSiege(Siege siege) {
SiegeMetaDataController.setStartTime(town, siege.getStartTime());
SiegeMetaDataController.setEndTime(town, siege.getScheduledEndTime());
SiegeMetaDataController.setActualEndTime(town, siege.getActualEndTime());
SiegeMetaDataController.setAttackerSiegeContributors(town, siege.getAttackerSiegeContributors());
SiegeMetaDataController.setResidentTimedPointContributors(town, siege.getResidentTimedPointContributors());
town.save();
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public static boolean loadSiege(Siege siege) {

siege.setActualEndTime(SiegeMetaDataController.getActualEndTime(town));

siege.setAttackerSiegeContributors(SiegeMetaDataController.getAttackerSiegeContributors(town));
siege.setResidentTimedPointContributors(SiegeMetaDataController.getResidentTimedPointContributors(town));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.exceptions.EconomyException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
Expand Down Expand Up @@ -272,8 +271,6 @@ private void parseSiegeWarNationCommand(Player player, String[] args) {

} catch (TownyException te) {
Messaging.sendErrorMsg(player, te.getMessage());
} catch (EconomyException ee) {
Messaging.sendErrorMsg(player, ee.getMessage());
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ public static void removeSiegeMeta (Town town) {
town.removeMetaData(ldf);
}

public static Map<String, Integer> getAttackerSiegeContributors(Town town) {
StringDataField sdf = (StringDataField) attackerSiegeContributors .clone();
public static Map<String, Integer> getResidentTimedPointContributors(Town town) {
StringDataField sdf = (StringDataField) attackerSiegeContributors.clone();

String dataAsString = null;
if (town.hasMeta(sdf.getKey()))
Expand All @@ -360,7 +360,7 @@ public static Map<String, Integer> getAttackerSiegeContributors(Town town) {
}
}

public static void setAttackerSiegeContributors(Town town, Map<String,Integer> contributorsMap) {
public static void setResidentTimedPointContributors(Town town, Map<String,Integer> contributorsMap) {
StringBuilder mapAsStringBuilder = new StringBuilder();
boolean firstEntry = true;
for(Map.Entry<String,Integer> contributorEntry: contributorsMap.entrySet()) {
Expand All @@ -372,7 +372,7 @@ public static void setAttackerSiegeContributors(Town town, Map<String,Integer> c
mapAsStringBuilder.append(contributorEntry.getKey()).append(":").append(contributorEntry.getValue());
}

StringDataField sdf = (StringDataField) attackerSiegeContributors .clone();
StringDataField sdf = (StringDataField) attackerSiegeContributors.clone();
if (town.hasMeta(sdf.getKey()))
MetaDataUtil.setString(town, sdf, mapAsStringBuilder.toString());
else
Expand Down
42 changes: 19 additions & 23 deletions src/main/java/com/gmail/goosius/siegewar/objects/Siege.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public class Siege {
private int cannonSessionRemainingShortTicks; //Short ticks remaining until standard cannon protections are restored
private int attackerBattlePoints;
private int defenderBattlePoints;
private Set<String> attackerBattleContributors; //UUID's of attackers who contributed during the current battle
private Map<String, Integer> attackerSiegeContributors; //UUID:numContributions map of attackers who contributed during current siege
private Set<String> successfulBattleContributors; //UUID's of residents who contributed during the current battle
private Map<String, Integer> residentTimedPointContributors; //UUID:numContributions map of residents who contributed during current siege

public Siege(Town town) {
this.town = town;
Expand All @@ -80,8 +80,8 @@ public Siege(Town town) {
cannonSessionRemainingShortTicks = 0;
attackerBattlePoints = 0;
defenderBattlePoints = 0;
attackerBattleContributors = new HashSet<>();
attackerSiegeContributors = new HashMap<>();
successfulBattleContributors = new HashSet<>();
residentTimedPointContributors = new HashMap<>();
}

public Town getTown() {
Expand Down Expand Up @@ -389,37 +389,33 @@ public String getFormattedBattleTimeRemaining() {
}
}

public Set<String> getAttackerBattleContributors() {
return attackerBattleContributors;
public Set<String> getSuccessfulBattleContributors() {
return successfulBattleContributors;
}

public void setAttackerBattleContributors(Set<String> attackerBattleContributors) {
this.attackerBattleContributors = attackerBattleContributors;
public void clearSuccessfulBattleContributors() {
successfulBattleContributors.clear();
}

public void clearAttackerBattleContributors() {
attackerBattleContributors.clear();
}
public Map<String, Integer> getAttackerSiegeContributors() {
return attackerSiegeContributors;
public Map<String, Integer> getResidentTimedPointContributors() {
return residentTimedPointContributors;
}

public void setAttackerSiegeContributors(Map<String, Integer> attackerSiegeContributors) {
this.attackerSiegeContributors = attackerSiegeContributors;
public void setResidentTimedPointContributors(Map<String, Integer> residentTimedPointContributors) {
this.residentTimedPointContributors = residentTimedPointContributors;
}

public void registerAttackerBattleContributorsFromBannerControl() {
public void registerSuccessfulBattleContributorsFromBannerControl() {
for(Resident resident: bannerControllingResidents) {
attackerBattleContributors.add(resident.getUUID().toString());
successfulBattleContributors.add(resident.getUUID().toString());
}
}

public void propagateAttackerBattleContributorsToAttackerSiegeContributors() {
for(String playerUuid: attackerBattleContributors) {
if(attackerSiegeContributors.containsKey(playerUuid)) {
attackerSiegeContributors.put(playerUuid, attackerSiegeContributors.get(playerUuid) + 1);
public void propagateSuccessfulBattleContributorsToResidentTimedPointContributors() {
for(String playerUuid: successfulBattleContributors) {
if(residentTimedPointContributors.containsKey(playerUuid)) {
residentTimedPointContributors.put(playerUuid, residentTimedPointContributors.get(playerUuid) + 1);
} else {
attackerSiegeContributors.put(playerUuid, 1);
residentTimedPointContributors.put(playerUuid, 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,8 @@ private static void plunderTown(Siege siege, Town town, Nation nation) {
private static void transferPlunderToNation(Siege siege, Nation nation, double totalPlunderAmount, boolean removeMoneyFromTownBank) throws EconomyException {
Town town = siege.getTown();

String distributionRatio = SiegeWarSettings.getWarSiegePlunderDistributionRatio();

//Calculate total plunder for nation & soldiers
String[] nationSoldierRatios = distributionRatio.split(":");
int nationRatio = Integer.parseInt(nationSoldierRatios[0]);
int soldierRatio = Integer.parseInt(nationSoldierRatios[1]);
int totalRatio = nationRatio + soldierRatio;
double totalPlunderForNation = totalPlunderAmount / totalRatio * nationRatio;
//Calculate plunder ratios for nation & soldiers
double totalPlunderForNation = getTotalPlunderForNationBank(totalPlunderAmount);
double totalPlunderForSoldiers = totalPlunderAmount - totalPlunderForNation;

//Pay nation
Expand All @@ -207,18 +201,10 @@ private static void transferPlunderToNation(Siege siege, Nation nation, double t
}

//Pay soldiers
Resident resident;
Map<Resident, Integer> residentSharesMap = new HashMap<>();
for(Map.Entry<String, Integer> uuidShareMapEntry: siege.getAttackerSiegeContributors().entrySet()) {
resident = TownyUniverse.getInstance().getResident(UUID.fromString(uuidShareMapEntry.getKey()));
if(resident != null) {
residentSharesMap.put(resident, uuidShareMapEntry.getValue());
}
}
boolean soldiersPaid = SiegeWarMoneyUtil.distributeMoneyAmongSoldiers(
totalPlunderForSoldiers,
town,
residentSharesMap,
gatherResidentsShareMap(siege),
"Plunder",
removeMoneyFromTownBank);

Expand All @@ -231,4 +217,23 @@ private static void transferPlunderToNation(Siege siege, Nation nation, double t
}
}
}

private static Map<Resident, Integer> gatherResidentsShareMap(Siege siege) {
Resident resident;
Map<Resident, Integer> residentSharesMap = new HashMap<>();
for(Map.Entry<String, Integer> uuidShareMapEntry: siege.getResidentTimedPointContributors().entrySet()) {
resident = TownyUniverse.getInstance().getResident(UUID.fromString(uuidShareMapEntry.getKey()));
if(resident != null)
residentSharesMap.put(resident, uuidShareMapEntry.getValue());
}
return residentSharesMap;
}

private static double getTotalPlunderForNationBank(double totalPlunderAmount) {
//Calculate amount that will be given to the winning government directly.
String[] ratios = SiegeWarSettings.getWarSiegePlunderDistributionRatio().split(":");
int bankRatio = Integer.parseInt(ratios[0]);
int soldierRatio = Integer.parseInt(ratios[1]);
return totalPlunderAmount / (bankRatio + soldierRatio) * bankRatio;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.gmail.goosius.siegewar.SiegeWar;
import com.gmail.goosius.siegewar.enums.SiegeSide;
import com.gmail.goosius.siegewar.enums.SiegeStatus;
import com.gmail.goosius.siegewar.enums.SiegeType;
import com.gmail.goosius.siegewar.objects.BannerControlSession;
import com.gmail.goosius.siegewar.objects.BattleSession;
import com.gmail.goosius.siegewar.objects.Siege;
Expand Down Expand Up @@ -35,7 +36,7 @@ public class SiegeWarBannerControlUtil {
public static void evaluateBannerControl(Siege siege) {
try {
if(siege.getStatus() == SiegeStatus.IN_PROGRESS) {
evaluateBannerControlEffects(siege);
evaluateBannerControlPoints(siege);
evaluateExistingBannerControlSessions(siege);
evaluateNewBannerControlSessions(siege);
}
Expand Down Expand Up @@ -290,7 +291,7 @@ public void run() {
}
}

private static void evaluateBannerControlEffects(Siege siege) {
private static void evaluateBannerControlPoints(Siege siege) {
if(!BattleSession.getBattleSession().isActive())
return;

Expand All @@ -305,12 +306,15 @@ private static void evaluateBannerControlEffects(Siege siege) {
battlePoints = siege.getBannerControllingResidents().size() * SiegeWarSettings.getWarBattlePointsForAttackerOccupation();
battlePoints = SiegeWarScoringUtil.applyBattlePointsAdjustmentForPopulationQuotient(true, battlePoints, siege);
siege.adjustAttackerBattlePoints(battlePoints);
siege.registerAttackerBattleContributorsFromBannerControl();
if (!siege.getSiegeType().equals(SiegeType.REVOLT))
siege.registerSuccessfulBattleContributorsFromBannerControl();
break;
case DEFENDERS:
battlePoints = siege.getBannerControllingResidents().size() * SiegeWarSettings.getWarBattlePointsForDefenderOccupation();
battlePoints = SiegeWarScoringUtil.applyBattlePointsAdjustmentForPopulationQuotient(false, battlePoints, siege);
siege.adjustDefenderBattlePoints(battlePoints);
if (siege.getSiegeType().equals(SiegeType.REVOLT))
siege.registerSuccessfulBattleContributorsFromBannerControl();
break;
default:
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void evaluateBattleSessions() {
siege.adjustSiegeBalance(battlePointsOfWinner);

//Propagate attacker battle contributions to siege history
siege.propagateAttackerBattleContributorsToAttackerSiegeContributors();
siege.propagateSuccessfulBattleContributorsToResidentTimedPointContributors();

//Prepare result for messaging
battleResults.put(siege, battlePointsOfWinner);
Expand All @@ -83,7 +83,7 @@ public void run() {
siege.clearBannerControlSessions();
siege.setAttackerBattlePoints(0);
siege.setDefenderBattlePoints(0);
siege.clearAttackerBattleContributors();
siege.clearSuccessfulBattleContributors();

//Save siege
SiegeController.saveSiege(siege);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.exceptions.EconomyException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Government;
Expand Down Expand Up @@ -241,13 +240,12 @@ public static double calculateSiegeCost(Town town) {
* @param reason reason for payment
* @param removeMoneyFromTownBank if true, remove money from town
* @return true if money was paid. False if there were no soldiers
* @throws EconomyException
*/
public static boolean distributeMoneyAmongSoldiers(double totalAmountForSoldiers,
Town town,
Map<Resident, Integer> soldierSharesMap,
String reason,
boolean removeMoneyFromTownBank) throws EconomyException {
boolean removeMoneyFromTownBank) {
if(soldierSharesMap.size() == 0)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import com.gmail.goosius.siegewar.Messaging;
import com.gmail.goosius.siegewar.SiegeController;
import com.gmail.goosius.siegewar.TownOccupationController;
import com.gmail.goosius.siegewar.enums.SiegeWarPermissionNodes;
import com.gmail.goosius.siegewar.metadata.TownMetaDataController;
Expand Down

0 comments on commit 1580960

Please sign in to comment.