xy-server/game/core/DebrisMall.ts
2025-04-23 09:34:08 +08:00

75 lines
2.3 KiB
TypeScript

import DB from "../utils/DB";
import {MsgCode} from "../role/EEnum";
import SKDataUtil from "../gear/SKDataUtil";
export default class DebrisMall {
static shared = new DebrisMall();
// 碎片物品编号
debris: number;
// 碎片商城数据
debris_mall_map: Map<number, any>;
constructor() {
this.debris = 70004;
this.debris_mall_map = new Map<number, any>();
}
init() {
DB.selectDebrisData((code: any, info: any) => {
if (MsgCode.SUCCESS == code){
this.debris_mall_map.clear();
for (const data of info) {
this.debris_mall_map.set(data.serial,{name: data.name, serial: data.serial, price: data.price, num: data.num, type: data.type});
}
}
});
}
/**
* 请求碎片商城
* @param player 角色信息
*/
getDebrisMallData(player: any){
let list: any[] = [];
for (let[k,v] of this.debris_mall_map) {
list.push(v);
}
player.send("s2c_debris_mall",{debris: SKDataUtil.toJson(list,"{}")});
}
/**
* 请求碎片商城兑换
* @param player 角色信息
* @param serial 物品编号
*/
getDebrisMallConvert(player: any, serial: number){
// 获取当前兑换商品信息
let debris_mall: any = this.debris_mall_map.get(serial);
// 获取玩家背包碎片数据
let item_count = player.getBagItemNum(this.debris);
if(debris_mall){
if (item_count < debris_mall.price) {
player.send('s2c_notice', {
strRichText: `碎片不足,无法兑换`,
});
player.send('s2c_debris_mall_convert', {serial: serial, state: MsgCode.FAILED, num: item_count});
return;
}else {
player.addBagItem(this.debris, -debris_mall.price, false);
player.addItem(serial,debris_mall.num, false, "碎片商城兑换!");
player.send('s2c_debris_mall_convert', {serial: serial, state: MsgCode.SUCCESS, num: (item_count - debris_mall.price)});
return;
}
}else{
player.send('s2c_notice', {
strRichText: `兑换商品不存在`,
});
}
}
}