Merge branch 'second' of http://git.kingsome.cn/node/card_svr into second

This commit is contained in:
yuexin 2021-01-14 19:20:31 +08:00
commit c0c3bc37fa
11 changed files with 95 additions and 87 deletions

View File

@ -1,5 +1,6 @@
import axios from "axios"; import axios from "axios";
import {Config} from "../cfg/Config"; import {Config} from "../cfg/Config";
import {debugRoom, error} from "./Debug";
let config: Config = require('../../config/config.json'); let config: Config = require('../../config/config.json');
@ -33,4 +34,45 @@ export function requestUnlockHero(accountid: string, heroid: number | string) {
return axios.post(`${config.info_svr}/${accountid}/hero/unlock/${heroid}`, data); return axios.post(`${config.info_svr}/${accountid}/hero/unlock/${heroid}`, data);
} }
/**
*
* @param data
*/
export function reportGameResult(data: any) {
let dataStr = JSON.stringify(data);
let reqConfig = {
method: 'post',
url: `${config.info_svr}/record/save`,
headers: {
'Content-Type': 'application/json',
},
data : dataStr
};
// @ts-ignore
axios(reqConfig)
.then(function (response) {
debugRoom(JSON.stringify(response.data));
})
.catch(function (err) {
error(err);
});
}
/**
*
* @param data
*/
export function createRobot(data: any) {
axios.get('http://127.0.0.1:2500/robot/create', {
params: data
}).then((res) => {
debugRoom(res.status);
debugRoom(res.data);
}).catch((err) => {
error(err);
})
}

7
src/global.d.ts vendored
View File

@ -282,6 +282,13 @@ declare module "colyseus" {
* @param srcPlayer * @param srcPlayer
*/ */
getOppositePlayer(srcPlayer: string|Player): Player; getOppositePlayer(srcPlayer: string|Player): Player;
/**
* index获取玩家
* @param {number} idx
* @return {Player}
*/
getPlayerByIdx(idx: number): Player;
} }
} }

View File

