Cache MakerRecipe, move stimulant to MakerInfoProvider

This commit is contained in:
P0nk
2023-03-04 17:20:16 +01:00
parent b329709776
commit 6073b20d65
8 changed files with 99 additions and 36 deletions

View File

@@ -101,7 +101,11 @@ public class MakerProcessor {
} else {
if (ItemConstants.isEquipment(toCreate)) { // only equips uses stimulant and reagents
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)) {
stimulantid = -1;
}

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

View File

@@ -1,7 +1,5 @@
package database;
import database.maker.MakerReagentRowMapper;
import database.note.NoteRowMapper;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
@@ -15,14 +13,7 @@ public class PgDatabaseConnection {
public PgDatabaseConnection(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbi = Jdbi.create(dataSource);
registerRowMappers();
// TODO: configure jdbi elsewhere
}
private void registerRowMappers() {
jdbi.registerRowMapper(new NoteRowMapper())
.registerRowMapper(new MakerReagentRowMapper());
this.jdbi = JdbiConfig.createConfigured(dataSource);
}
public Connection getConnection() throws SQLException {

View File

@@ -27,4 +27,18 @@ public class MakerDao {
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);
}
}
}

View File

@@ -1,19 +1,23 @@
package database.maker;
import net.jcip.annotations.ThreadSafe;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@ThreadSafe
public class MakerInfoProvider {
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) {
this.makerDao = makerDao;
}
public Optional<MakerReagent> getMakerReagent(int itemId) {
final MakerReagent cachedReagent = reagents.get(itemId);
final MakerReagent cachedReagent = reagentCache.get(itemId);
if (cachedReagent != null) {
return Optional.of(cachedReagent);
}
@@ -22,7 +26,25 @@ public class MakerInfoProvider {
if (reagentFromDb.isEmpty()) {
return Optional.empty();
}
reagents.put(itemId, reagentFromDb.get());
reagentCache.put(itemId, reagentFromDb.get());
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);
}
}

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

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

View File

@@ -40,7 +40,10 @@ import provider.wz.WZFiles;
import server.MakerItemFactory.MakerItemCreateEntry;
import server.life.LifeFactory;
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.PreparedStatement;
@@ -111,7 +114,6 @@ public class ItemInformationProvider {
protected Map<Integer, Integer> mobCrystalMakerCache = new HashMap<>();
protected Map<Integer, Pair<String, Integer>> statUpgradeMakerCache = 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, Data> skillUpgradeInfoCache = new HashMap<>();
protected Map<Integer, Pair<Integer, Set<Integer>>> cashPetFoodCache = new HashMap<>();
@@ -2119,26 +2121,6 @@ public class ItemInformationProvider {
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) {
Set<String> list = new HashSet<>();
try (Connection con = DatabaseConnection.getConnection();