增加几个抽卡相关的方法
This commit is contained in:
parent
d15c07eadc
commit
127a746b4a
75
src/global.d.ts
vendored
75
src/global.d.ts
vendored
@ -11,16 +11,24 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* GeneralRoom 扩展方法
|
|
||||||
*/
|
|
||||||
import {Client, Room} from "colyseus";
|
import {Client, Room} from "colyseus";
|
||||||
import {PetInfoMsg} from "./message/PetInfo";
|
import {PetInfoMsg} from "./message/PetInfo";
|
||||||
import {BattleHandler} from "./rooms/logic/Handler/BattleHandler";
|
import {BattleHandler} from "./rooms/logic/Handler/BattleHandler";
|
||||||
import {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo";
|
import {SkillInfoData, SkillInfoMsg} from "./message/SkillInfo";
|
||||||
|
/**
|
||||||
|
* GeneralRoom 扩展方法
|
||||||
|
*/
|
||||||
declare module "colyseus" {
|
declare module "colyseus" {
|
||||||
interface Room {
|
interface Room {
|
||||||
battleMan: BattleHandler;
|
battleMan: BattleHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据sessionId获取client
|
||||||
|
* @param sessionId
|
||||||
|
*/
|
||||||
|
getClient(sessionId: string): Client;
|
||||||
|
// >>>>>>>>> Begin of extend send message <<<<<<<<<<<<<
|
||||||
/**
|
/**
|
||||||
* 广播玩家加入房间
|
* 广播玩家加入房间
|
||||||
* @param data
|
* @param data
|
||||||
@ -65,12 +73,39 @@ declare module "colyseus" {
|
|||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
bCastSkill(data?: SkillInfoMsg): void;
|
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 client
|
||||||
* @param datas
|
* @param data
|
||||||
*/
|
*/
|
||||||
sMsgQueue(client: Client, datas: IMsg[]): void;
|
sMsgQueue(client: Client, data: IMsg[]): void;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,6 +119,36 @@ declare module "colyseus" {
|
|||||||
*/
|
*/
|
||||||
bMsgQueue(datas: IMsg[], options?: any): void;
|
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 srcplayer
|
||||||
|
* @param dstplayer 目标玩家, 可以和srcplayer相同
|
||||||
|
* @param count
|
||||||
|
*/
|
||||||
|
giveUpCard(srcplayer: string, dstplayer: string, count: number): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 补卡, 并广播消息
|
||||||
|
* @param srcplayer
|
||||||
|
* @param dstplayer 目标玩家, 可以和srcplayer相同
|
||||||
|
* @param count 补多少张, 该值和max_count至少一个不为0
|
||||||
|
* @param max_count 补到多少张, 如果count和max_count都不为0, 则抽 Math.min(count, (max_count - 当前手牌数))
|
||||||
|
*/
|
||||||
|
addCard(srcplayer: string, dstplayer: string, count: number, max_count: number): boolean;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +82,8 @@ export class GeneralRoom extends Room {
|
|||||||
this.dispatcher.stop();
|
this.dispatcher.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
getClient(sessionId: string) {
|
||||||
* begin of message
|
return this.clients.find(client => client.sessionId == sessionId );
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* end of message
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,9 @@ import {IMsg} from "../message/IMsg";
|
|||||||
import {PetInfoMsg} from "../message/PetInfo";
|
import {PetInfoMsg} from "../message/PetInfo";
|
||||||
import {SkillInfoMsg} from "../message/SkillInfo";
|
import {SkillInfoMsg} from "../message/SkillInfo";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一些封装了的下发消息的方法
|
||||||
|
*/
|
||||||
Object.defineProperties(Room.prototype, {
|
Object.defineProperties(Room.prototype, {
|
||||||
bUserJoin: {
|
bUserJoin: {
|
||||||
value: function (data?: any, options?: any) {
|
value: function (data?: any, options?: any) {
|
||||||
@ -28,6 +30,11 @@ Object.defineProperties(Room.prototype, {
|
|||||||
client.send('draw_card_s2c', data);
|
client.send('draw_card_s2c', data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
bDrawCard: {
|
||||||
|
value: function (data?: any, options?: any) {
|
||||||
|
this.broadcast('draw_card_s2c', data, options);
|
||||||
|
}
|
||||||
|
},
|
||||||
bPartResult: {
|
bPartResult: {
|
||||||
value: function (data?: any) {
|
value: function (data?: any) {
|
||||||
this.send("", data);
|
this.send("", data);
|
||||||
@ -56,7 +63,26 @@ Object.defineProperties(Room.prototype, {
|
|||||||
value: function (data?: SkillInfoMsg) {
|
value: function (data?: SkillInfoMsg) {
|
||||||
this.broadcast("cast_skill_s2c", data);
|
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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
80
src/rooms/RoomExtMethod.ts
Normal file
80
src/rooms/RoomExtMethod.ts
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
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);
|
||||||
|
client.send('draw_card_s2c', cards);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
@ -42,7 +42,7 @@ export class ChangeCardCommand extends Command<CardGameState, { client: Client,
|
|||||||
this.state.round = 0;
|
this.state.round = 0;
|
||||||
let curPlayer = this.state.players.get(curClient.sessionId);
|
let curPlayer = this.state.players.get(curClient.sessionId);
|
||||||
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
|
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
|
||||||
curClient.send('draw_card_s2c', cards);
|
this.room.sDrawCard(curClient, {player: sessionId, cards})
|
||||||
this.state.gameState = GameStateConst.STATE_BEGIN_DRAW;
|
this.state.gameState = GameStateConst.STATE_BEGIN_DRAW;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,6 @@ export class DrawCommand extends Command<CardGameState, {}> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
|
let cards = gameUtil.drawCard(this.state.cardQueue, curPlayer, singleton(GameEnv).roundDrawNum);
|
||||||
curClient.send('draw_card_s2c', cards);
|
this.room.sDrawCard(curClient, {player: sessionId, cards})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,38 @@ let arrUtil = {
|
|||||||
arr1.splice(Math.random() * length, 0 , value);
|
arr1.splice(Math.random() * length, 0 , value);
|
||||||
})
|
})
|
||||||
return arr1;
|
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;
|
export default arrUtil;
|
||||||
|
@ -98,12 +98,36 @@ let gameUtil = {
|
|||||||
drawCard(cardArr: Card[], player: Player, count: number): Card[] {
|
drawCard(cardArr: Card[], player: Player, count: number): Card[] {
|
||||||
let cards: Card[] = [];
|
let cards: Card[] = [];
|
||||||
for (let i = 0; i < count; i++) {
|
for (let i = 0; i < count; i++) {
|
||||||
let card = cardArr.pop();
|
cards.push(cardArr.pop());
|
||||||
cards.push(card);
|
}
|
||||||
|
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.cards.set(card.id + '', card);
|
||||||
player.cardSet.add(card.id + '');
|
player.cardSet.add(card.id + '');
|
||||||
}
|
}
|
||||||
return cards;
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* 更换相同数量的卡
|
* 更换相同数量的卡
|
||||||
|
Loading…
x
Reference in New Issue
Block a user