This commit is contained in:
yuexin 2020-12-04 15:45:31 +08:00
commit 2f223351cc
10 changed files with 241 additions and 36 deletions

73
src/global.d.ts vendored
View File

@ -11,16 +11,24 @@ declare global {
}
}
}
/**
* GeneralRoom
*/
import {Client, Room} from "colyseus";
import {PetInfoMsg} from "./message/PetInfo";
import {BattleHandler} from "./rooms/logic/Handler/BattleHandler";
import {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo";
/**
* GeneralRoom
*/
declare module "colyseus" {
interface Room {
battleMan: BattleHandler;
/**
* sessionId获取client
* @param sessionId
*/
getClient(sessionId: string): Client;
// >>>>>>>>> Begin of extend send message <<<<<<<<<<<<<
/**
* 广
* @param data
@ -65,12 +73,39 @@ declare module "colyseus" {
* @param data
*/
bCastSkill(data?: SkillInfoMsg): void;
/**
* 广
* @param data
* @param options
*/
bStealCard(data?: any, options?: any):void;
/**
*
* @param data
*/
sStealCard(data?: any):void;
/**
* 广
* @param data
* @param options
*/
bDrawCard(data?: any, options?: any):void;
/**
* 广
* @param data
* @param options
*/
bRemoveCard(data?: any, options?: any):void;
/**
*
* @param client
* @param datas
* @param data
*/
sMsgQueue(client: Client, datas: IMsg[]): void;
sMsgQueue(client: Client, data: IMsg[]): void;
/**
@ -84,6 +119,34 @@ declare module "colyseus" {
*/
bMsgQueue(datas: IMsg[], options?: any): void;
// >>>>>>>>> End of extend send message <<<<<<<<<<<<<
// >>>>>>>>> Begin of extend functions <<<<<<<<<<<<<
/**
* , 广
* @param srcplayer
* @param dstplayer
* @param count
*/
drawCardFromPlayer(srcplayer: string, dstplayer: string, count: number) :boolean;
/**
* , 广
* @param dstplayer
* @param count
*/
giveUpCard(dstplayer: string, count: number): boolean;
/**
* , 广
* @param dstplayer
* @param count , max_count至少一个不为0
* @param max_count , count和max_count都不为0, Math.min(count, (max_count - ))
*/
addCard(dstplayer: string, count: number, max_count: number): boolean;
}
}

View File

@ -10,6 +10,7 @@ import {MongooseDriver} from "colyseus/lib/matchmaker/drivers/MongooseDriver";
import {initData} from "./common/GConfig";
require('./rooms/MSender');
require('./rooms/RoomExtMethod');
const port = Number(process.env.PORT || 2567);
const app = express()

View File

@ -82,14 +82,8 @@ export class GeneralRoom extends Room {
this.dispatcher.stop();
}
/**
* begin of message
*/
/**
* end of message
*/
getClient(sessionId: string) {
return this.clients.find(client => client.sessionId == sessionId );
}
}

View File

