内存同步数据版的共斗框架
This commit is contained in:
@@ -39,7 +39,7 @@ adminfilePath.ADMIN_USER = 'config/adminUser';
|
||||
preload();
|
||||
|
||||
// 创建 mongodb 连接
|
||||
mongoose.connect('mongodb://root:zyz_2020@dds-8vbdb47c6fb58a541.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbdb47c6fb58a542.mongodb.zhangbei.rds.aliyuncs.com:3717/admin?replicaSet=mgset-500808098', (err) => {
|
||||
mongoose.connect('mongodb://root:zyz_2020@dds-8vbdb47c6fb58a541.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbdb47c6fb58a542.mongodb.zhangbei.rds.aliyuncs.com:3717/admin?replicaSet=mgset-500808098', { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
|
||||
if (err) {
|
||||
console.log('mongodb connect err', err);
|
||||
} else {
|
||||
@@ -140,6 +140,7 @@ app.configure('production|development', function () {
|
||||
|
||||
// route configures
|
||||
app.route('chat', routeUtil.chat);
|
||||
app.route('battle', routeUtil.battle);
|
||||
|
||||
// filter configures
|
||||
app.filter(new pinus.filters.timeout());
|
||||
@@ -162,6 +163,7 @@ app.configure('production|development', function () {
|
||||
app.configure('development', function () {
|
||||
// enable the system monitor modules
|
||||
app.enable('systemMonitor');
|
||||
app.enable('rpcDebugLog');
|
||||
});
|
||||
|
||||
if (app.isMaster()) {
|
||||
|
||||
95
game-server/app/servers/battle/handler/comBattleHandler.ts
Normal file
95
game-server/app/servers/battle/handler/comBattleHandler.ts
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
127
game-server/app/servers/battle/remote/comBattleRemote.ts
Normal file
127
game-server/app/servers/battle/remote/comBattleRemote.ts
Normal file
@@ -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,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);
|
||||
}
|
||||
@@ -19,6 +19,9 @@ module.exports = {
|
||||
'role': [
|
||||
{'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053, 'args': '--inspect=10006'}
|
||||
],
|
||||
'battle': [
|
||||
{'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054, 'args': '--inspect=10007'}
|
||||
],
|
||||
'gate': [
|
||||
{
|
||||
'id': 'gate-server-1',
|
||||
@@ -27,14 +30,22 @@ module.exports = {
|
||||
'clientPort': 3014,
|
||||
'frontend': true,
|
||||
'args': '--inspect=10003'
|
||||
},
|
||||
{
|
||||
'id': 'gate-server-2',
|
||||
'host': '127.0.0.1',
|
||||
'clientHost': 'pinus_test.trgame.cn',
|
||||
'clientPort': 3015,
|
||||
'frontend': true,
|
||||
'args': '--inspect=10008'
|
||||
}
|
||||
]
|
||||
},
|
||||
'production': {
|
||||
'connector': [
|
||||
{'id': 'connector-server-1', 'port': 4050, 'host': 'pinus_test.trgame.cn', 'clientPort': 3050, 'frontend': true},
|
||||
{'id': 'connector-server-2', 'port': 4051, 'host': 'pinus_test.trgame.cn', 'clientPort': 3051, 'frontend': true},
|
||||
{'id': 'connector-server-3', 'port': 4052, 'host': 'pinus_test.trgame.cn', 'clientPort': 3052, 'frontend': true}
|
||||
{'id': 'connector-server-1', 'port': 4050, 'clientHost': 'pinus_test.trgame.cn', 'host': '127.0.0.1', 'clientPort': 3050, 'frontend': true},
|
||||
{'id': 'connector-server-2', 'port': 4051, 'clientHost': 'pinus_test.trgame.cn', 'host': '127.0.0.1', 'clientPort': 3051, 'frontend': true},
|
||||
{'id': 'connector-server-3', 'port': 4052, 'clientHost': 'pinus_test.trgame.cn', 'host': '127.0.0.1', 'clientPort': 3052, 'frontend': true}
|
||||
],
|
||||
'chat': [
|
||||
{'id': 'chat-server-1', 'host': '127.0.0.1', 'port': 6050},
|
||||
@@ -42,10 +53,20 @@ module.exports = {
|
||||
{'id': 'chat-server-3', 'host': '127.0.0.1', 'port': 6052}
|
||||
],
|
||||
'role': [
|
||||
{'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053, 'args': '--inspect=10006'}
|
||||
{'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053}
|
||||
],
|
||||
'battle': [
|
||||
{'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054}
|
||||
],
|
||||
'gate': [
|
||||
{'id': 'gate-server-1', 'host': '127.0.0.1', 'clientHost': 'pinus_test.trgame.cn', 'clientPort': 3014, 'frontend': true}
|
||||
{'id': 'gate-server-1', 'host': '127.0.0.1', 'clientHost': 'pinus_test.trgame.cn', 'clientPort': 3014, 'frontend': true},
|
||||
{
|
||||
'id': 'gate-server-2',
|
||||
'host': '127.0.0.1',
|
||||
'clientHost': 'pinus_test.trgame.cn',
|
||||
'clientPort': 3015,
|
||||
'frontend': true
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
@@ -15,6 +15,9 @@ services:
|
||||
- "6050:6050"
|
||||
- "6051:6051"
|
||||
- "6052:6052"
|
||||
- "6053:6053"
|
||||
- "6054:6054"
|
||||
- "3014:3014"
|
||||
- "3015:3015"
|
||||
|
||||
command: sh ./startGameServer.sh #使用 shell脚本启动游戏服务器
|
||||
|
||||
@@ -86,6 +86,7 @@ for(serverType in serversConfig)
|
||||
{
|
||||
appPm2Config.args.push('frontend='+ singleServer.frontend);
|
||||
appPm2Config.args.push('clientPort='+singleServer.clientPort);
|
||||
appPm2Config.args.push('clientHost='+singleServer.clientHost);
|
||||
}
|
||||
|
||||
appPm2Config.cwd= cwd;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, arrayProp, Ref } from '@typegoose/typegoose';
|
||||
import { index, getModelForClass, prop, Ref } from '@typegoose/typegoose';
|
||||
import Equip from './Equip';
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user