团购:基础接口
This commit is contained in:
106
game-server/app/servers/activity/handler/groupShopHandler.ts
Normal file
106
game-server/app/servers/activity/handler/groupShopHandler.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { Application, BackendSession, HandlerService, } from "pinus";
|
||||
import { resResult } from "../../../pubUtils/util";
|
||||
import { STATUS, ITEM_CHANGE_REASON, GROUP_SHOP_PRICE_STATUS, PUSH_ROUTE, } from "../../../consts";
|
||||
import { getGroupShopData, getGroupShopDataShow, getGroupShopPriceStatus } from "../../../services/activity/groupShopService";
|
||||
import { addItems, getGoldObject, handleCost } from "../../../services/role/rewardService";
|
||||
import { ActivityGroupShopUserRecModel, GroupShopBuyRecord } from "../../../db/ActivityGroupShopUserRec";
|
||||
import { ActivityGroupShopRecModel, GroupShopRecord } from "../../../db/ActivityGroupShopRec";
|
||||
import { pick } from "underscore";
|
||||
import { addRoleToGroupShopChannel, leaveGroupShopChannel } from "../../../services/chatChannelService";
|
||||
import { sendMessageToGroupShopWithSuc } from "../../../services/pushService";
|
||||
|
||||
export default function (app: Application) {
|
||||
new HandlerService(app, {});
|
||||
return new GroupShopHandler(app);
|
||||
}
|
||||
|
||||
export class GroupShopHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 1. 进入团购主页面
|
||||
* @param {{}} msg
|
||||
* @param {BackendSession} session
|
||||
* @memberof GroupShopHandler
|
||||
*/
|
||||
async getGroupShopPage(msg: { activityId: number }, session: BackendSession) {
|
||||
const { activityId } = msg;
|
||||
const roleId: string = session.get('roleId');
|
||||
const sid: string = session.get('sid');
|
||||
|
||||
let playerData = await getGroupShopDataShow(activityId, roleId);
|
||||
if (!playerData) {
|
||||
return resResult(STATUS.ACTIVITY_MISSING);
|
||||
}
|
||||
await addRoleToGroupShopChannel(roleId, sid);
|
||||
|
||||
return resResult(STATUS.SUCCESS, { playerData });
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 2. 离开团购主页面
|
||||
* @param {{}} msg
|
||||
* @param {BackendSession} session
|
||||
* @memberof GroupShopHandler
|
||||
*/
|
||||
async leaveGroupShopPage(msg: { activityId: number }, session: BackendSession) {
|
||||
const roleId: string = session.get('roleId');
|
||||
const sid: string = session.get('sid');
|
||||
|
||||
await leaveGroupShopChannel(roleId, sid)
|
||||
return resResult(STATUS.SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 3. 购买
|
||||
* @param {{}} msg
|
||||
* @param {BackendSession} session
|
||||
* @memberof GroupShopHandler
|
||||
*/
|
||||
async buy(msg: { activityId: number, price: number, id: number, buyCnt: number }, session: BackendSession) {
|
||||
const { activityId, price: clientPrice, id, buyCnt } = msg;
|
||||
const roleId: string = session.get('roleId');
|
||||
const roleName: string = session.get('roleName');
|
||||
const sid: string = session.get('sid');
|
||||
|
||||
let playerData = await getGroupShopData(activityId, roleId);
|
||||
if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
|
||||
|
||||
let item = playerData.findItemById(id);
|
||||
if (!item) return resResult(STATUS.ACTIVITY_GROUP_SHOP_ITEM_NOT_FOUND);
|
||||
if(!item.checkBuyCnt(buyCnt)) return resResult(STATUS.ACTIVITY_GROUP_SHOP_BUY_CNT_MAX);
|
||||
|
||||
let curDiscount = item.getCurDiscount();
|
||||
let priceStatus = getGroupShopPriceStatus(clientPrice, curDiscount.price);
|
||||
if(priceStatus == GROUP_SHOP_PRICE_STATUS.NOT_ENOUGH) return resResult(STATUS.ACTIVITY_GROUP_SHOP_PRICE_ERR);
|
||||
let cost = [getGoldObject(curDiscount.price)];
|
||||
let result = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.ACT_GROUP_SHOP_BUY);
|
||||
if(!result) return resResult(STATUS.BATTLE_GOLD_NOT_ENOUGH);
|
||||
|
||||
// 玩家添加次数
|
||||
let playerRecord = await ActivityGroupShopUserRecModel.incBuyCnt(activityId, roleId, id, item.gid, buyCnt, new GroupShopBuyRecord(curDiscount, buyCnt));
|
||||
playerData.setPlayerRecord([playerRecord]);
|
||||
// 全服添加次数
|
||||
let serverRecord = await ActivityGroupShopRecModel.incBuyCnt(activityId, id, item.gid, buyCnt);
|
||||
let isRankUp = playerData.incAllRecord(serverRecord);
|
||||
|
||||
item = playerData.findItemById(id);
|
||||
let nextDiscount = item.getCurDiscount();
|
||||
if(isRankUp) {
|
||||
await ActivityGroupShopRecModel.addRecord(activityId, id, new GroupShopRecord(nextDiscount));
|
||||
// 推送频道
|
||||
await sendMessageToGroupShopWithSuc(PUSH_ROUTE.GROUP_SHOP_UPDATE, { activityId, id, curDiscount: nextDiscount });
|
||||
}
|
||||
let reward = [{ id: item.gid, count: item.count * buyCnt }];
|
||||
let goods = await addItems(roleId, roleName, sid, reward, ITEM_CHANGE_REASON.ACT_GROUP_SHOP_BUY);
|
||||
|
||||
return resResult(STATUS.SUCCESS, {
|
||||
curItem: pick(item, ['id', 'gid', 'sum', 'hasBoughtCnt']),
|
||||
goods,
|
||||
status: priceStatus,
|
||||
oldDiscount: curDiscount,
|
||||
nextDiscount
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user