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

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

Added complaints, managing credits, and lost connection screens

  • Property mode set to 100644
File size: 13.0 KB
Line 
1import axios from 'axios';
2
3require('dotenv').config();
4
5import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';
6import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
7
8import { rooms, update_rooms_to_database } from '../postgre/index'
9
10/**
11 * Set up a room
12 */
13function createARoom(session_id) {
14 let room = {
15 ...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards],
16 }
17
18 rooms[session_id] = room;
19}
20
21/**
22 * ********************* BEGIN OF REQUEST HANDLER *********************
23 */
24export default async function handler(req, res) {
25 /**
26 * GET method
27 */
28 if (req.method === 'GET') {
29 /**
30 * /---------------------- GET ----------------------/
31 * If game status is _5_game_over, restart the room for a new game.
32 * @action play_again
33 * @param session_id
34 */
35 if (req.query.action === 'play_again' && req.query?.session_id) {
36 const session_id = req.query.session_id;
37
38 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '5') {
39 rooms[session_id] = {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
40
41 res.json({
42 success: true,
43 game: rooms[session_id],
44 })
45
46 update_rooms_to_database();
47
48 return ;
49 }
50
51 res.json({
52 success: false,
53 })
54 }
55
56 /**
57 * /---------------------- GET ----------------------/
58 * If game status is _4_cards_on_the_table, draw cards for the dealer while handValue < 17, and calculate game outcome and player earnings.
59 * Also, update the player's credits and stats in the database through /api/postgre?action=add_credits.
60 * @action stand
61 * @param session_id
62 */
63 if (req.query.action === 'stand' && req.query?.session_id) {
64 const session_id = req.query.session_id;
65
66 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
67 const room = rooms[session_id];
68
69 while (calculateHandValue(room.dealerCards) < 17) {
70 room.dealerCards.push(drawASingleCard(room));
71 }
72
73 room.status = '_5_game_over';
74
75 if (calculateHandValue(room.dealerCards) > 21) {
76 room.outcome = 'dealer_busted';
77 }
78 else if (calculateHandValue(room.playerCards) > calculateHandValue(room.dealerCards)) {
79 room.outcome = 'player_won';
80 }
81 else if (calculateHandValue(room.playerCards) < calculateHandValue(room.dealerCards)) {
82 room.outcome = 'player_lost';
83 }
84 else {
85 room.outcome = 'draw';
86 }
87
88 room.earnings = calculateEarnings(room);
89
90 rooms[session_id] = room;
91
92 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.earnings}&game=blackjack&outcome=${room.outcome}`).then(postgreRes => {
93 if (postgreRes.data?.success) {
94 res.json({
95 success: true,
96 status: rooms[session_id].status,
97 playerCards: rooms[session_id].playerCards,
98 dealerCards: rooms[session_id].dealerCards,
99 outcome: rooms[session_id].outcome,
100 earnings: rooms[session_id].earnings,
101 credits: postgreRes.data?.credits,
102 })
103
104 update_rooms_to_database();
105 }
106 else {
107 res.json({
108 success: false,
109 })
110 }
111 });
112
113 return ;
114 }
115
116 res.json({
117 success: false,
118 })
119 }
120
121 /**
122 * /---------------------- GET ----------------------/
123 * If game status is _4_cards_on_the_table, draw a card for the player.
124 * If player busts, update the player's stats in the database through /api/postgre?action=add_credits.
125 * @action hit_a_card
126 * @param session_id
127 */
128 if (req.query.action === 'hit_a_card' && req.query?.session_id) {
129 const session_id = req.query.session_id;
130
131 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
132 const room = rooms[session_id];
133
134 room.playerCards.push(drawASingleCard(room));
135
136 rooms[session_id] = room;
137
138 if (calculateHandValue(room.playerCards) > 21) {
139 room.status = '_5_game_over';
140 room.outcome = 'player_busted';
141
142 room.earnings = calculateEarnings(room);
143
144 rooms[session_id] = room;
145
146 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.earnings}&game=blackjack&outcome=${room.outcome}`).then(postgreRes => {
147 if (postgreRes.data?.success) {
148 res.json({
149 success: true,
150 status: rooms[session_id].status,
151 playerCards: rooms[session_id].playerCards,
152 outcome: rooms[session_id].outcome,
153 earnings: rooms[session_id].earnings,
154 credits: postgreRes.data?.credits,
155 })
156
157 update_rooms_to_database();
158 }
159 else {
160 res.json({
161 success: false,
162 })
163 }
164 });
165 }
166 else {
167 res.json({
168 success: true,
169 status: rooms[session_id].status,
170 playerCards: rooms[session_id].playerCards,
171 outcome: rooms[session_id].outcome,
172 earnings: rooms[session_id].earnings,
173 })
174
175 update_rooms_to_database();
176 }
177
178 return ;
179 }
180
181 res.json({
182 success: false,
183 })
184 }
185
186 /**
187 * /---------------------- GET ----------------------/
188 * If game status is _3_made_side_bet, check if the player won the side bet or not (if they placed a side bet of course).
189 * Update the player's stats in the database through /api/postgre?action=add_credits.
190 * @action get_initial_cards
191 * @param session_id
192 */
193 if (req.query.action === 'get_initial_cards' && req.query?.session_id) {
194 const session_id = req.query.session_id;
195
196 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '3') {
197 const room = rooms[session_id];
198
199 getInitialCards(room);
200
201 room.status = '_4_cards_on_the_table';
202
203 rooms[session_id] = room;
204
205 if (room.sideBetName !== '' && room.sideBetName !== 'none') {
206 room.sideBetEarnings = calculateSideBetEarnings(room);
207 room.sideBetOutcome = room.sideBetEarnings > 0 ? 'side_bet_won' : 'side_bet_lost';
208
209 rooms[session_id] = room;
210
211 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.sideBetEarnings}`).then(postgreRes => {
212 if (postgreRes.data?.success) {
213 res.json({
214 success: true,
215 status: rooms[session_id].status,
216 playerCards: rooms[session_id].playerCards,
217 dealerCards: Array(rooms[session_id].dealerCards[0]).concat('back'),
218 sideBetOutcome: rooms[session_id].sideBetOutcome,
219 sideBetEarnings: rooms[session_id].sideBetEarnings,
220 credits: postgreRes.data?.credits,
221 })
222
223 update_rooms_to_database();
224 }
225 else {
226 res.json({
227 success: false,
228 })
229 }
230 });
231 }
232 else {
233 res.json({
234 success: true,
235 status: rooms[session_id].status,
236 playerCards: rooms[session_id].playerCards,
237 dealerCards: Array(rooms[session_id].dealerCards[0]).concat('back'),
238 sideBetOutcome: rooms[session_id].sideBetOutcome,
239 sideBetEarnings: rooms[session_id].sideBetEarnings,
240 })
241
242 update_rooms_to_database();
243 }
244
245 return ;
246 }
247
248 res.json({
249 success: false,
250 })
251 }
252
253 /**
254 * /---------------------- GET ----------------------/
255 * If game status is _2_made_initial_bet, place a side bet if the user has chosen one.
256 * @action make_side_bet
257 * @param session_id
258 * @param bet
259 * @param betName
260 */
261 if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) {
262 const session_id = req.query.session_id;
263
264 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '2') {
265 if (req.query?.skip !== 'true' && parseInt(req.query.bet) <= 0) {
266 return ;
267 }
268
269 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
270 if (postgreRes.data?.success) {
271 const room = rooms[session_id];
272
273 room.sideBet = parseInt(req.query.bet);
274 room.sideBetName = req.query.betName;
275 room.status = '_3_made_side_bet';
276
277 rooms[session_id] = room;
278
279 res.json({
280 success: true,
281 status: rooms[session_id].status,
282 credits: postgreRes.data?.credits,
283 })
284
285 update_rooms_to_database();
286 }
287 });
288
289 return ;
290 }
291
292 res.json({
293 success: false,
294 })
295 }
296
297 /**
298 * /---------------------- GET ----------------------/
299 * If game status is _1_room_created, get the initial bet placed by the player.
300 * @action make_initial_bet
301 * @param session_id
302 * @param bet
303 */
304 if (req.query.action === 'make_initial_bet' && req.query?.session_id && req.query?.bet) {
305 const session_id = req.query.session_id;
306
307 if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '1') {
308 if (parseInt(req.query.bet) <= 0) return ;
309
310 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
311 if (postgreRes.data?.success) {
312 const room = rooms[session_id];
313
314 room.initialBet = parseInt(req.query.bet);
315 room.status = '_2_made_initial_bet';
316
317 rooms[session_id] = room;
318
319 res.json({
320 success: true,
321 status: rooms[session_id].status,
322 credits: postgreRes.data?.credits,
323 })
324
325 update_rooms_to_database();
326 }
327 else {
328 res.json({
329 success: false,
330 })
331 }
332 });
333
334 return ;
335 }
336
337 res.json({
338 success: false,
339 })
340 }
341
342 /**
343 * /---------------------- GET ----------------------/
344 * Remove a room from the rooms array.
345 * @action remove_room
346 * @param session_id
347 */
348 if (req.query.action === 'remove_room' && req.query?.session_id) {
349 const session_id = req.query.session_id;
350
351 if (rooms[session_id] !== undefined) {
352 delete rooms[session_id];
353 }
354
355 res.json({
356 success: true,
357 })
358
359 update_rooms_to_database();
360 }
361
362 /**
363 * /---------------------- GET ----------------------/
364 * If the player is not in an existing room, create a room for them.
365 * If they are reconnecting, get the room they were in.
366 * @action get_player_info_on_enter
367 * @param session_id
368 */
369 if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
370 const session_id = req.query.session_id;
371
372 axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
373 if (postgreRes.data?.success) {
374 if (rooms[session_id] !== undefined) {
375 // room exists
376 }
377 else {
378 createARoom(session_id);
379 }
380
381 let dealerCardsTmp = [];
382 if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
383 rooms[session_id].dealerCards.forEach((card, i) => {
384 if (i === 0) {
385 dealerCardsTmp.push(card);
386 }
387 else {
388 dealerCardsTmp.push('back');
389 }
390 })
391 }
392 else {
393 dealerCardsTmp = rooms[session_id].dealerCards;
394 }
395
396 res.json({
397 success: true,
398 status: rooms[session_id].status,
399 initialBet: rooms[session_id].initialBet,
400 sideBet: rooms[session_id].sideBet,
401 sideBetName: rooms[session_id].sideBetName,
402 playerCards: rooms[session_id].playerCards,
403 dealerCards: dealerCardsTmp,
404 outcome: rooms[session_id].outcome,
405 earnings: rooms[session_id].earnings,
406 displayName: postgreRes.data?.displayName,
407 session_id: postgreRes.data?.session_id,
408 credits: postgreRes.data?.credits,
409 })
410
411 update_rooms_to_database();
412 }
413 else {
414 res.json({
415 success: false,
416 })
417 }
418 });
419 }
420 }
421}
422/**
423 * ********************* END OF REQUEST HANDLER *********************
424 */
Note: See TracBrowser for help on using the repository browser.