public CommandResult execute(Realm realm) {
System.out.println(attacker + " attacks " + defender);
CommandResult commandResult = null;
if (attacker.canAttack()) {
Unit winner = null;
Unit loser = null;
int attackPoints = attacker.getAttackPoints();
System.out.println("Attack points : " + attackPoints);
int defencePoints = defender.getDefencePoints();
System.out.println("Defense points : " + defencePoints);
attackPoints = Utility.increaseByPercent(attackPoints, attackBonusPercentage);
System.out.println("Actual attack points : " + attackPoints);
defenceBonusPercentage = Utility.getCoordinateDefenceBonus(realm, defender.getCoordinate());
defencePoints = Utility.increaseByPercent(defencePoints, defenceBonusPercentage);
System.out.println("Actual defense points : " + defencePoints);
int total = attackPoints + defencePoints;
Random random = new Random(System.currentTimeMillis());
int randomInt = random.nextInt(total);
System.out.println("Total : " + total + " Random : " + randomInt);
if (randomInt < attackPoints) {
System.out.println("Attacker won!");
loser = defender;
winner = attacker;
} else {
System.out.println("Defender won!");
loser = attacker;
winner = defender;
}
winner.setMovementPoints(0);
Executor.getInstance().execute(new RemoveUnitCommand(loser.getPlayer(), loser));
commandResult = new CommandResult(CommandResult.RESULT_OK, "", CommandResult.UNIT_ATTACKED_UPDATE);
commandResult.addParameter(attacker);
commandResult.addParameter(defender);
commandResult.addParameter(winner);
commandResult.addParameter(defender.getCoordinate());