Add table, data, and provider method for global drops

This commit is contained in:
P0nk
2023-03-09 20:08:20 +01:00
parent a70f4e303d
commit 53768555a2
7 changed files with 111 additions and 1 deletions

View File

@@ -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());
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,4 @@
package database.drop;
public record GlobalMonsterDrop(int itemId, int continent, int minQuantity, int maxQuantity, Integer questId, int chance) {
}

View 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);
}
}