Converted some Combat XP testcases from Java to Kotlin

This commit is contained in:
Ziggy
2023-03-30 16:16:03 +02:00
parent 6078649e25
commit 2c8b7ed6c4
14 changed files with 349 additions and 218 deletions
@@ -1,50 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CombatLevelCalculatorTest {
private final CombatLevel combatLevel1 = new CombatLevel(1, 500, 0);
private final CombatLevel combatLevel2 = new CombatLevel(2, 1000, 50);
private final CombatLevel combatLevel3 = new CombatLevel(3, 6000, 75);
private final FakeCombatLevelRepository repository = new FakeCombatLevelRepository();
private final CombatLevelCalculator calculator = new CombatLevelCalculator(repository);
@Test
public void combatLevelIsSelected_whenRequiredXpIsMatchedExactly() {
repository.addCombatLevel(combatLevel1);
repository.addCombatLevel(combatLevel2);
repository.addCombatLevel(combatLevel3);
int exactXpForCombatLevel2 = combatLevel2.getRequiredCombatXp();
CombatLevel combatLevel = calculator.calculate(exactXpForCombatLevel2);
assertEquals(combatLevel2, combatLevel);
}
@Test
public void smallestCombatLevelIsSelected_whenPlayerHasTooLittleXp() {
repository.addCombatLevel(combatLevel1);
repository.addCombatLevel(combatLevel2);
repository.addCombatLevel(combatLevel3);
int notEnoughXpForCombatLevel1 = 10;
CombatLevel combatLevel = calculator.calculate(notEnoughXpForCombatLevel1);
assertEquals(combatLevel1, combatLevel);
}
@Test
public void biggestCombatLevelIsSelected_whenPlayerHasMuchXp() {
repository.addCombatLevel(combatLevel1);
repository.addCombatLevel(combatLevel2);
repository.addCombatLevel(combatLevel3);
int muchXp = 100_000;
CombatLevel combatLevel = calculator.calculate(muchXp);
assertEquals(combatLevel3, combatLevel);
}
}
@@ -0,0 +1,68 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class CombatLevelCalculatorTest {
private val combatLevel1 = CombatLevel(1, 500, 0)
private val combatLevel2 = CombatLevel(2, 1000, 50)
private val combatLevel3 = CombatLevel(3, 6000, 75)
private val repository = FakeCombatLevelRepository()
private val calculator = CombatLevelCalculator(repository)
@Test
fun combatLevelIsSelected_whenRequiredXpIsMatchedExactly() {
repository.addCombatLevel(combatLevel1)
repository.addCombatLevel(combatLevel2)
repository.addCombatLevel(combatLevel3)
val exactXpForCombatLevel2 = combatLevel2.requiredCombatXp
val combatLevel = calculator.calculate(exactXpForCombatLevel2)
Assertions.assertEquals(combatLevel2, combatLevel)
}
@Test
fun smallestCombatLevelIsSelected_whenPlayerHasTooLittleXp() {
repository.addCombatLevel(combatLevel1)
repository.addCombatLevel(combatLevel2)
repository.addCombatLevel(combatLevel3)
val notEnoughXpForCombatLevel1 = 10
val combatLevel = calculator.calculate(notEnoughXpForCombatLevel1)
Assertions.assertEquals(combatLevel1, combatLevel)
}
@Test
fun biggestCombatLevelIsSelected_whenPlayerHasMuchXp() {
repository.addCombatLevel(combatLevel1)
repository.addCombatLevel(combatLevel2)
repository.addCombatLevel(combatLevel3)
val muchXp = 100000
val combatLevel = calculator.calculate(muchXp)
Assertions.assertEquals(combatLevel3, combatLevel)
}
}
@@ -1,41 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class CombatXpCalculatorTest {
private final FakeCombatXpMultiplierRepository repository = new FakeCombatXpMultiplierRepository();
private final CombatXpCalculator calculator = new CombatXpCalculator(repository);
@Test
public void xpTypeIsIgnored_whenMultiplierIsUndefined() {
Collection<Experience> experienceCollection = List.of(new Experience("has_no_multiplier", 100));
int combatXp = calculator.calculate(experienceCollection);
assertEquals(0, combatXp);
}
@Test
public void xpTypeCounts_whenMultiplierIsDefined() {
repository.addMultiplier("has_multiplier", 1);
Collection<Experience> experienceCollection = List.of(new Experience("has_multiplier", 100));
int combatXp = calculator.calculate(experienceCollection);
assertEquals(100, combatXp);
}
@Test
public void xpIsMultiplied_whenMultiplierForTypeIsAboveOne() {
repository.addMultiplier("has_multiplier", 5);
Collection<Experience> experienceCollection = List.of(new Experience("has_multiplier", 100));
int combatXp = calculator.calculate(experienceCollection);
assertEquals(500, combatXp);
}
}
@@ -0,0 +1,58 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class CombatXpCalculatorTest {
private val repository = FakeCombatXpMultiplierRepository()
private val calculator = CombatXpCalculator(repository)
@Test
fun xpTypeIsIgnored_whenMultiplierIsUndefined() {
val experienceCollection = listOf(Experience("has_no_multiplier", 100))
val combatXp = calculator.calculate(experienceCollection)
Assertions.assertEquals(0, combatXp)
}
@Test
fun xpTypeCounts_whenMultiplierIsDefined() {
repository.addMultiplier("has_multiplier", 1)
val experienceCollection = listOf(Experience("has_multiplier", 100))
val combatXp = calculator.calculate(experienceCollection)
Assertions.assertEquals(100, combatXp)
}
@Test
fun xpIsMultiplied_whenMultiplierForTypeIsAboveOne() {
repository.addMultiplier("has_multiplier", 5)
val experienceCollection = listOf(Experience("has_multiplier", 100))
val combatXp = calculator.calculate(experienceCollection)
Assertions.assertEquals(500, combatXp)
}
}
@@ -1,22 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import java.util.Collection;
import java.util.LinkedHashSet;
class FakeCombatLevelRepository implements CombatLevelRepository {
private final Collection<CombatLevel> combatLevels;
public FakeCombatLevelRepository() {
combatLevels = new LinkedHashSet<>();
}
public void addCombatLevel(CombatLevel combatLevel) {
combatLevels.add(combatLevel);
}
@Override
public Collection<CombatLevel> getCombatLevels() {
return combatLevels;
}
}
@@ -0,0 +1,39 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
internal class FakeCombatLevelRepository : CombatLevelRepository {
private val combatLevels = LinkedHashSet<CombatLevel>()
fun addCombatLevel(combatLevel: CombatLevel) {
combatLevels.add(combatLevel)
}
override fun getCombatLevels(): Collection<CombatLevel> {
return combatLevels
}
}
@@ -1,22 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import java.util.HashMap;
import java.util.Map;
public class FakeCombatXpMultiplierRepository implements CombatXpMultiplierRepository {
private final Map<String, Integer> map;
public FakeCombatXpMultiplierRepository() {
map = new HashMap<>();
}
public void addMultiplier(String xpType, int multiplier) {
map.put(xpType, multiplier);
}
@Override
public int getMultiplier(String xpType) {
return map.getOrDefault(xpType, 0);
}
}
@@ -0,0 +1,39 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
class FakeCombatXpMultiplierRepository : CombatXpMultiplierRepository {
private val map = mutableMapOf<String, Int>()
fun addMultiplier(xpType: String, multiplier: Int) {
map[xpType] = multiplier
}
override fun getMultiplier(xpType: String): Int {
return map.getOrDefault(xpType, 0)
}
}
@@ -1,34 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class HealthAddedCalculatorTest {
private final CombatLevel combatLevel1 = new CombatLevel(1, 500, 0);
private final CombatLevel combatLevel2 = new CombatLevel(2, 1000, 50);
private final CombatLevel combatLevel3 = new CombatLevel(2, 6000, 75);
private final HealthAddedCalculator healthAddedCalculator = new HealthAddedCalculator();
@Test
public void healthIsDecreased_whenLevelIsDecreased() {
int healthChange = healthAddedCalculator.calculate(combatLevel3, combatLevel1);
assertEquals(-75, healthChange);
}
@Test
public void healthIsIncreased_whenLevelIsIncreased() {
int healthChange = healthAddedCalculator.calculate(combatLevel2, combatLevel3);
assertEquals(25, healthChange);
}
@Test
public void healthIsUnchanged_whenLevelIsUnchanged() {
int healthChange = healthAddedCalculator.calculate(combatLevel2, combatLevel2);
assertEquals(0, healthChange);
}
}
@@ -0,0 +1,55 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class HealthAddedCalculatorTest {
private val combatLevel1 = CombatLevel(1, 500, 0)
private val combatLevel2 = CombatLevel(2, 1000, 50)
private val combatLevel3 = CombatLevel(2, 6000, 75)
private val healthAddedCalculator = HealthAddedCalculator()
@Test
fun healthIsDecreased_whenLevelIsDecreased() {
val healthChange = healthAddedCalculator.calculate(combatLevel3, combatLevel1)
Assertions.assertEquals(-75, healthChange)
}
@Test
fun healthIsIncreased_whenLevelIsIncreased() {
val healthChange = healthAddedCalculator.calculate(combatLevel2, combatLevel3)
Assertions.assertEquals(25, healthChange)
}
@Test
fun healthIsUnchanged_whenLevelIsUnchanged() {
val healthChange = healthAddedCalculator.calculate(combatLevel2, combatLevel2)
Assertions.assertEquals(0, healthChange)
}
}
@@ -1,19 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.*;
public class SdbCombatLevelRepositoryTest {
@Test
public void canLoadLevels() {
SdbCombatLevelRepository repository = new SdbCombatLevelRepository();
Collection<CombatLevel> combatLevels = repository.getCombatLevels();
int combatLevelCount = combatLevels.size();
assertEquals(80, combatLevelCount);
}
}
@@ -0,0 +1,40 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class SdbCombatLevelRepositoryTest {
@Test
fun canLoadLevels() {
val repository = SdbCombatLevelRepository()
val combatLevels = repository.combatLevels
val combatLevelCount = combatLevels.size
Assertions.assertEquals(80, combatLevelCount)
}
}
@@ -1,30 +0,0 @@
package com.projectswg.holocore.services.gameplay.player.experience;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SdbCombatXpMultiplierRepositoryTest {
private final SdbCombatXpMultiplierRepository repository = new SdbCombatXpMultiplierRepository();
@Test
public void multiplierIsAboveZero_whenXpTypeIsKnown() {
// These come from the SDB
String randomlySelectedXpType = "combat_general";
int multiplierForRandomlySelectedXpType = 3;
int multiplier = repository.getMultiplier(randomlySelectedXpType);
assertEquals(multiplierForRandomlySelectedXpType, multiplier);
}
@Test
public void multiplierIsZero_whenXpTypeIsUnknown() {
String randomlySelectedXpType = "unknown_xp_type";
int multiplier = repository.getMultiplier(randomlySelectedXpType);
assertEquals(0, multiplier);
}
}
@@ -0,0 +1,50 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.player.experience
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class SdbCombatXpMultiplierRepositoryTest {
private val repository = SdbCombatXpMultiplierRepository()
@Test
fun multiplierIsAboveZero_whenXpTypeIsKnown() {
// These come from the SDB
val randomlySelectedXpType = "combat_general"
val multiplierForRandomlySelectedXpType = 3
val multiplier = repository.getMultiplier(randomlySelectedXpType)
Assertions.assertEquals(multiplierForRandomlySelectedXpType, multiplier)
}
@Test
fun multiplierIsZero_whenXpTypeIsUnknown() {
val randomlySelectedXpType = "unknown_xp_type"
val multiplier = repository.getMultiplier(randomlySelectedXpType)
Assertions.assertEquals(0, multiplier)
}
}