Instantiate shops in ShopFactory
This commit is contained in:
@@ -30,25 +30,16 @@ import constants.id.ItemId;
|
||||
import constants.inventory.ItemConstants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.PacketCreator;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Matze
|
||||
*/
|
||||
public class Shop {
|
||||
private static final Logger log = LoggerFactory.getLogger(Shop.class);
|
||||
private static final Set<Integer> rechargeableItems = new LinkedHashSet<>();
|
||||
private static final short MAX_QUANTITY_PER_PURCHASE = 1000; // Should really use max stack size for the given item
|
||||
|
||||
private final int id;
|
||||
private final int npcId;
|
||||
@@ -56,27 +47,10 @@ public class Shop {
|
||||
private final int tokenvalue = 1000000000;
|
||||
private final int token = ItemId.GOLDEN_MAPLE_LEAF;
|
||||
|
||||
static {
|
||||
for (int throwingStarId : ItemId.allThrowingStarIds()) {
|
||||
rechargeableItems.add(throwingStarId);
|
||||
}
|
||||
rechargeableItems.add(ItemId.BLAZE_CAPSULE);
|
||||
rechargeableItems.add(ItemId.GLAZE_CAPSULE);
|
||||
rechargeableItems.add(ItemId.BALANCED_FURY);
|
||||
rechargeableItems.remove(ItemId.DEVIL_RAIN_THROWING_STAR); // doesn't exist
|
||||
for (int bulletId : ItemId.allBulletIds()) {
|
||||
rechargeableItems.add(bulletId);
|
||||
}
|
||||
}
|
||||
|
||||
private Shop(int id, int npcId) {
|
||||
public Shop(int id, int npcId, List<ShopItem> items) {
|
||||
this.id = id;
|
||||
this.npcId = npcId;
|
||||
items = new ArrayList<>();
|
||||
}
|
||||
|
||||
private void addItem(ShopItem item) {
|
||||
items.add(item);
|
||||
this.items = new ArrayList<>(items);
|
||||
}
|
||||
|
||||
public void sendShop(Client c) {
|
||||
@@ -242,57 +216,6 @@ public class Shop {
|
||||
return items.get(slot);
|
||||
}
|
||||
|
||||
public static Shop createFromDB(int id, boolean isShopId) {
|
||||
Shop ret = null;
|
||||
int shopId;
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
final String query;
|
||||
if (isShopId) {
|
||||
query = "SELECT * FROM shops WHERE shopid = ?";
|
||||
} else {
|
||||
query = "SELECT * FROM shops WHERE npcid = ?";
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = con.prepareStatement(query)) {
|
||||
ps.setInt(1, id);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
shopId = rs.getInt("shopid");
|
||||
ret = new Shop(shopId, rs.getInt("npcid"));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, price, pitch FROM shopitems WHERE shopid = ? ORDER BY position DESC")) {
|
||||
ps.setInt(1, shopId);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
List<Integer> recharges = new ArrayList<>(rechargeableItems);
|
||||
while (rs.next()) {
|
||||
if (ItemConstants.isRechargeable(rs.getInt("itemid"))) {
|
||||
ShopItem starItem = new ShopItem((short) 1, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch"));
|
||||
ret.addItem(starItem);
|
||||
if (rechargeableItems.contains(starItem.getItemId())) {
|
||||
recharges.remove(Integer.valueOf(starItem.getItemId()));
|
||||
}
|
||||
} else {
|
||||
ret.addItem(new ShopItem(MAX_QUANTITY_PER_PURCHASE, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("pitch")));
|
||||
}
|
||||
}
|
||||
for (Integer recharge : recharges) {
|
||||
ret.addItem(new ShopItem((short) 0, recharge, 0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int getNpcId() {
|
||||
return npcId;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/*
|
||||
This file is part of the OdinMS Maple Story Server
|
||||
This file is part of the OdinMS Maple Story Server
|
||||
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
|
||||
Matthias Butz <matze@odinms.de>
|
||||
Jan Christian Meyer <vimes@odinms.de>
|
||||
Matthias Butz <matze@odinms.de>
|
||||
Jan Christian Meyer <vimes@odinms.de>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
@@ -21,45 +21,67 @@
|
||||
*/
|
||||
package server;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import constants.id.ItemId;
|
||||
import constants.inventory.ItemConstants;
|
||||
import database.shop.ShopDao;
|
||||
import database.shop.ShopItem;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author Matze
|
||||
*/
|
||||
public class ShopFactory {
|
||||
private final Map<Integer, Shop> shops = new HashMap<>();
|
||||
private final Map<Integer, Shop> npcShops = new HashMap<>();
|
||||
private static final short MAX_QUANTITY_PER_PURCHASE = 1000; // Should really use max stack size for the given item
|
||||
private static final Set<Integer> rechargeableItemIds = rechargeableItemIds();
|
||||
private final Cache<Integer, Shop> shops = Caffeine.newBuilder().build();
|
||||
private final ShopDao shopDao;
|
||||
|
||||
private Shop loadShop(int id, boolean isShopId) {
|
||||
Shop ret = Shop.createFromDB(id, isShopId);
|
||||
if (ret != null) {
|
||||
shops.put(ret.getId(), ret);
|
||||
npcShops.put(ret.getNpcId(), ret);
|
||||
} else if (isShopId) {
|
||||
shops.put(id, null);
|
||||
} else {
|
||||
npcShops.put(id, null);
|
||||
}
|
||||
return ret;
|
||||
public ShopFactory(ShopDao shopDao) {
|
||||
this.shopDao = shopDao;
|
||||
}
|
||||
|
||||
private static Set<Integer> rechargeableItemIds() {
|
||||
IntStream stars = ItemId.allThrowingStarIds();
|
||||
IntStream ammo = IntStream.concat(ItemId.allBulletIds(), ItemId.allBulletCapsuleIds());
|
||||
return IntStream.concat(stars, ammo)
|
||||
.boxed()
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
public Shop getShop(int shopId) {
|
||||
if (shops.containsKey(shopId)) {
|
||||
return shops.get(shopId);
|
||||
}
|
||||
return loadShop(shopId, true);
|
||||
return shops.get(shopId, this::loadShop);
|
||||
}
|
||||
|
||||
public Shop getShopForNPC(int npcId) {
|
||||
if (npcShops.containsKey(npcId)) {
|
||||
return npcShops.get(npcId);
|
||||
private Shop loadShop(int shopId) {
|
||||
Optional<database.shop.Shop> dbShop = shopDao.getShop(shopId);
|
||||
if (dbShop.isEmpty()) {
|
||||
throw new IllegalArgumentException("Shop with id %d does not exist".formatted(shopId));
|
||||
}
|
||||
return loadShop(npcId, false);
|
||||
List<ShopItem> items = shopDao.getShopItems(shopId);
|
||||
return new Shop(dbShop.get().id(), dbShop.get().npcId(), fromDbShopItems(items));
|
||||
}
|
||||
|
||||
private List<server.ShopItem> fromDbShopItems(List<ShopItem> dbItems) {
|
||||
Stream<server.ShopItem> purchaseableItems = dbItems.stream()
|
||||
.map(dbItem -> {
|
||||
short buyable = ItemConstants.isRechargeable(dbItem.itemId()) ? (short) 1 : MAX_QUANTITY_PER_PURCHASE;
|
||||
int pitch = dbItem.pitch() == null ? 0 : dbItem.pitch();
|
||||
return new server.ShopItem(buyable, dbItem.itemId(), dbItem.price(), pitch);
|
||||
});
|
||||
Stream<server.ShopItem> rechargeableItems = rechargeableItemIds.stream()
|
||||
.map(rechItem -> new server.ShopItem((short) 0, rechItem, 0, 0));
|
||||
return Stream.concat(purchaseableItems, rechargeableItems).toList();
|
||||
}
|
||||
|
||||
public void reloadShops() {
|
||||
shops.clear();
|
||||
npcShops.clear();
|
||||
shops.invalidateAll();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user