Add table, data, and provider method for global drops
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package database;
|
||||
|
||||
import database.drop.GlobalMonsterDropRowMapper;
|
||||
import database.drop.MonsterDropRowMapper;
|
||||
import database.maker.MakerIngredientRowMapper;
|
||||
import database.maker.MakerReagentRowMapper;
|
||||
@@ -18,6 +19,7 @@ public final class JdbiConfig {
|
||||
.registerRowMapper(new MakerReagentRowMapper())
|
||||
.registerRowMapper(new MakerRecipeRowMapper())
|
||||
.registerRowMapper(new MakerIngredientRowMapper())
|
||||
.registerRowMapper(new MonsterDropRowMapper());
|
||||
.registerRowMapper(new MonsterDropRowMapper())
|
||||
.registerRowMapper(new GlobalMonsterDropRowMapper());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,4 +26,16 @@ public class DropDao {
|
||||
throw new DaoException("Failed to get monster drops for id %d".formatted(monsterId), e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<GlobalMonsterDrop> getGlobalMonsterDrops() {
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createQuery("""
|
||||
SELECT *
|
||||
FROM global_monster_drop;""")
|
||||
.mapTo(GlobalMonsterDrop.class)
|
||||
.list();
|
||||
} catch (JdbiException e) {
|
||||
throw new DaoException("Failed to get global monster drops", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,14 @@ package database.drop;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import server.life.MonsterDropEntry;
|
||||
import server.life.MonsterGlobalDropEntry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DropProvider {
|
||||
private final DropDao dropDao;
|
||||
private final Cache<Integer, List<MonsterDrop>> monsterDropCache = Caffeine.newBuilder().build();
|
||||
private volatile List<GlobalMonsterDrop> globalMonsterDrops = null;
|
||||
|
||||
public DropProvider(DropDao dropDao) {
|
||||
if (dropDao == null) {
|
||||
@@ -29,4 +31,25 @@ public class DropProvider {
|
||||
return new MonsterDropEntry(monsterDrop.itemId(), monsterDrop.chance(), monsterDrop.minQuantity(),
|
||||
monsterDrop.maxQuantity(), questId);
|
||||
}
|
||||
|
||||
public List<MonsterGlobalDropEntry> getGlobalDropEntries() {
|
||||
if (this.globalMonsterDrops == null) {
|
||||
loadGlobalDrops();
|
||||
}
|
||||
|
||||
return globalMonsterDrops.stream()
|
||||
.map(this::mapToDropEntry)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void loadGlobalDrops() {
|
||||
this.globalMonsterDrops = dropDao.getGlobalMonsterDrops();
|
||||
}
|
||||
|
||||
// TODO: Temporary. MonsterDropEntry should be removed.
|
||||
private MonsterGlobalDropEntry mapToDropEntry(GlobalMonsterDrop globalDrop) {
|
||||
short questId = globalDrop.questId() == null ? 0 : globalDrop.questId().shortValue();
|
||||
return new MonsterGlobalDropEntry(globalDrop.itemId(), globalDrop.chance(), globalDrop.continent(),
|
||||
globalDrop.minQuantity(), globalDrop.maxQuantity(), questId);
|
||||
}
|
||||
}
|
||||
|
||||
4
src/main/java/database/drop/GlobalMonsterDrop.java
Normal file
4
src/main/java/database/drop/GlobalMonsterDrop.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package database.drop;
|
||||
|
||||
public record GlobalMonsterDrop(int itemId, int continent, int minQuantity, int maxQuantity, Integer questId, int chance) {
|
||||
}
|
||||
21
src/main/java/database/drop/GlobalMonsterDropRowMapper.java
Normal file
21
src/main/java/database/drop/GlobalMonsterDropRowMapper.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package database.drop;
|
||||
|
||||
import org.jdbi.v3.core.mapper.RowMapper;
|
||||
import org.jdbi.v3.core.statement.StatementContext;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class GlobalMonsterDropRowMapper implements RowMapper<GlobalMonsterDrop> {
|
||||
|
||||
@Override
|
||||
public GlobalMonsterDrop map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||
final int itemId = rs.getInt("item_id");
|
||||
final int continent = rs.getInt("continent");
|
||||
final int minQuantity = rs.getInt("min_quantity");
|
||||
final int maxQuantity = rs.getInt("max_quantity");
|
||||
final Integer questId = rs.getObject("quest_id", Integer.class);
|
||||
final int chance = rs.getInt("chance");
|
||||
return new GlobalMonsterDrop(itemId, continent, minQuantity, maxQuantity, questId, chance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user