From 011f744efca8447c2a15776de4156ccac248b5cb Mon Sep 17 00:00:00 2001 From: Songyi Huang Date: Sat, 22 May 2021 21:54:46 -0700 Subject: [PATCH] show game statistics on game ends; fix the bug that landlord can pass on second game --- src/assets/doudizhu.scss | 7 ++ src/view/PvEView/PvEDoudizhuDemoView.js | 161 ++++++++++++++++++++---- 2 files changed, 146 insertions(+), 22 deletions(-) diff --git a/src/assets/doudizhu.scss b/src/assets/doudizhu.scss index 9fdb983..7b026c4 100644 --- a/src/assets/doudizhu.scss +++ b/src/assets/doudizhu.scss @@ -252,3 +252,10 @@ } } +.doudizhu-statistic-table { + .MuiTableHead-root { + th.MuiTableCell-root.MuiTableCell-head { + font-weight: bolder; + } + } +} \ No newline at end of file diff --git a/src/view/PvEView/PvEDoudizhuDemoView.js b/src/view/PvEView/PvEDoudizhuDemoView.js index 5f04677..35f033b 100644 --- a/src/view/PvEView/PvEDoudizhuDemoView.js +++ b/src/view/PvEView/PvEDoudizhuDemoView.js @@ -1,14 +1,14 @@ +import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@material-ui/core'; import Button from '@material-ui/core/Button'; import Dialog from '@material-ui/core/Dialog'; -import Slider from '@material-ui/core/Slider'; import DialogActions from '@material-ui/core/DialogActions'; import DialogContent from '@material-ui/core/DialogContent'; -import DialogContentText from '@material-ui/core/DialogContentText'; import DialogTitle from '@material-ui/core/DialogTitle'; import Divider from '@material-ui/core/Divider'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormGroup from '@material-ui/core/FormGroup'; import Paper from '@material-ui/core/Paper'; +import Slider from '@material-ui/core/Slider'; import Switch from '@material-ui/core/Switch'; import NotInterestedIcon from '@material-ui/icons/NotInterested'; import axios from 'axios'; @@ -59,7 +59,8 @@ let playedCardsLandlord = []; let playedCardsLandlordDown = []; let playedCardsLandlordUp = []; let legalActions = { turn: -1, actions: [] }; -let gameEndDialogText = ''; +let gameEndDialogTitle = ''; +let statisticRows = []; let syncGameStatus = 'ready'; function PvEDoudizhuDemoView() { @@ -221,8 +222,93 @@ function PvEDoudizhuDemoView() { if (newHand.length === 0) { const winner = playerInfo[gameState.currentPlayer]; + + // update game overall history + const gameStatistics = localStorage.getItem('GAME_STATISTICS') + ? JSON.parse(localStorage.getItem('GAME_STATISTICS')) + : { + totalGameNum: 0, + totalWinNum: 0, + landlordGameNum: 0, + landlordWinNum: 0, + landlordUpGameNum: 0, + landlordUpWinNum: 0, + landlordDownGameNum: 0, + landlordDownWinNum: 0, + }; + + gameStatistics.totalGameNum += 1; + switch (playerInfo[mainPlayerId].douzeroPlayerPosition) { + case 0: + gameStatistics.landlordGameNum += 1; + if (winner.role === playerInfo[mainPlayerId].role) { + gameStatistics.totalWinNum += 1; + gameStatistics.landlordWinNum += 1; + } + break; + case 1: + gameStatistics.landlordDownGameNum += 1; + if (winner.role === playerInfo[mainPlayerId].role) { + gameStatistics.totalWinNum += 1; + gameStatistics.landlordDownWinNum += 1; + } + break; + case 2: + gameStatistics.landlordUpGameNum += 1; + if (winner.role === playerInfo[mainPlayerId].role) { + gameStatistics.totalWinNum += 1; + gameStatistics.landlordUpWinNum += 1; + } + break; + default: + Message({ + message: 'Wrong douzero player position', + type: 'error', + showClose: true, + }); + } + localStorage.setItem('GAME_STATISTICS', JSON.stringify(gameStatistics)); + setTimeout(() => { - gameEndDialogText = winner.role + ' wins!'; + gameEndDialogTitle = winner.role === 'peasant' ? 'Peasants win!' : 'Landlord wins!'; + statisticRows = [ + { + role: 'Landlord', + win: gameStatistics.landlordWinNum, + total: gameStatistics.landlordGameNum, + winRate: gameStatistics.landlordGameNum + ? ((gameStatistics.landlordWinNum / gameStatistics.landlordGameNum) * 100).toFixed(2) + '%' + : '-', + }, + { + role: 'Landlord Up', + win: gameStatistics.landlordUpWinNum, + total: gameStatistics.landlordUpGameNum, + winRate: gameStatistics.landlordUpGameNum + ? ((gameStatistics.landlordUpWinNum / gameStatistics.landlordUpGameNum) * 100).toFixed(2) + + '%' + : '-', + }, + { + role: 'Landlord Down', + win: gameStatistics.landlordDownWinNum, + total: gameStatistics.landlordDownGameNum, + winRate: gameStatistics.landlordDownGameNum + ? ((gameStatistics.landlordDownWinNum / gameStatistics.landlordDownGameNum) * 100).toFixed( + 2, + ) + '%' + : '-', + }, + { + role: 'All', + win: gameStatistics.totalWinNum, + total: gameStatistics.totalGameNum, + winRate: gameStatistics.totalGameNum + ? ((gameStatistics.totalWinNum / gameStatistics.totalGameNum) * 100).toFixed(2) + '%' + : '-', + }, + ]; + setIsGameEndDialogOpen(true); }, 300); } else { @@ -471,10 +557,10 @@ function PvEDoudizhuDemoView() { playedCardsLandlordDown = []; playedCardsLandlordUp = []; legalActions = { turn: -1, actions: [] }; - gameEndDialogText = ''; setConsiderationTime(initConsiderationTime); setToggleFade(''); + setIsPassDisabled(true); setGameState({ hands: [[], [], []], latestAction: [[], [], []], @@ -487,7 +573,6 @@ function PvEDoudizhuDemoView() { setGameStatus('ready'); syncGameStatus = 'ready'; setIsGameEndDialogOpen(false); - }; const startGame = async () => { @@ -678,7 +763,7 @@ function PvEDoudizhuDemoView() { { value: 5, label: '30s', - } + }, ]; const gameSpeedMap = [ @@ -705,15 +790,13 @@ function PvEDoudizhuDemoView() { { value: 5, delay: 30000, - } + }, ]; const changeApiPlayerDelay = (newVal) => { - const found = gameSpeedMap.find(element => element.value === newVal); - console.log(newVal, found.delay); - if (found) - setApiPlayDelay(found.delay); - } + const found = gameSpeedMap.find((element) => element.value === newVal); + if (found) setApiPlayDelay(found.delay); + }; const sliderValueText = (value) => { return value; @@ -722,19 +805,43 @@ function PvEDoudizhuDemoView() { return (
- {'Game Ends!'} + {gameEndDialogTitle} - {gameEndDialogText} + + + + + Role + Win + Total + Win Rate + + + + {statisticRows.map((row) => ( + + + {row.role} + + {row.win} + {row.total} + {row.winRate} + + ))} + +
+
- @@ -762,7 +869,6 @@ function PvEDoudizhuDemoView() { gameStatus={gameStatus} handleMainPlayerAct={handleMainPlayerAct} /> - {/* )} */}
@@ -806,7 +912,11 @@ function PvEDoudizhuDemoView() {
{playerInfo.length > 0 && gameState.currentPlayer !== null ? ( - {['Landlord', 'Landlord Down', 'Landlord Up'][playerInfo[gameState.currentPlayer].douzeroPlayerPosition]} + { + ['Landlord', 'Landlord Down', 'Landlord Up'][ + playerInfo[gameState.currentPlayer].douzeroPlayerPosition + ] + } ) : ( Waiting... @@ -850,12 +960,19 @@ function PvEDoudizhuDemoView() {
- -
+ +
element.delay === apiPlayDelay).value} + value={gameSpeedMap.find((element) => element.delay === apiPlayDelay).value} getAriaValueText={sliderValueText} - onChange={(e, newVal)=>{changeApiPlayerDelay(newVal)}} + onChange={(e, newVal) => { + changeApiPlayerDelay(newVal); + }} aria-labelledby="discrete-slider-custom" step={1} min={0}