商店:一般购物流程

This commit is contained in:
luying
2021-03-31 17:52:34 +08:00
parent e487d07bfd
commit 8c8adacfdc
21 changed files with 1631 additions and 327 deletions

View File

@@ -0,0 +1,148 @@
import { Application, BackendSession } from "pinus";
import { gameData } from "../../../pubUtils/data";
import { resResult } from "../../../pubUtils/util";
import { STATUS, GUILD_STRUCTURE } from "../../../consts";
import { DicShopListModel } from "../../../db/DicShopList";
import { ShopItem } from "../../../domain/dbGeneral";
import { ShopItemListParam } from '../../../domain/roleField/shop';
import { UserShopModel } from "../../../db/UserShop";
import { handleCost, addItems } from "../../../services/rewardService";
import { GuildModel } from "../../../db/Guild";
export default function(app: Application) {
return new ShopHandler(app);
}
export class ShopHandler {
constructor(private app: Application) {
}
// 获得商品列表
async getShopList(msg: { shopId: number }, session: BackendSession) {
let roleId = session.get('roleId');
let { shopId } = msg;
if(!gameData.shopList.has(shopId)) {
return resResult(STATUS.WRONG_PARMS);
}
let shopItemList = new Array<ShopItemListParam>(); // 返回
let dbDicShop = await DicShopListModel.findByShopId(shopId);
let userShopRecs = await UserShopModel.findMapByShopId(roleId, shopId);
console.log(JSON.stringify([...userShopRecs]))
if(!dbDicShop || dbDicShop.useJson) { // 完全使用json中配置的商品数据库只做排序用数据库内没有的排到最后
let items = dbDicShop?.items||[];
let map = new Map<number, ShopItem>();
for(let item of items) {
map.set(item.id, item);
}
let dicShop = gameData.shop.get(shopId)||[];
for(let { id, type } of dicShop) {
let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;
if(map.has(id)) {
let item = map.get(id);
let param = new ShopItemListParam(id, item.discount, type, buyCount, item.order);
shopItemList.push(param);
} else {
let param = new ShopItemListParam(id, 1, type, buyCount, 0);
shopItemList.push(param);
}
}
} else { // 只返回数据库内的商品
let items = dbDicShop?.items||[];
for(let item of items) {
let { id, order, discount } = item;
let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;
let dicShop = gameData.shopItem.get(id);
if(dicShop) {
let param = new ShopItemListParam(id, discount, dicShop.type, buyCount, order);
shopItemList.push(param);
}
}
}
shopItemList = shopItemList.sort((a, b) => b.order - a.order);
return resResult(STATUS.SUCCESS, { shopItemList });
}
// 购买商品
async purchase(msg: { shopItemId: number, count: number }, session: BackendSession) {
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let sid = session.get('sid');
let guildCode = session.get('guildCode');
let serverId = session.get('serverId');
let { shopItemId, count } = msg;
let dicShopItem = gameData.shopItem.get(shopItemId);
if(!dicShopItem) return resResult(STATUS.DIC_DATA_NOT_FOUND);
if(dicShopItem.guildLvLimit) {
let myGuild = await GuildModel.findByCode(guildCode, serverId);
if(!myGuild) return resResult(STATUS.GUILD_LV_LIMIT);
let { structure } = myGuild;
let curStructure = structure.find(cur => cur.id == GUILD_STRUCTURE.STORE);
if(!curStructure || curStructure.lv < dicShopItem.guildLvLimit) {
return resResult(STATUS.GUILD_LV_LIMIT);
}
}
let dbShop = await DicShopListModel.findByShopId(dicShopItem.shop);
let discount = 1;
if(dbShop) {
let item = dbShop.items.find(cur => cur.id == shopItemId);
if(item) {
discount = item.discount;
}
}
let userShop = await UserShopModel.findByRoleAndItem(roleId, dicShopItem.id);
if(userShop && userShop.count + count > dicShopItem.purchaseLimit) {
return resResult(STATUS.BUY_COUNT_OVER);
}
// 消耗
let cost = [{
id: dicShopItem.money,
count: Math.round(dicShopItem.price * count * discount)
}];
let costResult = await handleCost(roleId, sid, cost);
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
// 次数
userShop = await UserShopModel.purchase(roleId, roleName, dicShopItem, count);
if(!userShop) return resResult(STATUS.BUY_COUNT_OVER);
// 获得
let reward = [{
id: dicShopItem.goodid,
count
}];
let goods = await addItems(roleId, roleName, sid, reward);
return resResult(STATUS.SUCCESS, {
shopItemId, count,
goods
});
}
// 将魂回收
async recycleSoul(msg: { goodsId: number, count: number }, session: BackendSession) {
}
// !测试接口初始化将json表中的内容加入数据库内
async initShopList() {
let index = gameData.shopItem.size;
for(let [id, dic] of gameData.shopItem) {
await DicShopListModel.createShop(dic.shop, true, { id, order: index--, discount: 1 })
}
}
}