From bf40610120db3c6b5c8f45e83c8ab04b99e02058 Mon Sep 17 00:00:00 2001 From: Songyi Huang Date: Tue, 14 Jan 2020 11:19:43 -0800 Subject: [PATCH] remove played cards from player's hands --- server/index.js | 2 +- server/sample_data/sample_doudizhu.json | 2 +- src/components/GameBoard/index.js | 6 +++--- src/utils/index.js | 21 +++++++++++++++++++++ src/view/DoudizhuGameView.js | 16 +++++++++++----- 5 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 src/utils/index.js diff --git a/server/index.js b/server/index.js index bfce01a..9a234be 100644 --- a/server/index.js +++ b/server/index.js @@ -29,7 +29,7 @@ io.on("connection", socket => { type: 0, message: { playerInfo: testDoudizhuData.playerInfo, - initHand: testDoudizhuData.initHand + initHands: testDoudizhuData.initHands } }; socket.emit("getMessage", res); diff --git a/server/sample_data/sample_doudizhu.json b/server/sample_data/sample_doudizhu.json index 060f28d..71585c6 100644 --- a/server/sample_data/sample_doudizhu.json +++ b/server/sample_data/sample_doudizhu.json @@ -1,5 +1,5 @@ { - "initHand": [ + "initHands": [ "S2 H2 HK DK HQ CQ DQ CJ S9 H9 D9 C7 S6 H6 C4 D4 S3", "C2 HA CA DA SQ ST HT D8 S7 H7 C6 D6 S5 H5 C5 S4 H4", "RJ BJ D2 SA SK CK SJ HJ DJ CT DT C9 S8 H8 C8 D7 D5 H3 S3 D3" diff --git a/src/components/GameBoard/index.js b/src/components/GameBoard/index.js index 8530a21..b500b79 100644 --- a/src/components/GameBoard/index.js +++ b/src/components/GameBoard/index.js @@ -127,7 +127,7 @@ class DoudizhuGameBoard extends React.Component {
{`Player Id ${leftId}\n${this.props.playerInfo.length > 0 ? this.props.playerInfo[leftIdx].role : ""}`}
- {leftIdx >= 0 ? this.computeSideHand(this.props.hand[leftIdx]) :
Waiting...
} + {leftIdx >= 0 ? this.computeSideHand(this.props.hands[leftIdx]) :
Waiting...
}
{leftIdx >= 0 ? this.computeSingleLineHand(this.props.latestAction[leftIdx]) : ""} @@ -138,7 +138,7 @@ class DoudizhuGameBoard extends React.Component {
{`Player Id ${rightId}\n${this.props.playerInfo.length > 0 ? this.props.playerInfo[rightIdx].role : ""}`}
- {rightIdx >= 0 ? this.computeSideHand(this.props.hand[rightIdx]) :
Waiting...
} + {rightIdx >= 0 ? this.computeSideHand(this.props.hands[rightIdx]) :
Waiting...
}
{rightIdx >= 0 ? this.computeSingleLineHand(this.props.latestAction[rightIdx]) : ""} @@ -152,7 +152,7 @@ class DoudizhuGameBoard extends React.Component {
{`Player Id ${bottomId}\n${this.props.playerInfo.length > 0 ? this.props.playerInfo[bottomIdx].role : ""}`}
- {bottomIdx >= 0 ?
{this.computeSingleLineHand(this.props.hand[bottomIdx])}
:
Waiting...
} + {bottomIdx >= 0 ?
{this.computeSingleLineHand(this.props.hands[bottomIdx])}
:
Waiting...
}
diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..da3f4ae --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,21 @@ +export function removeCards(cards, hands){ // remove cards from hands, return the remained hands + let remainedHands = JSON.parse(JSON.stringify(hands)); + // if the player's action is pass then return the copy of original hands + if(cards === "P"){ + return remainedHands; + } + let misMatch = false; + cards.forEach(card => { + let foundIdx = remainedHands.findIndex(element => {return element === card;}); + if(foundIdx > -1){ + remainedHands.splice(foundIdx, 1); + }else { + misMatch = true; + } + }); + if(misMatch) + return false; + else + return remainedHands; +} + diff --git a/src/view/DoudizhuGameView.js b/src/view/DoudizhuGameView.js index 19e67b7..475f20b 100644 --- a/src/view/DoudizhuGameView.js +++ b/src/view/DoudizhuGameView.js @@ -1,6 +1,7 @@ import React from 'react'; import DoudizhuGameBoard from '../components/GameBoard'; import webSocket from "socket.io-client"; +import {removeCards} from "../utils"; class DoudizhuGameView extends React.Component { constructor(props) { @@ -13,7 +14,7 @@ class DoudizhuGameView extends React.Component { ws: null, gameInfo: { playerInfo: [], - hand: [], + hands: [], latestAction: [[], [], []], mainViewerId: mainViewerId, turn: 0, @@ -67,7 +68,7 @@ class DoudizhuGameView extends React.Component { // init replay info let gameInfo = JSON.parse(JSON.stringify(this.state.gameInfo)); gameInfo.playerInfo = message.message.playerInfo; - gameInfo.hand = message.message.initHand.map(element => { + gameInfo.hands = message.message.initHands.map(element => { return element.split(" "); }); // the first player should be landlord @@ -83,8 +84,13 @@ class DoudizhuGameView extends React.Component { gameInfo.latestAction[res.playerIdx] = res.move === "P" ? "P" : res.move.split(" "); gameInfo.turn++; gameInfo.currentPlayer = (gameInfo.currentPlayer+1)%3; - // todo: take away played cards from player's hand - + // take away played cards from player's hands + const remainedCards = removeCards(gameInfo.latestAction[res.playerIdx], gameInfo.hands[res.playerIdx]); + if(remainedCards !== false){ + gameInfo.hands[res.playerIdx] = remainedCards; + }else{ + console.log("Cannot find cards in move from player's hand"); + } this.setState({gameInfo: gameInfo}); this.gameStateTimer(); }else{ @@ -106,7 +112,7 @@ class DoudizhuGameView extends React.Component {