团购:基础接口
This commit is contained in:
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import { COM_BTL_QUALITY, HERO_SELECT, DEBUG_MAGIC_WORD, REDIS_KEY, TASK_TYPE, E
|
||||
import { nowSeconds, getZeroPoint } from '../../../pubUtils/timeUtil';
|
||||
import { rmRoleFromQueue, roleLeave, getRoleOnlineInfo, roleLogin, getOnlineRoleByUserCode } from '../../../services/redisService';
|
||||
|
||||
import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGuildAuctionChannel, leaveGuildChannel, leaveSysChannel, leaveWorldAuctionChannel, leaveWorldChannel, recentGuildMsgs, recentPrivateChatInfos, recentSysMsgs, recentWorldMsgs } from '../../../services/chatService';
|
||||
import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGroupShopChannel, leaveGuildAuctionChannel, leaveGuildChannel, leaveSysChannel, leaveWorldAuctionChannel, leaveWorldChannel, recentGuildMsgs, recentPrivateChatInfos, recentSysMsgs, recentWorldMsgs } from '../../../services/chatService';
|
||||
import { reportOneOnline, savePlayTime } from '../../../services/authenticateService';
|
||||
import { checkTaskInEntry, } from '../../../services/task/taskService';
|
||||
import { pushData, kickUser, getModuleData, assignServer, leaveServer } from '../../../services/connectorService';
|
||||
@@ -205,6 +205,7 @@ export class EntryHandler {
|
||||
await leaveGuildChannel(roleId, sid, guildCode);
|
||||
await leaveGuildAuctionChannel(roleId, sid, guildCode);
|
||||
await leaveWorldAuctionChannel(roleId, sid, serverId);
|
||||
await leaveGroupShopChannel(roleId, sid);
|
||||
RoleModel.updateRoleInfo(roleId, { quitTime: nowSeconds() });
|
||||
// if(teamCode) { // 如果有寻宝中的队伍,那么等于战败
|
||||
// setComBtlOnUserLeave(roleId, teamCode)
|
||||
|
||||
@@ -39,6 +39,7 @@ import { isArray } from 'underscore';
|
||||
import { getGuideGachaData } from './gachaService';
|
||||
import { getPopNoticeData } from './popNoticeService';
|
||||
import { _getActivities, _getActivitiesByServerId, _getActivitiesByType, _getActivityById } from './activityRemoteService';
|
||||
import { getGroupShopDataShow } from './groupShopService';
|
||||
|
||||
/**
|
||||
* 获取活动数据
|
||||
@@ -222,6 +223,11 @@ export async function getActivity(serverId: number, roleId: string, guildCode: s
|
||||
activityData = await getPopNoticeData(serverId, activityId, roleId);
|
||||
break
|
||||
}
|
||||
case ACTIVITY_TYPE.GROUP_SHOP:
|
||||
{
|
||||
activityData = await getGroupShopDataShow(activityId, roleId);
|
||||
break
|
||||
}
|
||||
default: {
|
||||
console.log('未知活动类型.........', activityType)
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { GROUP_SHOP_PRICE_STATUS } from "../../consts";
|
||||
import { ActivityGroupShopRecModel } from "../../db/ActivityGroupShopRec";
|
||||
import { ActivityGroupShopUserRecModel } from "../../db/ActivityGroupShopUserRec";
|
||||
import { GroupShopData } from "../../domain/activityField/groupShopField";
|
||||
import { getRoleCreateTime, getServerCreateTime } from "../redisService";
|
||||
import { getActivityById } from "./activityService";
|
||||
|
||||
/**
|
||||
* 玩家活动数据
|
||||
*
|
||||
* @param {number} serverId 区Id
|
||||
* @param {number} activityId 活动Id
|
||||
* @param {string} roleId 角色Id
|
||||
*
|
||||
*/
|
||||
export async function getGroupShopData(activityId: number, roleId: string) {
|
||||
let activityData = await getActivityById(activityId);
|
||||
let createTime = await getRoleCreateTime(roleId);
|
||||
|
||||
let playerData = new GroupShopData(activityData, createTime, 0);
|
||||
let serverRecords = await ActivityGroupShopRecModel.findByActivity(activityId);
|
||||
playerData.setRecords(serverRecords);
|
||||
let playerRecords = await ActivityGroupShopUserRecModel.findByActivityAndRoleId(activityId, roleId);
|
||||
playerData.setPlayerRecord(playerRecords);
|
||||
|
||||
return playerData;
|
||||
}
|
||||
|
||||
export async function getGroupShopDataShow(activityId: number, roleId: string) {
|
||||
let playerData = await getGroupShopData(activityId, roleId);
|
||||
if(playerData && playerData.canShow && playerData.canShow()) {
|
||||
return playerData.getShowResult();
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function getGroupShopPriceStatus(clientPrice: number, price: number) {
|
||||
if(clientPrice == price) return GROUP_SHOP_PRICE_STATUS.NORMAL;
|
||||
if(clientPrice < price) return GROUP_SHOP_PRICE_STATUS.NOT_ENOUGH;
|
||||
return GROUP_SHOP_PRICE_STATUS.OVER;
|
||||
}
|
||||
@@ -56,6 +56,11 @@ export async function addRoleToWorldAuctionChannel(roleId: string, sid: string,
|
||||
await addRoleToChannel(roomId, roleId, sid);
|
||||
}
|
||||
|
||||
export async function addRoleToGroupShopChannel(roleId: string, sid: string) {
|
||||
const roomId = groupRoomId(CHANNEL_PREFIX.GROUP_SHOP);
|
||||
await addRoleToChannel(roomId, roleId, sid);
|
||||
}
|
||||
|
||||
async function leaveChannel(roomId: string, roleId: string, sid: string) {
|
||||
const channelSid = await channelServer(roomId);
|
||||
await pinus.app.rpc.chat.chatRemote.leaveChannel.toServer(channelSid, roomId, roleId, sid);
|
||||
@@ -86,6 +91,11 @@ export async function leaveWorldAuctionChannel(roleId: string, sid: string, serv
|
||||
await leaveChannel(roomId, roleId, sid);
|
||||
}
|
||||
|
||||
export async function leaveGroupShopChannel(roleId: string, sid: string) {
|
||||
const roomId = groupRoomId(CHANNEL_PREFIX.GROUP_SHOP);
|
||||
await leaveChannel(roomId, roleId, sid);
|
||||
}
|
||||
|
||||
export async function leaveGuildChannel(roleId: string, sid: string, guildCode: string) {
|
||||
if (!guildCode) return;
|
||||
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
|
||||
@@ -122,6 +132,12 @@ export async function getWorldAuctionChannelSid(serverId: number) {
|
||||
return channelSid;
|
||||
}
|
||||
|
||||
export async function getGroupShopSid() {
|
||||
const roomId = groupRoomId(CHANNEL_PREFIX.GROUP_SHOP);
|
||||
const channelSid = await channelServer(roomId);
|
||||
return channelSid;
|
||||
}
|
||||
|
||||
async function delChannel(roomId: string) {
|
||||
const channelSid = await channelServer(roomId);
|
||||
await pinus.app.rpc.chat.chatRemote.deleteChannel.toServer(channelSid, roomId);
|
||||
|
||||
@@ -38,8 +38,8 @@ export function privateRoomId(roleId: string, targetRoleId: string) {
|
||||
* @param {string} channelId
|
||||
* @returns
|
||||
*/
|
||||
export function groupRoomId(channel: string, channelId: string | number) {
|
||||
return `${channel}_${channelId}`;
|
||||
export function groupRoomId(channel: string, channelId?: string | number) {
|
||||
return channelId?`${channel}_${channelId}`: `${channel}`;
|
||||
}
|
||||
|
||||
function msgCounterName(roomId: string) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Channel, pinus } from "pinus";
|
||||
import { CHANNEL_PREFIX, PUSH_BATCH, PUSH_INTERVAL, PUSH_ROUTE, STATUS } from "../consts";
|
||||
import { genCode, resResult } from "../pubUtils/util";
|
||||
import { getCityChannelSid, getGuildChannelSid, getWorldChannelSid, groupRoomId } from "./chatService";
|
||||
import { getCityChannelSid, getGuildChannelSid, getWorldChannelSid, groupRoomId, getGroupShopSid } from "./chatService";
|
||||
import { getAllOnlineRoles, getRoleOnlineInfo } from "./redisService";
|
||||
import { errlogger, infologger } from '../util/logger';
|
||||
import { MsgEncrypt } from "../pubUtils/sysUtil";
|
||||
@@ -64,6 +64,12 @@ export async function sendMessageToCity(cityId: number, route: string, data: any
|
||||
await pinus.app.rpc.chat.chatRemote.pushMessage.toServer(channelSid, roomId, route, data);
|
||||
}
|
||||
|
||||
export async function sendMessageToGroupShopWithSuc(route: string, data: any) {
|
||||
let channelSid = await getGroupShopSid();
|
||||
let roomId = groupRoomId(CHANNEL_PREFIX.GROUP_SHOP);
|
||||
await pinus.app.rpc.chat.chatRemote.pushMessage.toServer(channelSid, roomId, route, resResult(STATUS.SUCCESS, data));
|
||||
}
|
||||
|
||||
export async function sendMessageToUserWithSuc(roleId: string, route: string, data: any, sid?: string) {
|
||||
await sendMessageToUser(roleId, route, resResult(STATUS.SUCCESS, data), sid);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user