Refactor shop functions

This commit is contained in:
Melledy
2023-11-18 23:55:40 -08:00
parent 839e18c2d0
commit fd011a97c4
4 changed files with 61 additions and 38 deletions

View File

@@ -586,43 +586,6 @@ public class InventoryService extends BaseGameService {
player.sendPacket(new PacketSellItemScRsp(returnItems));
}
public List<GameItem> buyShopGoods(Player player, int shopId, int goodsId, int count) {
// Get shop and goods excels
ShopExcel shop = GameData.getShopExcelMap().get(shopId);
if (shop == null) return null;
ShopGoodsExcel goods = shop.getGoods().get(goodsId);
if (goods == null) return null;
// Verify item params
if (!player.getInventory().verifyItems(goods.getCostList(), count)) {
return null;
}
// Pay items
player.getInventory().removeItemsByParams(goods.getCostList(), count);
// Buy items
List<GameItem> items = new ArrayList<>();
ItemExcel itemExcel = GameData.getItemExcelMap().get(goods.getItemID());
if (!itemExcel.isEquippable()) {
GameItem item = new GameItem(itemExcel, goods.getItemCount() * count);
items.add(item);
} else {
int num = goods.getItemCount() * count;
for (int i = 0; i < num; i++) {
GameItem item = new GameItem(itemExcel, 1);
items.add(item);
}
}
// Add to inventory
player.getInventory().addItems(items);
return items;
}
public List<GameItem> composeItem(Player player, int composeId, int count) {
// Sanity check
if (count <= 0) return null;

View File

@@ -0,0 +1,57 @@
package emu.lunarcore.game.shop;
import java.util.ArrayList;
import java.util.List;
import emu.lunarcore.data.GameData;
import emu.lunarcore.data.excel.ItemExcel;
import emu.lunarcore.data.excel.ShopExcel;
import emu.lunarcore.data.excel.ShopGoodsExcel;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.server.game.BaseGameService;
import emu.lunarcore.server.game.GameServer;
public class ShopService extends BaseGameService {
public ShopService(GameServer server) {
super(server);
}
public List<GameItem> buyGoods(Player player, int shopId, int goodsId, int count) {
// Get shop and goods excels
ShopExcel shop = GameData.getShopExcelMap().get(shopId);
if (shop == null) return null;
ShopGoodsExcel goods = shop.getGoods().get(goodsId);
if (goods == null) return null;
// Verify item params
if (!player.getInventory().verifyItems(goods.getCostList(), count)) {
return null;
}
// Pay items
player.getInventory().removeItemsByParams(goods.getCostList(), count);
// Buy items
List<GameItem> items = new ArrayList<>();
ItemExcel itemExcel = GameData.getItemExcelMap().get(goods.getItemID());
if (!itemExcel.isEquippable()) {
GameItem item = new GameItem(itemExcel, goods.getItemCount() * count);
items.add(item);
} else {
int num = goods.getItemCount() * count;
for (int i = 0; i < num; i++) {
GameItem item = new GameItem(itemExcel, 1);
items.add(item);
}
}
// Add to inventory
player.getInventory().addItems(items);
return items;
}
}

View File

@@ -13,6 +13,7 @@ import emu.lunarcore.game.drops.DropService;
import emu.lunarcore.game.gacha.GachaService;
import emu.lunarcore.game.inventory.InventoryService;
import emu.lunarcore.game.player.Player;
import emu.lunarcore.game.shop.ShopService;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import kcp.highway.ChannelConfig;
@@ -35,6 +36,7 @@ public class GameServer extends KcpServer {
@Getter private final DropService dropService;
@Getter private final InventoryService inventoryService;
@Getter private final GachaService gachaService;
@Getter private final ShopService shopService;
public GameServer(GameServerConfig serverConfig) {
// Game Server base
@@ -51,6 +53,7 @@ public class GameServer extends KcpServer {
this.dropService = new DropService(this);
this.inventoryService = new InventoryService(this);
this.gachaService = new GachaService(this);
this.shopService = new ShopService(this);
// Game loop
this.gameLoopTimer = new Timer();

View File

@@ -14,7 +14,7 @@ public class HandlerBuyGoodsCsReq extends PacketHandler {
public void handle(GameSession session, byte[] data) throws Exception {
var req = BuyGoodsCsReq.parseFrom(data);
var items = session.getServer().getInventoryService().buyShopGoods(session.getPlayer(), req.getShopId(), req.getGoodsId(), req.getGoodsNum());
var items = session.getServer().getShopService().buyGoods(session.getPlayer(), req.getShopId(), req.getGoodsId(), req.getGoodsNum());
session.send(new PacketBuyGoodsScRsp(req, items));
}