source: pages/api/poker/gameStates.js@ 55701f0

main
Last change on this file since 55701f0 was 95ce58b, checked in by anastasovv <simon@…>, 2 years ago

Saving sessions data and poker data to database

  • Property mode set to 100644
File size: 5.4 KB
Line 
1import { tables } from "../postgre/index";
2
3import { v4 as uuidv4 } from "uuid";
4
5export const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
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" ];
9
10export const deck = [...singleDeck];
11
12export const sampleTable = {
13 id: '',
14 name: '',
15 status: '_1_just_created',
16 creator: '',
17 started: false,
18 ended: false,
19 round: 0,
20 turnIdx: -1,
21 lastActivity: 0,
22 pot: 0,
23 lastBet: 20,
24 turnsSinceLastBet: 0,
25 deck: [...deck],
26 players: [],
27 winners: [],
28 splitWinners: false,
29 cards: [],
30}
31
32export const samplePlayer = {
33 id: '',
34 table: '',
35 credits: 0,
36 status: '_1_just_entered',
37 displayName: '',
38 cards: [],
39 betAmount: 0,
40 wonAmount: 0,
41 isSatDown: false,
42 isCoordinator: false,
43 isFolded: false,
44 isGhost: false,
45 hand: {
46 hand: '',
47 highCard: 0,
48 },
49}
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 TracBrowser for help on using the repository browser.