Changeset 95ce58b for pages/api/poker/index.js
- Timestamp:
- 07/08/22 13:51:13 (2 years ago)
- Branches:
- main
- Children:
- d0ef259
- Parents:
- aac3b2b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/poker/index.js
raac3b2b r95ce58b 3 3 require('dotenv').config(); 4 4 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 */ 5 import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates'; 6 7 import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific' 8 9 import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index' 187 10 188 11 /** … … 266 89 267 90 if (okayToGo) { 91 table.lastActivity = Date.now(); 268 92 setNextPlayerIdx(table.id); 269 93 } 270 94 } 271 95 96 update_tables_to_database(); 97 272 98 res.end(); 273 99 } … … 291 117 }) 292 118 119 table.lastActivity = Date.now(); 293 120 table.started = true; 294 121 table.round = 1; … … 309 136 }) 310 137 } 138 139 update_tables_to_database(); 311 140 312 141 res.end(); … … 327 156 } 328 157 158 update_tables_to_database(); 159 329 160 res.end(); 330 161 } … … 340 171 341 172 if (success) { 173 table.lastActivity = Date.now(); 174 342 175 player.isGhost = true; 343 176 player.isFolded = true; … … 352 185 } 353 186 } 187 188 update_tables_to_database(); 354 189 355 190 res.end(); … … 394 229 } 395 230 231 update_tables_to_database(); 232 396 233 res.end(); 397 234 } … … 412 249 } 413 250 251 update_tables_to_database(); 252 414 253 res.end(); 415 254 } … … 425 264 426 265 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 } 427 278 428 279 res.json({
Note:
See TracChangeset
for help on using the changeset viewer.