source: pages/api/roulette/index.js@ aac3b2b

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

Made poker tables system and round 1

  • Property mode set to 100644
File size: 9.1 KB
Line 
1import axios from 'axios';
2
3require('dotenv').config();
4
5function getWinningBets(magicNumber) {
6 let winningBets = [];
7 winningBets.push(magicNumber);
8
9 if (magicNumber != 0) {
10 if ((magicNumber <= 9 && magicNumber % 2 == 1) || (magicNumber > 10 && magicNumber <= 18 && magicNumber % 2 == 0) ||
11 (magicNumber > 19 && magicNumber <= 27 && magicNumber % 2 == 1) || (magicNumber > 27 && magicNumber % 2 == 0)) {
12
13 winningBets.push('Red');
14 }
15 else {
16 winningBets.push('Black');
17 }
18
19 if (magicNumber % 2 === 0) winningBets.push('Even');
20 else winningBets.push('Odd');
21
22 if (magicNumber <= 12) winningBets.push('1-12');
23 else if (magicNumber <= 24) winningBets.push('13-24');
24 else winningBets.push('25-36');
25
26 if (magicNumber <= 18) winningBets.push('1-18');
27 else winningBets.push('19-36');
28
29 if (magicNumber % 3 === 0) winningBets.push('Remainder3');
30 else if (magicNumber % 3 === 2) winningBets.push('Remainder2');
31 else winningBets.push('Remainder1');
32 }
33
34 return winningBets;
35}
36
37function updateGameWithWinners() {
38 const winningBets = getWinningBets(magicNumber);
39
40 for (let i = 0; i < game.players.length; i++) {
41 const player = game.players[i];
42
43 let playerWon = false;
44 player.whichBets.forEach(bet => {
45 if (winningBets.indexOf(bet) !== -1) {
46 playerWon = true;
47 }
48 })
49
50 if (playerWon) {
51 game.players[i].outcome = 'won';
52 }
53 else {
54 game.players[i].outcome = 'lost';
55 }
56 }
57}
58
59function resetPlayers() {
60 game.players.forEach(player => {
61 player.whichBets = [];
62 player.betAmount = 0;
63 player.outcome = 'none';
64 player.status = '_1_no_placed_bet';
65 })
66}
67
68function calculateWinnings(player) {
69 if (player.outcome === 'lost') return 0;
70
71 let bets = player.whichBets;
72 let bet = player.betAmount;
73
74 if (bets[0] === 'Even' || bets[0] === 'Odd') return 2 * bet;
75 else if (bets[0] === 'Red' || bets[0] === 'Black') return 2 * bet;
76 else if (bets[0].contains('Remainder')) return 3 * bet;
77 else if (bets[0] === '1-12' || bets[0] === '13-24' || bets[0] === '25-36') return 3 * bet;
78 else if (bets[0] === '1-18' || bets[0] === '19-36') return 2 * bet;
79 else if (bets.length === 4) return 9 * bet;
80 else if (bets.length === 2) return 18 * bet;
81 else if (bets.length === 1) return 36 * bet;
82
83 return 0;
84}
85
86const COUNTDOWN_FROM = 30;
87const WAIT_BEFORE = 20;
88
89let magicNumber = -1;
90
91(function() {
92 setInterval(() => {
93 game.timeToStart--;
94
95 // WAIT_BEFORE seconds is the time allocated for spinning the wheel and seeing the results.
96 if (game.timeToStart == 0) {
97 game.timeToStart = COUNTDOWN_FROM + WAIT_BEFORE;
98 }
99 else if (game.timeToStart == 10) {
100 magicNumber = Math.floor(Math.random() * 37);
101 game.status = '_2_spinning';
102 }
103 else if (game.timeToStart == COUNTDOWN_FROM) {
104 game.status = '_1_ongoing_timer';
105 }
106 else if (game.timeToStart == COUNTDOWN_FROM + 5) {
107 resetPlayers();
108 }
109
110 }, 1000);
111})();
112
113let game = {
114 status: '_1_ongoing_timer', // statuses: _1_ongoing_timer, _2_spinning,
115 timeToStart: COUNTDOWN_FROM, // in seconds
116 players: [] , // example player -> { session_id, name, whichBet, betAmount, status, outcome } // statuses: _1_no_placed_bet, _2_placed_bet
117}
118
119function addPlayer(session_id, name) {
120 if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
121 game.players.push({
122 session_id: session_id,
123 name: name,
124 whichBets: [],
125 betAmount: 0,
126 status: '_1_no_placed_bet',
127 outcome: 'none',
128 })
129 }
130}
131
132/**
133 * ********************* BEGIN OF REQUEST HANDLER *********************
134 */
135export default async function handler(req, res) {
136 /**
137 * GET method
138 */
139 if (req.method === 'GET') {
140 /**
141 * /---------------------- GET ----------------------/
142 * Return to the user info for starting a new game.
143 * @action reset_game
144 * @param session_id
145 */
146 if (req.query.action === 'reset_game' && req.query?.session_id) {
147 const session_id = req.query.session_id;
148 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
149
150 if (playerIdx !== -1) {
151 res.json({
152 success: true,
153 game: game,
154 })
155 }
156 else {
157 res.json({
158 success: false,
159 })
160 }
161 }
162
163 /**
164 * /---------------------- GET ----------------------/
165 * Timer done on client side.
166 * @action timer_done
167 * @param session_id
168 */
169 if (req.query.action === 'timer_done' && req.query?.session_id) {
170 const session_id = req.query.session_id;
171 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
172
173 if (playerIdx !== -1 && game.status.substr(1, 1) === '2') {
174 updateGameWithWinners();
175
176 const playerWinnings = calculateWinnings(game.players[playerIdx]);
177
178 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${playerWinnings}&game=roulette&outcome=${game.players[playerIdx].outcome}`).then(postgreRes => {
179 if (postgreRes.data?.success) {
180 res.json({
181 success: true,
182 game: game,
183 magicNumber: magicNumber,
184 winningBets: getWinningBets(magicNumber),
185 credits: postgreRes.data?.credits,
186 })
187 }
188 else {
189 res.json({
190 success: false,
191 })
192 }
193 });
194 }
195 else {
196 res.json({
197 success: false,
198 })
199 }
200 }
201
202 /**
203 * /---------------------- GET ----------------------/
204 * Place a bet.
205 * @action place_bet
206 * @param session_id
207 * @param betAmount
208 * @param whichBets
209 */
210 if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets) {
211 const session_id = req.query.session_id;
212 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
213
214 if (playerIdx !== -1 && game.status.substr(1, 1) === '1' && game.players[playerIdx].status.substr(1, 1) === '1') {
215 game.players[playerIdx].betAmount = parseInt(req.query.betAmount);
216 game.players[playerIdx].whichBets = req.query.whichBets.split(',');
217 game.players[playerIdx].status = '_2_placed_bet';
218
219 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.betAmount}`).then(postgreRes => {
220 if (postgreRes.data?.success) {
221 res.json({
222 success: true,
223 game: game,
224 credits: postgreRes.data?.credits,
225 })
226 }
227 else {
228 res.json({
229 success: false,
230 })
231 }
232 });
233 }
234 }
235
236 /**
237 * /---------------------- GET ----------------------/
238 * If the player is not in an existing room, create a room for them.
239 * If they are reconnecting, get the room they were in.
240 * @action get_player_info_on_enter
241 * @param session_id
242 */
243 if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
244 const session_id = req.query.session_id;
245
246 axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
247 if (postgreRes.data?.success) {
248 addPlayer(session_id, postgreRes.data?.displayName);
249
250 res.json({
251 success: true,
252 game: game,
253 displayName: postgreRes.data?.displayName,
254 session_id: postgreRes.data?.session_id,
255 credits: postgreRes.data?.credits,
256 })
257 }
258 else {
259 res.json({
260 success: false,
261 })
262 }
263 });
264 }
265 }
266}
267/**
268 * ********************* END OF REQUEST HANDLER *********************
269 */
Note: See TracBrowser for help on using the repository browser.