推送:对单人推送函数抽象

This commit is contained in:
luying
2022-04-09 10:28:21 +08:00
parent f486a8d8a5
commit 26a649b213
32 changed files with 146 additions and 703 deletions
+15 -21
View File
@@ -1,4 +1,4 @@
import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE, HANDLE_REWARD_TYPE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, ITEM_CHANGE_REASON, TA_USERSET_TYPE, TA_EVENT, POP_UP_SHOP_CONDITION_TYPE, ROLE_SELECT, FIGURE_UNLOCK_CONDITION } from '../../consts';
import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE, HANDLE_REWARD_TYPE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, ITEM_CHANGE_REASON, TA_USERSET_TYPE, TA_EVENT, POP_UP_SHOP_CONDITION_TYPE, ROLE_SELECT, FIGURE_UNLOCK_CONDITION, PUSH_ROUTE } from '../../consts';
import { getDecimalCnt, getRandEelm, getRandEelmWithWeight, getRandSingleEelm, getRandValueByMinMax, resResult } from '../../pubUtils/util';
import { RoleModel, RoleType } from '../../db/Role';
import { setAp } from '../actionPointService';
@@ -25,12 +25,12 @@ import { updateEplaces } from '../equipService';
import { combineItems, getCoinEventProperties, getGoldEventProperties, sortItems } from './util';
import { nowSeconds } from '../../pubUtils/timeUtil';
import { calculateCeWithHero, calculateCeWithRole } from '../playerCeService';
import { sendMessageToUsersWithSuc, sendMessageToUserWithSuc } from '../pushService';
export async function handleCost(roleId: string, sid: string, goods: Array<ItemInter>, reason: ITEM_CHANGE_REASON) {
let uids = [{ uid: roleId, sid }];
let { items, jewels, gold, coin } = sortItems(goods, HANDLE_REWARD_TYPE.COST);
let jewelSeqIds = jewels.map(cur => cur.seqId);
let resJewels: JewelType[] = [];
@@ -53,7 +53,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
if (items.length > 0) {
let { hasError, result } = await ItemModel.decreaseItems(roleId, items);
if (hasError) return false;
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, { goods: result.map(cur => ({...cur, reason })) }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.ITEM_UPDATE, { goods: result.map(cur => ({...cur, reason })) }, sid);
saveItemChangeLog(roleId, result, reason);
}
@@ -86,7 +86,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
let jewels = await JewelModel.deleteBySeqIds(roleId, jewelSeqIds);
saveItemChangeLog(roleId, jewels.map(jewel => ({ id: jewel.id, count: 1, inc: -1 })), reason);
pinus.app.get('channelService').pushMessageByUids('onJewelDel', resResult(STATUS.SUCCESS, { jewels: jewels.map(jewel => ({ seqId: jewel.seqId, id: jewel.id, inc: -1, reason })) }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.JEWEL_DEL, { jewels: jewels.map(jewel => ({ seqId: jewel.seqId, id: jewel.id, inc: -1, reason })) }, sid);
}
//消耗玩家货币
@@ -94,9 +94,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
let costGold = gold.reduce((pre, cur) => pre + cur.count, 0);
let costCoin = coin.reduce((pre, cur) => pre + cur, 0);
role = await RoleModel.decreaseGoldAndCoin(roleId, gold, costCoin);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, {
gold: role.gold, coin: role.coin, totalCost: role.totalCost
}), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.PLAYER_DATA_CHANGE, { gold: role.gold, coin: role.coin, totalCost: role.totalCost }, sid);
if(costGold > 0) {
reportTAEvent(roleId, TA_EVENT.ITEM_CONSUME, getGoldEventProperties(costGold, role.gold, reason));
@@ -114,9 +112,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
return true;
}
// TODO: sid 在方法内部获取,且不一定存在
export async function addItems(roleId: string, roleName: string, sid: string, goods: Array<ItemInter>, reason: ITEM_CHANGE_REASON) {
let uids = [{ uid: roleId, sid }];
let { items, jewels, gold, coin, ap, skins, figures } = sortItems(goods, HANDLE_REWARD_TYPE.RECEIVE);
let showItems: { id: number, seqId?: number, count: number, isBag?: boolean }[] = [];
let role = await RoleModel.findByRoleId(roleId);
@@ -141,7 +137,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
}
//装备推送
if (!!jewelInfos.length)
pinus.app.get('channelService').pushMessageByUids('onJewelAdd', resResult(STATUS.SUCCESS, { jewelInfos }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.JEWEL_ADD, { jewelInfos }, sid);
//统计装备
if (jewelInfos.length > 0) {
saveItemChangeLog(roleId, jewelInfos, reason);
@@ -160,7 +156,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
}
//背包除去装备推送
if (!!itemInfos.length) {
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, { goods: itemInfos }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.ITEM_UPDATE, { goods: itemInfos }, sid);
saveItemChangeLog(roleId, itemInfos, reason);
}
@@ -172,9 +168,8 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
let incCoin = coin.reduce((pre, cur) => pre + cur, 0);
let incGold = gold.reduce((pre, cur) => pre + cur.count, 0);
role = await RoleModel.increaseGoldAndCoin(roleId, gold, incCoin);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, {
gold: role.gold, coin: role.coin
}), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.PLAYER_DATA_CHANGE, { gold: role.gold, coin: role.coin }, sid);
if(gold.length > 0) {
gold.forEach(({ count }) => {
showItems.push(getGoldObject(count));
@@ -211,7 +206,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
}
}
if (!!skinInfos.length) {
pushHeroSkinMsg(heroskins, skinInfos, uids); // 推送onHeroSkinChange
pushHeroSkinMsg(heroskins, skinInfos, roleId, sid); // 推送onHeroSkinChange
saveItemChangeLog(roleId, skinInfos, reason);
}
}
@@ -228,7 +223,7 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
// 获得头像或相框或形象推送
if(!!figureInfos && figureInfos.length > 0) {
for(let figureInfo of figureInfos) {
pinus.app.get('channelService').pushMessageByUids('onHeadChange', resResult(STATUS.SUCCESS, { ...figureInfo }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.HEAD_CHANGE, { ...figureInfo }, sid);
saveFigureInfoLog(roleId, figureInfo, reason)
}
}
@@ -298,8 +293,7 @@ export async function unlockFigure(sid: string, roleId: string, conditions: { ty
let { figureInfo, heads, frames, spines } = unlockFigureWithoutSave(conditions, role);
role = await RoleModel.updateRoleInfo(roleId, { heads, frames, spines });
if (!!figureInfo && (figureInfo.heads.length > 0 || figureInfo.frames.length > 0 || figureInfo.spines.length > 0)) {
let uids = [{ uid: roleId, sid }];
pinus.app.get('channelService').pushMessageByUids('onHeadChange', resResult(STATUS.SUCCESS, { ...figureInfo }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.HEAD_CHANGE, { ...figureInfo }, sid);
}
}
@@ -307,9 +301,9 @@ export async function unlockFigure(sid: string, roleId: string, conditions: { ty
/**
* 皮肤数据变化去重、推送
* @param heroskins 推送的皮肤
* @param uids 玩家
* @param roleId 玩家
*/
function pushHeroSkinMsg(heroskins: {skins: HeroSkin[], hid: number}[], skinInfos: {id: number, hid: number }[], uids: {uid: string, sid: string}[]) {
function pushHeroSkinMsg(heroskins: {skins: HeroSkin[], hid: number}[], skinInfos: {id: number, hid: number }[], roleId: string, sid: string) {
let pushSkinInfos: {skins: HeroSkin[], hid: number}[] = []; // 可能会有重复的
for(let { skins, hid } of heroskins) {
let index = pushSkinInfos.findIndex(cur => cur.hid == hid);
@@ -322,7 +316,7 @@ function pushHeroSkinMsg(heroskins: {skins: HeroSkin[], hid: number}[], skinInf
}
}
if(pushSkinInfos.length > 0 || skinInfos.length > 0) {
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, { heros: heroskins, skins: skinInfos }), uids);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.HERO_SKIN_CHANGE, { heros: heroskins, skins: skinInfos }, sid);
}
}