寻宝:抽象队长匹配队友的逻辑;抽象 channel 中的用户

This commit is contained in:
liangtongchuan
2021-02-19 17:58:00 +08:00
parent ea5d507983
commit 45ce962709
4 changed files with 73 additions and 37 deletions

View File

@@ -16,9 +16,11 @@ import { RoleStatus, ComBattleTeamModel, ComBattleTeamType } from '../../../db/C
import { ItemModel } from '../../../db/Item'; import { ItemModel } from '../../../db/Item';
import { handleFixedReward, addItems, handleCost } from '../../../services/rewardService'; import { handleFixedReward, addItems, handleCost } from '../../../services/rewardService';
import { checkRoleInQueue, getTeamSearchByQuality, rmRoleFromQueue, setTeamSearchReq } from '../../../services/redisService'; import { checkRoleInQueue, getTeamSearchByQuality, rmRoleFromQueue, setTeamSearchReq } from '../../../services/redisService';
import { getRandBlueprtId, getRandComBtlRobots, clearComBtlTimer, getAssistTimesByQuality, getFrd, updateRobotHurtByTime, comBtlLvInvalid, clearRobotHurtTimer, setDismissTimer, dismissTeam, incEquipPrintDrop, randEquipPrintId, handleComBtlProgress, getComBattleFriendAdd, getValidTeammateRoleSt, teammateInBlackList, blueprtIdValid, createComTeamData, hasEnoughBlueprt, addRoleToTeam, addRoleStToTeam } from '../../../services/comBattleService'; import { getRandBlueprtId, getRandComBtlRobots, clearComBtlTimer, getAssistTimesByQuality, getFrd, updateRobotHurtByTime, comBtlLvInvalid, clearRobotHurtTimer, setDismissTimer, dismissTeam, incEquipPrintDrop, randEquipPrintId, handleComBtlProgress, getComBattleFriendAdd, getValidTeammateRoleSt, teammateInBlackList, blueprtIdValid, createComTeamData, hasEnoughBlueprt, addRoleToTeam, addRoleStToTeam, addValidSearchingRoles } from '../../../services/comBattleService';
import { setAp } from '../../../services/actionPointService'; import { setAp } from '../../../services/actionPointService';
import { roleLevelup } from '../../../services/normalBattleService'; import { roleLevelup } from '../../../services/normalBattleService';
import { addUserToChannel } from '../../../services/roleService';
import { ChannelUser } from '../../../domain/ChannelUser';
export default function(app: Application) { export default function(app: Application) {
return new ComBattleHandler(app); return new ComBattleHandler(app);
@@ -45,7 +47,6 @@ export class ComBattleHandler {
console.log('createTeam msg: ', msg); console.log('createTeam msg: ', msg);
// 检查藏宝图Id是否合法 // 检查藏宝图Id是否合法
let goodData = getGoodById(blueprtId);
if (!blueprtIdValid(blueprtId)) return resResult(STATUS.COM_BATTLE_BLUEPRT_INVALID); if (!blueprtIdValid(blueprtId)) return resResult(STATUS.COM_BATTLE_BLUEPRT_INVALID);
const enoughBlueprt = await hasEnoughBlueprt(roleId, blueprtId); const enoughBlueprt = await hasEnoughBlueprt(roleId, blueprtId);
if (!enoughBlueprt) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_ENOUGH); if (!enoughBlueprt) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_ENOUGH);
@@ -55,47 +56,24 @@ export class ComBattleHandler {
const topFiveCe = reduceCe(roleInfo.topFiveCe || 100); const topFiveCe = reduceCe(roleInfo.topFiveCe || 100);
if (lv < COM_BTL_CONST.ENABLE_LV) return resResult(STATUS.COM_BATTLE_LV_NOT_ENOUGH); if (lv < COM_BTL_CONST.ENABLE_LV) return resResult(STATUS.COM_BATTLE_LV_NOT_ENOUGH);
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(teamCode, true);
// 创建队伍数据结构 // 创建队伍数据结构
let comTeam: MemComBtlTeam = createComTeamData(teamCode, pub, blueprtId, roleId, ceLimit); let comTeam: MemComBtlTeam = createComTeamData(teamCode, pub, blueprtId, roleId, ceLimit);
addRoleToTeam(comTeam, roleInfo, true, false); addRoleToTeam(comTeam, roleInfo, true, false);
addUserToChannel(channel, new ChannelUser(roleId, sid));
// 将正在匹配的符合要求的玩家加入队伍,并推送入队消息
await addValidSearchingRoles(comTeam, channelService);
// 检查是否有正在匹配的符合要求的玩家 // 队伍数据持久化
let teammates = await getTeamSearchByQuality(goodData.quality, comBtlRangeByLv(goodData.lvLimited)); const team = await ComBattleTeamModel.createTeam(comTeam);
if (teammates && teammates.length) { if (!team) {
for (let teammate of teammates) { channel.destroy();
const { roleId: teammateRoleId } = teammate; return resResult(STATUS.COM_BATTLE_CREATE_ERR);
const st = await getValidTeammateRoleSt(teammateRoleId, comTeam.roleIds, ceLimit, goodData.quality);
if (!st) continue;
await rmRoleFromQueue(teammateRoleId, sid, COM_BTL_QUALITY, null); // 匹配成功后删除redis中该用户的匹配记录
addRoleStToTeam(comTeam, st);
}
} }
this.teamMap.set(teamCode, comTeam); this.teamMap.set(teamCode, comTeam);
// TODO: 处理助战回滚
const team = await ComBattleTeamModel.createTeam(comTeam);
if (!team) return resResult(STATUS.COM_BATTLE_CREATE_ERR);
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(teamCode, true);
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
let uids = [];
uids.push({roleId, sid});
if (teammates && teammates.length) {
for (let teammate of teammates) {
let { roleId, sid } = teammate;
uids.push({uid: roleId, sid});
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
}
channelService.pushMessageByUids('onTeamJoin', resResult(STATUS.SUCCESS, {teamInfo: comTeam}), uids);
}
let thiz = this; let thiz = this;
// 倒计时一定时间给队长匹配机器人 // 倒计时一定时间给队长匹配机器人

View File

@@ -13,11 +13,14 @@ import { getBluePrtByQuality, getComBtlSetByQuality, getRewardByBlueprtId, getWa
import { getRandEelm, getRandValue, resResult, ratioReward, getRandValueByMinMax, getRandomWithWeight, decodeStr, getRobotInfo, reduceCe } from "../pubUtils/util"; import { getRandEelm, getRandValue, resResult, ratioReward, getRandValueByMinMax, getRandomWithWeight, decodeStr, getRobotInfo, reduceCe } from "../pubUtils/util";
import { getRandRobot, transBossHpArr } from "./battleService"; import { getRandRobot, transBossHpArr } from "./battleService";
import { difference, omit } from 'underscore'; import { difference, omit } from 'underscore';
import { Channel } from 'pinus'; import { Channel, ChannelService } from 'pinus';
import { TREASURE } from '../pubUtils/dicParam'; import { TREASURE } from '../pubUtils/dicParam';
import { decreaseItems } from './rewardService'; import { decreaseItems } from './rewardService';
import { getFriendLvAdd } from './friendService'; import { getFriendLvAdd } from './friendService';
import { getRoleIds } from '../pubUtils/friendUtil'; import { getRoleIds } from '../pubUtils/friendUtil';
import { getTeamSearchByQuality, rmRoleFromQueue } from './redisService';
import { addUserToChannel } from './roleService';
import { ChannelUser } from '../domain/ChannelUser';
/** /**
@@ -590,4 +593,28 @@ export function addRoleStToTeam(comTeam: MemComBtlTeam, roleSt: RoleStatus) {
const { roleStatus = [], roleIds = [] } = comTeam; const { roleStatus = [], roleIds = [] } = comTeam;
comTeam.roleStatus = [roleSt, ...roleStatus]; comTeam.roleStatus = [roleSt, ...roleStatus];
comTeam.roleIds = [roleSt.roleId, ...roleIds]; comTeam.roleIds = [roleSt.roleId, ...roleIds];
}
/**
* @description 将符合要求的用户匹配到队伍中
* @export
* @param {MemComBtlTeam} comTeam 队伍数据
* @param {string} sid 当前用户 sid
* @returns
*/
export async function addValidSearchingRoles(comTeam: MemComBtlTeam, channelService: ChannelService) {
const { quality, lvLimited } = getGoodById(comTeam.blueprtId);
let teammates = await getTeamSearchByQuality(quality, comBtlRangeByLv(lvLimited));
if (teammates && teammates.length) {
for (let teammate of teammates) {
const { roleId: teammateRoleId, sid } = teammate;
const st = await getValidTeammateRoleSt(teammateRoleId, comTeam.roleIds, comTeam.ceLimit, quality);
if (!st) continue;
await rmRoleFromQueue(teammateRoleId, sid, COM_BTL_QUALITY, null); // 匹配成功后删除redis中该用户的匹配记录
addRoleStToTeam(comTeam, st);
const channel = channelService.getChannel(comTeam.teamCode, false);
addUserToChannel(channel, new ChannelUser(teammateRoleId, sid));
}
channelService.pushMessageByUids('onTeamJoin', resResult(STATUS.SUCCESS, {teamInfo: comTeam}), teammates.map(t => {return {uid: t.roleId, sid: t.sid}}));
}
} }

View File

@@ -1,3 +1,5 @@
import { ChannelUser } from './../domain/ChannelUser';
import { Channel } from 'pinus';
import { getRandNum, getRandomArr } from '../pubUtils/util'; import { getRandNum, getRandomArr } from '../pubUtils/util';
import { TERAPH_RANDOM } from "../consts/consts"; import { TERAPH_RANDOM } from "../consts/consts";
import { indexOf } from 'underscore'; import { indexOf } from 'underscore';
@@ -57,4 +59,12 @@ export function checkMaterialEnough(count: number, attrs:Array<any>, teraphInfo:
consumes.push({ id, count: count*times}); consumes.push({ id, count: count*times});
} }
return {attr: res, consumes}; return {attr: res, consumes};
}
export function addUserToChannel(channel: Channel, user: ChannelUser) {
const users = channel.getMembers();
const { uid, sid } = user;
if (users.indexOf(uid) === -1) {
channel.add(uid, sid);
}
} }

View File

@@ -0,0 +1,21 @@
export class ChannelUser {
private _roleId: string;
private _sid: string;
constructor(uid: string, sid: string) {
this._roleId = uid;
this._sid = sid;
}
get uid() {
return this._roleId;
}
get roleId() {
return this._roleId;
}
get sid() {
return this._sid;
}
}