[9bd09b0] | 1 | import axios from 'axios';
|
---|
| 2 |
|
---|
| 3 | require('dotenv').config();
|
---|
| 4 |
|
---|
| 5 | const COUNTDOWN_FROM = 90;
|
---|
| 6 | const WAIT_BEFORE = 30;
|
---|
| 7 |
|
---|
| 8 | (function() {
|
---|
| 9 | setInterval(() => {
|
---|
| 10 |
|
---|
| 11 | game.timeToStart--;
|
---|
| 12 |
|
---|
| 13 | // 30 seconds is the time to spin and see the results.
|
---|
| 14 | if (game.timeToStart == 0) {
|
---|
| 15 | game.timeToStart = COUNTDOWN_FROM + WAIT_BEFORE;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | }, 1000);
|
---|
| 19 | })();
|
---|
| 20 |
|
---|
| 21 | let game = {
|
---|
| 22 | status: '_1_ongoing_timer', // statuses: _1_ongoing_timer, _2_spinning, _3_results
|
---|
| 23 | timeToStart: COUNTDOWN_FROM, // in seconds
|
---|
| 24 | players: [] , // example player -> { session_id, name, whichBet, betAmount, status } // statuses: _1_no_placed_bet, _2_placed_bet
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | function addPlayer(session_id, name) {
|
---|
| 28 | if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
|
---|
| 29 | game.players.push({
|
---|
| 30 | session_id: session_id,
|
---|
| 31 | name: name,
|
---|
| 32 | whichBets: [],
|
---|
| 33 | betAmount: 0,
|
---|
| 34 | status: '_1_no_placed_bet',
|
---|
| 35 | })
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * ********************* BEGIN OF REQUEST HANDLER *********************
|
---|
| 41 | */
|
---|
| 42 | export default async function handler(req, res) {
|
---|
| 43 | /**
|
---|
| 44 | * GET method
|
---|
| 45 | */
|
---|
| 46 | if (req.method === 'GET') {
|
---|
| 47 | /**
|
---|
| 48 | * /---------------------- GET ----------------------/
|
---|
| 49 | * Place a bet.
|
---|
| 50 | * @action place_bet
|
---|
| 51 | * @param session_id
|
---|
| 52 | * @param betAmount
|
---|
| 53 | * @param whichBets
|
---|
| 54 | */
|
---|
| 55 | if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets) {
|
---|
| 56 | const session_id = req.query.session_id;
|
---|
| 57 | const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
|
---|
| 58 |
|
---|
| 59 | if (playerIdx !== -1 && game.players[playerIdx].status.substr(1, 1) === '1') {
|
---|
| 60 | game.players[playerIdx].betAmount = parseInt(req.query.betAmount);
|
---|
| 61 | game.players[playerIdx].whichBets = req.query.whichBets.split(',');
|
---|
| 62 | game.players[playerIdx].status = '_2_placed_bet';
|
---|
| 63 |
|
---|
| 64 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.betAmount}`).then(postgreRes => {
|
---|
| 65 | if (postgreRes.data?.success) {
|
---|
| 66 | res.json({
|
---|
| 67 | success: true,
|
---|
| 68 | game: game,
|
---|
| 69 | credits: postgreRes.data?.credits,
|
---|
| 70 | })
|
---|
| 71 | }
|
---|
| 72 | else {
|
---|
| 73 | res.json({
|
---|
| 74 | success: false,
|
---|
| 75 | })
|
---|
| 76 | }
|
---|
| 77 | });
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * /---------------------- GET ----------------------/
|
---|
| 83 | * If the player is not in an existing room, create a room for them.
|
---|
| 84 | * If they are reconnecting, get the room they were in.
|
---|
| 85 | * @action get_player_info_on_enter
|
---|
| 86 | * @param session_id
|
---|
| 87 | */
|
---|
| 88 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 89 | const session_id = req.query.session_id;
|
---|
| 90 |
|
---|
| 91 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 92 | if (postgreRes.data?.success) {
|
---|
| 93 | addPlayer(session_id, postgreRes.data?.displayName);
|
---|
| 94 |
|
---|
| 95 | res.json({
|
---|
| 96 | success: true,
|
---|
| 97 | game: game,
|
---|
| 98 | displayName: postgreRes.data?.displayName,
|
---|
| 99 | session_id: postgreRes.data?.session_id,
|
---|
| 100 | credits: postgreRes.data?.credits,
|
---|
| 101 | })
|
---|
| 102 | }
|
---|
| 103 | else {
|
---|
| 104 | res.json({
|
---|
| 105 | success: false,
|
---|
| 106 | })
|
---|
| 107 | }
|
---|
| 108 | });
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | /**
|
---|
| 113 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 114 | */
|
---|