Skip to content

Commit

Permalink
Dump tradable shops items to database
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Nov 9, 2023
1 parent fe79398 commit 36d6aed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ private void updateExistingTables(final DBTransaction transaction) throws SQLExc
if (!transaction.doesColumnExist("npcs", "hide_location")) {
transaction.execute("ALTER TABLE npcs ADD COLUMN (hide_location TINYINT DEFAULT 0);", null);
}

// 1.45: add trade for column to shopinventoryinfo
if (!transaction.doesColumnExist("shopinventoryinfo", "trade_for")) {
transaction.execute("ALTER TABLE shopinventoryinfo ADD COLUMN (trade_for VARCHAR(1000));", null);
}
}


Expand Down
21 changes: 18 additions & 3 deletions src/games/stendhal/server/core/engine/db/StendhalShopDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import games.stendhal.server.entity.npc.shop.ShopInventory;
import games.stendhal.server.entity.npc.shop.ShopType;
import games.stendhal.server.entity.npc.shop.ShopsList;
import marauroa.common.Pair;
import marauroa.server.db.DBTransaction;
import marauroa.server.game.db.CharacterDAO;
import marauroa.server.game.db.DAORegister;
Expand All @@ -39,7 +40,7 @@ public class StendhalShopDAO extends CharacterDAO {
private static Logger logger = Logger.getLogger(StendhalCreatureDAO.class);

/*
* Shop name type ["buy", "sell", "outfit"]
* Shop name type ["buy", "sell", "outfit", "trade"]
*
*
* Item name price
Expand Down Expand Up @@ -122,12 +123,25 @@ private void dumpShopInventory(PreparedStatement stmt, ShopInventory<?, ?> shop,
} else if (shop instanceof OutfitShopInventory){
outfit = ((OutfitShopInventory) shop).get(name).first();
}
String tradeFor = null;
final List<Pair<String, Integer>> tradeList = shop.getTradeFor(name);
if (tradeList != null && tradeList.size() > 0) {
tradeFor = "";
for (final Pair<String, Integer> tradeItem: tradeList) {
if (!tradeFor.isEmpty()) {
tradeFor += ",";
}
tradeFor += tradeItem.first() + ":" + tradeItem.second();
}
}

stmt.setInt(1, 1);
stmt.setObject(2, shopId);
stmt.setString(3, name);
stmt.setObject(4, shop.getPrice(name));
stmt.setObject(5, itemId);
stmt.setObject(6, outfit);
stmt.setObject(7, tradeFor);
stmt.addBatch();
}
}
Expand All @@ -137,8 +151,8 @@ private void dumpShopIventories(DBTransaction transaction) throws SQLException {

List<ShopInventory<?, ?>> shops = getShops();
PreparedStatement stmt = transaction.prepareStatement("INSERT INTO shopinventoryinfo "
+ "(active, shopinfo_id, name, price, iteminfo_id, outfit) "
+ "VALUES (?, ?, ?, ?, ?, ?);", null);
+ "(active, shopinfo_id, name, price, iteminfo_id, outfit, trade_for) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?);", null);
Map<String, Integer> shopIdMap = getShopIdMap(transaction);
Map<String, Integer> itemInfoIdMap = DAORegister.get().get(StendhalItemDAO.class).getItemInfoIdMap(transaction);
for (ShopInventory<?, ?> shop : shops) {
Expand Down Expand Up @@ -189,6 +203,7 @@ private void dumpShopOwners(DBTransaction transaction) throws SQLException {
List<ShopInventory<?, ?>> shops = new LinkedList<>();
shops.addAll(ShopsList.get().getContents(ShopType.ITEM_BUY).values());
shops.addAll(ShopsList.get().getContents(ShopType.ITEM_SELL).values());
shops.addAll(ShopsList.get().getContents(ShopType.TRADE).values());
shops.addAll(OutfitShopsList.get().getContents().values());
return shops;
}
Expand Down
1 change: 1 addition & 0 deletions src/games/stendhal/server/stendhal_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ CREATE TABLE IF NOT EXISTS shopinventoryinfo (
price INT,
iteminfo_id INT,
outfit VARCHAR(1000),
trade_for VARCHAR(1000),
PRIMARY KEY (id)
);

Expand Down

0 comments on commit 36d6aed

Please sign in to comment.