Cache MakerRecipe, move stimulant to MakerInfoProvider
This commit is contained in:
@@ -101,7 +101,11 @@ public class MakerProcessor {
|
|||||||
} else {
|
} else {
|
||||||
if (ItemConstants.isEquipment(toCreate)) { // only equips uses stimulant and reagents
|
if (ItemConstants.isEquipment(toCreate)) { // only equips uses stimulant and reagents
|
||||||
if (p.readByte() != 0) { // stimulant
|
if (p.readByte() != 0) { // stimulant
|
||||||
stimulantid = ii.getMakerStimulant(toCreate);
|
Optional<Integer> makerStimulant = infoProvider.getStimulant(toCreate);
|
||||||
|
if (makerStimulant.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Maker item %d has no stimulator".formatted(toCreate));
|
||||||
|
}
|
||||||
|
stimulantid = makerStimulant.get();
|
||||||
if (!c.getAbstractPlayerInteraction().haveItem(stimulantid)) {
|
if (!c.getAbstractPlayerInteraction().haveItem(stimulantid)) {
|
||||||
stimulantid = -1;
|
stimulantid = -1;
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/main/java/database/JdbiConfig.java
Normal file
19
src/main/java/database/JdbiConfig.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package database;
|
||||||
|
|
||||||
|
import database.maker.MakerReagentRowMapper;
|
||||||
|
import database.maker.MakerRecipeRowMapper;
|
||||||
|
import database.note.NoteRowMapper;
|
||||||
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
public final class JdbiConfig {
|
||||||
|
private JdbiConfig() {}
|
||||||
|
|
||||||
|
public static Jdbi createConfigured(DataSource dataSource) {
|
||||||
|
return Jdbi.create(dataSource)
|
||||||
|
.registerRowMapper(new NoteRowMapper())
|
||||||
|
.registerRowMapper(new MakerReagentRowMapper())
|
||||||
|
.registerRowMapper(new MakerRecipeRowMapper());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package database;
|
package database;
|
||||||
|
|
||||||
import database.maker.MakerReagentRowMapper;
|
|
||||||
import database.note.NoteRowMapper;
|
|
||||||
import org.jdbi.v3.core.Handle;
|
import org.jdbi.v3.core.Handle;
|
||||||
import org.jdbi.v3.core.Jdbi;
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
|
||||||
@@ -15,14 +13,7 @@ public class PgDatabaseConnection {
|
|||||||
|
|
||||||
public PgDatabaseConnection(DataSource dataSource) {
|
public PgDatabaseConnection(DataSource dataSource) {
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
this.jdbi = Jdbi.create(dataSource);
|
this.jdbi = JdbiConfig.createConfigured(dataSource);
|
||||||
registerRowMappers();
|
|
||||||
// TODO: configure jdbi elsewhere
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerRowMappers() {
|
|
||||||
jdbi.registerRowMapper(new NoteRowMapper())
|
|
||||||
.registerRowMapper(new MakerReagentRowMapper());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
|
|||||||
@@ -27,4 +27,18 @@ public class MakerDao {
|
|||||||
throw new DaoException("Failed to get maker reagent with item id: %d".formatted(itemId), e);
|
throw new DaoException("Failed to get maker reagent with item id: %d".formatted(itemId), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<MakerRecipe> getRecipe(int itemId) {
|
||||||
|
try (Handle handle = connection.getHandle()) {
|
||||||
|
return handle.createQuery("""
|
||||||
|
SELECT *
|
||||||
|
FROM maker_recipe
|
||||||
|
WHERE item_id = ?;""")
|
||||||
|
.bind(0, itemId)
|
||||||
|
.mapTo(MakerRecipe.class)
|
||||||
|
.findOne();
|
||||||
|
} catch (JdbiException e) {
|
||||||
|
throw new DaoException("Failed to get maker recipe with item id: %d".formatted(itemId), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,23 @@
|
|||||||
package database.maker;
|
package database.maker;
|
||||||
|
|
||||||
|
import net.jcip.annotations.ThreadSafe;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
public class MakerInfoProvider {
|
public class MakerInfoProvider {
|
||||||
private final MakerDao makerDao;
|
private final MakerDao makerDao;
|
||||||
private final Map<Integer, MakerReagent> reagents = new ConcurrentHashMap<>();
|
private final Map<Integer, MakerReagent> reagentCache = new ConcurrentHashMap<>();
|
||||||
|
private final Map<Integer, MakerRecipe> recipeCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public MakerInfoProvider(MakerDao makerDao) {
|
public MakerInfoProvider(MakerDao makerDao) {
|
||||||
this.makerDao = makerDao;
|
this.makerDao = makerDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<MakerReagent> getMakerReagent(int itemId) {
|
public Optional<MakerReagent> getMakerReagent(int itemId) {
|
||||||
final MakerReagent cachedReagent = reagents.get(itemId);
|
final MakerReagent cachedReagent = reagentCache.get(itemId);
|
||||||
if (cachedReagent != null) {
|
if (cachedReagent != null) {
|
||||||
return Optional.of(cachedReagent);
|
return Optional.of(cachedReagent);
|
||||||
}
|
}
|
||||||
@@ -22,7 +26,25 @@ public class MakerInfoProvider {
|
|||||||
if (reagentFromDb.isEmpty()) {
|
if (reagentFromDb.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
reagents.put(itemId, reagentFromDb.get());
|
reagentCache.put(itemId, reagentFromDb.get());
|
||||||
return reagentFromDb;
|
return reagentFromDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<MakerRecipe> getMakerRecipe(int itemId) {
|
||||||
|
final MakerRecipe cachedRecipe = recipeCache.get(itemId);
|
||||||
|
if (cachedRecipe != null) {
|
||||||
|
return Optional.of(cachedRecipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Optional<MakerRecipe> recipeFromDb = makerDao.getRecipe(itemId);
|
||||||
|
if (recipeFromDb.isEmpty()) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
recipeCache.put(itemId, recipeFromDb.get());
|
||||||
|
return recipeFromDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Integer> getStimulant(int itemId) {
|
||||||
|
return getMakerRecipe(itemId).map(MakerRecipe::catalyst);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/main/java/database/maker/MakerRecipe.java
Normal file
5
src/main/java/database/maker/MakerRecipe.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package database.maker;
|
||||||
|
|
||||||
|
public record MakerRecipe(int itemId, short jobCategory, short requiredLevel, short requiredMakerLevel, int mesoCost,
|
||||||
|
Integer requiredItem, Integer requiredEquip, Integer catalyst, short quantity,
|
||||||
|
short reagentSlots) {}
|
||||||
26
src/main/java/database/maker/MakerRecipeRowMapper.java
Normal file
26
src/main/java/database/maker/MakerRecipeRowMapper.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package database.maker;
|
||||||
|
|
||||||
|
import org.jdbi.v3.core.mapper.RowMapper;
|
||||||
|
import org.jdbi.v3.core.statement.StatementContext;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class MakerRecipeRowMapper implements RowMapper<MakerRecipe> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MakerRecipe map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
|
int itemId = rs.getInt("item_id");
|
||||||
|
short jobCategory = rs.getShort("job_category");
|
||||||
|
short requiredLevel = rs.getShort("required_level");
|
||||||
|
short requiredMakerLevel = rs.getShort("required_maker_level");
|
||||||
|
int mesoCost = rs.getInt("meso_cost");
|
||||||
|
Integer requiredItem = rs.getObject("required_item", Integer.class);
|
||||||
|
Integer requiredEquip = rs.getObject("required_equip", Integer.class);
|
||||||
|
Integer catalyst = rs.getObject("catalyst", Integer.class);
|
||||||
|
short quantity = rs.getShort("quantity");
|
||||||
|
short reagentSlots = rs.getShort("reagent_slots");
|
||||||
|
return new MakerRecipe(itemId, jobCategory, requiredLevel, requiredMakerLevel, mesoCost, requiredItem,
|
||||||
|
requiredEquip, catalyst, quantity, reagentSlots);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,10 @@ import provider.wz.WZFiles;
|
|||||||
import server.MakerItemFactory.MakerItemCreateEntry;
|
import server.MakerItemFactory.MakerItemCreateEntry;
|
||||||
import server.life.LifeFactory;
|
import server.life.LifeFactory;
|
||||||
import server.life.MonsterInformationProvider;
|
import server.life.MonsterInformationProvider;
|
||||||
import tools.*;
|
import tools.DatabaseConnection;
|
||||||
|
import tools.PacketCreator;
|
||||||
|
import tools.Pair;
|
||||||
|
import tools.Randomizer;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
@@ -111,7 +114,6 @@ public class ItemInformationProvider {
|
|||||||
protected Map<Integer, Integer> mobCrystalMakerCache = new HashMap<>();
|
protected Map<Integer, Integer> mobCrystalMakerCache = new HashMap<>();
|
||||||
protected Map<Integer, Pair<String, Integer>> statUpgradeMakerCache = new HashMap<>();
|
protected Map<Integer, Pair<String, Integer>> statUpgradeMakerCache = new HashMap<>();
|
||||||
protected Map<Integer, MakerItemFactory.MakerItemCreateEntry> makerItemCache = new HashMap<>();
|
protected Map<Integer, MakerItemFactory.MakerItemCreateEntry> makerItemCache = new HashMap<>();
|
||||||
protected Map<Integer, Integer> makerCatalystCache = new HashMap<>();
|
|
||||||
protected Map<Integer, Map<String, Integer>> skillUpgradeCache = new HashMap<>();
|
protected Map<Integer, Map<String, Integer>> skillUpgradeCache = new HashMap<>();
|
||||||
protected Map<Integer, Data> skillUpgradeInfoCache = new HashMap<>();
|
protected Map<Integer, Data> skillUpgradeInfoCache = new HashMap<>();
|
||||||
protected Map<Integer, Pair<Integer, Set<Integer>>> cashPetFoodCache = new HashMap<>();
|
protected Map<Integer, Pair<Integer, Set<Integer>>> cashPetFoodCache = new HashMap<>();
|
||||||
@@ -2119,26 +2121,6 @@ public class ItemInformationProvider {
|
|||||||
return fee;
|
return fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMakerStimulant(int itemId) { // thanks to Arnah
|
|
||||||
Integer itemid = makerCatalystCache.get(itemId);
|
|
||||||
if (itemid != null) {
|
|
||||||
return itemid;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemid = -1;
|
|
||||||
for (Data md : etcData.getData("ItemMake.img").getChildren()) {
|
|
||||||
Data me = md.getChildByPath(StringUtil.getLeftPaddedStr(Integer.toString(itemId), '0', 8));
|
|
||||||
|
|
||||||
if (me != null) {
|
|
||||||
itemid = DataTool.getInt(me.getChildByPath("catalyst"), -1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
makerCatalystCache.put(itemId, itemid);
|
|
||||||
return itemid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getWhoDrops(Integer itemId) {
|
public Set<String> getWhoDrops(Integer itemId) {
|
||||||
Set<String> list = new HashSet<>();
|
Set<String> list = new HashSet<>();
|
||||||
try (Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
|
|||||||
Reference in New Issue
Block a user