Global monster drops are retrieved from DropProvider (postgres db)

This commit is contained in:
P0nk
2023-03-16 18:00:21 +01:00
parent cc88d382e6
commit 02aa4a237f
7 changed files with 43 additions and 16 deletions

View File

@@ -38,7 +38,14 @@ public class DropProvider {
monsterDrop.maxQuantity(), questId); monsterDrop.maxQuantity(), questId);
} }
public List<MonsterGlobalDropEntry> getGlobalDropEntries() { public List<MonsterGlobalDropEntry> getRelevantGlobalDrops(int mapId) {
int continentId = mapId / 100_000_000;
return getGlobalDropEntries().stream()
.filter(drop -> drop.continentid < 0 || drop.continentid == continentId)
.toList();
}
private List<MonsterGlobalDropEntry> getGlobalDropEntries() {
if (this.globalMonsterDrops == null) { if (this.globalMonsterDrops == null) {
loadGlobalDrops(); loadGlobalDrops();
} }

View File

@@ -46,7 +46,6 @@ public class MonsterInformationProvider {
return instance; return instance;
} }
private final Map<Integer, List<MonsterDropEntry>> drops = new HashMap<>();
private final List<MonsterGlobalDropEntry> globaldrops = new ArrayList<>(); private final List<MonsterGlobalDropEntry> globaldrops = new ArrayList<>();
private final Map<Integer, List<MonsterGlobalDropEntry>> continentdrops = new HashMap<>(); private final Map<Integer, List<MonsterGlobalDropEntry>> continentdrops = new HashMap<>();
@@ -179,7 +178,6 @@ public class MonsterInformationProvider {
} }
public final void clearDrops() { public final void clearDrops() {
drops.clear();
globaldrops.clear(); globaldrops.clear();
continentdrops.clear(); continentdrops.clear();
retrieveGlobal(); retrieveGlobal();

View File

@@ -729,8 +729,7 @@ public class MapleMap {
chRate = 1; chRate = 1;
} }
final MonsterInformationProvider mi = MonsterInformationProvider.getInstance(); final List<MonsterGlobalDropEntry> globalEntry = dropProvider.getRelevantGlobalDrops(this.getId());
final List<MonsterGlobalDropEntry> globalEntry = mi.getRelevantGlobalDrops(this.getId());
final List<MonsterDropEntry> dropEntry = new ArrayList<>(); final List<MonsterDropEntry> dropEntry = new ArrayList<>();
final List<MonsterDropEntry> visibleQuestEntry = new ArrayList<>(); final List<MonsterDropEntry> visibleQuestEntry = new ArrayList<>();

View File

@@ -6,6 +6,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import server.life.MonsterDropEntry; import server.life.MonsterDropEntry;
import server.life.MonsterGlobalDropEntry; import server.life.MonsterGlobalDropEntry;
import testutil.Any;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -82,8 +83,8 @@ class DropProviderTest {
GlobalMonsterDrop globalDrop = new GlobalMonsterDrop(2049100, -1, 2, 3, null, 450); GlobalMonsterDrop globalDrop = new GlobalMonsterDrop(2049100, -1, 2, 3, null, 450);
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop)); when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop));
List<MonsterGlobalDropEntry> dropEntries1 = dropProvider.getGlobalDropEntries(); List<MonsterGlobalDropEntry> dropEntries1 = dropProvider.getRelevantGlobalDrops(Any.integer());
List<MonsterGlobalDropEntry> dropEntries2 = dropProvider.getGlobalDropEntries(); List<MonsterGlobalDropEntry> dropEntries2 = dropProvider.getRelevantGlobalDrops(Any.integer());
assertEquals(1, dropEntries1.size()); assertEquals(1, dropEntries1.size());
assertEquals(1, dropEntries2.size()); assertEquals(1, dropEntries2.size());
@@ -104,6 +105,28 @@ class DropProviderTest {
verify(dropDao, times(1)).getGlobalMonsterDrops(); verify(dropDao, times(1)).getGlobalMonsterDrops();
} }
@Test
void getRelevantGlobalDrop() {
GlobalMonsterDrop ossyriaDrop = new GlobalMonsterDrop(Any.integer(), 2, Any.integer(), Any.integer(), Any.integer(), Any.integer());
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(ossyriaDrop));
int ossyriaMapId = 200_000_200;
List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(ossyriaMapId);
assertEquals(1, dropEntries.size());
}
@Test
void getRelevantGlobalDrop_wrongContinent() {
GlobalMonsterDrop ellinDrop = new GlobalMonsterDrop(Any.integer(), 3, Any.integer(), Any.integer(), Any.integer(), Any.integer());
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(ellinDrop));
int victoriaMapId = 102_000_000;
List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(victoriaMapId);
assertTrue(dropEntries.isEmpty());
}
// TODO: add tests for getRandomStealDrop() once ItemInformationProvider is able to be mocked. // TODO: add tests for getRandomStealDrop() once ItemInformationProvider is able to be mocked.
// Currently, it does database calls (and a bunch of other stuff) in the constructor, which is problematic. // Currently, it does database calls (and a bunch of other stuff) in the constructor, which is problematic.
} }

