216 lines
8.6 KiB
TypeScript
216 lines
8.6 KiB
TypeScript
import { IT_TYPE } from '../../../consts/consts';
|
|
import { getGoodById } from '../../../pubUtils/gamedata';
|
|
import { COM_TEAM_STATUS, COM_TEAM_ENABLE_LV } from '../../../consts/consts';
|
|
import { ComBattleTeamModel } from './../../../db/ComBattleTeam';
|
|
import Role, { RoleModel } from './../../../db/Role';
|
|
import { STATUS } from '../../../consts/statusCode';
|
|
import { Application, BackendSession } from 'pinus';
|
|
import { resResult } from '../../../pubUtils/util';
|
|
import { RoleStatus } from './../../../db/ComBattleTeam';
|
|
|
|
export default function(app: Application) {
|
|
return new ComBattleHandler(app);
|
|
}
|
|
|
|
class ComTeam {
|
|
// 队伍唯一编号
|
|
teamCode: string;
|
|
// 玩家列表
|
|
roleIds: Array<string>
|
|
// 队伍是否开放加入
|
|
pub: boolean;
|
|
// 对应藏宝图 Id
|
|
blueprtId: number;
|
|
// 战斗状态 0:未开始,1:已开始,2:胜利,3:失败
|
|
status: number;
|
|
// 玩家状态
|
|
roleStatus: Array<RoleStatus>;
|
|
// 队长 roleId
|
|
capId: string;
|
|
// 战力限制
|
|
ceLimit: number;
|
|
}
|
|
export class ComBattleHandler {
|
|
constructor(private app: Application) {
|
|
}
|
|
private bossHp: number = 10000;
|
|
private teamMap: Map<string, ComTeam> = new Map();
|
|
|
|
async createTeam(msg: {blueprtId: number, heroes: [ number ], pub: boolean, ceLimit: number}, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let sid = session.get('sid');
|
|
let teamCode = session.get('teamCode');
|
|
const { blueprtId, heroes, pub, ceLimit } = msg;
|
|
|
|
console.log('createTeam msg: ', msg);
|
|
// 检查藏宝图Id是否合法
|
|
let goodData = getGoodById(blueprtId);
|
|
if (!goodData || goodData.itid !== IT_TYPE.BLUEPRT) return resResult(STATUS.COM_BATTLE_BLUEPRT_INVALID);
|
|
|
|
// 检查藏宝图是否足够
|
|
let { consumeGoods, lv, headHid, topFiveCe } = await RoleModel.findByRoleId(roleId);
|
|
if (lv < COM_TEAM_ENABLE_LV) return resResult(STATUS.COM_BATTLE_LV_NOT_ENOUGH);
|
|
let blueprt = consumeGoods.find(good => good.id === blueprtId && good.count >= 1);
|
|
if (!blueprt) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_FOUND);
|
|
// 检查是否有已创建未结束的寻宝,预先占用一张藏宝图
|
|
let teams = await ComBattleTeamModel.findTeamByCapAndStatus(roleId, COM_TEAM_STATUS.FIGHTING);
|
|
if (teams && blueprt.count <= teams.length) return resResult(STATUS.COM_BATTLE_BLUEPRT_NOT_ENOUGH);
|
|
//TODO: 检查武将是否拥有
|
|
|
|
// 创建队伍数据结构
|
|
let comTeam = new ComTeam();
|
|
comTeam.blueprtId = blueprtId;
|
|
comTeam.capId = roleId;
|
|
comTeam.pub = pub;
|
|
comTeam.teamCode = teamCode;
|
|
comTeam.roleIds = [roleId];
|
|
comTeam.status = COM_TEAM_STATUS.DEFAULT;
|
|
comTeam.ceLimit = ceLimit;
|
|
|
|
let roleStatus = new RoleStatus();
|
|
roleStatus.heroes = heroes;
|
|
roleStatus.isCap = true;
|
|
roleStatus.headHid = headHid;
|
|
roleStatus.topFiveCe = topFiveCe;
|
|
roleStatus.roleId = roleId;
|
|
comTeam.roleStatus = [roleStatus];
|
|
|
|
this.teamMap.set(teamCode, comTeam);
|
|
|
|
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);
|
|
}
|
|
|
|
return resResult(STATUS.SUCCESS, { teamCode });
|
|
}
|
|
|
|
async joinTeam(msg: {teamCode: string, isFrd: boolean}, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let roleName = session.get('roleName');
|
|
let sid = session.get('sid');
|
|
console.log('role in joinTeam: ', roleId, roleName, msg, this.app.rpc);
|
|
console.log('teamMap:' + JSON.stringify(this.teamMap));
|
|
let { teamCode, isFrd } = msg;
|
|
let teamStatus = this.teamMap.get(teamCode);
|
|
if (teamStatus.status !== 0 || teamStatus.roleIds.length === 3) return resResult(STATUS.COM_BATTLE_MEMBER_LIMIT);
|
|
if (teamStatus.roleIds.indexOf(roleId) !== -1) return resResult(STATUS.COM_BATTLE_DUP_ENTER);
|
|
// TODO:助战等级限制,等表
|
|
let { lv, headHid, topFiveCe } = await Role.findByRoleId(roleId);
|
|
if (lv < COM_TEAM_ENABLE_LV) {
|
|
return resResult(STATUS.COM_BATTLE_LV_NOT_ENOUGH);
|
|
} else if (topFiveCe < teamStatus.ceLimit) {
|
|
return resResult(STATUS.COM_BATTLE_CE_LIMIT);
|
|
}
|
|
|
|
let roleStatus = new RoleStatus();
|
|
roleStatus.heroes = [];
|
|
roleStatus.isCap = false;
|
|
roleStatus.headHid = headHid || roleStatus.headHid;
|
|
roleStatus.topFiveCe = topFiveCe || roleStatus.topFiveCe;
|
|
roleStatus.roleId = roleId;
|
|
roleStatus.isFrd = isFrd;
|
|
const team = await ComBattleTeamModel.addRole(teamCode, roleStatus);
|
|
if (!team) {
|
|
return resResult(STATUS.COM_BATTLE_CREATE_ERR);
|
|
}
|
|
|
|
let channelService = this.app.get('channelService');
|
|
let channel = channelService.getChannel(teamCode, false);
|
|
let users = channel.getMembers();
|
|
if (users.indexOf(roleId) === -1) {
|
|
channel.add(roleId, sid);
|
|
}
|
|
channel.pushMessage('onTeamJoin', {teamCode, roleInfo: {roleId, headHid, topFiveCe}});
|
|
teamStatus.roleIds.push(roleId);
|
|
teamStatus.roleStatus.push(roleStatus);
|
|
|
|
return resResult(STATUS.SUCCESS, { teamInfo: {
|
|
teamCode, roleStatus: teamStatus.roleStatus, capId: teamStatus.capId, ceLimit: teamStatus.ceLimit
|
|
}});
|
|
}
|
|
|
|
async getTeams(msg: { blueprtIds: Array<number> }, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let { lv } = await Role.findByRoleId(roleId);
|
|
if (lv < COM_TEAM_ENABLE_LV) return resResult(STATUS.COM_BATTLE_LV_NOT_ENOUGH);
|
|
const teams = await ComBattleTeamModel.getTeamByBlueprt(msg.blueprtIds, COM_TEAM_STATUS.DEFAULT, true);
|
|
if (!teams) return resResult(STATUS.COM_BATTLE_NO_VALID_TEAM);
|
|
return resResult(STATUS.SUCCESS, { teamInfos: teams});
|
|
}
|
|
|
|
async startBattle(msg: {}, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let roleName = session.get('roleName');
|
|
let sid = session.get('sid');
|
|
let teamCode = session.get('teamCode');
|
|
console.log('role in startBattle: ', roleId, roleName, teamCode);
|
|
|
|
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);
|
|
}
|
|
|
|
users = channel.getMembers();
|
|
channel.pushMessage('onAdd', {users});
|
|
let tsid = channel.getMember(roleId)['sid'];
|
|
channelService.pushMessageByUids('bossHp', {data: 'datadata'}, [{
|
|
uid: roleId,
|
|
sid: tsid
|
|
}]);
|
|
|
|
return resResult(STATUS.SUCCESS, { users, teamCode });
|
|
}
|
|
|
|
/**
|
|
* Send messages to users
|
|
*
|
|
* @param {Object} msg message from client
|
|
* @param {Object} session
|
|
*
|
|
*/
|
|
async enterBattle(msg: {battleId: string , target: string}, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let roleName = session.get('roleName');
|
|
let sid = session.get('sid');
|
|
console.log('role in enterBattle: ', roleId, roleName, msg, this.app.rpc);
|
|
|
|
let channelService = this.app.get('channelService');
|
|
let channel = channelService.getChannel(msg.battleId, false);
|
|
let users = channel.getMembers();
|
|
if (users.indexOf(roleId) === -1) {
|
|
channel.add(roleId, sid);
|
|
} else {
|
|
return resResult(STATUS.COM_BATTLE_DUP_ENTER);
|
|
}
|
|
channel.pushMessage('onAddBattle', {users});
|
|
|
|
return resResult(STATUS.SUCCESS, { users });
|
|
}
|
|
|
|
async hurtHp(msg: {battleId: string, bossHurt: number, actorHurt: [{ actorId: number, actorHurt: number}]}, session: BackendSession) {
|
|
let roleId = session.get('roleId');
|
|
let roleName = session.get('roleName');
|
|
console.log('role in enterBattle: ', roleId, roleName, msg);
|
|
|
|
let channelService = this.app.get('channelService');
|
|
let channel = channelService.getChannel(msg.battleId, false);
|
|
|
|
this.bossHp -= msg.bossHurt;
|
|
if (this.bossHp < 0) {
|
|
this.bossHp = 0;
|
|
channel.pushMessage('bossHp', {data: 'boss 挂啦!!!!!'}, undefined);
|
|
}
|
|
channel.pushMessage('bossHp', {bossHp: this.bossHp}, undefined);
|
|
|
|
return resResult(STATUS.SUCCESS, { bossHp: this.bossHp});
|
|
}
|
|
}
|