programing sendcard
This commit is contained in:
+14
-14
@@ -1,14 +1,14 @@
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: '10'
|
||||
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
- npm i npminstall && node_modules\.bin\npminstall
|
||||
|
||||
test_script:
|
||||
- node --version
|
||||
- npm --version
|
||||
- npm run test
|
||||
|
||||
build: off
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: '10'
|
||||
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
- npm i npminstall && node_modules\.bin\npminstall
|
||||
|
||||
test_script:
|
||||
- node --version
|
||||
- npm --version
|
||||
- npm run test
|
||||
|
||||
build: off
|
||||
|
||||
@@ -4,6 +4,7 @@ export interface IPlayer {
|
||||
userId: string;
|
||||
nick_name: string;
|
||||
account: string;
|
||||
socketId: string;
|
||||
}
|
||||
|
||||
export enum ECommand {
|
||||
@@ -29,6 +30,7 @@ export class Player {
|
||||
position: number = 0;
|
||||
counter: number = 0;
|
||||
userId: string = '';
|
||||
socketId: string = '';
|
||||
actionSize: number = 0;
|
||||
type: string = EPlayerType.DEFAULT;
|
||||
evPot: number = Infinity;
|
||||
@@ -39,6 +41,7 @@ export class Player {
|
||||
this.counter = config.counter;
|
||||
this.position = config.position || 0;
|
||||
this.userId = config.userId;
|
||||
this.socketId = config.socketId;
|
||||
if (this.position === 0) {
|
||||
this.type = EPlayerType.DEALER;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ import { Poker } from './Poker';
|
||||
import { ECommand, EPlayerType, IPlayer, Player } from './Player';
|
||||
import { PokerStyle } from './PokerStyle';
|
||||
import { ILinkNode, Link } from '../../utils/Link';
|
||||
import Timeout = NodeJS.Timeout;
|
||||
|
||||
interface IPokerGame {
|
||||
users: IPlayer[];
|
||||
smallBlind: number;
|
||||
updateCommonCard: () => void;
|
||||
}
|
||||
|
||||
export enum EGameStatus {
|
||||
@@ -35,13 +37,16 @@ export class PokerGame {
|
||||
playerSize: number;
|
||||
prevSize: number;
|
||||
prevPot: number;
|
||||
actionTimeOut: Timeout;
|
||||
allInPlayers: Player[] = [];
|
||||
currActionAllinPlayer: Player[] = [];
|
||||
updateCommonCard: () => void;
|
||||
hasStraddle = false;
|
||||
winner: Player[][] = [];
|
||||
|
||||
constructor(config: IPokerGame) {
|
||||
this.smallBlind = config.smallBlind;
|
||||
this.updateCommonCard = config.updateCommonCard;
|
||||
this.init(config.users);
|
||||
}
|
||||
|
||||
@@ -56,7 +61,7 @@ export class PokerGame {
|
||||
this.playerSize = users.length;
|
||||
// set SB, BB,Straddle
|
||||
this.getBlind();
|
||||
// utg
|
||||
// UTG
|
||||
this.currPlayer = this.playerLink.getNode(3);
|
||||
}
|
||||
|
||||
@@ -143,9 +148,19 @@ export class PokerGame {
|
||||
&& command === ECommand.CHECK)) {
|
||||
// console.log('ccc------', this.currPlayer, nextPlayer, command, this.playerSize);
|
||||
this.actionComplete();
|
||||
clearTimeout(this.actionTimeOut);
|
||||
return;
|
||||
}
|
||||
this.currPlayer = this.currPlayer.next;
|
||||
// action time is 60s
|
||||
clearTimeout(this.actionTimeOut);
|
||||
this.actionTimeOut = setTimeout(async () => {
|
||||
if (command === ECommand.CHECK || command === ECommand.FOLD) {
|
||||
this.action('check');
|
||||
} else {
|
||||
this.action('fold');
|
||||
}
|
||||
}, 6000);
|
||||
} else {
|
||||
throw 'incorrect action flow';
|
||||
}
|
||||
@@ -194,6 +209,8 @@ export class PokerGame {
|
||||
console.log(this.playerSize, 'playerS-------', this.status);
|
||||
if (this.status === EGameStatus.GAME_SHOWDOWN || this.playerSize <= 1) {
|
||||
this.gameOver();
|
||||
} else {
|
||||
this.sendCard();
|
||||
}
|
||||
}
|
||||
setSate() {
|
||||
@@ -219,18 +236,21 @@ export class PokerGame {
|
||||
if (this.status === EGameStatus.GAME_START) {
|
||||
this.setHandCard();
|
||||
this.setSate();
|
||||
this.updateCommonCard();
|
||||
return;
|
||||
}
|
||||
if (this.status === EGameStatus.GAME_FLOP) {
|
||||
this.fireCards.push(this.poker.getCard());
|
||||
this.flop();
|
||||
this.setSate();
|
||||
this.updateCommonCard();
|
||||
return;
|
||||
}
|
||||
if (this.status === EGameStatus.GAME_TURN || this.status === EGameStatus.GAME_RIVER) {
|
||||
this.fireCards.push(this.poker.getCard());
|
||||
this.commonCard.push(this.poker.getCard());
|
||||
this.setSate();
|
||||
this.updateCommonCard();
|
||||
return;
|
||||
}
|
||||
throw 'error flow sendCard';
|
||||
|
||||
@@ -20,27 +20,50 @@ class GameController extends BaseSocketController {
|
||||
// };
|
||||
// await this.gameRecordService.add(gameRecord);
|
||||
const roomInfo = await this.getRoomInfo();
|
||||
roomInfo.game = new PokerGame({
|
||||
users: roomInfo.players,
|
||||
smallBlind: 1,
|
||||
});
|
||||
roomInfo.game.play();
|
||||
console.log('hand card', roomInfo.game.allPlayer);
|
||||
this.nsp.adapter.clients([ room ], (err: any, clients: any) => {
|
||||
// 广播信息
|
||||
this.nsp.to(room).emit('game', {
|
||||
clients,
|
||||
action: 'broadcast',
|
||||
target: 'participator',
|
||||
message: '',
|
||||
if (!roomInfo.game) {
|
||||
roomInfo.game = new PokerGame({
|
||||
users: roomInfo.players,
|
||||
smallBlind: 1,
|
||||
updateCommonCard: () => {
|
||||
console.log('send common card');
|
||||
this.nsp.adapter.clients([ this.roomNumber ], (err: any, clients: any) => {
|
||||
if (roomInfo.game) {
|
||||
// 广播信息
|
||||
this.nsp.to(this.roomNumber).emit('online', {
|
||||
clients,
|
||||
action: 'commonCard',
|
||||
target: 'participator',
|
||||
message: JSON.stringify(roomInfo.game.commonCard),
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
roomInfo.game.play();
|
||||
console.log('hand card', roomInfo.game.allPlayer);
|
||||
roomInfo.players.forEach(p => {
|
||||
if (roomInfo.game) {
|
||||
console.log('game msg---------1');
|
||||
const player = roomInfo.game.allPlayer.find(player => player.socketId === p.socketId);
|
||||
console.log(player, 'game msg---------1');
|
||||
if (player) {
|
||||
const msg = this.ctx.helper.parseMsg('handCard', {
|
||||
handCard: player.handCard,
|
||||
}, { client: p.socketId });
|
||||
console.log(msg, 'game msg---------');
|
||||
this.socket.emit(p.socketId, msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw 'game already paling';
|
||||
}
|
||||
} catch (error) {
|
||||
this.app.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async buyIn() {
|
||||
const { room } = this.socket.handshake.query;
|
||||
try {
|
||||
const userInfo: IPlayer = await this.getUserInfo();
|
||||
const roomInfo: IRoomInfo = await this.getRoomInfo();
|
||||
@@ -57,9 +80,9 @@ class GameController extends BaseSocketController {
|
||||
};
|
||||
roomInfo.players.push(player);
|
||||
}
|
||||
this.nsp.adapter.clients([ room ], (err: any, clients: any) => {
|
||||
this.nsp.adapter.clients([ this.roomNumber ], (err: any, clients: any) => {
|
||||
// 广播信息
|
||||
this.nsp.to(room).emit('online', {
|
||||
this.nsp.to(this.roomNumber).emit('online', {
|
||||
clients,
|
||||
action: 'players',
|
||||
target: 'participator',
|
||||
@@ -70,6 +93,45 @@ class GameController extends BaseSocketController {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
async handCard() {
|
||||
try {
|
||||
const userInfo: IPlayer = await this.getUserInfo();
|
||||
const roomInfo: IRoomInfo = await this.getRoomInfo();
|
||||
const player = roomInfo.players.find((p: IPlayer) => p.nick_name === userInfo.nick_name);
|
||||
console.log(userInfo, 'userInfo------');
|
||||
if (player && roomInfo.game) {
|
||||
const gamePlayer = roomInfo.game.allPlayer.find(p => player.socketId === p.socketId);
|
||||
if (gamePlayer) {
|
||||
const msg = this.ctx.helper.parseMsg('handCard', {
|
||||
handCard: gamePlayer.handCard,
|
||||
}, { client: player.socketId });
|
||||
console.log(msg, 'game msg---------');
|
||||
this.nsp.emit(player.socketId, msg);
|
||||
}
|
||||
} else {
|
||||
throw 'game over';
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
async action() {
|
||||
try {
|
||||
const { payload } = this.message;
|
||||
const userInfo: IPlayer = await this.getUserInfo();
|
||||
const roomInfo = await this.getRoomInfo();
|
||||
if (roomInfo.game && roomInfo.game.currPlayer.node.userId === userInfo.userId) {
|
||||
roomInfo.game.action(payload.command);
|
||||
// todo notice next player action
|
||||
} else {
|
||||
throw 'action flow incorrect';
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GameController;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { Context } from 'midway';
|
||||
import { ITickMsg } from '../../../interface/ITickMsg';
|
||||
import { IGameRoom } from '../../../interface/IGameRoom';
|
||||
import { IPlayer } from '../../core/Player';
|
||||
|
||||
export default function auth(): any {
|
||||
return async (ctx: Context, next: () => Promise<any>) => {
|
||||
@@ -23,62 +21,8 @@ export default function auth(): any {
|
||||
|
||||
function leave() {
|
||||
}
|
||||
|
||||
function join(roomNumber: string, user: IPlayer, nsp: any, socket: any) {
|
||||
const hasRoom = nsp.gameRoom.find((r: IGameRoom) => r.number === roomNumber);
|
||||
let gameRoom: IGameRoom = {
|
||||
number: roomNumber,
|
||||
roomInfo: {
|
||||
players: [],
|
||||
game: null,
|
||||
},
|
||||
};
|
||||
if (!hasRoom) {
|
||||
nsp.gameRoom.push(gameRoom);
|
||||
gameRoom.roomInfo = {
|
||||
players: [{
|
||||
...user,
|
||||
counter: 0,
|
||||
}],
|
||||
game: null,
|
||||
};
|
||||
socket.join(roomNumber);
|
||||
} else {
|
||||
gameRoom = nsp.gameRoom.find((r: IGameRoom) => r.number === roomNumber);
|
||||
const player = gameRoom.roomInfo.players.find((p: IPlayer) => p.account === user.account);
|
||||
if (!player) {
|
||||
const player = {
|
||||
...user,
|
||||
counter: 0,
|
||||
};
|
||||
gameRoom.roomInfo.players.push(player);
|
||||
socket.join(roomNumber);
|
||||
}
|
||||
}
|
||||
console.log('players', JSON.stringify(gameRoom.roomInfo.players));
|
||||
updatePlayer(roomNumber, `User(${user.nick_name}) joined.`, 'join', nsp);
|
||||
updatePlayer(roomNumber, JSON.stringify(gameRoom.roomInfo.players), 'players', nsp);
|
||||
}
|
||||
|
||||
function updatePlayer(roomNumber: string, message: string, action: string, nsp: any) {
|
||||
// 在线列表
|
||||
nsp.adapter.clients([ roomNumber ], (err: any, clients: any) => {
|
||||
// 更新在线用户列表
|
||||
nsp.to(roomNumber).emit('online', {
|
||||
clients,
|
||||
action,
|
||||
target: 'participator',
|
||||
message,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// room缓存信息是否存在
|
||||
if (!nsp.gameRoom) {
|
||||
nsp.gameRoom = [];
|
||||
}
|
||||
const userInfo = await app.jwt.verify(token);
|
||||
await app.jwt.verify(token);
|
||||
// const { nick_name: userName } = userInfo.user;
|
||||
|
||||
// 检查房间是否存在,不存在则踢出用户
|
||||
@@ -91,7 +35,6 @@ export default function auth(): any {
|
||||
}, nsp, socket);
|
||||
return;
|
||||
}
|
||||
join(room, userInfo.user, nsp, socket);
|
||||
console.log('play------------', room);
|
||||
await next();
|
||||
leave();
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import { Context } from 'midway';
|
||||
import { IGameRoom } from '../../../interface/IGameRoom';
|
||||
import { IPlayer } from '../../core/Player';
|
||||
|
||||
export default function join(): any {
|
||||
function updatePlayer(roomNumber: string, message: string, action: string, nsp: any) {
|
||||
// 在线列表
|
||||
nsp.adapter.clients([ roomNumber ], (err: any, clients: any) => {
|
||||
// 更新在线用户列表
|
||||
nsp.to(roomNumber).emit('online', {
|
||||
clients,
|
||||
action,
|
||||
target: 'participator',
|
||||
message,
|
||||
});
|
||||
});
|
||||
}
|
||||
return async (ctx: Context, next: () => Promise<any>) => {
|
||||
const socket = ctx.socket as any;
|
||||
const id = socket.id;
|
||||
const app = ctx.app as any;
|
||||
const nsp = app.io.of('/socket');
|
||||
const query = socket.handshake.query;
|
||||
const { room, token } = query;
|
||||
// room缓存信息是否存在
|
||||
if (!nsp.gameRooms) {
|
||||
nsp.gameRooms = [];
|
||||
}
|
||||
try {
|
||||
const hasRoom = nsp.gameRooms.find((r: IGameRoom) => r.number === room);
|
||||
const { user } = await app.jwt.verify(token);
|
||||
let gameRoom: IGameRoom = {
|
||||
number: room,
|
||||
roomInfo: {
|
||||
players: [],
|
||||
game: null,
|
||||
},
|
||||
};
|
||||
if (!hasRoom) {
|
||||
nsp.gameRooms.push(gameRoom);
|
||||
gameRoom.roomInfo = {
|
||||
players: [{
|
||||
...user,
|
||||
socketId: id,
|
||||
counter: 0,
|
||||
}],
|
||||
game: null,
|
||||
};
|
||||
} else {
|
||||
gameRoom = nsp.gameRooms.find((r: IGameRoom) => r.number === room);
|
||||
const player = gameRoom.roomInfo.players.find((p: IPlayer) => p.account === user.account);
|
||||
if (!player) {
|
||||
const player = {
|
||||
...user,
|
||||
socketId: id,
|
||||
counter: 0,
|
||||
};
|
||||
gameRoom.roomInfo.players.push(player);
|
||||
}
|
||||
}
|
||||
socket.join(room);
|
||||
console.log('players', JSON.stringify(gameRoom.roomInfo.players));
|
||||
updatePlayer(room, `User(${user.nick_name}) joined.`, 'join', nsp);
|
||||
updatePlayer(room, JSON.stringify(gameRoom.roomInfo.players), 'players', nsp);
|
||||
// in the game, update hand cards
|
||||
const player = gameRoom.roomInfo.players.find((p: IPlayer) => p.nick_name === user.nick_name);
|
||||
if (player && gameRoom.roomInfo.game) {
|
||||
const gamePlayer = gameRoom.roomInfo.game.allPlayer.find(p => player.socketId === p.socketId);
|
||||
if (gamePlayer) {
|
||||
const msg = ctx.helper.parseMsg('handCard', {
|
||||
handCard: gamePlayer.handCard,
|
||||
}, { client: player.socketId });
|
||||
console.log(msg, 'join: game msg---------2222222');
|
||||
socket.emit(id, msg);
|
||||
}
|
||||
}
|
||||
await next();
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Context } from 'midway';
|
||||
|
||||
export default function leave(): any {
|
||||
return async (ctx: Context, next: () => Promise<any>) => {
|
||||
};
|
||||
}
|
||||
@@ -78,7 +78,7 @@ export default (appInfo: EggAppInfo) => {
|
||||
config.io = {
|
||||
namespace: {
|
||||
'/socket': {
|
||||
connectionMiddleware: [ 'auth' ],
|
||||
connectionMiddleware: [ 'auth', 'join', 'leave' ],
|
||||
packetMiddleware: [],
|
||||
},
|
||||
},
|
||||
|
||||
@@ -6,10 +6,12 @@ export default class BaseSocketController extends Controller {
|
||||
|
||||
public app = this.ctx.app as any;
|
||||
public nsp = this.app.io.of('/socket');
|
||||
public gameRoom = this.nsp.gameRoom;
|
||||
public gameRooms = this.nsp.gameRooms;
|
||||
public socket = this.ctx.socket as any;
|
||||
public query = this.socket.handshake.query;
|
||||
public roomNumber = this.query.room;
|
||||
public jwt: any = this.app.jwt;
|
||||
public message = this.ctx.args[0] || {};
|
||||
|
||||
async getUserInfo() {
|
||||
const { token } = this.query;
|
||||
@@ -19,7 +21,7 @@ export default class BaseSocketController extends Controller {
|
||||
|
||||
async getRoomInfo(): Promise<IRoomInfo> {
|
||||
const { room } = this.query;
|
||||
const roomInfo = this.gameRoom.find((gr: IGameRoom) => gr.number === room);
|
||||
const roomInfo = this.gameRooms.find((gr: IGameRoom) => gr.number === room);
|
||||
return roomInfo.roomInfo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ export class AccountService extends BaseService implements IAccountService {
|
||||
userId: id,
|
||||
},
|
||||
},
|
||||
this.jwtConfig.secret, { expiresIn: 60 * 60 });
|
||||
this.jwtConfig.secret, { expiresIn: 60 * 60 * 6 });
|
||||
this.ctx.logger.info(`AccountService getToken token--${token}`);
|
||||
return token;
|
||||
}
|
||||
|
||||
@@ -8,22 +8,37 @@ describe('test/app/core/pokerGame.test.ts', () => {
|
||||
{
|
||||
userId: '1',
|
||||
counter: 200,
|
||||
nick_name: '1',
|
||||
account: '',
|
||||
socketId: '',
|
||||
},
|
||||
{
|
||||
userId: '2',
|
||||
counter: 200,
|
||||
nick_name: '1',
|
||||
account: '',
|
||||
socketId: '',
|
||||
},
|
||||
{
|
||||
userId: '3',
|
||||
counter: 50,
|
||||
nick_name: '1',
|
||||
account: '',
|
||||
socketId: '',
|
||||
},
|
||||
{
|
||||
userId: '4',
|
||||
counter: 400,
|
||||
nick_name: '1',
|
||||
account: '',
|
||||
socketId: '',
|
||||
},
|
||||
{
|
||||
userId: '5',
|
||||
counter: 1200,
|
||||
nick_name: '1',
|
||||
account: '',
|
||||
socketId: '',
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user