source: pages/api/poker/index.js@ 433e0c5

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

Saving roulette data to database

  • Property mode set to 100644
File size: 11.8 KB
Line 
1import axios from 'axios';
2
3require('dotenv').config();
4
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'
10
11/**
12 * ********************* BEGIN OF REQUEST HANDLER *********************
13 */
14export default async function handler(req, res) {
15 /**
16 * GET method
17 */
18 if (req.method === 'GET') {
19
20 /**
21 * /---------------------- GET ----------------------/
22 * Creates the table and enters the user inside
23 * @action game_action
24 * @param session_id
25 * @param specificAction
26 * @param betAmount
27 */
28 if (req.query.action === 'game_action' && req.query?.session_id && req.query?.specificAction && req.query?.betAmount) {
29 const { success, table, player } = getTableAndPlayer(req.query.session_id)
30
31 if (success && table.started && !table.ended && player.isSatDown && !player.isFolded) {
32 if (table.players.map(e=>e.id).indexOf(req.query.session_id) !== table.turnIdx) {
33 res.end();
34 return ;
35 }
36
37 let okayToGo = false;
38
39 if (req.query.specificAction === 'check') {
40 if (table.lastBet === 0) {
41 table.turnsSinceLastBet++;
42 okayToGo = true;
43
44 progressRoundIfNeeded(table.id);
45 }
46 }
47 else if (req.query.specificAction === 'call') {
48 await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${table.lastBet}&takeWhatYouCan=true`).then(postgreRes => {
49 if (postgreRes.data?.success) {
50 player.credits = postgreRes.data?.credits;
51
52 if (player.credits >= table.lastBet)
53 player.betAmount += table.lastBet;
54 else
55 player.betAmount += player.credits;
56
57 table.pot += table.lastBet;
58 table.turnsSinceLastBet++;
59 okayToGo = true;
60
61 progressRoundIfNeeded(table.id);
62 }
63 });
64 }
65 else if (req.query.specificAction === 'raise') {
66 const betAmount = parseInt(req.query.betAmount);
67
68 if (betAmount >= table.lastBet) {
69 await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${betAmount}&takeWhatYouCan=true`).then(postgreRes => {
70 if (postgreRes.data?.success) {
71 player.credits = postgreRes.data?.credits;
72
73 player.betAmount += betAmount;
74 table.pot += betAmount;
75 table.turnsSinceLastBet = 1;
76 okayToGo = true;
77
78 progressRoundIfNeeded(table.id);
79 }
80 });
81 }
82 }
83 else if (req.query.specificAction === 'fold') {
84 player.isFolded = true;
85 okayToGo = true;
86
87 progressRoundIfNeeded(table.id);
88 }
89
90 if (okayToGo) {
91 table.lastActivity = Date.now();
92 setNextPlayerIdx(table.id);
93 }
94 }
95
96 update_tables_to_database();
97
98 res.end();
99 }
100
101 /**
102 * /---------------------- GET ----------------------/
103 * Creates the table and enters the user inside
104 * @action start_game
105 * @param session_id
106 */
107 if (req.query.action === 'start_game' && req.query?.session_id) {
108 const { success, table } = getTableAndPlayer(req.query.session_id)
109
110 if (success && !table.started && !table.ended && table.players.filter(e=>e.isSatDown===true).length >= 2) {
111 table.players.forEach(player => {
112 axios.get(`${process.env.HOME_URL}/api/postgre/?action=check_if_logged_in&session_id=${player.id}`).then(postgreRes => {
113 if (postgreRes.data?.success) {
114 player.credits = postgreRes.data?.credits;
115 }
116 });
117 })
118
119 table.lastActivity = Date.now();
120 table.started = true;
121 table.round = 1;
122
123 table.turnIdx = Math.floor(Math.random(0, table.players.length))
124 setNextPlayerIdx(table.id);
125
126 table.players.forEach(player => {
127 if (player.isSatDown) {
128 for (let i = 0; i < 2; i++) {
129 const card = drawASingleCard(table.id);
130
131 if (card !== undefined) {
132 player.cards.push(card);
133 }
134 }
135 }
136 })
137 }
138
139 update_tables_to_database();
140
141 res.end();
142 }
143
144 /**
145 * /---------------------- GET ----------------------/
146 * Creates the table and enters the user inside
147 * @action sit_down
148 * @param session_id
149 * @param tableId
150 */
151 if (req.query.action === 'sit_down' && req.query?.session_id && req.query?.tableId) {
152 const { success, table, player } = getTableAndPlayer(req.query.session_id)
153
154 if (success && !table.started) {
155 player.isSatDown = true;
156 }
157
158 update_tables_to_database();
159
160 res.end();
161 }
162
163 /**
164 * /---------------------- GET ----------------------/
165 * Creates the table and enters the user inside
166 * @action leave_table
167 * @param session_id
168 */
169 if (req.query.action === 'leave_table' && req.query?.session_id) {
170 const { success, table, player } = getTableAndPlayer(req.query.session_id);
171
172 if (success) {
173 table.lastActivity = Date.now();
174
175 player.isGhost = true;
176 player.isFolded = true;
177
178 if (table.started) {
179 if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx] === player) {
180 setNextPlayerIdx(table.id);
181 }
182 }
183 else {
184 table.players = table.players.filter(e=>e.isGhost === false);
185 }
186 }
187
188 update_tables_to_database();
189
190 res.end();
191 }
192
193 /**
194 * /---------------------- GET ----------------------/
195 * Creates the table and enters the user inside
196 * @action join_a_table
197 * @param session_id
198 * @param tableId
199 * @param displayName
200 */
201 if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName) {
202 if (req.query.tableId.length > 0) {
203 const { success } = getTableAndPlayer(req.query.session_id);
204
205 if (!success) {
206 const table = getTable(req.query.tableId)
207
208 if (table !== undefined && !table.started) {
209 table.players.push({
210 id: req.query.session_id,
211 table: req.query.tableId,
212 credits: 0,
213 status: '_1_just_entered',
214 displayName: req.query.displayName,
215 cards: [],
216 betAmount: 0,
217 wonAmount: 0,
218 isSatDown: false,
219 isCoordinator: false,
220 isFolded: false,
221 isGhost: false,
222 hand: {
223 hand: '',
224 highCard: 0,
225 },
226 })
227 }
228 }
229 }
230
231 update_tables_to_database();
232
233 res.end();
234 }
235
236 /**
237 * /---------------------- GET ----------------------/
238 * Creates the table and enters the user inside
239 * @action create_a_table
240 * @param session_id
241 * @param displayName
242 * @param tableName
243 */
244 if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName) {
245 const { success } = getTableAndPlayer(req.query.session_id);
246
247 if (!success) {
248 createTable(req.query.session_id, req.query.displayName, req.query.tableName);
249 }
250
251 update_tables_to_database();
252
253 res.end();
254 }
255
256 /**
257 * /---------------------- GET ----------------------/
258 * Updates the state periodically
259 * @action update_state
260 * @param session_id
261 */
262 if (req.query.action === 'update_state' && req.query?.session_id) {
263 const session_id = req.query.session_id;
264
265 const { success, table, player } = getTableAndPlayer(session_id);
266
267 if (success && 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 table.lastActivity = Date.now();
275 setNextPlayerIdx(table.id);
276
277 update_tables_to_database();
278 }
279 }
280 }
281
282 res.json({
283 success: true,
284 pokerGame: {
285 tables: getRestrictedTablesArray(),
286 table: getRestrictedTableArray(table.id, req.query.session_id),
287 player: player,
288 }
289 })
290 }
291
292 /**
293 * /---------------------- GET ----------------------/
294 * If the player is not in an existing room, create a room for them.
295 * If they are reconnecting, get the room they were in.
296 * @action get_player_info_on_enter
297 * @param session_id
298 */
299 if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
300 const session_id = req.query.session_id;
301
302 axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
303 if (postgreRes.data?.success) {
304 res.json({
305 success: true,
306 displayName: postgreRes.data?.displayName,
307 session_id: postgreRes.data?.session_id,
308 credits: postgreRes.data?.credits,
309 })
310 }
311 else {
312 res.json({
313 success: false,
314 })
315 }
316 });
317 }
318 }
319}
320/**
321 * ********************* END OF REQUEST HANDLER *********************
322 */
Note: See TracBrowser for help on using the repository browser.