内存同步数据版的共斗框架

This commit is contained in:
liangtongchuan
2020-09-08 19:59:19 +08:00
parent 2298f36f02
commit de563792cc
12 changed files with 280 additions and 12 deletions
@@ -0,0 +1,95 @@
import { Application, BackendSession } from 'pinus';
export default function(app: Application) {
return new ComBattleHandler(app);
}
export class ComBattleHandler {
constructor(private app: Application) {
}
private bossHp: number = 10000;
async startBattle(msg: {}, session: BackendSession) {
const battleId = Math.random().toString(36).slice(-8);
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let sid = session.get('sid');
console.log('role in startBattle: ', roleId, roleName, battleId);
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(battleId, 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 {
code: 200,
data: {
users,
battleId
}
}
}
/**
* 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 {
code: 202,
data: '不能重复加入'
};
}
channel.pushMessage('onAddBattle', {users});
return {
code: 200,
data: 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 {
code: 200,
data: this.bossHp
}
}
}
@@ -0,0 +1,127 @@
import { Application, ChannelService, FrontendSession, RemoterClass } from 'pinus';
export default function (app: Application) {
return new ComBattleRemote(app);
}
export class ComBattleRemote {
bossHp = 10000;
constructor(private app: Application) {
this.app = app;
this.channelService = app.get('channelService');
}
private channelService: ChannelService;
public async create(uid: string, sid: string, name: string, flag: boolean) {
console.log('comBattleRemote create: ', name, flag);
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
let username = uid.split('*')[0];
let param = {
user: username
};
channel.pushMessage('onAdd', param);
if (!!channel) {
channel.add(uid, sid);
}
}
return this.get(name, flag);
}
/**
* Add user into chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
* @param {boolean} flag channel parameter
*
*/
public async add(uid: string, sid: string, name: string, flag: boolean) {
console.log('comBattleRemote add: ', name, flag);
let channel = this.channelService.getChannel(name, flag);
if (!!channel && !this.get(name, false).includes(uid)) {
let username = uid.split('*')[0];
let param = {
user: username
};
channel.pushMessage('onAdd', param);
if (!!channel) {
channel.add(uid, sid);
}
}
return this.get(name, flag);
}
public async available(uid: string, sid: string, name: string, flag: boolean) {
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
const users = this.get(name, false);
if (users.includes(uid)) {
console.log('不得重复加入');
return false;
}
}
return true;
}
/**
* Get user from chat channel.
*
* @param {Object} opts parameters for request
* @param {String} name channel name
* @param {boolean} flag channel parameter
* @return {Array} users uids in channel
*
*/
private get(name: string, flag: boolean) {
let users: string[] = [];
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
users = channel.getMembers();
}
for (let i = 0; i < users.length; i++) {
users[i] = users[i].split('*')[0];
}
return users;
}
/**
* Kick user out chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
*
*/
public async kick(uid: string, sid: string, name: string) {
let channel = this.channelService.getChannel(name, false);
// leave channel
if (!!channel) {
channel.leave(uid, sid);
}
let username = uid.split('*')[0];
let param = {
user: username
};
channel.pushMessage('onLeave', param);
}
public async hurt(uid: string, sid: string, name: string, bossHurt: number, actorHurt: [{actorId: number, actorHurt: number}]) {
console.log('hurt channel name: ', name);
let channelService = this.app.get('channelService');
this.bossHp -= bossHurt;
let channel = channelService.getChannel(name, false);
if (!!channel) {
if (this.bossHp < 0) {
this.bossHp = 0;
}
channel.pushMessage('bossHp', {bossHp: this.bossHp});
}
return this.bossHp;
}
}
@@ -44,7 +44,7 @@ export class EntryHandler {
}
}
// duplicate log in
if (!!sessionService.getByUid(uid)) {
if (!!sessionService.getByUid(role.roleId)) {
console.log('uid not found');
return {
code: 500,
@@ -52,11 +52,13 @@ export class EntryHandler {
};
}
await session.abind(uid);
await session.abind(role.roleId);
session.set('rid', rid);
session.set('uid', user.uid);
session.set('uid', role.roleId);
session.set('roleId', role.roleId);
session.set('roleName', role.roleName);
session.set('sid', self.app.get('serverId'));
session.push('sid', () => {});
session.push('roleId', () => {});
session.push('roleName', () => {});
session.push('rid', function (err) {
@@ -67,7 +69,7 @@ export class EntryHandler {
session.on('closed', this.onUserLeave.bind(this));
// put user into channel
let users = await self.app.rpc.chat.chatRemote.add.route(session)(uid, self.app.get('serverId'), rid, true);
let users = await self.app.rpc.chat.chatRemote.add.route(session)(role.roleId, self.app.get('serverId'), rid, true);
let heros = await Hero.findByRole(role.roleId);
let equips = await Equip.findbyRole(role.roleId);
role['heros'] = heros;
@@ -34,7 +34,7 @@ export class GateHandler {
let res = dispatch(uid, connectors);
return {
code: 200,
host: res.host,
host: res.clientHost,
port: res.clientPort
};
}
@@ -10,4 +10,5 @@ export class RoleRemote {
this.app = app;
this.channelService = app.get('channelService');
}
private channelService: ChannelService;
}
@@ -4,11 +4,15 @@
// UserRpc的命名空间自动合并
import { FrontendSession, RemoterClass } from 'pinus';
import { ChatRemote } from './chat/remote/chatRemote';
import { ComBattleRemote } from './battle/remote/comBattleRemote';
declare global {
interface UserRpc {
chat: {
chatRemote: RemoterClass<FrontendSession, ChatRemote>;
};
battle: {
comBattleRemote: RemoterClass<FrontendSession, ComBattleRemote>;
};
}
}
+12
View File
@@ -12,4 +12,16 @@ export function chat(session: Session, msg: any, app: Application, cb: (err: Err
let res = dispatch(session.get('rid'), chatServers);
cb(null, res.id);
}
export function battle(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {
let battleServers = app.getServersByType('battle');
if(!battleServers || battleServers.length === 0) {
cb(new Error('can not find battle servers.'));
return;
}
let res = dispatch(session.get('rid'), battleServers);
cb(null, res.id);
}