Merge branch 'second' of http://git.kingsome.cn/node/card_svr into second
This commit is contained in:
commit
c0c3bc37fa
@ -1,5 +1,6 @@
|
||||
import axios from "axios";
|
||||
import {Config} from "../cfg/Config";
|
||||
import {debugRoom, error} from "./Debug";
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报游戏结果
|
||||
* @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
7
src/global.d.ts
vendored
@ -282,6 +282,13 @@ declare module "colyseus" {
|
||||
* @param srcPlayer
|
||||
*/
|
||||
getOppositePlayer(srcPlayer: string|Player): Player;
|
||||
|
||||
/**
|
||||
* 根据index获取玩家
|
||||
* @param {number} idx
|
||||
* @return {Player}
|
||||
*/
|
||||
getPlayerByIdx(idx: number): Player;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {Client, generateId, Room} from "colyseus";
|
||||
import {Client, Room} from "colyseus";
|
||||
import {CardGameState} from "./schema/CardGameState";
|
||||
import {OnJoinCommand} from "./commands/OnJoinCommand";
|
||||
import {PlayReadyCommand} from "./commands/PlayReadyCommand";
|
||||
@ -19,9 +19,8 @@ import {GMCommand} from "./commands/GMCommand";
|
||||
import {GameStateConst} from "../constants/GameStateConst";
|
||||
import {GameRestartCommand} from "./commands/GameRestartCommand";
|
||||
import {RobotClient} from "../robot/RobotClient";
|
||||
import axios from 'axios';
|
||||
import {wait} from "../decorators/cfg";
|
||||
import {ChangePetCommand} from "./commands/ChangePetCommand";
|
||||
import {createRobot} from "../common/WebApi";
|
||||
|
||||
export class GeneralRoom extends Room {
|
||||
dispatcher = new Dispatcher(this);
|
||||
@ -245,39 +244,7 @@ export class GeneralRoom extends Room {
|
||||
room: this.roomId,
|
||||
sessionId: playerId
|
||||
}
|
||||
|
||||
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);
|
||||
createRobot(data);
|
||||
}
|
||||
|
||||
addAssistClient(sessionId: string) {
|
||||
@ -290,7 +257,17 @@ export class GeneralRoom extends Room {
|
||||
getAssistClient(sessionId: string): RobotClient {
|
||||
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 {
|
||||
let playerId;
|
||||
if (typeof srcPlayer === 'string') {
|
||||
@ -301,9 +278,8 @@ export class GeneralRoom extends Room {
|
||||
if (!this.state.players.has(playerId)) {
|
||||
return null;
|
||||
}
|
||||
const sessionIds = [...this.state.players.keys()];
|
||||
let idx = sessionIds.indexOf(playerId);
|
||||
let playerId2 = sessionIds[(2 + idx) % 4];
|
||||
return this.state.players.get(playerId2);
|
||||
let idx = this.state.players.get(playerId).idx;
|
||||
let opposIdx = (2 + idx) % 4;
|
||||
return this.getPlayerByIdx(opposIdx);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,9 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
|
||||
async execute({timeUp} = this.payload) {
|
||||
let giveUpCount = 0;
|
||||
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));
|
||||
|
||||
|
@ -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) {
|
||||
let str = ''
|
||||
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 playerIdx = parseInt(arr[0]);
|
||||
let effectId = parseInt(arr[1]);
|
||||
let player = this.getPlayerByIdx(playerIdx);
|
||||
let player = this.room.getPlayerByIdx(playerIdx);
|
||||
for (let [, card] of player.cards) {
|
||||
card.effect = effectId;
|
||||
}
|
||||
@ -131,7 +127,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
|
||||
let arr = msg.split('|');
|
||||
let playerIdx = parseInt(arr[0]);
|
||||
let heroId = parseInt(arr[1]);
|
||||
let player = this.getPlayerByIdx(playerIdx);
|
||||
let player = this.room.getPlayerByIdx(playerIdx);
|
||||
player.heroId = heroId;
|
||||
this.room.battleMan.updatePlayerHero(player);
|
||||
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 playerIdx = parseInt(arr[0]);
|
||||
let effectId = parseInt(arr[1]);
|
||||
let player = this.getPlayerByIdx(playerIdx);
|
||||
let player = this.room.getPlayerByIdx(playerIdx);
|
||||
let count = parseInt(arr[2]);
|
||||
this.room.generateCard({player: player, count, effectId, fromplayer: player});
|
||||
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 playerIdx = parseInt(arr[0]);
|
||||
let player = this.getPlayerByIdx(playerIdx);
|
||||
let player = this.room.getPlayerByIdx(playerIdx);
|
||||
let count = parseInt(arr[1]);
|
||||
let extData;
|
||||
if (arr.length > 2) {
|
||||
@ -208,7 +204,7 @@ export class GMCommand extends Command<CardGameState, {client: Client, message:
|
||||
updateHeroHp(msg: string) {
|
||||
let arr = msg.split('|');
|
||||
let playerIdx = parseInt(arr[0]);
|
||||
let player = this.getPlayerByIdx(playerIdx);
|
||||
let player = this.room.getPlayerByIdx(playerIdx);
|
||||
let count = parseInt(arr[1]);
|
||||
this.room.updateHp(player.id, count);
|
||||
}
|
||||
|
@ -9,11 +9,9 @@ import gameUtil from "../../utils/game.util";
|
||||
import {Card} from "../schema/Card";
|
||||
import {MapSchema, SetSchema} from "@colyseus/schema";
|
||||
import {StateTypeEnum} from "../enums/StateTypeEnum";
|
||||
import axios from "axios";
|
||||
import {Config} from "../../cfg/Config";
|
||||
import {reportGameResult} from "../../common/WebApi";
|
||||
|
||||
|
||||
let config: Config = require('../../../config/config.json');
|
||||
/**
|
||||
* 游戏结束
|
||||
*/
|
||||
@ -100,7 +98,7 @@ export class GameResultCommand extends Command<CardGameState, {}> {
|
||||
val: val[1]
|
||||
})
|
||||
}
|
||||
resultData[statics] = statics;
|
||||
resultData['statics'] = statics;
|
||||
let self = this;
|
||||
this.room.bGameResult(resultData);
|
||||
this.state.updateGameState(GameStateConst.STATE_GAME_OVER);
|
||||
@ -207,25 +205,7 @@ export class GameResultCommand extends Command<CardGameState, {}> {
|
||||
});
|
||||
}
|
||||
data.players = players;
|
||||
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);
|
||||
});
|
||||
reportGameResult(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,9 @@ export class NextTurnCommand extends Command<CardGameState, {}> {
|
||||
|
||||
async execute(){
|
||||
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) {
|
||||
this.state.round = 0;
|
||||
}
|
||||
|
@ -13,9 +13,9 @@ export class OnJoinCommand extends Command<CardGameState, {
|
||||
accountId: string
|
||||
}> {
|
||||
execute({client, accountId} = this.payload) {
|
||||
let team = (this.state.players.size == 1 || this.state.players.size == 2)? 1 : 0;
|
||||
// 实际的team会在PlayReadyCommand中设置
|
||||
let player = new Player(client.sessionId, 0, team);
|
||||
let count = this.state.players.size;
|
||||
let team = (count == 1 || count == 2)? 1 : 0;
|
||||
let player = new Player(client.sessionId, count, team);
|
||||
if (accountId && accountId == 'robot') {
|
||||
accountId = `robot_${client.sessionId}`;
|
||||
} else if (!accountId) {
|
||||
|
@ -30,11 +30,11 @@ export class PlayReadyCommand extends Command<CardGameState, {
|
||||
this.room.stopSchedule('waiting_player');
|
||||
// 比大小, 确定先手
|
||||
// return [new PrepareCommand()];
|
||||
let i = 0;
|
||||
for (let [,player] of this.state.players) {
|
||||
player.team = (i == 1 || i == 2) ? 1 : 0;
|
||||
i += 1;
|
||||
}
|
||||
// let i = 0;
|
||||
// for (let [,player] of this.state.players) {
|
||||
// player.team = (i == 1 || i == 2) ? 1 : 0;
|
||||
// i += 1;
|
||||
// }
|
||||
await this.room.setPrivate(true);
|
||||
this.room.state.updateGameState(GameStateConst.CHANGE_HERO);
|
||||
let self = this;
|
||||
|
@ -7,7 +7,6 @@ import {BaseConst} from "../../constants/BaseConst";
|
||||
import {error} from "../../common/Debug";
|
||||
import {GameEnv} from "../../cfg/GameEnv";
|
||||
import {StateTypeEnum} from "../enums/StateTypeEnum";
|
||||
import {raw} from "express";
|
||||
import {getCardGroup} from "../../common/WebApi";
|
||||
|
||||
/**
|
||||
@ -37,7 +36,7 @@ export class SelectHeroCommand extends Command<CardGameState, { client: Client,
|
||||
for (let key in StateTypeEnum) {
|
||||
if (!isNaN(Number(key))) {
|
||||
// @ts-ignore
|
||||
let type: StateTypeEnum = key;
|
||||
let type: StateTypeEnum = Number(key);
|
||||
player.statData.set(type, 0);
|
||||
}
|
||||
}
|
||||
|
@ -59,6 +59,9 @@ export class Player extends Schema {
|
||||
*/
|
||||
@type("number")
|
||||
team: number;
|
||||
|
||||
@type("number")
|
||||
idx: number;
|
||||
/**
|
||||
* 玩家灵活时限, 客户端每次显示倒计时的时候, 需读取该时间
|
||||
*/
|
||||
@ -89,12 +92,13 @@ export class Player extends Schema {
|
||||
@type({ map: "number" })
|
||||
unitCfgs = new MapSchema<number>();
|
||||
|
||||
constructor(id: string, heroId: number, team: number) {
|
||||
constructor(id: string, idx: number, team: number) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.state = PlayerStateConst.PLAYER_NORMAL;
|
||||
this.hp = 0;
|
||||
this.heroId = heroId;
|
||||
this.idx = idx;
|
||||
this.heroId = 0;
|
||||
this.team = team;
|
||||
this.countTotal = 0;
|
||||
this.countPresent = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user