implemented basic game state loop for leduc holdem

This commit is contained in:
songyih 2020-01-30 22:14:06 -08:00
parent 68d5bcbe2a
commit db813574ae
11 changed files with 163 additions and 191 deletions

View File

@ -4,15 +4,12 @@
"private": true,
"dependencies": {
"@material-ui/core": "^4.9.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"element-react": "^1.4.34",
"element-theme-default": "^1.4.13",
"node-sass": "^4.13.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-hot-loader": "^4.12.18",
"react-hot-loader": "^4.12.19",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"socket.io-client": "^2.3.0"
@ -37,5 +34,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"axios": "^0.19.2"
}
}

4
public/config.js Normal file
View File

@ -0,0 +1,4 @@
window.g = {
apiUrl: 'http://localhost:10080', // 配置服务器地址
// WebSocketUrl: '/api/' // 配置WebSocket地址
};

View File

@ -24,6 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<script type="text/javascript" src="./config.js"></script>
<title>RLcard Showdown</title>
</head>
<body>

View File

@ -13,6 +13,7 @@
"index": 1
}
],
"publicCard": "SJ",
"moveHistory": [
[
{
@ -32,7 +33,6 @@
"move": "Check"
}
],
"SJ",
[
{
"playerIdx": 0,

View File

@ -9,17 +9,17 @@ class LeducHoldemGameBoard extends React.Component {
render() {
return (
<div style={{width: "100%", height: "100%", backgroundColor: "#ffcc99", position: "relative"}}>
<div id={"bottom-player"}>
<div className="played-card-area">
played card area
</div>
<div className="player-main-area">
<div className="player-info">
<span>{`Player Id ${bottomId}\n${this.props.playerInfo.length > 0 ? this.props.playerInfo[bottomIdx].role : ""}`}</span>
</div>
{bottomIdx >= 0 ? <div className="player-hand">{this.computeSingleLineHand(this.props.hands[bottomIdx])}</div> : <div className="player-hand-placeholder"><span>Waiting...</span></div>}
</div>
</div>
{/*<div id={"bottom-player"}>*/}
{/* <div className="played-card-area">*/}
{/* played card area*/}
{/* </div>*/}
{/* <div className="player-main-area">*/}
{/* <div className="player-info">*/}
{/* <span>{`Player Id ${bottomId}\n${this.props.playerInfo.length > 0 ? this.props.playerInfo[bottomIdx].role : ""}`}</span>*/}
{/* </div>*/}
{/* {bottomIdx >= 0 ? <div className="player-hand">{this.computeSingleLineHand(this.props.hands[bottomIdx])}</div> : <div className="player-hand-placeholder"><span>Waiting...</span></div>}*/}
{/* </div>*/}
{/*</div>*/}
</div>
);
}

View File

@ -4,14 +4,6 @@ import './assets/index.css';
import App from './App';
// import element ui
import 'element-theme-default';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

View File

@ -1,137 +0,0 @@
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' }
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}

View File

@ -1,5 +0,0 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';

View File

@ -1,5 +1,5 @@
export function removeCards(cards, hands){ // remove cards from hands, return the remained hands
let remainedHands = JSON.parse(JSON.stringify(hands));
let remainedHands = deepCopy(hands);
// if the player's action is pass then return the copy of original hands
if(cards === "P"){
return remainedHands;
@ -26,3 +26,6 @@ export function doubleRaf (callback) {
})
}
export function deepCopy(toCopy){
return JSON.parse(JSON.stringify(toCopy));
}

View File

@ -2,9 +2,9 @@ import React from 'react';
import '../assets/gameview.scss';
import { DoudizhuGameBoard } from '../components/GameBoard';
import webSocket from "socket.io-client";
import {removeCards, doubleRaf} from "../utils";
import { removeCards, doubleRaf, deepCopy } from "../utils";
import { Button, Layout, Slider as elSlider } from 'element-react';
import { Button, Layout } from 'element-react';
import Slider from '@material-ui/core/Slider';
class DoudizhuGameView extends React.Component {
@ -30,7 +30,7 @@ class DoudizhuGameView extends React.Component {
ws: null,
gameInfo: this.initGameState,
gameStateLoop: null,
considerationTime: this.initConsiderationTime
considerationTimeSetting: this.initConsiderationTime
};
}
@ -39,7 +39,7 @@ class DoudizhuGameView extends React.Component {
let currentConsiderationTime = this.state.gameInfo.considerationTime;
if(currentConsiderationTime > 0) {
currentConsiderationTime -= this.considerationTimeDeduction;
let gameInfo = JSON.parse(JSON.stringify(this.state.gameInfo));
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.considerationTime = currentConsiderationTime;
this.setState({gameInfo: gameInfo});
this.gameStateTimer();
@ -49,7 +49,7 @@ class DoudizhuGameView extends React.Component {
type: 1,
message: {turn: turn}
};
let gameInfo = JSON.parse(JSON.stringify(this.state.gameInfo));
let gameInfo = deepCopy(this.state.gameInfo);
this.setState({gameInfo: gameInfo});
this.state.ws.emit("getMessage", gameStateReq);
}
@ -80,7 +80,7 @@ class DoudizhuGameView extends React.Component {
switch(message.type){
case 0:
// init replay info
let gameInfo = JSON.parse(JSON.stringify(this.state.gameInfo));
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.playerInfo = message.message.playerInfo;
gameInfo.hands = message.message.initHands.map(element => {
return element.split(" ");
@ -93,7 +93,7 @@ class DoudizhuGameView extends React.Component {
// getting player actions
let res = message.message;
if(res.turn === this.state.gameInfo.turn && res.playerIdx === this.state.gameInfo.currentPlayer){
let gameInfo = JSON.parse(JSON.stringify(this.state.gameInfo));
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.latestAction[res.playerIdx] = res.move === "P" ? "P" : res.move.split(" ");
gameInfo.turn++;
gameInfo.currentPlayer = (gameInfo.currentPlayer+1)%3;
@ -104,7 +104,7 @@ class DoudizhuGameView extends React.Component {
}else{
console.log("Cannot find cards in move from player's hand");
}
gameInfo.considerationTime = this.state.considerationTime;
gameInfo.considerationTime = this.state.considerationTimeSetting;
this.setState({gameInfo: gameInfo});
}else{
console.log("Mismatched game turn or current player index", message);
@ -140,6 +140,7 @@ class DoudizhuGameView extends React.Component {
}
render(){
// todo: reset game state timer when considerationTimeSetting changes
return (
<div>
<div style={{width: "960px", height: "540px"}}>
@ -169,8 +170,8 @@ class DoudizhuGameView extends React.Component {
</Layout.Col>
<Layout.Col span="16">
<Slider
value={this.state.considerationTime}
onChange={(e, newVal)=>{console.log('slider val', newVal);this.setState({considerationTime: newVal})}}
value={this.state.considerationTimeSetting}
onChange={(e, newVal)=>{console.log('slider val', newVal);this.setState({considerationTimeSetting: newVal})}}
aria-labelledby="discrete-slider"
valueLabelDisplay="auto"
step={1000}

View File

@ -1,7 +1,8 @@
import React from 'react';
import axios from 'axios';
import '../assets/gameview.scss';
import {DoudizhuGameBoard, LeducHoldemGameBoard} from '../components/GameBoard';
import {removeCards, doubleRaf} from "../utils";
import {LeducHoldemGameBoard} from '../components/GameBoard';
import {doubleRaf, deepCopy} from "../utils";
import { Button, Layout, Slider as elSlider } from 'element-react';
import Slider from '@material-ui/core/Slider';
@ -9,21 +10,133 @@ import Slider from '@material-ui/core/Slider';
class LeducHoldemGameView extends React.Component {
constructor(props) {
super(props);
const mainViewerId = 0; // Id of the player at the bottom of screen
this.initConsiderationTime = 1000;
this.considerationTimeDeduction = 100;
this.gameStateTimeout = null;
this.apiUrl = window.g.apiUrl;
this.moveHistory = [];
this.initGameState = {
playerInfo: [],
hands: [],
latestAction: ["", ""],
mainViewerId: mainViewerId,
round: 0,
turn: 0,
pot: [1, 1],
publicCard: "",
currentPlayer: null,
considerationTime: this.initConsiderationTime
};
this.state = {
gameInfo: this.initGameState,
gameStateLoop: null,
considerationTimeSetting: this.initConsiderationTime
}
}
retrieveReplayData(){
// for test use
const replayId = 0;
axios.get(`${this.apiUrl}/replay/leduc_holdem/${replayId}`)
.then(res => {
res = res.data;
this.moveHistory = res.moveHistory;
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.hands = res.initHands;
gameInfo.playerInfo = res.playerInfo;
gameInfo.currentPlayer = res.moveHistory[0][0].playerIdx;
gameInfo.publicCard = res.publicCard;
this.setState({gameInfo: gameInfo}, ()=>{
this.startReplay();
});
})
.catch(err=>{
console.log("err", err);
})
}
startReplay(){
if(this.gameStateTimeout){
window.clearTimeout(this.gameStateTimeout);
this.gameStateTimeout = null;
}
// loop to update game state
this.gameStateTimer();
}
gameStateTimer(){
this.gameStateTimeout = setTimeout(()=>{
let currentConsiderationTime = this.state.gameInfo.considerationTime;
if(currentConsiderationTime > 0) {
currentConsiderationTime -= this.considerationTimeDeduction;
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.considerationTime = currentConsiderationTime;
this.setState({gameInfo: gameInfo});
this.gameStateTimer();
}else{
console.log(this.state.gameInfo.latestAction);
const turn = this.state.gameInfo.turn;
if(turn >= this.moveHistory[this.state.gameInfo.round].length){
if(this.state.gameInfo.round === 0){
// todo: if it's the first round, then reveal the public card and start the second round
let gameInfo = deepCopy(this.state.gameInfo);
gameInfo.turn = 0;
gameInfo.round = 1;
gameInfo.latestAction = ["", ""];
gameInfo.currentPlayer = this.moveHistory[1][0].playerIdx;
gameInfo.considerationTime = this.state.considerationTimeSetting;
this.setState({gameInfo: gameInfo}, ()=>{
this.gameStateTimer();
});
}else{
// todo: if it's the second round, game ends
console.log("game ends");
}
}else{
let gameInfo = deepCopy(this.state.gameInfo);
// debugger;
if(gameInfo.currentPlayer === this.moveHistory[gameInfo.round][gameInfo.turn].playerIdx){
gameInfo.latestAction[gameInfo.currentPlayer] = this.moveHistory[gameInfo.round][gameInfo.turn].move;
// todo: check if the player choose to fold in this turn
gameInfo.turn++;
gameInfo.currentPlayer = (gameInfo.currentPlayer+1)%2;
gameInfo.considerationTime = this.state.considerationTimeSetting;
this.setState({gameInfo: gameInfo}, ()=>{
this.gameStateTimer();
});
}else{
console.log("Mismatch in current player & move history");
}
}
}
}, this.considerationTimeDeduction);
}
render(){
return (
<div>
<div style={{width: "960px", height: "540px"}}>
<LeducHoldemGameBoard
playerInfo={this.state.gameInfo.playerInfo}
hands={this.state.gameInfo.hands}
latestAction={this.state.gameInfo.latestAction}
mainPlayerId={this.state.gameInfo.mainViewerId}
currentPlayer={this.state.gameInfo.currentPlayer}
considerationTime={this.state.gameInfo.considerationTime}
turn={this.state.gameInfo.turn}
runNewTurn={(prevTurn)=>this.runNewTurn(prevTurn)}
/>
{/*<LeducHoldemGameBoard*/}
{/* playerInfo={this.state.gameInfo.playerInfo}*/}
{/* hands={this.state.gameInfo.hands}*/}
{/* latestAction={this.state.gameInfo.latestAction}*/}
{/* mainPlayerId={this.state.gameInfo.mainViewerId}*/}
{/* currentPlayer={this.state.gameInfo.currentPlayer}*/}
{/* considerationTime={this.state.gameInfo.considerationTime}*/}
{/* turn={this.state.gameInfo.turn}*/}
{/* runNewTurn={(prevTurn)=>this.runNewTurn(prevTurn)}*/}
{/*/>*/}
</div>
<div className="game-controller">
<Layout.Row>
<Layout.Col span="24">
<Button type="primary" onClick={()=>{this.retrieveReplayData()}}>Connect</Button>
</Layout.Col>
</Layout.Row>
</div>
</div>
);
}