Changeset b13f93b for pages


Ignore:
Timestamp:
07/03/22 22:59:15 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
3a783f2
Parents:
ace7865
Message:

Made poker tables system and round 1

Location:
pages/api
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pages/api/blackjack/gameStates.js

    race7865 rb13f93b  
    2929 */
    3030function checkDeckSize(game) {
    31     if (game.deck.length === 0) {
    32       game.deck = [...deck];
    33     }
     31  if (game.deck.length === 0) {
     32    game.deck = [...deck];
    3433  }
     34}
    3535 
    3636/**
  • pages/api/poker/index.js

    race7865 rb13f93b  
     1import axios from 'axios';
     2
     3require('dotenv').config();
     4
     5import { v4 as uuidv4 } from 'uuid';
     6
     7const sampleTable = {
     8    id: '',
     9    name: '',
     10    status: '_1_just_created',
     11    creator: '',
     12    started: false,
     13    round: 0,
     14    turnIdx: 0,
     15    lastBet: 0,
     16    turnsSinceLastBet: 0,
     17    players: [],
     18    deck: [],
     19    cardsOnTable: [],
     20}
     21
     22const samplePlayer = {
     23    id: '',
     24    table: '',
     25    status: '_1_just_entered',
     26    displayName: '',
     27    cards: [],
     28    betAmount: 0,
     29    isSatDown: false,
     30    isCoordinator: false,
     31    isFolded: false,
     32}
     33
     34let tables = []
     35// contures -> { status, round, turnIdx, lastBet, turnsSinceLastBet,
     36//
     37//              players -> { id, table, status, displayName, cards,
     38//                          betAmount, isSatDown, isCoordinator },
     39//
     40//              cardsOnTable }
     41
     42const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
     43                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
     44                    "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
     45                    "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
     46
     47/* We are using 5 decks */
     48const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
     49
     50/**
     51 * Replace deck if empty
     52 */
     53function checkDeckSize(tableId) {
     54    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     55
     56    if (tables[tableIdx] !== undefined) {
     57        if (tables[tableIdx].deck.length === 0) {
     58            tables[tableIdx].deck = [...deck];
     59        }
     60    }
     61}
     62
     63/**
     64 * Draw a SINGLE random card
     65 */
     66function drawASingleCard(tableId) {
     67    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     68
     69    if (tables[tableIdx] !== undefined) {
     70        checkDeckSize(tableId);
     71       
     72        let idx = Math.floor(Math.random() * tables[tableIdx].deck.length);
     73        let card = tables[tableIdx].deck[idx];
     74
     75        tables[tableIdx].deck.splice(idx, 1);
     76
     77        return card;
     78    }
     79
     80    return undefined;
     81}
     82
     83function setNextPlayerIdx(tableId) {
     84    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     85
     86    if (tables[tableIdx] !== undefined) {
     87        const table = tables[tableIdx];
     88
     89        while (true) {
     90            table.turnIdx++;
     91            table.turnIdx %= table.players.length;
     92           
     93            if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
     94                return ;
     95            }
     96        }
     97    }
     98}
     99
     100function getCardsOnTable(tableId) {
     101    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     102
     103    if (tables[tableIdx] !== undefined) {
     104        const table = tables[tableIdx];
     105
     106        if (table.round === 2) {
     107            for (let i = 0; i < 3; i++) {
     108                const card = drawASingleCard(table.id);
     109                           
     110                if (card !== undefined) {
     111                    table.cards.push(card);
     112                }
     113            }
     114        }
     115    }
     116}
     117
     118/**
     119 * ********************* BEGIN OF FUNCTIONS *********************
     120 */
     121
     122function createTable(playerId, playerName, tableName) {
     123    const tableId = uuidv4();
     124
     125    const table = {
     126        id: tableId,
     127        name: tableName,
     128        status: '_1_just_created',
     129        creator: playerName,
     130        started: false,
     131        round: 0,
     132        turnIdx: 0,
     133        lastBet: 20,
     134        turnsSinceLastBet: 0,
     135        deck: [...deck],
     136        players: [{
     137            id: playerId,
     138            table: tableId,
     139            status: '_1_just_entered',
     140            displayName: playerName,
     141            cards: [],
     142            betAmount: 0,
     143            isSatDown: false,
     144            isCoordinator: true,
     145            isFolded: false,
     146        }],
     147        cards: [],
     148    }
     149
     150    tables.push(table)
     151
     152    return table;
     153}
     154
     155function getRestrictedTablesArray() {
     156    let result = [];
     157
     158    tables.forEach(table => {
     159        let tmpPlayers = [];
     160        table.players.forEach(player => {
     161            tmpPlayers.push({
     162                ...player,
     163                id: '',
     164                table: '',
     165                cards: '',
     166            })
     167        });
     168
     169        let tmp = {
     170            ...table,
     171            deck: [],
     172            players: tmpPlayers,
     173        }
     174
     175        result.push({...tmp});
     176    })
     177
     178    return result;
     179}
     180
     181function getRestrictedTableArray(tableId, session_id) {
     182    let result = {...sampleTable};
     183
     184    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
     185
     186    if (tableIdx !== -1) {
     187        let table = tables[tableIdx];
     188
     189        let tmpPlayers = [];
     190        table.players.forEach(player => {
     191            if (player.id === session_id) {
     192                tmpPlayers.push({
     193                    ...player,
     194                    id: '',
     195                    table: '',
     196                })
     197            }
     198            else {
     199                tmpPlayers.push({
     200                    ...player,
     201                    id: '',
     202                    table: '',
     203                    cards: player.cards.length > 0 ? ['back', 'back'] : '',
     204                })
     205            }
     206        });
     207
     208        result = {
     209            ...table,
     210            players: tmpPlayers,
     211        }
     212    }
     213
     214    return result;
     215}
     216
     217function getTable(tableId) {
     218    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     219
     220    if (tableIdx !== -1) {
     221        return tables[tableIdx];
     222    }
     223
     224    return undefined;
     225}
     226
     227function getTableAndPlayer(session_id) {
     228    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
     229        const playerIdx = tables[tableIdx].players.map(e=>e.id).indexOf(session_id);
     230
     231        if (playerIdx !== -1) {
     232            return {
     233                success: true,
     234                table: tables[tableIdx],
     235                player: tables[tableIdx].players[playerIdx],
     236            }
     237        }
     238    }
     239
     240    return {
     241        success: false,
     242        table: sampleTable,
     243        player: samplePlayer,
     244    };
     245}
     246
     247/**
     248 * ********************* END OF FUNCTIONS *********************
     249 */
     250
     251/**
     252 * ********************* BEGIN OF REQUEST HANDLER *********************
     253 */
     254export default async function handler(req, res) {
     255    /**
     256     * GET method
     257     */
     258    if (req.method === 'GET') {
     259       
     260        /**
     261         * /---------------------- GET ----------------------/
     262         * Creates the table and enters the user inside
     263         * @action game_action
     264         * @param session_id
     265         * @param specificAction
     266         * @param betAmount
     267         */
     268        if (req.query.action === 'game_action' && req.query?.session_id && req.query?.specificAction && req.query?.betAmount) {
     269            const { success, table, player } = getTableAndPlayer(req.query.session_id)
     270
     271            if (success && table.started) {
     272                if (table.players.map(e=>e.id).indexOf(req.query.session_id) !== table.turnIdx) {
     273                    res.end();
     274                    return ;
     275                }
     276
     277                let okayToGo = false;
     278
     279                const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
     280                const remainingPlayers = satDownPlayers.filter(e=>e.folded === false);
     281
     282                if (req.query.specificAction === 'check') {
     283
     284                }
     285                else if (req.query.specificAction === 'call') {
     286                    player.betAmount += table.lastBet;
     287                    table.turnsSinceLastBet++;
     288                    okayToGo = true;
     289
     290                    if (table.turnsSinceLastBet === remainingPlayers.length) {
     291                        table.round++;
     292                        table.lastBet = 0;
     293
     294                        getCardsOnTable(table.id);
     295                    }
     296                }
     297                else if (req.query.specificAction === 'raise') {
     298                   
     299                }
     300                else if (req.query.specificAction === 'fold') {
     301                    player.folded = true;
     302                }
     303
     304                if (okayToGo) {
     305                    setNextPlayerIdx(table.id);
     306                }
     307            }
     308           
     309            res.end();
     310        }
     311
     312        /**
     313         * /---------------------- GET ----------------------/
     314         * Creates the table and enters the user inside
     315         * @action start_game
     316         * @param session_id
     317         */
     318        if (req.query.action === 'start_game' && req.query?.session_id) {
     319            const { success, table } = getTableAndPlayer(req.query.session_id)
     320
     321            if (success && !table.started) {
     322                table.started = true;
     323                table.round = 1;
     324
     325                const satDownPlayers = table.players.filter(e=>e.isSatDown === true);
     326
     327                table.turnIdx = Math.floor(Math.random(0, satDownPlayers.length))
     328
     329                table.players.forEach(player => {
     330                    if (player.isSatDown) {
     331                        for (let i = 0; i < 2; i++) {
     332                            const card = drawASingleCard(table.id);
     333                           
     334                            if (card !== undefined) {
     335                                player.cards.push(card);
     336                            }
     337                        }
     338                    }
     339                })
     340            }
     341           
     342            res.end();
     343        }
     344
     345        /**
     346         * /---------------------- GET ----------------------/
     347         * Creates the table and enters the user inside
     348         * @action sit_down
     349         * @param session_id
     350         * @param tableId
     351         */
     352         if (req.query.action === 'sit_down' && req.query?.session_id && req.query?.tableId) {
     353            const { success, table, player } = getTableAndPlayer(req.query.session_id)
     354
     355            if (success && !table.started) {
     356                player.isSatDown = true;
     357            }
     358
     359            res.end();
     360        }
     361
     362        /**
     363         * /---------------------- GET ----------------------/
     364         * Creates the table and enters the user inside
     365         * @action join_a_table
     366         * @param session_id
     367         * @param tableId
     368         * @param displayName
     369         */
     370         if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName) {
     371            if (req.query.tableId.length > 0) {
     372                const { success } = getTableAndPlayer(req.query.session_id);
     373
     374                if (!success) {
     375                    const table = getTable(req.query.tableId)
     376
     377                    if (!table.started) {
     378                        table.players.push({
     379                            ...samplePlayer,
     380                            id: req.query.session_id,
     381                            table: req.query.tableId,
     382                            displayName: req.query.displayName
     383                        })
     384                    }
     385                }
     386            }
     387
     388            res.end();
     389        }
     390
     391        /**
     392         * /---------------------- GET ----------------------/
     393         * Creates the table and enters the user inside
     394         * @action create_a_table
     395         * @param session_id
     396         * @param displayName
     397         * @param tableName
     398         */
     399        if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName) {
     400            const { success } = getTableAndPlayer(req.query.session_id);
     401
     402            if (!success) {
     403                createTable(req.query.session_id, req.query.displayName, req.query.tableName);
     404            }
     405
     406            res.end();
     407        }
     408
     409        /**
     410         * /---------------------- GET ----------------------/
     411         * Creates the table and enters the user inside
     412         * @action update_state
     413         * @param session_id
     414         */
     415        if (req.query.action === 'update_state' && req.query?.session_id) {
     416            const session_id = req.query.session_id;
     417
     418            const { table, player } = getTableAndPlayer(session_id);
     419
     420            res.json({
     421                success: true,
     422                pokerGame: {
     423                    tables: getRestrictedTablesArray(),
     424                    table: getRestrictedTableArray(table.id, req.query.session_id),
     425                    player: player,
     426                }
     427            })
     428        }
     429
     430        /**
     431         * /---------------------- GET ----------------------/
     432         * If the player is not in an existing room, create a room for them.
     433         * If they are reconnecting, get the room they were in.
     434         * @action get_player_info_on_enter
     435         * @param session_id
     436         */
     437        if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
     438            const session_id = req.query.session_id;
     439
     440            axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
     441                if (postgreRes.data?.success) {
     442                    res.json({
     443                        success: true,
     444                        displayName: postgreRes.data?.displayName,
     445                        session_id: postgreRes.data?.session_id,
     446                        credits: postgreRes.data?.credits,
     447                    })
     448                }
     449                else {
     450                    res.json({
     451                        success: false,
     452                    })
     453                }
     454            });
     455        }
     456    }
     457}
     458/**
     459 * ********************* END OF REQUEST HANDLER *********************
     460 */
  • pages/api/roulette/index.js

    race7865 rb13f93b  
    138138     */
    139139    if (req.method === 'GET') {
    140        
    141140        /**
    142141         * /---------------------- GET ----------------------/
Note: See TracChangeset for help on using the changeset viewer.