- Timestamp:
- 07/15/22 14:45:30 (2 years ago)
- Branches:
- main
- Children:
- 55701f0
- Parents:
- 1df3fde
- Location:
- pages
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/blackjack/index.js
r1df3fde r433e0c5 6 6 import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings'; 7 7 8 let rooms = [] 8 import { rooms, update_rooms_to_database } from '../postgre/index' 9 9 10 10 /** … … 43 43 game: rooms[session_id], 44 44 }) 45 46 update_rooms_to_database(); 45 47 46 48 return ; … … 99 101 credits: postgreRes.data?.credits, 100 102 }) 103 104 update_rooms_to_database(); 101 105 } 102 106 else { … … 150 154 credits: postgreRes.data?.credits, 151 155 }) 156 157 update_rooms_to_database(); 152 158 } 153 159 else { … … 166 172 earnings: rooms[session_id].earnings, 167 173 }) 174 175 update_rooms_to_database(); 168 176 } 169 177 … … 212 220 credits: postgreRes.data?.credits, 213 221 }) 222 223 update_rooms_to_database(); 214 224 } 215 225 else { … … 229 239 sideBetEarnings: rooms[session_id].sideBetEarnings, 230 240 }) 241 242 update_rooms_to_database(); 231 243 } 232 244 … … 270 282 credits: postgreRes.data?.credits, 271 283 }) 284 285 update_rooms_to_database(); 272 286 } 273 287 }); … … 308 322 credits: postgreRes.data?.credits, 309 323 }) 324 325 update_rooms_to_database(); 310 326 } 311 327 else { … … 340 356 success: true, 341 357 }) 358 359 update_rooms_to_database(); 342 360 } 343 361 … … 390 408 credits: postgreRes.data?.credits, 391 409 }) 410 411 update_rooms_to_database(); 392 412 } 393 413 else { -
pages/api/postgre/index.js
r1df3fde r433e0c5 275 275 /** 276 276 * /---------------------- POST ----------------------/ 277 * Deposits money from credit card to game account. 278 * @action register 279 * @param session_id 280 * @param data 281 */ 282 if (body?.action === 'deposit') { 283 // checks 284 if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") { 285 res.json({ 286 success: false, 287 message: 'You are not logged in. Please log in first.', 288 }); 289 return ; 290 } 291 if (body?.data?.name == "undefined" || body?.data?.name == "null" || body?.data?.name == "") { 292 res.json({ 293 success: false, 294 message: 'Name field cannot be empty', 295 }); 296 return ; 297 } 298 if (body?.data?.card == "undefined" || body?.data?.card == "null" || body?.data?.card == "") { 299 res.json({ 300 success: false, 301 message: 'Card numbers field cannot be empty', 302 }); 303 return ; 304 } 305 if (body?.data?.expire == "undefined" || body?.data?.expire == "null" || body?.data?.expire == "") { 306 res.json({ 307 success: false, 308 message: 'Expiration date field cannot be empty', 309 }); 310 return ; 311 } 312 if (body?.data?.ccv == "undefined" || body?.data?.ccv == "null" || body?.data?.ccv == "") { 313 res.json({ 314 success: false, 315 message: 'CCV field cannot be empty', 316 }); 317 return ; 318 } 319 if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") { 320 res.json({ 321 success: false, 322 message: 'Amount field cannot be empty', 323 }); 324 return ; 325 } 326 327 let session = sessions.find(session => session.id === body?.session_id) 328 329 if (session) { 330 if (parseInt(body.data.amount) > 0) { 331 session.credits = session.credits + parseInt(body.data.amount) 332 333 pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => { 334 if (error) throw error; 335 336 res.json({ 337 success: true, 338 credits: session.credits 339 }) 340 }); 341 } 342 } 343 } 344 345 /** 346 * /---------------------- POST ----------------------/ 347 * Withdraws money from game account to personal account. 348 * @action register 349 * @param session_id 350 * @param data 351 */ 352 if (body?.action === 'withdraw') { 353 // checks 354 if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") { 355 res.json({ 356 success: false, 357 message: 'You are not logged in. Please log in first.', 358 }); 359 return ; 360 } 361 if (body?.data?.citibank == "undefined" || body?.data?.citibank == "null" || body?.data?.citibank == "") { 362 res.json({ 363 success: false, 364 message: 'Bank name field cannot be empty', 365 }); 366 return ; 367 } 368 if (body?.data?.iban == "undefined" || body?.data?.iban == "null" || body?.data?.iban == "") { 369 res.json({ 370 success: false, 371 message: 'IBAN code field cannot be empty', 372 }); 373 return ; 374 } 375 if (body?.data?.bic == "undefined" || body?.data?.bic == "null" || body?.data?.bic == "") { 376 res.json({ 377 success: false, 378 message: 'BIC code field cannot be empty', 379 }); 380 return ; 381 } 382 if (body?.data?.beneficiary == "undefined" || body?.data?.beneficiary == "null" || body?.data?.beneficiary == "") { 383 res.json({ 384 success: false, 385 message: 'Beneficiary name field cannot be empty', 386 }); 387 return ; 388 } 389 if (body?.data?.address == "undefined" || body?.data?.address == "null" || body?.data?.address == "") { 390 res.json({ 391 success: false, 392 message: 'Bank address field cannot be empty', 393 }); 394 return ; 395 } 396 if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") { 397 res.json({ 398 success: false, 399 message: 'Amount field cannot be empty', 400 }); 401 return ; 402 } 403 404 let session = sessions.find(session => session.id === body?.session_id) 405 406 if (session) { 407 if (parseInt(body.data.amount) > 0) { 408 session.credits = Math.max(session.credits - parseInt(body.data.amount), 0) 409 410 pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => { 411 if (error) throw error; 412 413 res.json({ 414 success: true, 415 credits: session.credits 416 }) 417 }); 418 } 419 } 420 } 421 422 /** 423 * /---------------------- POST ----------------------/ 424 * Sends a complaint. 425 * @action complain 426 * @param session_id 427 * @param description 428 */ 429 if (body?.action === 'complain') { 430 // checks 431 if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") { 432 res.json({ 433 success: false, 434 message: 'You are not logged in. Please log in first.', 435 }); 436 return ; 437 } 438 if (body?.description == "undefined" || body?.description == "null" || body?.description == "") { 439 res.json({ 440 success: false, 441 message: 'You cannot submit an empty complaint.', 442 }); 443 return ; 444 } 445 446 let session = sessions.find(session => session.id === body.session_id) 447 448 if (session) { 449 // date, by, description, answered 450 const date = new Date(); 451 pool.query('INSERT INTO complaints (date, by, description, answered) VALUES ($1, $2, $3, $4)', [date, session.username, body.description, false], (error, complaintResults) => { 452 if (error) throw error; 453 454 res.json({ 455 success: true, 456 }) 457 }); 458 } 459 } 460 461 /** 462 * /---------------------- POST ----------------------/ 277 463 * Checks if the entered account info is good, and registers a new user in the database if so. 278 464 * @action register … … 539 725 } 540 726 load_game_from_database(); 541 727 728 /** 729 * Blackjack game data 730 */ 731 export var rooms = [] 732 733 export function update_rooms_to_database() { 734 let tmpRooms = []; 735 736 for (let key in rooms) { 737 if (key === "loaded") continue ; 738 739 tmpRooms.push(rooms[key]); 740 tmpRooms[tmpRooms.length - 1].id = key; 741 } 742 743 pool.query('UPDATE blackjack SET data = $1 WHERE identifier = $2', [JSON.stringify(tmpRooms), 'blackjack_data'], (error, results) => { 744 if (error) throw error; 745 }); 746 } 747 748 export async function load_rooms_from_database() { 749 pool.query('SELECT data FROM blackjack WHERE identifier = $1', ['blackjack_data'], (error, results) => { 750 if (error) throw error; 751 752 if (results?.rows[0]?.data) { 753 const tmpRooms = JSON.parse(results.rows[0].data); 754 755 tmpRooms.forEach(room => { 756 rooms[room.id] = {...room, id: ''} 757 }) 758 759 rooms["loaded"] = true; 760 } 761 }); 762 } 763 load_rooms_from_database(); 764 -
pages/api/roulette/index.js
r1df3fde r433e0c5 5 5 require('dotenv').config(); 6 6 7 let samplePlayer = { 8 session_id: '', 9 name: '', 10 whichBets: [], 11 coinPlaced: { 12 x: -1, 13 y: -1, 14 }, 15 credits: -1, 16 betAmount: 0, 17 wonAmount: 0, 18 status: '_1_', 19 outcome: 'none', 20 gotResults: false, 21 } 7 import { resetGame, updateGameWithWinners, addPlayer, getPlayer, restrictGameInfo } from './gameStates' 22 8 23 function getWinningBets(magicNumber) { 24 let winningBets = []; 25 winningBets.push(magicNumber); 26 27 if (magicNumber != 0) { 28 if ((magicNumber <= 9 && magicNumber % 2 == 1) || (magicNumber > 10 && magicNumber <= 18 && magicNumber % 2 == 0) || 29 (magicNumber > 19 && magicNumber <= 27 && magicNumber % 2 == 1) || (magicNumber > 27 && magicNumber % 2 == 0)) { 30 31 winningBets.push('Red'); 32 } 33 else { 34 winningBets.push('Black'); 35 } 36 37 if (magicNumber % 2 === 0) winningBets.push('Even'); 38 else winningBets.push('Odd'); 39 40 if (magicNumber <= 12) winningBets.push('1-12'); 41 else if (magicNumber <= 24) winningBets.push('13-24'); 42 else winningBets.push('25-36'); 43 44 if (magicNumber <= 18) winningBets.push('1-18'); 45 else winningBets.push('19-36'); 46 47 if (magicNumber % 3 === 0) winningBets.push('Remainder3'); 48 else if (magicNumber % 3 === 2) winningBets.push('Remainder2'); 49 else winningBets.push('Remainder1'); 50 } 51 52 return winningBets; 53 } 54 55 function updateGameWithWinners() { 56 for (let i = 0; i < game.players.length; i++) { 57 const player = game.players[i]; 58 59 let playerWon = false; 60 player.whichBets.forEach(bet => { 61 if (game.winningBets.indexOf(bet) !== -1) { 62 playerWon = true; 63 } 64 }) 65 66 if (playerWon) { 67 player.outcome = 'won'; 68 } 69 else { 70 player.outcome = 'lost'; 71 } 72 73 player.wonAmount = calculateWinnings(player); 74 75 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.session_id}&credits=${player.wonAmount}&game=roulette&outcome=${player.outcome}`).then(postgreRes => { 76 if (postgreRes.data?.success) { 77 player.credits = postgreRes.data?.credits; 78 } 79 }); 80 } 81 } 82 83 function resetGame() { 84 game.magicNumber = -1; 85 game.winningBets = []; 86 game.status = '_1_ongoing_timer'; 87 88 game.players.forEach(player => { 89 player.whichBets = []; 90 player.betAmount = 0; 91 player.wonAmount = 0; 92 player.coinPlaced = { 93 x: -1, 94 y: -1, 95 }, 96 player.outcome = 'none'; 97 player.status = '_1_no_placed_bet'; 98 player.gotResults = false; 99 }) 100 } 101 102 function calculateWinnings(player) { 103 if (player.outcome === 'lost') return 0; 104 105 let bets = player.whichBets; 106 let bet = player.betAmount; 107 108 if (bets[0] === 'Even' || bets[0] === 'Odd') return 2 * bet; 109 else if (bets[0] === 'Red' || bets[0] === 'Black') return 2 * bet; 110 else if (bets[0].includes('Remainder')) return 3 * bet; 111 else if (bets[0] === '1-12' || bets[0] === '13-24' || bets[0] === '25-36') return 3 * bet; 112 else if (bets[0] === '1-18' || bets[0] === '19-36') return 2 * bet; 113 else if (bets.length === 4) return 9 * bet; 114 else if (bets.length === 2) return 18 * bet; 115 else if (bets.length === 1) return 36 * bet; 116 117 return 0; 118 } 9 import { getWinningBets } from './calculateWinnings' 119 10 120 11 (function() { … … 151 42 game.winningBets = []; 152 43 game.players = []; 153 }154 155 function addPlayer(session_id, name) {156 if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {157 game.players.push({158 session_id: session_id,159 name: name,160 whichBets: [],161 coinPlaced: {162 x: -1,163 y: -1,164 },165 credits: -1,166 betAmount: 0,167 wonAmount: 0,168 status: '_1_no_placed_bet',169 outcome: 'none',170 gotResults: false,171 })172 }173 }174 175 export function getPlayer(session_id) {176 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);177 178 if (playerIdx !== -1) {179 return {180 success: true,181 player: game.players[playerIdx],182 }183 }184 185 return {186 success: false,187 player: {...samplePlayer},188 };189 }190 191 export function restrictGameInfo() {192 const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))193 194 return {195 ...game,196 players: restrictedPlayers,197 }198 44 } 199 45 -
pages/index.js
r1df3fde r433e0c5 11 11 import Stats from '../components/Stats' 12 12 import ManageCredits from '../components/ManageCredits' 13 import ComplainScreen from '../components/Complain' 13 14 14 15 export default function Home() { … … 49 50 50 51 <ManageCredits/> 52 53 <ComplainScreen/> 51 54 </div> 52 55 )
Note:
See TracChangeset
for help on using the changeset viewer.