添加寻宝数据库和简单的组队接口
This commit is contained in:
@@ -1,25 +1,158 @@
|
||||
import { STATUS } from './../../../consts/statusCode';
|
||||
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 startBattle(msg: {}, session: BackendSession) {
|
||||
const battleId = Math.random().toString(36).slice(-8);
|
||||
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 startBattle: ', roleId, roleName, battleId);
|
||||
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.topFiveCe = 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(battleId, true);
|
||||
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);
|
||||
@@ -33,7 +166,7 @@ export class ComBattleHandler {
|
||||
sid: tsid
|
||||
}]);
|
||||
|
||||
return resResult(STATUS.SUCCESS, { users, battleId });
|
||||
return resResult(STATUS.SUCCESS, { users, teamCode });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,23 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E
|
||||
return;
|
||||
}
|
||||
|
||||
let res = dispatch(session.get('roleId'), battleServers);
|
||||
let rid = session.get('roleId');
|
||||
|
||||
if (msg.args && msg.args.length > 0) {
|
||||
for (let arg of msg.args) {
|
||||
if (!arg.route) continue;
|
||||
if (arg.route === 'battle.comBattleHandler.createTeam') {
|
||||
rid = Math.random().toString(36).slice(-8);
|
||||
session.set('teamCode', rid);
|
||||
} else if (arg.route.indexOf('battle.comBattleHandler') !== -1) {
|
||||
if (arg.body.teamCode) {
|
||||
rid = arg.body.teamCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let res = dispatch(rid, battleServers);
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user