增加多人比赛的相关代码

This commit is contained in:
zhl 2021-04-29 18:39:24 +08:00
parent b66f94bf57
commit 9314735fb5
9 changed files with 145 additions and 14 deletions

View File

@ -36,6 +36,7 @@
"@colyseus/proxy": "^0.12.2",
"@colyseus/social": "^0.10.9",
"axios": "^0.21.0",
"body-parser": "^1.19.0",
"colyseus": "^0.14.0",
"cors": "^2.8.5",
"debug": "^4.3.1",

View File

@ -14,14 +14,14 @@ export function getAPI (opts: Partial<ApiOptions>) {
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
console.error(message);
res.status(500);
res.json({ message });
res.json({ errcode: 1, message });
}
});
api.get("/room/call", async (req: express.Request, res: express.Response) => {
const roomId = req.query.roomId as string;
const method = req.query.method as string;
const args = JSON.parse(req.query.args as string);
api.post("/room/call", async (req: express.Request, res: express.Response) => {
const roomId = req.body.roomId as string;
const method = req.body.method as string;
const args = JSON.parse(req.body.args as string);
try {
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);
console.error(e);
res.status(500);
res.json({ message });
res.json({ errcode: 1, message });
}
});
api.get("/broadcast", async (req: express.Request, res: express.Response) => {
const roomId = req.query.roomId as string;
const type = req.query.type as string;
const msg = req.query.msg as string;
api.post("/broadcast", async (req: express.Request, res: express.Response) => {
const roomId = req.body.roomId as string;
const type = req.body.type as string;
const msg = req.body.msg as string;
const method = 'broadcast'
const paramArr = [type, msg]
@ -48,7 +48,7 @@ export function getAPI (opts: Partial<ApiOptions>) {
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
console.error(e);
res.status(500);
res.json({ message });
res.json({ errcode: 1, message });
}
});

View 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;
}

View File

@ -10,6 +10,7 @@ import { MongooseDriver } from 'colyseus/lib/matchmaker/drivers/MongooseDriver'
import { RedisClient } from './redis/RedisClient'
import { PuzzleMathRoom } from './rooms/PuzzleMathRoom'
import { zApis } from './api/api'
import bodyParser from 'body-parser'
require('./common/Extend')
@ -22,6 +23,12 @@ const app = express()
app.use(cors())
app.use(express.json())
app.use(bodyParser.json({limit: '500mb'}));
app.use(bodyParser.urlencoded({
limit: '500mb',
parameterLimit: 50000,
extended: true,
}));
initData()
global.isProd = isProd
const server = http.createServer(app)

View File

@ -3,6 +3,9 @@ import { Dispatcher } from '@colyseus/command'
import { IncomingMessage } from 'http'
import { debugRoom, msgLog } from '../common/Debug'
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 {
@ -14,6 +17,8 @@ export class PuzzleMathRoom extends Room {
return true
}
onCreate(options: any) {
let cs = new PuzzleGameState()
this.setState(cs)
this.onMessage('*', (client, type, message) => {
msgLog(client.sessionId, 'sent', type, JSON.stringify(message))
@ -33,7 +38,56 @@ export class PuzzleMathRoom extends Room {
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() {
}
}

View 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', {})
}
}

View File

@ -1,12 +1,14 @@
import { Command } from '@colyseus/command'
import { PuzzleGameState } from '../schema/PuzzleGameState'
import { Client } from 'colyseus'
import { Player } from '../schema/Player'
export class OnJoinCommand extends Command<PuzzleGameState, {
client: Client,
accountId: string,
}> {
async execute({ client, accountId} = this.payload) {
let player = new Player(client.id, accountId)
this.state.players.set(client.sessionId, player)
}
}

View 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
}
}

View File

@ -1,5 +1,25 @@
import { MapSchema, Schema, type } from '@colyseus/schema'
import { GameStateConst } from '../../constants/GameStateConst'
import { Player } from './Player'
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)
}
}