View File

@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import server.MakerItemFactory; import server.MakerItemFactory;
import testutil.AnyValues; import testutil.Any;
import tools.Pair; import tools.Pair;
import java.util.List; import java.util.List;
@@ -80,7 +80,7 @@ class MakerInfoProviderTest {
void getRecipeFromDb_notFound() { void getRecipeFromDb_notFound() {
givenNoRecipe(); givenNoRecipe();
Optional<MakerRecipe> recipe = makerInfoProvider.getMakerRecipe(AnyValues.integer()); Optional<MakerRecipe> recipe = makerInfoProvider.getMakerRecipe(Any.integer());
assertTrue(recipe.isEmpty()); assertTrue(recipe.isEmpty());
} }
@@ -111,7 +111,7 @@ class MakerInfoProviderTest {
MakerRecipe recipeWithCatalyst = new MakerRecipe(0, (short) 0, (short) 0, (short) 0, 0, null, null, catalyst, (short) 0, (short) 0); MakerRecipe recipeWithCatalyst = new MakerRecipe(0, (short) 0, (short) 0, (short) 0, 0, null, null, catalyst, (short) 0, (short) 0);
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipeWithCatalyst)); when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipeWithCatalyst));
Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer()); Optional<Integer> stimulant = makerInfoProvider.getStimulant(Any.integer());
assertTrue(stimulant.isPresent()); assertTrue(stimulant.isPresent());
assertEquals(catalyst, stimulant.get()); assertEquals(catalyst, stimulant.get());
@@ -121,7 +121,7 @@ class MakerInfoProviderTest {
void getStimulant_noRecipe() { void getStimulant_noRecipe() {
givenNoRecipe(); givenNoRecipe();
Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer()); Optional<Integer> stimulant = makerInfoProvider.getStimulant(Any.integer());
assertTrue(stimulant.isEmpty()); assertTrue(stimulant.isEmpty());
} }

View File

@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import testutil.Any;
import testutil.Mocks; import testutil.Mocks;
import java.util.Collections; import java.util.Collections;
@@ -17,8 +18,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static testutil.AnyValues.daoException; import static testutil.Any.daoException;
import static testutil.AnyValues.string;
class NoteServiceTest { class NoteServiceTest {
@@ -73,7 +73,7 @@ class NoteServiceTest {
void sendFailure() { void sendFailure() {
doThrow(daoException()).when(noteDao).save(any()); doThrow(daoException()).when(noteDao).save(any());
boolean success = noteService.sendNormal(string(), string(), string()); boolean success = noteService.sendNormal(Any.string(), Any.string(), Any.string());
assertFalse(success); assertFalse(success);
verify(noteDao).save(any()); verify(noteDao).save(any());

View File

@@ -2,7 +2,7 @@ package testutil;
import database.DaoException; import database.DaoException;
public class AnyValues { public class Any {
public static String string() { public static String string() {
return "string"; return "string";