source: pages/api/poker/gameStates.js@ 22367db

main
Last change on this file since 22367db was 22367db, checked in by anastasovv <simon@…>, 2 years ago

Added authentication with google

  • 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: -1,
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, username) {
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 username: username,
78 credits: -1,
79 status: '_1_just_entered',
80 displayName: playerName,
81 cards: [],
82 betAmount: 0,
83 wonAmount: 0,
84 isSatDown: false,
85 isCoordinator: true,
86 isFolded: false,
87 isGhost: false,
88 hand: {
89 hand: '',
90 highCard: 0,
91 },
92 }],
93 onlyOnePlayerLeft: false,
94 winners: [],
95 splitWinners: false,
96 cards: [],
97 }
98
99 tables.push(table)
100
101 return table;
102}
103
104export function getRestrictedTablesArray() {
105 let result = [];
106
107 tables.forEach(table => {
108 let tmpPlayers = [];
109 table.players.forEach(player => {
110 tmpPlayers.push({
111 ...player,
112 id: '',
113 table: '',
114 cards: '',
115 })
116 });
117
118 let tmpWinners = [];
119 table.winners.forEach(winner => {
120 tmpWinners.push({
121 ...winner,
122 id: '',
123 table: '',
124 cards: '',
125 })
126 });
127
128 let tmp = {
129 ...table,
130 deck: [],
131 players: tmpPlayers,
132 winners: tmpWinners,
133 turnTimeout: null,
134 }
135
136 result.push({...tmp});
137 })
138
139 return result;
140}
141
142export function getRestrictedTableArray(tableId, session_id) {
143 let result = undefined;
144
145 let tableIdx = tables.map(e=>e.id).indexOf(tableId);
146
147 if (tableIdx !== -1) {
148 let table = tables[tableIdx];
149
150 let tmpPlayers = [];
151 table.players.forEach(player => {
152 if (player.id === session_id) {
153 tmpPlayers.push({
154 ...player,
155 id: '',
156 table: '',
157 })
158 }
159 else {
160 tmpPlayers.push({
161 ...player,
162 id: '',
163 table: '',
164 cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
165 })
166 }
167 });
168
169 let tmpWinners = [];
170 table.winners.forEach(winner => {
171 if (winner.id === session_id) {
172 tmpWinners.push({
173 ...winner,
174 id: '',
175 table: '',
176 })
177 }
178 else {
179 tmpWinners.push({
180 ...winner,
181 id: '',
182 table: '',
183 cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
184 })
185 }
186 });
187 result = {
188 ...table,
189 players: tmpPlayers,
190 winners: tmpWinners,
191 turnTimeout: null,
192 }
193 }
194
195 return result;
196}
197
198export function getTable(tableId) {
199 const tableIdx = tables.map(e=>e.id).indexOf(tableId);
200
201 if (tableIdx !== -1) {
202 return tables[tableIdx];
203 }
204
205 return undefined;
206}
207
208export function getTableAndPlayer(session_id) {
209 for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
210 const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
211
212 if (playerIdx !== -1) {
213 return {
214 success: true,
215 table: tables[tableIdx],
216 player: tables[tableIdx].players[playerIdx],
217 }
218 }
219 }
220
221 return {
222 success: false,
223 table: {...sampleTable},
224 player: {...samplePlayer},
225 };
226}
227
228/**
229 * ********************* END OF FUNCTIONS *********************
230 */
Note: See TracBrowser for help on using the repository browser.