寻宝助战匹配简单机器人

This commit is contained in:
liangtongchuan
2020-11-27 23:27:35 +08:00
parent 369c831846
commit 3e499de1bc
8 changed files with 159 additions and 23 deletions

View File

@@ -1,3 +1,4 @@
import { COM_BATTLE_ROBOT_CE_RATIO, COM_BATTLE_ROBOT_ID_NAME, COM_BATTLE_ASSIST_TIME } from './../../../consts/consts';
import { IT_TYPE, GOLD_COST_RATIO, CURRENCY_BY_TYPE, CURRENCY_TYPE } from '../../../consts/consts';
import { getGoodById, getBossHpByBlueprtId, getComBtlSetByQuality, getBlueprtComposeByQuality, getBluePrtByQuality } from '../../../pubUtils/gamedata';
import { COM_TEAM_STATUS, COM_TEAM_ENABLE_LV } from '../../../consts/consts';
@@ -5,11 +6,13 @@ import { ComBattleTeamModel } from '../../../db/ComBattleTeam';
import Role, { RoleModel } from '../../../db/Role';
import { STATUS } from '../../../consts/statusCode';
import { Application, BackendSession, BlackListFunction } from 'pinus';
import { resResult, getRandomByLen, calculateNum } from '../../../pubUtils/util';
import { resResult, getRandomByLen, calculateNum, getRandValue } from '../../../pubUtils/util';
import { RoleStatus } from '../../../db/ComBattleTeam';
import { ItemModel } from '../../../db/Item';
import { handleReward } from '../../../services/rewardService';
import { getTeamSearchByQuality, setTeamSearchReq } from '../../../services/redisService';
import { getRandRobot } from '../../../services/battleService';
import { getRandBlueprtId } from '../../../services/comBattleService';
export default function(app: Application) {
return new ComBattleHandler(app);
@@ -84,16 +87,18 @@ export class ComBattleHandler {
console.log('teammates: ', teammates);
if (teammates) {
for (let teammate of teammates) {
// TODO: 要判断战力
let roleInfo = await RoleModel.findByRoleId(teammate.roleId);
if (!roleInfo) continue;
let {roleId, roleName, headHid = 19, sHid = 19, topFiveCe, lv} = roleInfo;
// TODO: 没处理情谊助战
roleStatus.push(new RoleStatus(roleId, roleName, false, false, headHid, sHid, topFiveCe, lv));
const st = new RoleStatus(roleId, roleName, false, false, headHid, sHid, topFiveCe, lv);
roleStatus.push(st);
roleIds.push(roleInfo.roleId);
}
}
// 创建队伍数据结构
let comTeam = new ComTeam(teamCode, pub, blueprtId, 0, roleId, ceLimit, getBossHpByBlueprtId(blueprtId) || 10000, goodData.quality);
let comTeam = new ComTeam(teamCode, pub, blueprtId, COM_TEAM_STATUS.DEFAULT, roleId, ceLimit, getBossHpByBlueprtId(blueprtId) || 10000, goodData.quality);
comTeam.roleStatus = roleStatus;
comTeam.roleIds = roleIds;
@@ -127,14 +132,46 @@ export class ComBattleHandler {
async searchTeam(msg: {qualityArr: [number]}, session: BackendSession) {
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let sid = session.get('sid');
let teamCode = session.get('teamCode');
const { qualityArr } = msg;
let { lv, headHid = 19, topFiveCe, sHid = 19 } = await RoleModel.findByRoleId(roleId);
let { lv, headHid = 19, topFiveCe = 1000, sHid = 19 } = await RoleModel.findByRoleId(roleId);
await setTeamSearchReq(roleId, sid, qualityArr);
setTimeout(() => {
}, 10000);
let thiz = this;
setTimeout(async () => {
let roleStatus = [];
let roleIds = []
let robotHeroes = getRandRobot(2);
let roleInfo = new RoleStatus(roleId, roleName, false, false, headHid, sHid, topFiveCe, lv);
roleStatus.push(roleInfo);
roleIds.push(roleId);
let blueprtId = (await getRandBlueprtId(qualityArr)).pop();
let { quality } = getGoodById(blueprtId);
let comTeam = new ComTeam(teamCode, false, blueprtId, COM_TEAM_STATUS.DEFAULT, 'robot', 0, getBossHpByBlueprtId(blueprtId) || 10000, quality);
for (let robot of robotHeroes) {
const robotCe = getRandValue(topFiveCe, COM_BATTLE_ROBOT_CE_RATIO, 0);
const robotLv = getRandValue(lv, COM_BATTLE_ROBOT_CE_RATIO, 0);
const imgHid = robot[Math.floor(Math.random() * robot.length)];
const { robotRoleId, robotRoleName } = COM_BATTLE_ROBOT_ID_NAME[Math.floor(Math.random() * COM_BATTLE_ROBOT_ID_NAME.length)];
let robotStatus = new RoleStatus(robotRoleId, robotRoleName, false, false, imgHid, imgHid, robotCe, robotLv, robot, true);
roleStatus.push(robotStatus);
roleIds.push(robotRoleId);
}
comTeam.roleStatus = roleStatus;
comTeam.roleIds = roleIds;
let channelService = thiz.app.get('channelService');
const team = await ComBattleTeamModel.createTeam(comTeam);
// if (!team) channelService.pushMessageByUids('onTeamJoin', {teamInfo: null}, [{uid: roleId, sid}]);
thiz.teamMap.set(teamCode, comTeam);
channelService.pushMessageByUids('onTeamJoin', {teamInfo: comTeam}, [{uid: roleId, sid}]);
let channel = channelService.getChannel(teamCode, true);
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
}, COM_BATTLE_ASSIST_TIME);
return resResult(STATUS.SUCCESS);
}
@@ -215,7 +252,27 @@ export class ComBattleHandler {
teamStatus.roleStatus.forEach(st => {
if (st && st.roleId === roleId) {
st.heroes = heroes;
st.isReady = true;
}
});
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(teamCode, false);
channel.pushMessage('onTeammateReady', {teamCode, roleId, heroes});
return resResult(STATUS.SUCCESS);
}
async setupHeroes(msg: {teamCode: string, heroes: Array<number>}, session: BackendSession) {
let roleId = session.get('roleId');
let { teamCode, heroes } = msg;
let teamStatus = this.teamMap.get(teamCode);
if (!teamStatus || !teamStatus.roleIds || teamStatus.roleIds.indexOf(roleId) === -1) return resResult(STATUS.COM_BATTLE_TEAM_INVALID);
let team = await ComBattleTeamModel.updateHeroes(teamCode, roleId, heroes);
if (!team) return resResult(STATUS.COM_BATTLE_UPDATE_HEROES_ERR);
teamStatus.roleStatus.forEach(st => {
if (st && st.roleId === roleId) {
st.heroes = heroes;
}
});
@@ -284,11 +341,6 @@ export class ComBattleHandler {
if (teamStatus.capId !== roleId) return resResult(STATUS.COM_BATTLE_CAP_ONLY);
if (teamStatus.status !== COM_TEAM_STATUS.DEFAULT) return resResult(STATUS.COM_BATTLE_ALREADY_START);
for (let roleSt of teamStatus.roleStatus) {
if (!roleSt.isReady) {
return resResult(STATUS.COM_BATTLE_TEAM_NO_READY);
}
}
teamStatus.status = COM_TEAM_STATUS.FIGHTING;
let channelService = this.app.get('channelService');

View File

@@ -5,7 +5,7 @@ import { HANG_UP_CONSTS, TOWER_TASK_CONST, REDIS_KEY } from './../consts/consts'
import { BattleRecordModel } from './../db/BattleRecord';
import { TowerRecordModel } from './../db/TowerRecord';
import { RoleModel } from './../db/Role';
import { getHeroInfoById, getJobInfoById, getTowerDataByLv, getTaskById, getGamedata } from "../pubUtils/gamedata"
import { getHeroInfoById, getJobInfoById, getTowerDataByLv, getTaskById, getGamedata, getRandExpedition, getWarById, getWarJsons } from "../pubUtils/gamedata"
import { decodeArrayStr, shouldRefresh, resResult, decodeStr, cal, getRandomWithWeight, getRefTime, decodeStrSingle, genCode } from '../pubUtils/util';
import { handleFixedReward } from './rewardService';
import { STATUS } from '../consts/statusCode';
@@ -347,4 +347,30 @@ export async function getTasksReward(roleId: string, curTime: Date) {
}
}
return goods
}
/**
* 获取随机机器人数据
* @param cnt 机器人数量
* @param withAttr 是否返回属性false: 仅返回阵容
*/
export function getRandRobot(cnt = 1, withAttr = false) {
let setInfo = getRandExpedition(cnt);
if (!setInfo || setInfo.length !== cnt) return null;
let robots = [];
for (let info of setInfo) {
if (!info || !info.warId) continue;
const warInfo = getWarJsons(info.warId).json;
if (!warInfo || !warInfo.length) continue;
let heroes = [];
for(let hero of warInfo) {
if (hero && hero.relation === 2 && hero.actorId) {
heroes.push(hero.actorId);
}
}
robots.push(heroes);
}
return robots;
}

View File

@@ -0,0 +1,18 @@
import { getBluePrtByQuality } from "../pubUtils/gamedata";
import { getRandEelm } from "../pubUtils/util";
/**
* 在给定的品质列表中随机返回一定数量的藏宝图Id
* @param qualityArr 品质数组在所有给定品质的藏宝图中筛选1
* @param cnt 返回藏宝图数量
*/
export async function getRandBlueprtId(qualityArr: Array<number>, cnt = 1) {
if (!qualityArr || !qualityArr.length) return null;
let blueprtIdArr = [];
for (let q of qualityArr) {
blueprtIdArr = blueprtIdArr.concat(getBluePrtByQuality(q));
}
if (blueprtIdArr.length === 0) return null;
const res = getRandEelm(blueprtIdArr, cnt);
return res;
}

View File

@@ -27,7 +27,7 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E
if (msg.args && msg.args.length > 0) {
for (let arg of msg.args) {
if (!arg.route) continue;
if (arg.route === 'battle.comBattleHandler.createTeam') {
if (['battle.comBattleHandler.createTeam', 'battle.comBattleHandler.searchTeam'].indexOf(arg.route) !== -1) {
rid = Math.random().toString(36).slice(-8);
session.set('teamCode', rid);
} else if (arg.route.indexOf('battle.comBattleHandler') !== -1) {