商店:基础数据及购买
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Application, BackendSession } from "pinus";
|
||||
import { gameData } from "../../../pubUtils/data";
|
||||
import { gameData, hasShopType } from "../../../pubUtils/data";
|
||||
import { parseGoodStr, resResult } from "../../../pubUtils/util";
|
||||
import { STATUS, GUILD_STRUCTURE, ITID, CONSUME_TYPE, HERO_QUALITY_TYPE, HERO_GROW_MAX, ITEM_CHANGE_REASON } from "../../../consts";
|
||||
import { DicShopListModel } from "../../../db/DicShopList";
|
||||
@@ -8,7 +8,7 @@ import { handleCost, addItems } from "../../../services/role/rewardService";
|
||||
import { GuildModel } from "../../../db/Guild";
|
||||
import { SHOP } from "../../../pubUtils/dicParam";
|
||||
import { HeroModel } from "../../../db/Hero";
|
||||
import { getShopListById } from "../../../services/shopService";
|
||||
import { getShopDicById, getShopListByType, getShopPrice } from "../../../services/shopService";
|
||||
import { RewardInter } from "../../../pubUtils/interface";
|
||||
import { RoleModel } from "../../../db/Role";
|
||||
|
||||
@@ -21,20 +21,21 @@ export class ShopHandler {
|
||||
}
|
||||
|
||||
// 获得商品列表
|
||||
async getShopList(msg: { shopId: number }, session: BackendSession) {
|
||||
async getShopList(msg: { shop: number, type: number }, session: BackendSession) {
|
||||
let roleId = session.get('roleId');
|
||||
let { shopId } = msg;
|
||||
let serverId = session.get('serverId');
|
||||
let { shop, type } = msg;
|
||||
|
||||
if(!gameData.shopList.has(shopId)) {
|
||||
if(!hasShopType(shop, type)) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
|
||||
const shopItemList = await getShopListById(shopId, roleId);
|
||||
return resResult(STATUS.SUCCESS, { shopItemList });
|
||||
const result = await getShopListByType(shop, type, roleId, serverId);
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
// 购买商品
|
||||
async purchase(msg: { shopItemId: number, count: number }, session: BackendSession) {
|
||||
async purchase(msg: { activityId: number, shopItemId: number, count: number }, session: BackendSession) {
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let sid = session.get('sid');
|
||||
@@ -42,24 +43,24 @@ export class ShopHandler {
|
||||
let serverId = session.get('serverId');
|
||||
let vipStartTime: number = session.get('vipStartTime');
|
||||
|
||||
let { shopItemId, count } = msg;
|
||||
let { activityId, shopItemId, count } = msg;
|
||||
if( count < 0) return resResult(STATUS.WRONG_PARMS);
|
||||
|
||||
let dicShopItem = gameData.shopItem.get(shopItemId);
|
||||
let dicShopItem = await getShopDicById(activityId, shopItemId, roleId, serverId);
|
||||
if(!dicShopItem) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
||||
|
||||
if(dicShopItem.guildLvLimit) {
|
||||
let myGuild = await GuildModel.findByCode(guildCode, serverId);
|
||||
if(!myGuild || myGuild.lv < dicShopItem.guildLvLimit) return resResult(STATUS.GUILD_LV_LIMIT);
|
||||
}
|
||||
if(dicShopItem.lvlimit) {
|
||||
if(dicShopItem.lvLimit) {
|
||||
let role = await RoleModel.findByRoleId(roleId, 'lv');
|
||||
if(role.lv < dicShopItem.lvlimit) {
|
||||
if(role.lv < dicShopItem.lvLimit) {
|
||||
return resResult(STATUS.LV_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
let userShop = await UserShopModel.findByRoleAndItem(roleId, dicShopItem.id);
|
||||
let userShop = await UserShopModel.findByRoleAndItem(roleId, activityId, dicShopItem);
|
||||
if(dicShopItem.purchaseLimit != -1) {
|
||||
if(vipStartTime > 0 && dicShopItem.purchaseLimit != -1) {
|
||||
if(userShop && userShop.count + count > dicShopItem.purchaseLimit + dicShopItem.vipPurchaseLimit) {
|
||||
@@ -73,20 +74,17 @@ export class ShopHandler {
|
||||
}
|
||||
|
||||
// 消耗
|
||||
let cost = [{
|
||||
id: dicShopItem.money,
|
||||
count: Math.round(dicShopItem.price * count)
|
||||
}];
|
||||
let cost = getShopPrice(dicShopItem.money, count, userShop?.count||0, dicShopItem.price);
|
||||
let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.SHOP_PURCHASE);
|
||||
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
|
||||
// 次数
|
||||
userShop = await UserShopModel.purchase(roleId, roleName, dicShopItem, count);
|
||||
userShop = await UserShopModel.purchase(roleId, roleName, activityId, dicShopItem, count);
|
||||
if(!userShop) return resResult(STATUS.BUY_COUNT_OVER);
|
||||
|
||||
// 获得
|
||||
let reward = [{
|
||||
id: dicShopItem.goodid,
|
||||
id: dicShopItem.goodId,
|
||||
count
|
||||
}];
|
||||
let goods = await addItems(roleId, roleName, sid, reward, ITEM_CHANGE_REASON.SHOP_PURCHASE);
|
||||
|
||||
Reference in New Issue
Block a user