Maker disassembly info to MakerInfoProvider

MakerProcessor is such a mess...
This commit is contained in:
P0nk
2023-03-04 22:33:34 +01:00
parent 9d6574d3ba
commit d2d4b442d2
10 changed files with 89 additions and 61 deletions

View File

@@ -1,5 +1,6 @@
package database;
import database.maker.MakerIngredientRowMapper;
import database.maker.MakerReagentRowMapper;
import database.maker.MakerRecipeRowMapper;
import database.note.NoteRowMapper;
@@ -14,6 +15,7 @@ public final class JdbiConfig {
return Jdbi.create(dataSource)
.registerRowMapper(new NoteRowMapper())
.registerRowMapper(new MakerReagentRowMapper())
.registerRowMapper(new MakerRecipeRowMapper());
.registerRowMapper(new MakerRecipeRowMapper())
.registerRowMapper(new MakerIngredientRowMapper());
}
}

View File

@@ -5,6 +5,7 @@ import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException;
import java.util.List;
import java.util.Optional;
public class MakerDao {
@@ -41,4 +42,18 @@ public class MakerDao {
throw new DaoException("Failed to get maker recipe with item id: %d".formatted(itemId), e);
}
}
public List<MakerIngredient> getIngredients(int recipeItemId) {
try (Handle handle = connection.getHandle()) {
return handle.createQuery("""
SELECT *
FROM maker_ingredient
WHERE maker_recipe = ?;""")
.bind(0, recipeItemId)
.mapTo(MakerIngredient.class)
.list();
} catch (JdbiException e) {
throw new DaoException("Failed to get maker ingredients for recipe item id %d".formatted(recipeItemId), e);
}
}
}

View File

@@ -0,0 +1,6 @@
package database.maker;
import java.util.Collection;
public record MakerDisassemblyInfo(int fee, Collection<MakerIngredient> gainedItems) {
}

View File

@@ -2,8 +2,10 @@ package database.maker;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import constants.id.ItemId;
import net.jcip.annotations.ThreadSafe;
import java.util.List;
import java.util.Optional;
@ThreadSafe
@@ -11,6 +13,7 @@ public class MakerInfoProvider {
private final MakerDao makerDao;
private final Cache<Integer, Optional<MakerReagent>> reagentCache = Caffeine.newBuilder().build();
private final Cache<Integer, Optional<MakerRecipe>> recipeCache = Caffeine.newBuilder().build();
private final Cache<Integer, List<MakerIngredient>> ingredientsCache = Caffeine.newBuilder().build();
public MakerInfoProvider(MakerDao makerDao) {
if (makerDao == null) {
@@ -30,4 +33,31 @@ public class MakerInfoProvider {
public Optional<Integer> getStimulant(int itemId) {
return getMakerRecipe(itemId).map(MakerRecipe::catalyst);
}
public List<MakerIngredient> getIngredients(int recipeItemId) {
return ingredientsCache.get(recipeItemId, makerDao::getIngredients);
}
public Optional<MakerDisassemblyInfo> getDisassemblyInfo(int itemId) {
Optional<MakerRecipe> recipe = getMakerRecipe(itemId);
if (recipe.isEmpty()) {
return Optional.empty();
}
int fee = calculateDisassemblyFee(recipe.get().mesoCost());
List<MakerIngredient> gainedItems = getIngredients(itemId).stream()
.filter(i -> ItemId.isMonsterCrystal(i.itemId()))
.map(i -> new MakerIngredient(i.itemId(), (short) (i.count() / 2)))
.toList();
return Optional.of(new MakerDisassemblyInfo(fee, gainedItems));
}
private int calculateDisassemblyFee(int creationCost) {
// cost is 13.6363~ % of the original value, trim by 1000.
float val = (float) (creationCost * 0.13636363636364);
int fee = (int) (val / 1000);
fee *= 1000;
return fee;
}
}

View File

@@ -0,0 +1,4 @@
package database.maker;
public record MakerIngredient(int itemId, short count) {
}

View File

@@ -0,0 +1,17 @@
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 MakerIngredientRowMapper implements RowMapper<MakerIngredient> {
@Override
public MakerIngredient map(ResultSet rs, StatementContext ctx) throws SQLException {
int itemId = rs.getInt("item_id");
short count = rs.getShort("count");
return new MakerIngredient(itemId, count);
}
}