Ignore:
Timestamp:
07/08/22 13:51:13 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
d0ef259
Parents:
aac3b2b
Message:

Saving sessions data and poker data to database

File:
1 edited

Legend:

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

    raac3b2b r95ce58b  
    1 export let tables = []
     1import { tables } from "../postgre/index";
     2
     3import { v4 as uuidv4 } from "uuid";
    24
    35export const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
    4                     "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
    5                     "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
    6                     "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
     6                           "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
     7                           "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
     8                           "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
    79
    810export const deck = [...singleDeck];
     
    1719    round: 0,
    1820    turnIdx: -1,
     21    lastActivity: 0,
    1922    pot: 0,
    2023    lastBet: 20,
     
    4548    },
    4649}
     50
     51
     52/**
     53 * ********************* BEGIN OF FUNCTIONS *********************
     54 */
     55
     56export function createTable(playerId, playerName, tableName) {
     57    const tableId = uuidv4();
     58
     59    const table = {
     60        id: tableId,
     61        name: tableName,
     62        status: '_1_just_created',
     63        creator: playerName,
     64        started: false,
     65        ended: false,
     66        round: 0,
     67        turnIdx: -1,
     68        lastActivity: 0,
     69        prevTurnIdx: -2,
     70        pot: 0,
     71        lastBet: 20,
     72        turnsSinceLastBet: 0,
     73        deck: [...deck],
     74        players: [{
     75            id: playerId,
     76            table: tableId,
     77            credits: 0,
     78            status: '_1_just_entered',
     79            displayName: playerName,
     80            cards: [],
     81            betAmount: 0,
     82            wonAmount: 0,
     83            isSatDown: false,
     84            isCoordinator: true,
     85            isFolded: false,
     86            isGhost: false,
     87            hand: {
     88                hand: '',
     89                highCard: 0,
     90            },
     91        }],
     92        onlyOnePlayerLeft: false,
     93        winners: [],
     94        splitWinners: false,
     95        cards: [],
     96    }
     97
     98    tables.push(table)
     99
     100    return table;
     101}
     102
     103export function getRestrictedTablesArray() {
     104    let result = [];
     105
     106    tables.forEach(table => {
     107        let tmpPlayers = [];
     108        table.players.forEach(player => {
     109            tmpPlayers.push({
     110                ...player,
     111                id: '',
     112                table: '',
     113                cards: '',
     114            })
     115        });
     116
     117        let tmpWinners = [];
     118        table.winners.forEach(winner => {
     119            tmpWinners.push({
     120                ...winner,
     121                id: '',
     122                table: '',
     123                cards: '',
     124            })
     125        });
     126
     127        let tmp = {
     128            ...table,
     129            deck: [],
     130            players: tmpPlayers,
     131            winners: tmpWinners,
     132            turnTimeout: null,
     133        }
     134
     135        result.push({...tmp});
     136    })
     137
     138    return result;
     139}
     140
     141export function getRestrictedTableArray(tableId, session_id) {
     142    let result = undefined;
     143
     144    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
     145
     146    if (tableIdx !== -1) {
     147        let table = tables[tableIdx];
     148
     149        let tmpPlayers = [];
     150        table.players.forEach(player => {
     151            if (player.id === session_id) {
     152                tmpPlayers.push({
     153                    ...player,
     154                    id: '',
     155                    table: '',
     156                })
     157            }
     158            else {
     159                tmpPlayers.push({
     160                    ...player,
     161                    id: '',
     162                    table: '',
     163                    cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
     164                })
     165            }
     166        });
     167
     168        let tmpWinners = [];
     169        table.winners.forEach(winner => {
     170            if (winner.id === session_id) {
     171                tmpWinners.push({
     172                    ...winner,
     173                    id: '',
     174                    table: '',
     175                })
     176            }
     177            else {
     178                tmpWinners.push({
     179                    ...winner,
     180                    id: '',
     181                    table: '',
     182                    cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
     183                })
     184            }
     185        });
     186        result = {
     187            ...table,
     188            players: tmpPlayers,
     189            winners: tmpWinners,
     190            turnTimeout: null,
     191        }
     192    }
     193
     194    return result;
     195}
     196
     197export function getTable(tableId) {
     198    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
     199
     200    if (tableIdx !== -1) {
     201        return tables[tableIdx];
     202    }
     203
     204    return undefined;
     205}
     206
     207export function getTableAndPlayer(session_id) {
     208    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
     209        const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
     210
     211        if (playerIdx !== -1) {
     212            return {
     213                success: true,
     214                table: tables[tableIdx],
     215                player: tables[tableIdx].players[playerIdx],
     216            }
     217        }
     218    }
     219
     220    return {
     221        success: false,
     222        table: {...sampleTable},
     223        player: {...samplePlayer},
     224    };
     225}
     226
     227/**
     228 * ********************* END OF FUNCTIONS *********************
     229 */
Note: See TracChangeset for help on using the changeset viewer.