1 | import axios from 'axios';
|
---|
2 |
|
---|
3 | require('dotenv').config();
|
---|
4 |
|
---|
5 | import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';
|
---|
6 | import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
|
---|
7 |
|
---|
8 | let rooms = []
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Set up a room
|
---|
12 | */
|
---|
13 | function 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 | */
|
---|
24 | export 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 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
258 | if (postgreRes.data?.success) {
|
---|
259 | const room = rooms[session_id];
|
---|
260 |
|
---|
261 | room.sideBet = parseInt(req.query.bet);
|
---|
262 | room.sideBetName = req.query.betName;
|
---|
263 | room.status = '_3_made_side_bet';
|
---|
264 |
|
---|
265 | rooms[session_id] = room;
|
---|
266 |
|
---|
267 | res.json({
|
---|
268 | success: true,
|
---|
269 | status: rooms[session_id].status,
|
---|
270 | credits: postgreRes.data?.credits,
|
---|
271 | })
|
---|
272 | }
|
---|
273 | });
|
---|
274 |
|
---|
275 | return ;
|
---|
276 | }
|
---|
277 |
|
---|
278 | res.json({
|
---|
279 | success: false,
|
---|
280 | })
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * /---------------------- GET ----------------------/
|
---|
285 | * If game status is _1_room_created, get the initial bet placed by the player.
|
---|
286 | * @action make_initial_bet
|
---|
287 | * @param session_id
|
---|
288 | * @param bet
|
---|
289 | */
|
---|
290 | if (req.query.action === 'make_initial_bet' && req.query?.session_id && req.query?.bet) {
|
---|
291 | const session_id = req.query.session_id;
|
---|
292 |
|
---|
293 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '1') {
|
---|
294 | if (parseInt(req.query.bet) <= 0) return ;
|
---|
295 |
|
---|
296 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
297 | if (postgreRes.data?.success) {
|
---|
298 | const room = rooms[session_id];
|
---|
299 |
|
---|
300 | room.initialBet = parseInt(req.query.bet);
|
---|
301 | room.status = '_2_made_initial_bet';
|
---|
302 |
|
---|
303 | rooms[session_id] = room;
|
---|
304 |
|
---|
305 | res.json({
|
---|
306 | success: true,
|
---|
307 | status: rooms[session_id].status,
|
---|
308 | credits: postgreRes.data?.credits,
|
---|
309 | })
|
---|
310 | }
|
---|
311 | else {
|
---|
312 | res.json({
|
---|
313 | success: false,
|
---|
314 | })
|
---|
315 | }
|
---|
316 | });
|
---|
317 |
|
---|
318 | return ;
|
---|
319 | }
|
---|
320 |
|
---|
321 | res.json({
|
---|
322 | success: false,
|
---|
323 | })
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * /---------------------- GET ----------------------/
|
---|
328 | * Remove a room from the rooms array.
|
---|
329 | * @action remove_room
|
---|
330 | * @param session_id
|
---|
331 | */
|
---|
332 | if (req.query.action === 'remove_room' && req.query?.session_id) {
|
---|
333 | const session_id = req.query.session_id;
|
---|
334 |
|
---|
335 | if (rooms[session_id] !== undefined) {
|
---|
336 | delete rooms[session_id];
|
---|
337 | }
|
---|
338 |
|
---|
339 | res.json({
|
---|
340 | success: true,
|
---|
341 | })
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * /---------------------- GET ----------------------/
|
---|
346 | * If the player is not in an existing room, create a room for them.
|
---|
347 | * If they are reconnecting, get the room they were in.
|
---|
348 | * @action get_player_info_on_enter
|
---|
349 | * @param session_id
|
---|
350 | */
|
---|
351 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
352 | const session_id = req.query.session_id;
|
---|
353 |
|
---|
354 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
355 | if (postgreRes.data?.success) {
|
---|
356 | if (rooms[session_id] !== undefined) {
|
---|
357 | // room exists
|
---|
358 | }
|
---|
359 | else {
|
---|
360 | createARoom(session_id);
|
---|
361 | }
|
---|
362 |
|
---|
363 | let dealerCardsTmp = [];
|
---|
364 | if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
|
---|
365 | rooms[session_id].dealerCards.forEach((card, i) => {
|
---|
366 | if (i === 0) {
|
---|
367 | dealerCardsTmp.push(card);
|
---|
368 | }
|
---|
369 | else {
|
---|
370 | dealerCardsTmp.push('back');
|
---|
371 | }
|
---|
372 | })
|
---|
373 | }
|
---|
374 | else {
|
---|
375 | dealerCardsTmp = rooms[session_id].dealerCards;
|
---|
376 | }
|
---|
377 |
|
---|
378 | res.json({
|
---|
379 | success: true,
|
---|
380 | status: rooms[session_id].status,
|
---|
381 | initialBet: rooms[session_id].initialBet,
|
---|
382 | sideBet: rooms[session_id].sideBet,
|
---|
383 | sideBetName: rooms[session_id].sideBetName,
|
---|
384 | playerCards: rooms[session_id].playerCards,
|
---|
385 | dealerCards: dealerCardsTmp,
|
---|
386 | outcome: rooms[session_id].outcome,
|
---|
387 | earnings: rooms[session_id].earnings,
|
---|
388 | displayName: postgreRes.data?.displayName,
|
---|
389 | session_id: postgreRes.data?.session_id,
|
---|
390 | credits: postgreRes.data?.credits,
|
---|
391 | })
|
---|
392 | }
|
---|
393 | else {
|
---|
394 | res.json({
|
---|
395 | success: false,
|
---|
396 | })
|
---|
397 | }
|
---|
398 | });
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 | /**
|
---|
403 | * ********************* END OF REQUEST HANDLER *********************
|
---|
404 | */
|
---|