增加多人比赛的相关代码
This commit is contained in:
parent
b66f94bf57
commit
9314735fb5
@ -36,6 +36,7 @@
|
|||||||
"@colyseus/proxy": "^0.12.2",
|
"@colyseus/proxy": "^0.12.2",
|
||||||
"@colyseus/social": "^0.10.9",
|
"@colyseus/social": "^0.10.9",
|
||||||
"axios": "^0.21.0",
|
"axios": "^0.21.0",
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
"colyseus": "^0.14.0",
|
"colyseus": "^0.14.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -14,14 +14,14 @@ export function getAPI (opts: Partial<ApiOptions>) {
|
|||||||
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
||||||
console.error(message);
|
console.error(message);
|
||||||
res.status(500);
|
res.status(500);
|
||||||
res.json({ message });
|
res.json({ errcode: 1, message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.get("/room/call", async (req: express.Request, res: express.Response) => {
|
api.post("/room/call", async (req: express.Request, res: express.Response) => {
|
||||||
const roomId = req.query.roomId as string;
|
const roomId = req.body.roomId as string;
|
||||||
const method = req.query.method as string;
|
const method = req.body.method as string;
|
||||||
const args = JSON.parse(req.query.args as string);
|
const args = JSON.parse(req.body.args as string);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await matchMaker.remoteRoomCall(roomId, method, args);
|
const data = await matchMaker.remoteRoomCall(roomId, method, args);
|
||||||
@ -30,14 +30,14 @@ export function getAPI (opts: Partial<ApiOptions>) {
|
|||||||
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
||||||
console.error(e);
|
console.error(e);
|
||||||
res.status(500);
|
res.status(500);
|
||||||
res.json({ message });
|
res.json({ errcode: 1, message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.get("/broadcast", async (req: express.Request, res: express.Response) => {
|
api.post("/broadcast", async (req: express.Request, res: express.Response) => {
|
||||||
const roomId = req.query.roomId as string;
|
const roomId = req.body.roomId as string;
|
||||||
const type = req.query.type as string;
|
const type = req.body.type as string;
|
||||||
const msg = req.query.msg as string;
|
const msg = req.body.msg as string;
|
||||||
const method = 'broadcast'
|
const method = 'broadcast'
|
||||||
const paramArr = [type, msg]
|
const paramArr = [type, msg]
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ export function getAPI (opts: Partial<ApiOptions>) {
|
|||||||
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
|
||||||
console.error(e);
|
console.error(e);
|
||||||
res.status(500);
|
res.status(500);
|
||||||
res.json({ message });
|
res.json({ errcode: 1, message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
12
src/constants/GameStateConst.ts
Normal file
12
src/constants/GameStateConst.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export class GameStateConst {
|
||||||
|
|
||||||
|
// 等待玩家加入
|
||||||
|
public static readonly STATE_WAIT_JOIN = 0;
|
||||||
|
// 等待玩家准备
|
||||||
|
public static readonly STATE_WAIT_PREPARE = 1;
|
||||||
|
// 游戏进行中
|
||||||
|
public static readonly STATE_GAMING = 2;
|
||||||
|
|
||||||
|
// 游戏结束
|
||||||
|
public static readonly STATE_GAME_OVER = 9;
|
||||||
|
}
|
@ -10,6 +10,7 @@ import { MongooseDriver } from 'colyseus/lib/matchmaker/drivers/MongooseDriver'
|
|||||||
import { RedisClient } from './redis/RedisClient'
|
import { RedisClient } from './redis/RedisClient'
|
||||||
import { PuzzleMathRoom } from './rooms/PuzzleMathRoom'
|
import { PuzzleMathRoom } from './rooms/PuzzleMathRoom'
|
||||||
import { zApis } from './api/api'
|
import { zApis } from './api/api'
|
||||||
|
import bodyParser from 'body-parser'
|
||||||
|
|
||||||
require('./common/Extend')
|
require('./common/Extend')
|
||||||
|
|
||||||
@ -22,6 +23,12 @@ const app = express()
|
|||||||
|
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
|
app.use(bodyParser.json({limit: '500mb'}));
|
||||||
|
app.use(bodyParser.urlencoded({
|
||||||
|
limit: '500mb',
|
||||||
|
parameterLimit: 50000,
|
||||||
|
extended: true,
|
||||||
|
}));
|
||||||
initData()
|
initData()
|
||||||
global.isProd = isProd
|
global.isProd = isProd
|
||||||
const server = http.createServer(app)
|
const server = http.createServer(app)
|
||||||
|
@ -3,6 +3,9 @@ import { Dispatcher } from '@colyseus/command'
|
|||||||
import { IncomingMessage } from 'http'
|
import { IncomingMessage } from 'http'
|
||||||
import { debugRoom, msgLog } from '../common/Debug'
|
import { debugRoom, msgLog } from '../common/Debug'
|
||||||
import { OnJoinCommand } from './commands/OnJoinCommand'
|
import { OnJoinCommand } from './commands/OnJoinCommand'
|
||||||
|
import { Player } from './schema/Player'
|
||||||
|
import { BeginGameCommand } from './commands/BeginGameCommand'
|
||||||
|
import { PuzzleGameState } from './schema/PuzzleGameState'
|
||||||
|
|
||||||
|
|
||||||
export class PuzzleMathRoom extends Room {
|
export class PuzzleMathRoom extends Room {
|
||||||
@ -14,6 +17,8 @@ export class PuzzleMathRoom extends Room {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
onCreate(options: any) {
|
onCreate(options: any) {
|
||||||
|
let cs = new PuzzleGameState()
|
||||||
|
this.setState(cs)
|
||||||
|
|
||||||
this.onMessage('*', (client, type, message) => {
|
this.onMessage('*', (client, type, message) => {
|
||||||
msgLog(client.sessionId, 'sent', type, JSON.stringify(message))
|
msgLog(client.sessionId, 'sent', type, JSON.stringify(message))
|
||||||
@ -33,7 +38,56 @@ export class PuzzleMathRoom extends Room {
|
|||||||
this.dispatcher.stop()
|
this.dispatcher.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
adminMsg(str: string) {
|
/**
|
||||||
console.log(`receive admin msg: ${str}`);
|
* 获取指定player的client实例, 如果玩家已掉线, 则获取该玩家的assist client
|
||||||
|
* @param {string | Player} player
|
||||||
|
* @return {Client}
|
||||||
|
*/
|
||||||
|
getRemoteClient(player: string | Player): Client {
|
||||||
|
let result: Client
|
||||||
|
if (typeof player == 'string') {
|
||||||
|
result = this.clients.find(client => client.sessionId == player)
|
||||||
|
} else {
|
||||||
|
result = this.clients.find(client => client.sessionId == player.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送私信给玩家
|
||||||
|
* @param {string} clientId
|
||||||
|
* @param {string} type
|
||||||
|
* @param {string} msg
|
||||||
|
*/
|
||||||
|
smsg(clientId: string, type: string, msg: string) {
|
||||||
|
msgLog(`sendMsg to: ${clientId}, type: ${type}, msg: ${msg}`);
|
||||||
|
const client = this.getRemoteClient(clientId)
|
||||||
|
if (client) {
|
||||||
|
client.send(type, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入当前房间的client数量
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
get clientCount(): number {
|
||||||
|
return this.clients.length
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始游戏
|
||||||
|
*/
|
||||||
|
beginGame() {
|
||||||
|
console.log(`admin send begin game cmd`)
|
||||||
|
this.dispatcher.dispatch(new BeginGameCommand() )
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 游戏结束
|
||||||
|
*/
|
||||||
|
endGame() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/rooms/commands/BeginGameCommand.ts
Normal file
13
src/rooms/commands/BeginGameCommand.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Command } from '@colyseus/command'
|
||||||
|
import { PuzzleGameState } from '../schema/PuzzleGameState'
|
||||||
|
import { GameStateConst } from '../../constants/GameStateConst'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始游戏
|
||||||
|
*/
|
||||||
|
export class BeginGameCommand extends Command<PuzzleGameState, {}> {
|
||||||
|
async execute() {
|
||||||
|
this.state.updateGameState(GameStateConst.STATE_GAMING)
|
||||||
|
this.room.broadcast('begingame', {})
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,14 @@
|
|||||||
import { Command } from '@colyseus/command'
|
import { Command } from '@colyseus/command'
|
||||||
import { PuzzleGameState } from '../schema/PuzzleGameState'
|
import { PuzzleGameState } from '../schema/PuzzleGameState'
|
||||||
import { Client } from 'colyseus'
|
import { Client } from 'colyseus'
|
||||||
|
import { Player } from '../schema/Player'
|
||||||
|
|
||||||
export class OnJoinCommand extends Command<PuzzleGameState, {
|
export class OnJoinCommand extends Command<PuzzleGameState, {
|
||||||
client: Client,
|
client: Client,
|
||||||
accountId: string,
|
accountId: string,
|
||||||
}> {
|
}> {
|
||||||
async execute({ client, accountId} = this.payload) {
|
async execute({ client, accountId} = this.payload) {
|
||||||
|
let player = new Player(client.id, accountId)
|
||||||
|
this.state.players.set(client.sessionId, player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
src/rooms/schema/Player.ts
Normal file
22
src/rooms/schema/Player.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { Schema, type } from '@colyseus/schema'
|
||||||
|
|
||||||
|
export class Player extends Schema {
|
||||||
|
@type('string')
|
||||||
|
id: string
|
||||||
|
|
||||||
|
accountId: string
|
||||||
|
|
||||||
|
@type('number')
|
||||||
|
score: number
|
||||||
|
|
||||||
|
@type('number')
|
||||||
|
rank: number
|
||||||
|
|
||||||
|
team: number
|
||||||
|
|
||||||
|
constructor(id: string, accountId: string) {
|
||||||
|
super()
|
||||||
|
this.id = id
|
||||||
|
this.accountId = accountId
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,25 @@
|
|||||||
import { MapSchema, Schema, type } from '@colyseus/schema'
|
import { MapSchema, Schema, type } from '@colyseus/schema'
|
||||||
|
import { GameStateConst } from '../../constants/GameStateConst'
|
||||||
|
import { Player } from './Player'
|
||||||
|
|
||||||
export class PuzzleGameState extends Schema {
|
export class PuzzleGameState extends Schema {
|
||||||
|
@type('number')
|
||||||
|
gameState: number = GameStateConst.STATE_WAIT_JOIN
|
||||||
|
|
||||||
|
@type({ map: Player })
|
||||||
|
players = new MapSchema<Player>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 轮次
|
||||||
|
*/
|
||||||
|
@type('number')
|
||||||
|
round: number
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
updateGameState(val: number) {
|
||||||
|
let preVal = this.gameState
|
||||||
|
this.gameState = val
|
||||||
|
this.$listeners?.gameState?.invoke(val, preVal)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user