mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 22:04:36 +01:00
Refactor shop functions
This commit is contained in:
@@ -586,43 +586,6 @@ public class InventoryService extends BaseGameService {
|
|||||||
player.sendPacket(new PacketSellItemScRsp(returnItems));
|
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) {
|
public List<GameItem> composeItem(Player player, int composeId, int count) {
|
||||||
// Sanity check
|
// Sanity check
|
||||||
if (count <= 0) return null;
|
if (count <= 0) return null;
|
||||||
|
|||||||
57
src/main/java/emu/lunarcore/game/shop/ShopService.java
Normal file
57
src/main/java/emu/lunarcore/game/shop/ShopService.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import emu.lunarcore.game.drops.DropService;
|
|||||||
import emu.lunarcore.game.gacha.GachaService;
|
import emu.lunarcore.game.gacha.GachaService;
|
||||||
import emu.lunarcore.game.inventory.InventoryService;
|
import emu.lunarcore.game.inventory.InventoryService;
|
||||||
import emu.lunarcore.game.player.Player;
|
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.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import kcp.highway.ChannelConfig;
|
import kcp.highway.ChannelConfig;
|
||||||
@@ -35,6 +36,7 @@ public class GameServer extends KcpServer {
|
|||||||
@Getter private final DropService dropService;
|
@Getter private final DropService dropService;
|
||||||
@Getter private final InventoryService inventoryService;
|
@Getter private final InventoryService inventoryService;
|
||||||
@Getter private final GachaService gachaService;
|
@Getter private final GachaService gachaService;
|
||||||
|
@Getter private final ShopService shopService;
|
||||||
|
|
||||||
public GameServer(GameServerConfig serverConfig) {
|
public GameServer(GameServerConfig serverConfig) {
|
||||||
// Game Server base
|
// Game Server base
|
||||||
@@ -51,6 +53,7 @@ public class GameServer extends KcpServer {
|
|||||||
this.dropService = new DropService(this);
|
this.dropService = new DropService(this);
|
||||||
this.inventoryService = new InventoryService(this);
|
this.inventoryService = new InventoryService(this);
|
||||||
this.gachaService = new GachaService(this);
|
this.gachaService = new GachaService(this);
|
||||||
|
this.shopService = new ShopService(this);
|
||||||
|
|
||||||
// Game loop
|
// Game loop
|
||||||
this.gameLoopTimer = new Timer();
|
this.gameLoopTimer = new Timer();
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class HandlerBuyGoodsCsReq extends PacketHandler {
|
|||||||
public void handle(GameSession session, byte[] data) throws Exception {
|
public void handle(GameSession session, byte[] data) throws Exception {
|
||||||
var req = BuyGoodsCsReq.parseFrom(data);
|
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));
|
session.send(new PacketBuyGoodsScRsp(req, items));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user