Implement material exchange

This commit is contained in:
Melledy 2023-12-03 04:30:48 -08:00
parent 1d99769d4f
commit 6453c54777
3 changed files with 67 additions and 23 deletions

View File

@ -15,7 +15,8 @@ public class ItemComposeExcel extends GameResource {
private int ItemID;
private FormulaType FormulaType;
private List<ItemParam> MaterialCost;
private int[] SpecialMaterialCost;
private IntOpenHashSet SpecialMaterialCost;
private int SpecialMaterialCostNumber;
private int CoinCost;
private int WorldLevelRequire;
private IntOpenHashSet RelicList;

View File

@ -569,34 +569,68 @@ public class InventoryService extends BaseGameService {
return returnItems;
}
public List<GameItem> composeItem(Player player, int composeId, int count) {
public List<GameItem> composeItem(Player player, int composeId, int count, List<ItemParam> costItems) {
// Sanity check
if (count <= 0) return null;
// Get item compose excel data
ItemComposeExcel excel = GameData.getItemComposeExcelMap().get(composeId);
if (excel == null || excel.getFormulaType() != FormulaType.Normal) {
return null;
}
if (excel == null) return null;
// Verify items + credits
if (!player.getInventory().verifyItems(excel.getMaterialCost(), count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) {
return null;
}
// Pay items
player.getInventory().removeItemsByParams(excel.getMaterialCost(), count);
player.addSCoin(-excel.getCoinCost() * count);
// Compose item
// Composed item list
List<GameItem> items = new ArrayList<>();
GameItem item = new GameItem(excel.getItemID(), count);
items.add(item);
if (excel.getFormulaType() == FormulaType.Normal) { // Material synthesis
// Verify items + credits
if (!player.getInventory().verifyItems(excel.getMaterialCost(), count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) {
return null;
}
// Pay items
player.getInventory().removeItemsByParams(excel.getMaterialCost(), count);
player.addSCoin(-excel.getCoinCost() * count);
// Create item
items.add(new GameItem(excel.getItemID(), count));
} else if (excel.getFormulaType() == FormulaType.Sepcial) { // Material exchange
// Verify items
int totalAmount = 0;
for (ItemParam param : costItems) {
// Make sure param item is in special material cost
if (!excel.getSpecialMaterialCost().contains(param.getId())) {
return null;
}
// Make sure we have enough
GameItem costItem = player.getInventory().getItemByParam(param);
if (costItem == null) return null;
// Verify amount
if (costItem.getCount() >= param.getCount()) {
totalAmount += param.getCount();
}
}
// Sanity check the amount of materials were exchanging
if (totalAmount != count * excel.getSpecialMaterialCostNumber()) {
return null;
}
// Pay items
player.getInventory().removeItemsByParams(costItems, count);
// Create item
items.add(new GameItem(excel.getItemID(), count));
}
// Add items to inventory
player.getInventory().addItems(items);
return items;
if (items.size() > 0) {
player.getInventory().addItems(items);
return items;
} else {
return null;
}
}
public List<GameItem> composeRelic(Player player, int composeId, int relicId, int mainAffix, int count) {

View File

@ -1,9 +1,12 @@
package emu.lunarcore.server.packet.recv;
import java.util.ArrayList;
import java.util.List;
import emu.lunarcore.data.common.ItemParam;
import emu.lunarcore.game.inventory.GameItem;
import emu.lunarcore.proto.ComposeItemCsReqOuterClass.ComposeItemCsReq;
import emu.lunarcore.proto.ItemCostOuterClass.ItemCost;
import emu.lunarcore.server.game.GameSession;
import emu.lunarcore.server.packet.CmdId;
import emu.lunarcore.server.packet.Opcodes;
@ -17,10 +20,16 @@ public class HandlerComposeItemCsReq extends PacketHandler {
public void handle(GameSession session, byte[] data) throws Exception {
var req = ComposeItemCsReq.parseFrom(data);
List<ItemParam> costItems = new ArrayList<>(req.getComposeItemList().getItemList().length());
for (ItemCost cost : req.getComposeItemList().getItemList()) {
costItems.add(new ItemParam(cost));
}
List<GameItem> returnList = session.getServer().getInventoryService().composeItem(
session.getPlayer(),
req.getComposeId(),
req.getCount()
session.getPlayer(),
req.getComposeId(),
req.getCount(),
costItems
);
session.send(new PacketComposeItemScRsp(req.getComposeId(), req.getCount(), returnList));