商店:rmb购买
This commit is contained in:
@@ -2,15 +2,13 @@ import { Application, BackendSession } from "pinus";
|
||||
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";
|
||||
import { UserShopModel } from "../../../db/UserShop";
|
||||
import { handleCost, addItems } from "../../../services/role/rewardService";
|
||||
import { GuildModel } from "../../../db/Guild";
|
||||
import { SHOP } from "../../../pubUtils/dicParam";
|
||||
import { HeroModel } from "../../../db/Hero";
|
||||
import { getShopDicById, getShopListByType, getShopPrice } from "../../../services/shopService";
|
||||
import { checkShopInPurchase, getShopDicById, getShopListByType, getShopPrice } from "../../../services/shopService";
|
||||
import { RewardInter } from "../../../pubUtils/interface";
|
||||
import { RoleModel } from "../../../db/Role";
|
||||
import { UserShopTypeModel } from "../../../db/UserShopType";
|
||||
|
||||
export default function(app: Application) {
|
||||
return new ShopHandler(app);
|
||||
@@ -34,43 +32,38 @@ export class ShopHandler {
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
// 读取红点
|
||||
async readShop(msg: { shop: number, type: number }, session: BackendSession) {
|
||||
let roleId = session.get('roleId');
|
||||
let { shop, type } = msg;
|
||||
|
||||
if(!hasShopType(shop, type)) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
|
||||
await UserShopTypeModel.read(roleId, shop, type);
|
||||
return resResult(STATUS.SUCCESS);
|
||||
}
|
||||
|
||||
// 购买商品
|
||||
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');
|
||||
let guildCode = session.get('guildCode');
|
||||
let serverId = session.get('serverId');
|
||||
let vipStartTime: number = session.get('vipStartTime');
|
||||
|
||||
let { activityId, shopItemId, count } = msg;
|
||||
if( count < 0) return resResult(STATUS.WRONG_PARMS);
|
||||
|
||||
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) {
|
||||
let role = await RoleModel.findByRoleId(roleId, 'lv');
|
||||
if(role.lv < dicShopItem.lvLimit) {
|
||||
return resResult(STATUS.LV_LIMIT);
|
||||
}
|
||||
}
|
||||
if(!dicShopItem) return STATUS.DIC_DATA_NOT_FOUND;
|
||||
if(dicShopItem['productID']) return resResult(STATUS.CAN_NOT_PURCHASE);
|
||||
|
||||
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) {
|
||||
return resResult(STATUS.BUY_COUNT_OVER);
|
||||
}
|
||||
} else {
|
||||
if(userShop && userShop.count + count > dicShopItem.purchaseLimit) {
|
||||
return resResult(STATUS.BUY_COUNT_OVER);
|
||||
}
|
||||
}
|
||||
|
||||
let checkResult = await checkShopInPurchase(session, activityId, count, userShop?.count||0, dicShopItem);
|
||||
if(checkResult.code != STATUS.SUCCESS.code) {
|
||||
return resResult(checkResult);
|
||||
}
|
||||
|
||||
// 消耗
|
||||
|
||||
@@ -33,15 +33,20 @@ import { addGuildPay } from './activity/guildPayService';
|
||||
import { sendMessageToUserWithSuc } from './pushService';
|
||||
import { gameData } from '../pubUtils/data';
|
||||
import { checkParamPrice } from '../pubUtils/sdkUtil';
|
||||
import { checkShopCanBuyInOrder, makeShopOrder } from './shopService';
|
||||
|
||||
export async function checkOrderCanBuy(roleId: string, serverId: number, activityData: ActivityModelType, productID: string, paramStr: string) {
|
||||
let activityId = activityData.activityId;
|
||||
console.log('*****', activityData.type)
|
||||
|
||||
switch(activityData.type) {
|
||||
case ACTIVITY_TYPE.POP_UP_SHOP:
|
||||
{
|
||||
return await checkPopUpShopCanBuy(roleId, serverId, activityId, productID, paramStr)
|
||||
}
|
||||
case ACTIVITY_TYPE.SHOP:
|
||||
{
|
||||
return await checkShopCanBuyInOrder(roleId, serverId, activityData, productID)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -151,6 +156,12 @@ export async function makeOrder(orderInfo: UserOrderModelType, sid: string) {
|
||||
rewardResult = await makeTaskPass(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
|
||||
break;
|
||||
|
||||
}
|
||||
case ACTIVITY_TYPE.SHOP: // 商店
|
||||
{
|
||||
rewardResult = await makeShopOrder(roleId, roleInfo.roleName, sid, orderInfo.serverId, orderInfo.activityId, orderInfo.productID)
|
||||
break;
|
||||
|
||||
}
|
||||
default:
|
||||
rewardResult = STATUS.ERROR_TYPE;
|
||||
|
||||
@@ -1,18 +1,28 @@
|
||||
import { gameData, getShopType } from "../pubUtils/data";
|
||||
import { ShopData, ShopReturnData } from '../domain/roleField/shop';
|
||||
import { ShopData, ShopDicExtendData, ShopReturnData } from '../domain/roleField/shop';
|
||||
import { UserShopModel, UserShopType } from "../db/UserShop";
|
||||
import { getActivitiesByType, getActivityById } from "./activity/activityService";
|
||||
import { ACTIVITY_TYPE } from "../consts";
|
||||
import { ACTIVITY_TYPE, ITEM_CHANGE_REASON, STATUS } from "../consts";
|
||||
import { getRoleCreateTime, getServerCreateTime } from "./redisService";
|
||||
import { decodeArrayListStr } from "../pubUtils/util";
|
||||
import { UserShopTypeModel, UserShopTypeType } from "../db/UserShopType";
|
||||
import { GuildModel } from "../db/Guild";
|
||||
import { RoleModel } from "../db/Role";
|
||||
import { DicShop } from "../pubUtils/dictionary/DicShop";
|
||||
import { BackendSession } from "pinus";
|
||||
import { ActivityModelType } from "../db/Activity";
|
||||
import { addItems } from "./role/rewardService";
|
||||
|
||||
export async function getAllShopList(roleId: string, serverId: number) {
|
||||
let userShopRecs = await UserShopModel.findByRoleId(roleId);
|
||||
let userShopTypeRecs = await UserShopTypeModel.findByRoleId(roleId);
|
||||
let activities = await getShopActivityDatas(roleId, serverId);
|
||||
|
||||
let shops: ShopReturnData[] = [];
|
||||
for(let [_, { shop, type } ] of gameData.shopType) {
|
||||
let shopData = await getShopListByData(shop, type, userShopRecs, activities);
|
||||
let activity = activities.find(cur => cur.shop == shop && cur.type == type);
|
||||
let readRecord = userShopTypeRecs.find(cur => cur.shop == shop && cur.type == type);
|
||||
let shopData = await getShopListByData(shop, type, userShopRecs, activity, readRecord);
|
||||
shops.push(shopData);
|
||||
}
|
||||
return shops;
|
||||
@@ -21,20 +31,22 @@ export async function getAllShopList(roleId: string, serverId: number) {
|
||||
export async function getShopListByType(shop: number, type: number, roleId: string, serverId: number) {
|
||||
let userShopRecs = await UserShopModel.findByShopType(roleId, shop, type);
|
||||
let activities = await getShopActivityDatas(roleId, serverId);
|
||||
return await getShopListByData(shop, type, userShopRecs, activities);
|
||||
let activity = activities.find(cur => cur.shop == shop && cur.type == type);
|
||||
let readRecord = await UserShopTypeModel.findByType(roleId, shop, type);
|
||||
return await getShopListByData(shop, type, userShopRecs, activity, readRecord);
|
||||
}
|
||||
|
||||
async function getShopListByData(shop: number, type: number, userShopRecs: UserShopType[], activities: ShopData[]) {
|
||||
async function getShopListByData(shop: number, type: number, userShopRecs: UserShopType[], activity: ShopData, readRecord: UserShopTypeType) {
|
||||
let result = new ShopReturnData(shop, type);
|
||||
|
||||
let dicShopType = getShopType(shop, type);
|
||||
if(dicShopType && dicShopType.isLimit) {
|
||||
for(let activity of activities) {
|
||||
result.setDicByActivity(activity);
|
||||
}
|
||||
if(activity) result.setDicByActivity(activity);
|
||||
if(readRecord) result.setReadRecord(readRecord);
|
||||
}
|
||||
console.log('###### getShopListByData', userShopRecs)
|
||||
for(let userShop of userShopRecs) {
|
||||
result.setUserRecords(userShop);
|
||||
result.addUserRecords(userShop);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -101,4 +113,94 @@ function parseShopPrice(str: string) {
|
||||
result.push({ times: parseInt(times), price: parseInt(price) });
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export async function checkShopInPurchase(session: BackendSession, activityId: number, count: number, buyCount: number, dicShopItem: DicShop|ShopDicExtendData) {
|
||||
let roleId = session.get('roleId');
|
||||
let guildCode = session.get('guildCode');
|
||||
let serverId = session.get('serverId');
|
||||
let vipStartTime: number = session.get('vipStartTime');
|
||||
|
||||
return await checkShopItemCanBuy(activityId, dicShopItem.id, roleId, serverId, guildCode, vipStartTime, count, buyCount, dicShopItem)
|
||||
}
|
||||
|
||||
export async function checkShopCanBuyInOrder(roleId: string, serverId: number, activity: ActivityModelType, productID: string) {
|
||||
let role = await RoleModel.findByRoleId(roleId, 'guildCode createTime vipStartTime');
|
||||
let { createTime, guildCode, vipStartTime } = role;
|
||||
let serverTime = await getServerCreateTime(serverId);
|
||||
let shopData = new ShopData(activity, createTime, serverTime);
|
||||
if(!shopData) return false;
|
||||
let dicItem = shopData.findByProductID(productID);
|
||||
if(!dicItem) return false;
|
||||
|
||||
let userShop = await UserShopModel.findByRoleAndItem(roleId, activity.activityId, dicItem);
|
||||
let result = await checkShopItemCanBuy(activity.activityId, dicItem.id, roleId, serverId, guildCode, vipStartTime, 1, userShop?.count||0, dicItem);
|
||||
return result.code == STATUS.SUCCESS.code;
|
||||
}
|
||||
|
||||
export async function checkShopItemCanBuy(activityId: number, shopItemId: number, roleId: string, serverId: number, guildCode: string, vipStartTime: number, count: number, buyCount: number, dicShopItem?: DicShop|ShopDicExtendData) {
|
||||
|
||||
if(!dicShopItem) {
|
||||
dicShopItem = await getShopDicById(activityId, shopItemId, roleId, serverId);
|
||||
}
|
||||
if(!dicShopItem) return STATUS.DIC_DATA_NOT_FOUND;
|
||||
|
||||
if(dicShopItem.guildLvLimit) {
|
||||
let myGuild = await GuildModel.findByCode(guildCode, serverId);
|
||||
if(!myGuild || myGuild.lv < dicShopItem.guildLvLimit) return STATUS.GUILD_LV_LIMIT;
|
||||
}
|
||||
if(dicShopItem.lvLimit) {
|
||||
let role = await RoleModel.findByRoleId(roleId, 'lv');
|
||||
if(role.lv < dicShopItem.lvLimit) {
|
||||
return STATUS.LV_LIMIT;
|
||||
}
|
||||
}
|
||||
|
||||
if(dicShopItem.purchaseLimit != -1) {
|
||||
if(vipStartTime > 0 && dicShopItem.vipPurchaseLimit != -1) {
|
||||
if(buyCount + count > dicShopItem.purchaseLimit + dicShopItem.vipPurchaseLimit) {
|
||||
return STATUS.BUY_COUNT_OVER;
|
||||
}
|
||||
} else {
|
||||
if(buyCount + count > dicShopItem.purchaseLimit) {
|
||||
return STATUS.BUY_COUNT_OVER;
|
||||
}
|
||||
}
|
||||
}
|
||||
return STATUS.SUCCESS
|
||||
}
|
||||
|
||||
/**
|
||||
* rmb购买
|
||||
*
|
||||
* @param {number} serverId 区Id
|
||||
* @param {number} activityId 活动Id
|
||||
* @param {string} roleId 角色Id
|
||||
* @param {string} productID 商品ID
|
||||
*
|
||||
*/
|
||||
export async function makeShopOrder(roleId: string, roleName: string, sid: string, serverId: number, activityId: number, productID: string) {
|
||||
let activityData: ActivityModelType = await getActivityById(activityId);
|
||||
if (!activityData) {
|
||||
return STATUS.ACTIVITY_MISSING;
|
||||
}
|
||||
if (activityData.type !== ACTIVITY_TYPE.SHOP) {
|
||||
return STATUS.ACTIVITY_TYPE_ERROR;
|
||||
}
|
||||
let createTime = await getRoleCreateTime(roleId);
|
||||
let serverTime = await getServerCreateTime(serverId);
|
||||
let shopData = new ShopData(activityData, createTime, serverTime);
|
||||
let dicItem = shopData.findByProductID(productID);
|
||||
if(!dicItem) return STATUS.DIC_DATA_NOT_FOUND;
|
||||
let reward = [{
|
||||
id: dicItem.goodId,
|
||||
count: 1
|
||||
}];
|
||||
await addItems(roleId, roleName, sid, reward, ITEM_CHANGE_REASON.SHOP_PURCHASE);
|
||||
|
||||
await UserShopModel.purchase(roleId, roleName, activityId, dicItem, 1);
|
||||
return {
|
||||
code: 0,
|
||||
data: Object.assign({}, { item: { shop: dicItem.shop, type: dicItem.type, shopItemId: dicItem.id }, activityId: activityId })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user