source: pages/api/blackjack/index.js@ ebf5e04

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

Code cleanings

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