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/index.js

    raac3b2b r95ce58b  
    33require('dotenv').config();
    44
    5 import { v4 as uuidv4 } from 'uuid';
    6 
    7 import { tables, deck, sampleTable, samplePlayer } from './gameStates'
    8 
    9 import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
    10 
    11 /**
    12  * ********************* BEGIN OF FUNCTIONS *********************
    13  */
    14 
    15 function createTable(playerId, playerName, tableName) {
    16     const tableId = uuidv4();
    17 
    18     const table = {
    19         id: tableId,
    20         name: tableName,
    21         status: '_1_just_created',
    22         creator: playerName,
    23         started: false,
    24         ended: false,
    25         round: 0,
    26         turnIdx: -1,
    27         pot: 0,
    28         lastBet: 20,
    29         turnsSinceLastBet: 0,
    30         deck: [...deck],
    31         players: [{
    32             id: playerId,
    33             table: tableId,
    34             credits: 0,
    35             status: '_1_just_entered',
    36             displayName: playerName,
    37             cards: [],
    38             betAmount: 0,
    39             wonAmount: 0,
    40             isSatDown: false,
    41             isCoordinator: true,
    42             isFolded: false,
    43             isGhost: false,
    44             hand: {
    45                 hand: '',
    46                 highCard: 0,
    47             },
    48         }],
    49         onlyOnePlayerLeft: false,
    50         winners: [],
    51         splitWinners: false,
    52         cards: [],
    53     }
    54 
    55     tables.push(table)
    56 
    57     return table;
    58 }
    59 
    60 function getRestrictedTablesArray() {
    61     let result = [];
    62 
    63     tables.forEach(table => {
    64         let tmpPlayers = [];
    65         table.players.forEach(player => {
    66             tmpPlayers.push({
    67                 ...player,
    68                 id: '',
    69                 table: '',
    70                 cards: '',
    71             })
    72         });
    73 
    74         let tmpWinners = [];
    75         table.winners.forEach(winner => {
    76             tmpWinners.push({
    77                 ...winner,
    78                 id: '',
    79                 table: '',
    80                 cards: '',
    81             })
    82         });
    83 
    84         let tmp = {
    85             ...table,
    86             deck: [],
    87             players: tmpPlayers,
    88             winners: tmpWinners,
    89             turnTimeout: null,
    90         }
    91 
    92         result.push({...tmp});
    93     })
    94 
    95     return result;
    96 }
    97 
    98 function getRestrictedTableArray(tableId, session_id) {
    99     let result = undefined;
    100 
    101     let tableIdx = tables.map(e=>e.id).indexOf(tableId);
    102 
    103     if (tableIdx !== -1) {
    104         let table = tables[tableIdx];
    105 
    106         let tmpPlayers = [];
    107         table.players.forEach(player => {
    108             if (player.id === session_id) {
    109                 tmpPlayers.push({
    110                     ...player,
    111                     id: '',
    112                     table: '',
    113                 })
    114             }
    115             else {
    116                 tmpPlayers.push({
    117                     ...player,
    118                     id: '',
    119                     table: '',
    120                     cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
    121                 })
    122             }
    123         });
    124 
    125         let tmpWinners = [];
    126         table.winners.forEach(winner => {
    127             if (winner.id === session_id) {
    128                 tmpWinners.push({
    129                     ...winner,
    130                     id: '',
    131                     table: '',
    132                 })
    133             }
    134             else {
    135                 tmpWinners.push({
    136                     ...winner,
    137                     id: '',
    138                     table: '',
    139                     cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
    140                 })
    141             }
    142         });
    143         result = {
    144             ...table,
    145             players: tmpPlayers,
    146             winners: tmpWinners,
    147             turnTimeout: null,
    148         }
    149     }
    150 
    151     return result;
    152 }
    153 
    154 function getTable(tableId) {
    155     const tableIdx = tables.map(e=>e.id).indexOf(tableId);
    156 
    157     if (tableIdx !== -1) {
    158         return tables[tableIdx];
    159     }
    160 
    161     return undefined;
    162 }
    163 
    164 function getTableAndPlayer(session_id) {
    165     for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
    166         const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
    167 
    168         if (playerIdx !== -1) {
    169             return {
    170                 success: true,
    171                 table: tables[tableIdx],
    172                 player: tables[tableIdx].players[playerIdx],
    173             }
    174         }
    175     }
    176 
    177     return {
    178         success: false,
    179         table: {...sampleTable},
    180         player: {...samplePlayer},
    181     };
    182 }
    183 
    184 /**
    185  * ********************* END OF FUNCTIONS *********************
    186  */
     5import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
     6
     7import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific'
     8
     9import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index'
    18710
    18811/**
     
    26689
    26790                if (okayToGo) {
     91                    table.lastActivity = Date.now();
    26892                    setNextPlayerIdx(table.id);
    26993                }
    27094            }
    27195           
     96            update_tables_to_database();
     97
    27298            res.end();
    27399        }
     
    291117                })
    292118
     119                table.lastActivity = Date.now();
    293120                table.started = true;
    294121                table.round = 1;
     
    309136                })
    310137            }
     138
     139            update_tables_to_database();
    311140           
    312141            res.end();
     
    327156            }
    328157
     158            update_tables_to_database();
     159
    329160            res.end();
    330161        }
     
    340171
    341172            if (success) {
     173                table.lastActivity = Date.now();
     174               
    342175                player.isGhost = true;
    343176                player.isFolded = true;
     
    352185                }
    353186            }
     187
     188            update_tables_to_database();
    354189
    355190            res.end();
     
    394229            }
    395230
     231            update_tables_to_database();
     232
    396233            res.end();
    397234        }
     
    412249            }
    413250
     251            update_tables_to_database();
     252
    414253            res.end();
    415254        }
     
    425264
    426265            const { success, table, player } = getTableAndPlayer(session_id);
     266
     267            if (table.started && !table.ended) {
     268                const d = Date.now();
     269
     270                if (d - table.lastActivity > 30000) {
     271                    if (table.players[table.turnIdx] !== undefined) {
     272                        table.players[table.turnIdx].isFolded = true;
     273
     274                        setNextPlayerIdx(table.id);
     275                    }
     276                }
     277            }
    427278
    428279            res.json({
Note: See TracChangeset for help on using the changeset viewer.