Skip to content

Commit

Permalink
feat: add proper responses for troop upgrade failures
Browse files Browse the repository at this point in the history
  • Loading branch information
LooFifteen committed Apr 28, 2024
1 parent f7a532e commit 8689409
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import pink.zak.minestom.towerdefence.queue.QueueFailureReason;
import pink.zak.minestom.towerdefence.storage.MobStorage;
import pink.zak.minestom.towerdefence.ui.ConfirmationUI;
import pink.zak.minestom.towerdefence.upgrade.MobUpgradeFailureReason;
import pink.zak.minestom.towerdefence.upgrade.UpgradeHandler;
import pink.zak.minestom.towerdefence.utils.Result;

Expand Down Expand Up @@ -182,7 +183,14 @@ private void attemptToUnlockMob(@NotNull Player player, @NotNull UpgradeHandler
}

player.openInventory(new ConfirmationUI(player, Component.text("This action will cost you $" + cost + ".", NamedTextColor.RED), result -> {
if (result) upgradeHandler.unlock(mob);
if (result) {
if (upgradeHandler.unlock(mob) instanceof Result.Failure<MobUpgradeFailureReason> failure) {
player.sendMessage(switch (failure.reason()) { // todo: better feedback
case ALREADY_AT_LEVEL -> "You have already unlocked this mob.";
case CANNOT_AFFORD -> "You can not afford to unlock this mob.";
});
}
}
player.openInventory(this);
}, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import pink.zak.minestom.towerdefence.model.mob.config.EnemyMob;
import pink.zak.minestom.towerdefence.model.mob.config.EnemyMobLevel;
import pink.zak.minestom.towerdefence.model.user.GameUser;
import pink.zak.minestom.towerdefence.upgrade.MobUpgradeFailureReason;
import pink.zak.minestom.towerdefence.upgrade.UpgradeHandler;
import pink.zak.minestom.towerdefence.utils.Result;

public final class TroopUpgradeUI extends Inventory {

Expand Down Expand Up @@ -85,7 +87,12 @@ public void onClick(int slot) {
EnemyMobLevel level = this.mob.getLevel(clickedLevel);
if (level == null) return;

this.gameUser.getUpgradeHandler().upgrade(this.mob, level);
if (this.gameUser.getUpgradeHandler().upgrade(this.mob, level) instanceof Result.Failure<MobUpgradeFailureReason> failure) {
this.gameUser.getPlayer().sendMessage(switch (failure.reason()) {
case ALREADY_AT_LEVEL -> "You already have this level!";
case CANNOT_AFFORD -> "You cannot afford this upgrade!";
});
}
this.refresh();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pink.zak.minestom.towerdefence.upgrade;

public enum MobUpgradeFailureReason {

ALREADY_AT_LEVEL,
CANNOT_AFFORD

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pink.zak.minestom.towerdefence.model.mob.config.EnemyMob;
import pink.zak.minestom.towerdefence.model.mob.config.EnemyMobLevel;
import pink.zak.minestom.towerdefence.model.user.GameUser;
import pink.zak.minestom.towerdefence.utils.Result;

public final class UpgradeHandler {

Expand All @@ -36,27 +37,28 @@ public boolean has(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) {
return Optional.ofNullable(this.mobs.get(mob));
}

public boolean unlock(@NotNull EnemyMob mob) {
public @NotNull Result<MobUpgradeFailureReason> unlock(@NotNull EnemyMob mob) {
//noinspection DataFlowIssue there will always be a level 1
return this.upgrade(mob, mob.getLevel(1));
}

public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) {
public @NotNull Result<MobUpgradeFailureReason> upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) {
return this.upgrade(mob, level, false);
}

public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, boolean free) {
public @NotNull Result<MobUpgradeFailureReason> upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, boolean free) {
// get current level
EnemyMobLevel currentLevel = this.getLevel(mob).orElse(null);

// check if the mob is already at the level
if (currentLevel != null && currentLevel.compareTo(level) >= 0) return false;
if (currentLevel != null && currentLevel.compareTo(level) >= 0) return Result.failure(MobUpgradeFailureReason.ALREADY_AT_LEVEL);

if (!free) {
// calculate cost of upgrade
int cost = this.getCost(mob, level);

// check if user can afford upgrade
if (this.user.getCoins() < cost) return false;
if (this.user.getCoins() < cost) return Result.failure(MobUpgradeFailureReason.CANNOT_AFFORD);

// charge user for upgrade
this.user.updateCoins(balance -> balance - cost);
Expand All @@ -72,7 +74,7 @@ public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, bool
level
));

return true;
return Result.success();
}

public int getCost(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) {
Expand Down

0 comments on commit 8689409

Please sign in to comment.