- Timestamp:
- 06/25/22 11:30:19 (2 years ago)
- Branches:
- main
- Children:
- b13f93b
- Parents:
- 9bd09b0
- Location:
- pages/api
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/postgre/index.js
r9bd09b0 race7865 66 66 else if (req.query?.outcome === 'dealer_busted' || req.query?.outcome === 'player_won') { 67 67 pool.query('UPDATE stats SET blackjack_games = $1, blackjack_won_games = $2 WHERE username = $3', [parseInt(stats.blackjack_games) + 1, parseInt(stats.blackjack_won_games) + 1, session.username], (error, results) => { 68 if (error) throw error; 69 }); 70 } 71 } 72 else if (req.query?.game === 'roulette') { 73 if (req.query?.outcome === 'lost') { 74 pool.query('UPDATE stats SET roulette_games = $1 WHERE username = $2', [parseInt(stats.roulette_games) + 1, session.username], (error, results) => { 75 if (error) throw error; 76 }); 77 } 78 else if (req.query?.outcome === 'won') { 79 pool.query('UPDATE stats SET roulette_games = $1, roulette_won_games = $2 WHERE username = $3', [parseInt(stats.roulette_games) + 1, parseInt(stats.roulette_won_games) + 1, session.username], (error, results) => { 68 80 if (error) throw error; 69 81 }); -
pages/api/roulette/index.js
r9bd09b0 race7865 3 3 require('dotenv').config(); 4 4 5 const COUNTDOWN_FROM = 90; 6 const WAIT_BEFORE = 30; 5 function 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 37 function 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 59 function 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 68 function 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 86 const COUNTDOWN_FROM = 30; 87 const WAIT_BEFORE = 20; 88 89 let magicNumber = -1; 7 90 8 91 (function() { 9 92 setInterval(() => { 10 11 93 game.timeToStart--; 12 94 13 // 30 seconds is the time to spin and seethe results.95 // WAIT_BEFORE seconds is the time allocated for spinning the wheel and seeing the results. 14 96 if (game.timeToStart == 0) { 15 97 game.timeToStart = COUNTDOWN_FROM + WAIT_BEFORE; 16 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 } 17 109 18 110 }, 1000); … … 20 112 21 113 let game = { 22 status: '_1_ongoing_timer', // statuses: _1_ongoing_timer, _2_spinning, _3_results114 status: '_1_ongoing_timer', // statuses: _1_ongoing_timer, _2_spinning, 23 115 timeToStart: COUNTDOWN_FROM, // in seconds 24 players: [] , // example player -> { session_id, name, whichBet, betAmount, status } // statuses: _1_no_placed_bet, _2_placed_bet116 players: [] , // example player -> { session_id, name, whichBet, betAmount, status, outcome } // statuses: _1_no_placed_bet, _2_placed_bet 25 117 } 26 118 … … 33 125 betAmount: 0, 34 126 status: '_1_no_placed_bet', 127 outcome: 'none', 35 128 }) 36 129 } … … 45 138 */ 46 139 if (req.method === 'GET') { 140 141 /** 142 * /---------------------- GET ----------------------/ 143 * Return to the user info for starting a new game. 144 * @action reset_game 145 * @param session_id 146 */ 147 if (req.query.action === 'reset_game' && req.query?.session_id) { 148 const session_id = req.query.session_id; 149 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id); 150 151 if (playerIdx !== -1) { 152 res.json({ 153 success: true, 154 game: game, 155 }) 156 } 157 else { 158 res.json({ 159 success: false, 160 }) 161 } 162 } 163 164 /** 165 * /---------------------- GET ----------------------/ 166 * Timer done on client side. 167 * @action timer_done 168 * @param session_id 169 */ 170 if (req.query.action === 'timer_done' && req.query?.session_id) { 171 const session_id = req.query.session_id; 172 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id); 173 174 if (playerIdx !== -1 && game.status.substr(1, 1) === '2') { 175 updateGameWithWinners(); 176 177 const playerWinnings = calculateWinnings(game.players[playerIdx]); 178 179 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 => { 180 if (postgreRes.data?.success) { 181 res.json({ 182 success: true, 183 game: game, 184 magicNumber: magicNumber, 185 winningBets: getWinningBets(magicNumber), 186 credits: postgreRes.data?.credits, 187 }) 188 } 189 else { 190 res.json({ 191 success: false, 192 }) 193 } 194 }); 195 } 196 else { 197 res.json({ 198 success: false, 199 }) 200 } 201 } 202 47 203 /** 48 204 * /---------------------- GET ----------------------/ … … 57 213 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id); 58 214 59 if (playerIdx !== -1 && game. players[playerIdx].status.substr(1, 1) === '1') {215 if (playerIdx !== -1 && game.status.substr(1, 1) === '1' && game.players[playerIdx].status.substr(1, 1) === '1') { 60 216 game.players[playerIdx].betAmount = parseInt(req.query.betAmount); 61 217 game.players[playerIdx].whichBets = req.query.whichBets.split(',');
Note:
See TracChangeset
for help on using the changeset viewer.