@ -1,4 +1,4 @@
import {Client, generateId, Room} from "colyseus"; import {Client, Room} from "colyseus";
import {CardGameState} from "./schema/CardGameState"; import {CardGameState} from "./schema/CardGameState";
import {OnJoinCommand} from "./commands/OnJoinCommand"; import {OnJoinCommand} from "./commands/OnJoinCommand";
import {PlayReadyCommand} from "./commands/PlayReadyCommand"; import {PlayReadyCommand} from "./commands/PlayReadyCommand";
@ -19,9 +19,8 @@ import {GMCommand} from "./commands/GMCommand";
import {GameStateConst} from "../constants/GameStateConst"; import {GameStateConst} from "../constants/GameStateConst";
import {GameRestartCommand} from "./commands/GameRestartCommand"; import {GameRestartCommand} from "./commands/GameRestartCommand";
import {RobotClient} from "../robot/RobotClient"; import {RobotClient} from "../robot/RobotClient";
import axios from 'axios';
import {wait} from "../decorators/cfg";
import {ChangePetCommand} from "./commands/ChangePetCommand"; import {ChangePetCommand} from "./commands/ChangePetCommand";
import {createRobot} from "../common/WebApi";
export class GeneralRoom extends Room { export class GeneralRoom extends Room {
dispatcher = new Dispatcher(this); dispatcher = new Dispatcher(this);
@ -245,39 +244,7 @@ export class GeneralRoom extends Room {
room: this.roomId, room: this.roomId,
sessionId: playerId sessionId: playerId
} }
createRobot(data);
axios.get('http://127.0.0.1:2500/robot/create', {
params: data
}).then((res) => {
debugRoom(res.status);
debugRoom(res.data);
}).catch((err) => {
error(err);
})
// const sessionId = playerId || generateId();
// let client = new RobotClient(sessionId, this.state, this.clock, this['onMessageHandlers']);
// if (this.reservedSeatTimeouts[sessionId]) {
// clearTimeout(this.reservedSeatTimeouts[sessionId]);
// delete this.reservedSeatTimeouts[sessionId];
// }
// // get seat reservation options and clear it
// const options = this.reservedSeats[sessionId];
// delete this.reservedSeats[sessionId];
// this.clients.push(client);
// client.ref.once('close', this['_onLeave'].bind(this, client));
// // client.ref.on('message', this.onMessage.bind(this, client));
// const reconnection = this.reconnections[sessionId];
// if (reconnection) {
// reconnection.resolve(client);
// }
// else {
// if (this.onJoin) {
// this.onJoin(client, options);
// }
// delete this.reservedSeats[sessionId];
// }
// this._events.emit('join', client);
} }
addAssistClient(sessionId: string) { addAssistClient(sessionId: string) {
@ -290,7 +257,17 @@ export class GeneralRoom extends Room {
getAssistClient(sessionId: string): RobotClient { getAssistClient(sessionId: string): RobotClient {
return this.assistMap.get(sessionId); return this.assistMap.get(sessionId);
} }
getPlayerByIdx(idx: number) {
for (let [, player] of this.state.players) {
if (player.idx == idx) {
return player;
}
}
}
/**
*
* @param srcPlayer
*/
getOppositePlayer(srcPlayer: string | Player): Player { getOppositePlayer(srcPlayer: string | Player): Player {
let playerId; let playerId;
if (typeof srcPlayer === 'string') { if (typeof srcPlayer === 'string') {
@ -301,9 +278,8 @@ export class GeneralRoom extends Room {
if (!this.state.players.has(playerId)) { if (!this.state.players.has(playerId)) {
return null; return null;
} }
const sessionIds = [...this.state.players.keys()]; let idx = this.state.players.get(playerId).idx;
let idx = sessionIds.indexOf(playerId); let opposIdx = (2 + idx) % 4;
let playerId2 = sessionIds[(2 + idx) % 4]; return this.getPlayerByIdx(opposIdx);
return this.state.players.get(playerId2);
} }
} }

View File

@ -19,7 +19,9 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
async execute({timeUp} = this.payload) { async execute({timeUp} = this.payload) {
let giveUpCount = 0; let giveUpCount = 0;
const playerCount = this.room.maxClients - 1; const playerCount = this.room.maxClients - 1;
const sessionIds = [...this.state.players.keys()]; const players = [...this.state.players.values()];
players.sort((a, b) => a.idx - b.idx);
const sessionIds = players.map(p => p.id);
// 将当前轮玩家移至第一位 // 将当前轮玩家移至第一位
let pids: string[] = sessionIds.moveElement(-sessionIds.indexOf(this.state.currentTurn)); let pids: string[] = sessionIds.moveElement(-sessionIds.indexOf(this.state.currentTurn));

View File

@ -45,10 +45,6 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
} }
} }
getPlayerByIdx(idx: number) {
let players = [...this.state.players.values()];
return players[idx];
}
sendHelp(client: Client, msg: string) { sendHelp(client: Client, msg: string) {
let str = '' let str = ''
str += '将一个玩家的手牌全变成指定的效果卡: changeeffect:玩家index|效果卡id 例: changeeffect:0|20011 \n'; str += '将一个玩家的手牌全变成指定的效果卡: changeeffect:玩家index|效果卡id 例: changeeffect:0|20011 \n';
@ -110,7 +106,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
let arr = msg.split('|'); let arr = msg.split('|');
let playerIdx = parseInt(arr[0]); let playerIdx = parseInt(arr[0]);
let effectId = parseInt(arr[1]); let effectId = parseInt(arr[1]);
let player = this.getPlayerByIdx(playerIdx); let player = this.room.getPlayerByIdx(playerIdx);
for (let [, card] of player.cards) { for (let [, card] of player.cards) {
card.effect = effectId; card.effect = effectId;
} }
@ -131,7 +127,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
let arr = msg.split('|'); let arr = msg.split('|');
let playerIdx = parseInt(arr[0]); let playerIdx = parseInt(arr[0]);
let heroId = parseInt(arr[1]); let heroId = parseInt(arr[1]);
let player = this.getPlayerByIdx(playerIdx); let player = this.room.getPlayerByIdx(playerIdx);
player.heroId = heroId; player.heroId = heroId;
this.room.battleMan.updatePlayerHero(player); this.room.battleMan.updatePlayerHero(player);
let client = this.room.getClient(player.id); let client = this.room.getClient(player.id);
@ -158,7 +154,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
let arr = msg.split('|'); let arr = msg.split('|');
let playerIdx = parseInt(arr[0]); let playerIdx = parseInt(arr[0]);
let effectId = parseInt(arr[1]); let effectId = parseInt(arr[1]);
let player = this.getPlayerByIdx(playerIdx); let player = this.room.getPlayerByIdx(playerIdx);
let count = parseInt(arr[2]); let count = parseInt(arr[2]);
this.room.generateCard({player: player, count, effectId, fromplayer: player}); this.room.generateCard({player: player, count, effectId, fromplayer: player});
let client = this.room.getClient(player.id); let client = this.room.getClient(player.id);
@ -178,7 +174,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
} }
let arr = msg.split('|'); let arr = msg.split('|');
let playerIdx = parseInt(arr[0]); let playerIdx = parseInt(arr[0]);
let player = this.getPlayerByIdx(playerIdx); let player = this.room.getPlayerByIdx(playerIdx);
let count = parseInt(arr[1]); let count = parseInt(arr[1]);
let extData; let extData;
if (arr.length > 2) { if (arr.length > 2) {
@ -208,7 +204,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
updateHeroHp(msg: string) { updateHeroHp(msg: string) {
let arr = msg.split('|'); let arr = msg.split('|');
let playerIdx = parseInt(arr[0]); let playerIdx = parseInt(arr[0]);
let player = this.getPlayerByIdx(playerIdx); let player = this.room.getPlayerByIdx(playerIdx);
let count = parseInt(arr[1]); let count = parseInt(arr[1]);
this.room.updateHp(player.id, count); this.room.updateHp(player.id, count);
} }

View File

@ -9,11 +9,9 @@ import gameUtil from "../../utils/game.util";
import {Card} from "../schema/Card"; import {Card} from "../schema/Card";
import {MapSchema, SetSchema} from "@colyseus/schema"; import {MapSchema, SetSchema} from "@colyseus/schema";
import {StateTypeEnum} from "../enums/StateTypeEnum"; import {StateTypeEnum} from "../enums/StateTypeEnum";
import axios from "axios"; import {reportGameResult} from "../../common/WebApi";
import {Config} from "../../cfg/Config";
let config: Config = require('../../../config/config.json');
/** /**
* *
*/ */
@ -100,7 +98,7 @@ export class GameResultCommand extends Command<CardGameState, {}> {
val: val[1] val: val[1]
}) })
} }
resultData[statics] = statics; resultData['statics'] = statics;
let self = this; let self = this;
this.room.bGameResult(resultData); this.room.bGameResult(resultData);
this.state.updateGameState(GameStateConst.STATE_GAME_OVER); this.state.updateGameState(GameStateConst.STATE_GAME_OVER);
@ -207,25 +205,7 @@ export class GameResultCommand extends Command<CardGameState, {}> {
}); });
} }
data.players = players; data.players = players;
let dataStr = JSON.stringify(data); reportGameResult(data);
let reqConfig = {
method: 'post',
url: `${config.info_svr}/record/save`,
headers: {
'Content-Type': 'application/json',
},
data : dataStr
};
// @ts-ignore
axios(reqConfig)
.then(function (response) {
debugRoom(JSON.stringify(response.data));
})
.catch(function (err) {
error(err);
});
} }
} }

