Skip to content

Commit

Permalink
Feat: Updated playerstatuscontroller and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 11, 2026
1 parent 589f60a commit fe6f10b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import edu.ntnu.idi.idatt2003.g40.mappe.model.Player;
import edu.ntnu.idi.idatt2003.g40.mappe.model.PlayerStatus;

import java.math.BigDecimal;

/**
* Controls and calculates the players' status.
*
Expand All @@ -20,38 +18,22 @@ private PlayerStatusController() {
*
* <p>Calculates what status the player should get
* based on players' net worth compared to starting
* money, and current week.</p>
* money.</p>
*
* @param player the player to give a status to.
*
* @return a PlayerStatus
* */
public static PlayerStatus getStatus(final Player player) {
Integer startingMoneyInt = player.getStartingMoney().intValue();

BigDecimal difference =
player.getNetWorth().subtract(player.getStartingMoney());

return switch ((Integer) difference.intValue()) {
case Integer val when val >= percentAmount(startingMoneyInt, 900) -> PlayerStatus.OMNIPOTENT;
case Integer val when val >= percentAmount(startingMoneyInt, 400) -> PlayerStatus.SEER;
case Integer val when val >= percentAmount(startingMoneyInt, 200) -> PlayerStatus.PRO;
case Integer val when val >= percentAmount(startingMoneyInt, 100) -> PlayerStatus.TRYHARD;
case Integer val when val >= percentAmount(startingMoneyInt, 50) -> PlayerStatus.GOOD;
case Integer val when val >= percentAmount(startingMoneyInt, 20) -> PlayerStatus.NOVICE;
default -> PlayerStatus.NOOB;
};
}
float differenceInPercent = ((player.getNetWorth().floatValue()
/ player.getStartingMoney().floatValue()) - 1) * 100;

/**
* Helper method for increasing a value by x percent.
*
* @param value the value to calculate from
* @param x the percent amount
*
* @return the increased value.
* */
private static Integer percentAmount(final Integer value, final Integer x) {
return (value * x) / 100;
for (int i = PlayerStatus.values().length; i > 0; i--) {
PlayerStatus curStatus = PlayerStatus.values()[i - 1];
if (differenceInPercent >= curStatus.getPercentIncreaseNeeded()) {
return curStatus;
}
}
return PlayerStatus.NOOB;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
package edu.ntnu.idi.idatt2003.g40.mappe.model;

import edu.ntnu.idi.idatt2003.g40.mappe.controller.PlayerStatusController;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class PlayerStatusTest {

/**
* Player object used for testing.
* */
private Player testPlayer;

@BeforeEach
void setUp() {
testPlayer = new Player("Player", new BigDecimal(1000));
}

@Test
void getStatusReturnsProperStatuses() {
Player testPlayer = new Player("Player", new BigDecimal(1000));
PlayerStatus gottenStatus = PlayerStatusController.getStatus(testPlayer);
assertEquals(PlayerStatus.NOOB, gottenStatus);
void getStatusAtStartReturnsWorstStatus() {
assertEquals(PlayerStatus.NOOB, testPlayer.getStatus());
}

@Test
void getStatusAfterDoubleIncreaseReturnsBetterStatus() {
testPlayer.addMoney(new BigDecimal(1000));
PlayerStatus gottenStatus2 = PlayerStatusController.getStatus(testPlayer);
assertEquals(PlayerStatus.TRYHARD, testPlayer.getStatus());
}

@Test
void gettingStatusWhenNegativeDifferenceReturnsWorstStatus() {
testPlayer.addMoney(new BigDecimal(-1000));
assertEquals(PlayerStatus.NOOB, testPlayer.getStatus());
}

@Test
void multipleChangesInValueCauseCorrectStatus() {
testPlayer.addMoney(new BigDecimal(2000));
assertEquals(PlayerStatus.PRO, testPlayer.getStatus());

testPlayer.addMoney(new BigDecimal(-1000));
assertEquals(PlayerStatus.TRYHARD, testPlayer.getStatus());

assertEquals(PlayerStatus.TRYHARD, gottenStatus2);
testPlayer.addMoney(new BigDecimal(-500));
assertEquals(PlayerStatus.GOOD, testPlayer.getStatus());
}
}

0 comments on commit fe6f10b

Please sign in to comment.