Changeset aac3b2b


Ignore:
Timestamp:
07/07/22 23:53:57 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
95ce58b
Parents:
189cd8f
Message:

Fixes to poker functionality

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • components/poker/sections/Messages.jsx

    r189cd8f raac3b2b  
    2424    return (
    2525        <div className="pokerMessagesContainer">
    26             { playerState.pokerGame.table.started && playerState.pokerGame.table.ended && playerState.pokerGame.table.winners.length === 1 && <p>Game over - {playerState.pokerGame.table.winners[0]?.displayName} won ${playerState.pokerGame.table.winners[0]?.wonAmount} with a {playerState.pokerGame.table.winners[0]?.hand?.hand} combination! Congratulations.</p> }
    27             { playerState.pokerGame.table.started && playerState.pokerGame.table.ended && playerState.pokerGame.table.winners.length > 1 && <p>Game over - {playerState.pokerGame.table.winners.map(e=>e.displayName).join(", ")} drew!</p> }
    28             { playerState.pokerGame.table.started && playerState.pokerGame.table.ended && <p>New game will start soon.</p> }
     26            { playerState.pokerGame.table.ended && playerState.pokerGame.table.onlyOnePlayerLeft && <p>Game over - {playerState.pokerGame.table.winners[0]?.displayName} won ${playerState.pokerGame.table.winners[0]?.wonAmount} because everyone else folded! Congratulations.</p> }
     27            { playerState.pokerGame.table.ended && !playerState.pokerGame.table.onlyOnePlayerLeft && playerState.pokerGame.table.winners.length === 1 && <p>Game over - {playerState.pokerGame.table.winners[0]?.displayName} won ${playerState.pokerGame.table.winners[0]?.wonAmount} with a {playerState.pokerGame.table.winners[0]?.hand?.hand} combination! Congratulations.</p> }
     28            { playerState.pokerGame.table.ended && playerState.pokerGame.table.winners.length > 1 && <p>Game over - {playerState.pokerGame.table.winners.map(e=>e.displayName).join(", ")} drew!</p> }
     29            { playerState.pokerGame.table.ended && <p>New game will start soon.</p> }
    2930
    3031            { playerState.pokerGame.table.started && !playerState.pokerGame.table.ended && <p>Round {playerState.pokerGame.table.round}/4{roundMessage}</p> }
    31             { !playerState.pokerGame.table.started && <p>Waiting for coordinator {playerState.pokerGame.table.creator} to start the game.</p> }
     32            { !playerState.pokerGame.table.started && !playerState.pokerGame.table.ended && <p>Waiting for coordinator {playerState.pokerGame.table.creator} to start the game.</p> }
    3233            { playerState.pokerGame.table.started && !playerState.pokerGame.table.ended && <p>{turnMessage}</p> }
    3334            { playerState.pokerGame.table.started && !playerState.pokerGame.table.ended && <p>{callMessage}</p> }
  • components/poker/sections/PickATable.jsx

    r189cd8f raac3b2b  
    4949                <h3>Pick a table:</h3>
    5050                <div onClick={(e) => joinATable(e)}>
    51                     {playerState.pokerGame.tables.map(table => (
     51                    {playerState.pokerGame.tables.filter(table=>table.started===false).map(table => (
    5252                        <div data-table={table.id} key={table.id}>
    5353                            <p data-table={table.id}>Table name: {table.name}</p>
    5454                            <p data-table={table.id}>Creator: {table.creator}</p>
    55                             <p data-table={table.id}>Players: {table.players.length}/8</p>
     55                            <p data-table={table.id}>Players: {table.players.filter(e=>e.isGhost===false).length}/8</p>
    5656                            <p data-table={table.id}>Join</p>
    5757                        </div>
  • components/poker/sections/PlayButtons.jsx

    r189cd8f raac3b2b  
    6060    return (
    6161      <div className="pokerPlayButtonsContainer">
    62         {!playerState.pokerGame.table.started && playerState.pokerGame.player.isCoordinator && playerState.pokerGame.player.isSatDown && <button className="secondaryButton" onClick={() => startGame()}>Start game</button>}
     62        {!playerState.pokerGame.table.started && !playerState.pokerGame.table.ended && playerState.pokerGame.player.isCoordinator && playerState.pokerGame.player.isSatDown && <button className="secondaryButton" onClick={() => startGame()}>Start game</button>}
    6363        {!playerState.pokerGame.table.started && !playerState.pokerGame.player.isSatDown && <button className="secondaryButton" onClick={() => sitDown()}>Take a seat</button>}
    6464      </div>
  • pages/api/poker/index.js

    r189cd8f raac3b2b  
    4747            },
    4848        }],
     49        onlyOnePlayerLeft: false,
    4950        winners: [],
    5051        splitWinners: false,
     
    281282            const { success, table } = getTableAndPlayer(req.query.session_id)
    282283
    283             if (success && !table.started) {
     284            if (success && !table.started && !table.ended && table.players.filter(e=>e.isSatDown===true).length >= 2) {
    284285                table.players.forEach(player => {
    285286                    axios.get(`${process.env.HOME_URL}/api/postgre/?action=check_if_logged_in&session_id=${player.id}`).then(postgreRes => {
     
    342343                player.isFolded = true;
    343344
    344                 if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx] === player) {
    345                     setNextPlayerIdx(table.id);
     345                if (table.started) {
     346                    if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx] === player) {
     347                        setNextPlayerIdx(table.id);
     348                    }
     349                }
     350                else {
     351                    table.players = table.players.filter(e=>e.isGhost === false);
    346352                }
    347353            }
  • pages/api/poker/tableSpecific.js

    r189cd8f raac3b2b  
    6262    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
    6363
    64     if (tables[tableIdx] !== undefined) {
    65         const table = tables[tableIdx];
     64    if (tables[tableIdx] !== undefined && !tables[tableIdx].ended) {
     65        const table = tables[tableIdx];
     66
     67        const remainingPlayers = table.players.filter(e=>e.isSatDown===true).filter(e=>e.isGhost===false);
     68        if (remainingPlayers.length === 1) {
     69            setWinnerDirect(table.id, remainingPlayers[0]);
     70
     71            return ;
     72        }
     73
     74        const remainingPlayersWithCredits = table.players.filter(e=>e.isSatDown===true).filter(e=>e.isGhost===false).filter(e=>e.isFolded===false).filter(e=>e.credits > 0);
     75        if (remainingPlayersWithCredits.length === 1) {
     76            progressRoundTillTheEnd(table.id);
     77
     78            return ;
     79        }
    6680
    6781        if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
     
    8397                        if (table.players[table.turnIdx] !== undefined) {
    8498                            table.players[table.turnIdx].isFolded = true;
    85 
     99                           
    86100                            setNextPlayerIdx(table.id);
    87101                        }
     
    136150        table.ended = false;
    137151        table.round = 0;
    138         table.turnIdx = 0;
     152        table.turnIdx = -1;
    139153        table.turnTimeout = null;
    140154        table.pot = 0;
     
    143157        table.deck = [...deck];
    144158
    145         table.players = table.players.filter(e=>e.isGhost === false);
    146         table.players = table.players.filter(e=>e.credits >= 20);
     159        table.players = table.players.filter(e=>e.isGhost === false).filter(e=>e.credits >= 20);
    147160
    148161        table.players.forEach(player => {
     
    157170            }
    158171        })
     172
     173        table.onlyOnePlayerLeft = false;
    159174        table.winners = [];
    160175        table.splitWinners = false;
     
    168183    if (tables[tableIdx] !== undefined) {
    169184        const table = tables[tableIdx];
    170 
    171         const satDownPlayers = table.players.filter(e => e.isSatDown === true);
    172         const satDownCount = satDownPlayers.length;
    173185
    174186        table.players.forEach(player => {
     
    212224}
    213225
     226export function setWinnerDirect(tableId, player) {
     227    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     228
     229    if (tables[tableIdx] !== undefined) {
     230        const table = tables[tableIdx];
     231
     232        table.turnIdx = -1;
     233        table.started = false;
     234        table.ended = true;
     235
     236        table.onlyOnePlayerLeft = true;
     237        table.winners = [player];
     238
     239        giveMoneyToTheWinners(table.id);
     240    }
     241}
     242
    214243export function setWinner(tableId) {
    215244    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     
    259288}
    260289
     290export function progressRoundTillTheEnd(tableId) {
     291    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     292
     293    if (tables[tableIdx] !== undefined) {
     294        const table = tables[tableIdx];
     295
     296        while (table.round < 4) {
     297            table.round++;
     298            getCardsOnTable(table.id);
     299        }
     300
     301        table.started = false;
     302        table.ended = true;
     303        if (table.ended && table.winners.length === 0) {
     304            setWinner(table.id);
     305        }
     306    }
     307}
     308
    261309export function progressRoundIfNeeded(tableId) {
    262310    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     
    277325            }
    278326            else {
     327                table.started = false;
    279328                table.ended = true;
    280329            }
  • pages/api/postgre/index.js

    r189cd8f raac3b2b  
    413413                    // Already logged in
    414414                    res.json({
    415                       success: false,
    416                       message: 'You are already logged in',
     415                      success: true,
     416                      message: 'Login successful',
     417                      session: session,
    417418                    })
    418419                  }
Note: See TracChangeset for help on using the changeset viewer.