View File

@ -14,7 +14,9 @@ export class NextTurnCommand extends Command<CardGameState, {}> {
async execute(){ async execute(){
this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW); this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW);
const sessionIds = [...this.state.players.keys()]; const players = [...this.state.players.values()];
players.sort((a, b) => a.idx - b.idx);
const sessionIds = players.map(p => p.id);
if (!this.state.currentTurn) { if (!this.state.currentTurn) {
this.state.round = 0; this.state.round = 0;
} }

View File

@ -13,9 +13,9 @@ export class OnJoinCommand extends Command<CardGameState, {
accountId: string accountId: string
}> { }> {
execute({client, accountId} = this.payload) { execute({client, accountId} = this.payload) {
let team = (this.state.players.size == 1 || this.state.players.size == 2)? 1 : 0; let count = this.state.players.size;
// 实际的team会在PlayReadyCommand中设置 let team = (count == 1 || count == 2)? 1 : 0;
let player = new Player(client.sessionId, 0, team); let player = new Player(client.sessionId, count, team);
if (accountId && accountId == 'robot') { if (accountId && accountId == 'robot') {
accountId = `robot_${client.sessionId}`; accountId = `robot_${client.sessionId}`;
} else if (!accountId) { } else if (!accountId) {

View File

@ -30,11 +30,11 @@ export class PlayReadyCommand extends Command<CardGameState, {
this.room.stopSchedule('waiting_player'); this.room.stopSchedule('waiting_player');
// 比大小, 确定先手 // 比大小, 确定先手
// return [new PrepareCommand()]; // return [new PrepareCommand()];
let i = 0; // let i = 0;
for (let [,player] of this.state.players) { // for (let [,player] of this.state.players) {
player.team = (i == 1 || i == 2) ? 1 : 0; // player.team = (i == 1 || i == 2) ? 1 : 0;
i += 1; // i += 1;
} // }
await this.room.setPrivate(true); await this.room.setPrivate(true);
this.room.state.updateGameState(GameStateConst.CHANGE_HERO); this.room.state.updateGameState(GameStateConst.CHANGE_HERO);
let self = this; let self = this;

View File

@ -7,7 +7,6 @@ import {BaseConst} from "../../constants/BaseConst";
import {error} from "../../common/Debug"; import {error} from "../../common/Debug";
import {GameEnv} from "../../cfg/GameEnv"; import {GameEnv} from "../../cfg/GameEnv";
import {StateTypeEnum} from "../enums/StateTypeEnum"; import {StateTypeEnum} from "../enums/StateTypeEnum";
import {raw} from "express";
import {getCardGroup} from "../../common/WebApi"; import {getCardGroup} from "../../common/WebApi";
/** /**
@ -37,7 +36,7 @@ export class SelectHeroCommand extends Command<CardGameState, { client: Client,
for (let key in StateTypeEnum) { for (let key in StateTypeEnum) {
if (!isNaN(Number(key))) { if (!isNaN(Number(key))) {
// @ts-ignore // @ts-ignore
let type: StateTypeEnum = key; let type: StateTypeEnum = Number(key);
player.statData.set(type, 0); player.statData.set(type, 0);
} }
} }

View File

@ -59,6 +59,9 @@ export class Player extends Schema {
*/ */
@type("number") @type("number")
team: number; team: number;
@type("number")
idx: number;
/** /**
* , , * , ,
*/ */
@ -89,12 +92,13 @@ export class Player extends Schema {
@type({ map: "number" }) @type({ map: "number" })
unitCfgs = new MapSchema<number>(); unitCfgs = new MapSchema<number>();
constructor(id: string, heroId: number, team: number) { constructor(id: string, idx: number, team: number) {
super(); super();
this.id = id; this.id = id;
this.state = PlayerStateConst.PLAYER_NORMAL; this.state = PlayerStateConst.PLAYER_NORMAL;
this.hp = 0; this.hp = 0;
this.heroId = heroId; this.idx = idx;
this.heroId = 0;
this.team = team; this.team = team;
this.countTotal = 0; this.countTotal = 0;
this.countPresent = 0; this.countPresent = 0;