xy-server/game/core/MallMgr.ts

371 lines
13 KiB
TypeScript
Raw Permalink Normal View History

2025-04-23 09:34:08 +08:00
import SKDataUtil from "../gear/SKDataUtil";
import GameUtil from "./GameUtil";
import ItemUtil from "./ItemUtil";
import Player from "../object/Player";
import DB from "../utils/DB";
import {MsgCode, Operate} from "../role/EEnum";
import SKCxfUtil from "../gear/SKCxfUtil";
export default class MallMgr {
static shared = new MallMgr();
// 新鲜玩意
freshList: any;
// 强化材料
mallList: any;
// 每日限购
dayList: any;
// 每日推荐
recList: any;
// 积分兑换
integralList: any;
//绑定仙玉
bindList: any
mapNpcShop: any;
constructor() {
this.mapNpcShop = {};
}
init() {
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code){
// 新鲜玩意
let freshList: any = {}
for (const propFresh of info.propFresh) {
freshList[propFresh.id] = propFresh;
}
this.freshList = freshList;
// 强化材料
let mallList: any = {}
for (const propMall of info.propMall) {
mallList[propMall.id] = propMall;
}
this.mallList = mallList;
// 每日限购
let dayList: any = {}
for (const propDay of info.propDayLimit) {
dayList[propDay.id] = propDay;
}
this.dayList = dayList;
// 每日推荐
let recList: any = {}
for (const propRec of info.propRecommend) {
recList[propRec.id] = propRec;
}
this.recList = recList;
// 积分兑换
let integralList: any = {}
for (const propInt of info.propIntegral) {
integralList[propInt.id] = propInt;
}
this.integralList = integralList;
// 绑定仙玉
let bindList: any = {}
for (const propInt of info.propBindJade) {
bindList[propInt.id] = propInt;
}
this.bindList = bindList;
}
});
// NPC商店
let npcdata = GameUtil.require_ex('../../conf/prop_data/prop_npc_shop');
for (let _ in npcdata) {
const npcmall = npcdata[_];
if (this.mapNpcShop[npcmall.npcid] == null) {
this.mapNpcShop[npcmall.npcid] = { goods: [] }
}
let mtype = npcmall.type == '' ? null : npcmall.type;
this.mapNpcShop[npcmall.npcid].goods.push({
itemid: npcmall.itemid,
moneykind: npcmall.kind,
price: npcmall.price,
type: mtype,
})
}
}
// 同步新鲜玩意
syncFresh(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 新鲜玩意
this.freshList ={};
let freshList: any = {};
for (const propFresh of info.propFresh) {
freshList[propFresh.id] = propFresh;
}
this.freshList = freshList;
}
});
}
// 同步强化材料
syncIntensify(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 强化材料
this.mallList = {};
let mallList: any = {};
for (const propMall of info.propMall) {
mallList[propMall.id] = propMall;
}
this.mallList = mallList;
}
});
}
// 同步限购商店
syncQuota(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 每日限购
this.dayList = {};
let dayList: any = {}
for (const propDay of info.propDayLimit) {
dayList[propDay.id] = propDay;
}
this.dayList = dayList;
}
});
}
// 同步每日推荐
syncRecommend(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 每日限购
this.recList = {};
let recList: any = {}
for (const propRec of info.propRecommend) {
recList[propRec.id] = propRec;
}
this.recList = recList;
}
});
}
// 同步积分商店
syncIntegral(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 每日限购
this.integralList = {};
let integralList: any = {}
for (const propInte of info.propIntegral) {
integralList[propInte.id] = propInte;
}
this.integralList = integralList;
}
});
}
// 同步绑定商店
syncBindJade(){
DB.selectShopData((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
// 每日限购
this.bindList = {};
let bindList: any = {}
for (const propInte of info.propBindJade) {
bindList[propInte.id] = propInte;
}
this.bindList = bindList;
}
});
}
checkNpcData(npcid: any) {
if (this.mapNpcShop.hasOwnProperty(npcid) == false)
return false;
return true;
}
getNpcShopData(npcId: any) {
return this.mapNpcShop[npcId];
}
getMallData(type: number, mallid: any): any {
if (type == 0) {
return this.freshList[mallid];
} else if (type == 1) {
return this.mallList[mallid];
} else if (type == 2) {
return this.dayList[mallid];
} else if (type == 3) {
return this.recList[mallid];
} else if (type == 4){
return this.integralList[mallid];
} else if (type == 5){
return this.bindList[mallid];
}
return null;
}
buyItem(player: Player, type: number, mallId: any, num: any) {
if (player.getBagItemAllKindNum() >= player.bagKindNum) {
player.send('s2c_notice', {
strRichText: '背包已满,无法购买'
});
return;
}
let mallData = this.getMallData(type, mallId);
if (!mallData) {
player.send('s2c_notice', {
strRichText: `商品[${mallId}]不存在!`
});
return;
}
let itemData = ItemUtil.getItemData(mallData.itemid);
if (!itemData) {
player.send('s2c_notice', {
strRichText: `商品[${mallId}]定义不存在!`
});
return;
}
if (num < 1) {
player.send('s2c_notice', {
strRichText: `商品[${itemData.name}]购买数量必须大于0个`
});
return;
}
let current = player.getDayCount(mallId);
if (mallData.time > 0) {
num = Math.min(num, mallData.time - current);
if (num < 1) {
player.send('s2c_notice', {
strRichText: `[${itemData.name}]已购买${mallData.time}个达到今日可购上限!`
})
return;
}
}
let discount = 0; //打折后的价格
if(mallData.discount > 0){
discount = Math.floor(mallData.price * (mallData.discount / 100));
}else{
discount = mallData.price;
}
let cost = discount * num;
if(type == 4){
if(mallData.item_kind == 4){ //功绩兑换
if(player.shuilu.gongji < cost){
player.send_notice("您的积分不够,不能兑换!");
return;
}
player.shuilu.gongji -= cost
player.addItem(mallData.itemid, num, true, '积分购物');
player.send('s2c_player_data', player.getData());
}else if(mallData.item_kind == 6){ //竞技场积分兑换
if(player.arena.jpoint < cost){
player.send_notice("您的积分不够,不能兑换!");
return;
}
player.arena.jpoint -= cost;
player.addItem(mallData.itemid, num, true, '积分购物');
player.send('s2c_player_data', player.getData());
}else if(mallData.item_kind == 5){ //地宫积分兑换
if(player.discore < cost){
player.send_notice("您的积分不够,不能兑换!");
return;
}
player.addMoney(GameUtil.goldKind.di_Score, -cost, "积分购物")
player.addItem(mallData.itemid, num, true, '积分购物');
player.send('s2c_player_data', player.getData());
}else if(mallData.item_kind == 2){ //除魔积分兑换
if(player.xiuscore < cost){
player.send_notice("您的积分不够,不能兑换!");
return;
}
player.addMoney(GameUtil.goldKind.xiu_Score, -cost, "积分购物")
player.addItem(mallData.itemid, num, true, '积分购物');
player.send('s2c_player_data', player.getData());
}else if(mallData.item_kind == 3){ //郭氏积分兑换
if(player.guoscore < cost){
player.send_notice("您的积分不够,不能兑换!");
return;
}
player.addMoney(GameUtil.goldKind.guo_Score, -cost, "积分购物")
player.addItem(mallData.itemid, num, true, '积分购物');
player.send('s2c_player_data', player.getData());
}else{
player.send_notice("这个商店还没开通哦!")
}
}else{
let info = '';
if(type == 5){
info = player.CostFee(2, cost, `从多宝购买${num}${itemData.name}`);
}else{
info = player.CostFee(1, cost, `从多宝购买${num}${itemData.name}`);
}
if (info != '') {
player.send('s2c_notice', {
strRichText: info
});
return;
}
player.addDayCount(mallId, num);
player.addItem(mallData.itemid, num, true, '多宝购物');
if(player){
SKCxfUtil.getCxfRecordOperate({
roleId: player.roleid,
roleName: player.name,
operateType: Operate.PLAYER,
operateDepict: "购买物品",
operateResSerial: mallData.itemid,
operateResName: itemData.name,
operateContent: SKDataUtil.toJson(player.jade, "{}")
})
}
}
this.sendList(player);
}
// 发送商店列表
sendList(player: Player) {
if (player == null) {
return;
}
let freshList = this.freshList;
let mallList = this.mallList;
let dayList = this.dayList;
let recList = this.recList;
let bindList = this.bindList;
for (let key in dayList) {
let item = dayList[key];
item.count = player.getDayCount(item.id);
if(item.discount > 0){
item.discountPrice = Math.floor(item.price * (item.discount / 100));
}else{
item.discountPrice = item.price;
}
}
for (let key in recList) {
let item = recList[key];
item.count = player.getDayCount(item.id);
if(item.discount > 0){
item.discountPrice = Math.floor(item.price * (item.discount / 100));
}else{
item.discountPrice = item.price;
}
}
for (let key in bindList) {
let item = bindList[key];
item.count = player.getDayCount(item.id);
if(item.discount > 0){
item.discountPrice = Math.floor(item.price * (item.discount / 100));
}else{
item.discountPrice = item.price;
}
}
player.send('s2c_mallitems', {
info: SKDataUtil.toJson([freshList, mallList, dayList, recList, bindList],"[]")
});
}
// 发送积分商店列表
sendIntegralList(player: Player) {
if (player == null) {
return;
}
let integralList = this.integralList;
player.send('s2c_integralmall', {
info: SKDataUtil.toJson(integralList,"[]")
});
}
}