mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-12 20:34:36 +01:00
Implement buying from mall shop
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import emu.nebula.data.BaseDef;
|
||||
import emu.nebula.data.ResourceType;
|
||||
import emu.nebula.game.inventory.ItemParamMap;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@@ -15,9 +16,17 @@ public class MallPackageDef extends BaseDef {
|
||||
private int CurrencyType;
|
||||
private int CurrencyItemId;
|
||||
private int CurrencyItemQty;
|
||||
private String Items;
|
||||
|
||||
private transient ItemParamMap products;
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return IdString.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
this.products = ItemParamMap.fromJsonString(this.Items);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,8 +558,35 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
|
||||
return change;
|
||||
}
|
||||
|
||||
public PlayerChangeInfo buyItem(int currencyId, int currencyCount, ItemParamMap buyItems, int buyCount) {
|
||||
return this.buyItem(currencyId, currencyCount, buyItems, buyCount, null);
|
||||
}
|
||||
|
||||
public PlayerChangeInfo buyItem(int currencyId, int currencyCount, ItemParamMap buyItems, int buyCount, PlayerChangeInfo change) {
|
||||
// Player change info
|
||||
if (change == null) {
|
||||
change = new PlayerChangeInfo();
|
||||
}
|
||||
|
||||
// Make sure we have the currency
|
||||
int cost = buyCount * currencyCount;
|
||||
|
||||
if (!this.verifyItem(currencyId, cost)) {
|
||||
return change;
|
||||
}
|
||||
|
||||
// Remove currency item
|
||||
this.removeItem(currencyId, cost, change);
|
||||
|
||||
// Add items
|
||||
this.addItems(buyItems.mulitply(buyCount), change);
|
||||
|
||||
// Success
|
||||
return change.setSuccess(true);
|
||||
}
|
||||
|
||||
public PlayerChangeInfo useItem(int id, int count, PlayerChangeInfo change) {
|
||||
// Changes
|
||||
// Player change info
|
||||
if (change == null) {
|
||||
change = new PlayerChangeInfo();
|
||||
}
|
||||
@@ -597,7 +624,7 @@ public class Inventory extends PlayerManager implements GameDatabaseObject {
|
||||
}
|
||||
|
||||
// Success
|
||||
return change;
|
||||
return change.setSuccess(true);
|
||||
}
|
||||
|
||||
// Database
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package emu.nebula.server.handlers;
|
||||
|
||||
import emu.nebula.net.NetHandler;
|
||||
import emu.nebula.net.NetMsgId;
|
||||
import emu.nebula.proto.MallShopOrder.MallShopOrderReq;
|
||||
import emu.nebula.net.HandlerId;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.net.GameSession;
|
||||
|
||||
@HandlerId(NetMsgId.mall_shop_order_req )
|
||||
public class HandlerMallShopOrderReq extends NetHandler {
|
||||
|
||||
@Override
|
||||
public byte[] handle(GameSession session, byte[] message) throws Exception {
|
||||
// Parse request
|
||||
var req = MallShopOrderReq.parseFrom(message);
|
||||
|
||||
// Get package data
|
||||
var data = GameData.getMallPackageDataTable().get(req.getId().hashCode());
|
||||
if (data == null) {
|
||||
return session.encodeMsg(NetMsgId.mall_shop_order_failed_ack);
|
||||
}
|
||||
|
||||
// Buy items
|
||||
var change = session.getPlayer().getInventory().buyItem(
|
||||
data.getCurrencyItemId(),
|
||||
data.getCurrencyItemQty(),
|
||||
data.getProducts(),
|
||||
req.getQty()
|
||||
);
|
||||
|
||||
if (change == null) {
|
||||
return session.encodeMsg(NetMsgId.mall_shop_order_failed_ack);
|
||||
}
|
||||
|
||||
// Encode and send
|
||||
return session.encodeMsg(NetMsgId.mall_shop_order_succeed_ack, change.toProto());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user