@ -3,7 +3,9 @@ import {IMsg} from "../message/IMsg";
import {PetInfoMsg} from "../message/PetInfo";
import {SkillInfoMsg} from "../message/SkillInfo";
/**
*
*/
Object.defineProperties(Room.prototype, {
bUserJoin: {
value: function (data?: any, options?: any) {
@ -28,6 +30,11 @@ Object.defineProperties(Room.prototype, {
client.send('draw_card_s2c', data);
}
},
bDrawCard: {
value: function (data?: any, options?: any) {
this.broadcast('draw_card_s2c', data, options);
}
},
bPartResult: {
value: function (data?: any) {
this.send("", data);
@ -56,7 +63,26 @@ Object.defineProperties(Room.prototype, {
value: function (data?: SkillInfoMsg) {
this.broadcast("cast_skill_s2c", data);
}
}
},
bStealCard: {
value: function (data?: any, options?: any) {
this.broadcast("steal_card_s2c", data, options);
}
},
sStealCard: {
value: function (client: Client, data?: any) {
client.send("steal_card_s2c", data);
}
},
bRemoveCard: {
value: function (client: Client, data?: any) {
client.send("remove_card_s2c", data);
}
},
});

View File

@ -0,0 +1,79 @@
import {Client, Room} from "colyseus";
import gameUtil from "../utils/game.util";
import {singleton} from "../common/Singleton";
import {GameEnv} from "../cfg/GameEnv";
import {error} from "../common/Debug";
/**
*
*/
Object.defineProperties(Room.prototype, {
drawCardFromPlayer: {
value: function (srcplayer: string, dstplayer: string, count: number) {
let player1 = this.state.players.get(dstplayer);
let tmpCards = gameUtil.removeCard(player1, count);
let player0 = this.state.players.get(srcplayer);
gameUtil.addCardToPlayer(player0, tmpCards);
let cardIds = tmpCards.map(card => card.id);
let client = this.getClient(player0);
//广播一个偷卡成功信息, 并私信一个偷卡详情给当前玩家
let msgData = {
srcplayer,
dstplayer,
cards: cardIds
};
this.bStealCard(msgData, {except: client});
let sMsgData = {
srcplayer,
dstplayer,
cards: tmpCards
}
this.sStealCard(client, sMsgData);
return true;
}
},
giveUpCard: {
value: function (dstplayer: string, count: number){
let player = this.state.players.get(dstplayer);
let tmpCards = gameUtil.removeCard(player, count);
let cardIds = tmpCards.map(card => card.id);
let msgData = {
player: dstplayer,
cards: cardIds
};
this.bRemoveCard(msgData);
return true;
}
},
addCard: {
value: function ( dstplayer: string, count: number, max_count: number) {
let player = this.state.players.get(dstplayer);
if (count > 0) {
if (max_count > 0) {
let curCount = player.cards.size;
count = Math.min(count, max_count - curCount);
}
} else if (max_count > 0){
let curCount = player.cards.size;
count = max_count - curCount;
} else {
error("补卡方法的参数有问题, count和max_count都为0");
return false;
}
let cards = gameUtil.drawCard(this.state.cardQueue, player, count);
let client = this.getClient(dstplayer);
let sData = {
player: dstplayer,
cards: cards
};
this.sDrawCard(client, sData);
let cardIds = cards.map(card => card.id);
let bData = {
player: dstplayer,
cards: cardIds
};
this.bDrawCard(bData, {except: client});
return true;
}
}
});

View File

@ -16,9 +16,7 @@ export class BeginGameCommand extends Command<CardGameState, {}> {
execute() {
this.state.cardQueue = gameUtil.initCardQue();
for (let client of this.room.clients) {
let player = this.state.players.get(client.sessionId);
let cards = gameUtil.drawCard(this.state.cardQueue, player, singleton(GameEnv).initCardNum);
this.room.sDrawCard(client, cards);
this.room.addCard(client.sessionId, singleton(GameEnv).initCardNum, 0);
}
this.state.gameState = GameStateConst.STATE_CHANGE_CARD;
}

View File

@ -40,9 +40,7 @@ export class ChangeCardCommand extends Command<CardGameState, { client: Client,
let curClient = this.room.clients[0];
this.state.currentTurn = curClient.sessionId;
this.state.round = 0;
let curPlayer = this.state.players.get(curClient.sessionId);
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
curClient.send('draw_card_s2c', cards);
this.room.addCard(curClient.sessionId, singleton(GameEnv).roundDrawNum, 0);
this.state.gameState = GameStateConst.STATE_BEGIN_DRAW;
}
}

View File

@ -10,15 +10,6 @@ import gameUtil from "../../utils/game.util";
export class DrawCommand extends Command<CardGameState, {}> {
execute() {
let sessionId = this.state.currentTurn;
let curPlayer = this.state.players.get(sessionId);
let curClient;
for (let client of this.room.clients) {
if (client.sessionId === sessionId) {
curClient = client;
break;
}
}
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
curClient.send('draw_card_s2c', cards);
this.room.addCard(sessionId, singleton(GameEnv).roundDrawNum, 0);
}
}

View File

@ -83,7 +83,38 @@ let arrUtil = {
arr1.splice(Math.random() * length, 0 , value);
})
return arr1;
}
},
/**
* n个元素
* @param arr
* @param count
*/
randomGet<T>(arr: Array<T>, count: number = 1) {
let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
if (min < 0) {
return shuffled;
}
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
},
/**
* n个元素
* @param arr
* @param count
*/
randomRemove<T>(arr: Array<T>, count: number = 1) {
let result = [];
while (count -- > 0 && arr.length > 0) {
let index = Math.random() * arr.length | 0;
result.push(...arr.splice(index, 1));
}
return result;
},
}
export default arrUtil;

View File

@ -98,12 +98,36 @@ let gameUtil = {
drawCard(cardArr: Card[], player: Player, count: number): Card[] {
let cards: Card[] = [];
for (let i = 0; i < count; i++) {
let card = cardArr.pop();
cards.push(card);
cards.push(cardArr.pop());
}
this.addCardToPlayer(player, cards);
return cards;
},
/**
* n张手牌
* @param player
* @param count
*/
removeCard(player: Player, count: number): Card[] {
let cards = [];
let cardArr: Card[] = [...player.cards.values()];
cards = arrUtil.randomGet(cardArr, count);
for (let card of cards) {
player.cards.delete(card.id + '');
player.cardSet.delete(card.id + '');
}
return cards;
},
/**
*
* @param player
* @param cards
*/
addCardToPlayer(player: Player, cards: Card[]) {
for (let card of cards) {
player.cards.set(card.id + '', card);
player.cardSet.add(card.id + '');
}
return cards;
},
/**
*