[9bd09b0] | 1 | import axios from 'axios';
|
---|
| 2 |
|
---|
[e9f11ac] | 3 | import { game, update_game_to_database } from '../postgre/index'
|
---|
| 4 |
|
---|
[9bd09b0] | 5 | require('dotenv').config();
|
---|
| 6 |
|
---|
[433e0c5] | 7 | import { resetGame, updateGameWithWinners, addPlayer, getPlayer, restrictGameInfo } from './gameStates'
|
---|
[ace7865] | 8 |
|
---|
[433e0c5] | 9 | import { getWinningBets } from './calculateWinnings'
|
---|
[ace7865] | 10 |
|
---|
[9bd09b0] | 11 | (function() {
|
---|
| 12 | setInterval(() => {
|
---|
| 13 | game.timeToStart--;
|
---|
| 14 |
|
---|
[ace7865] | 15 | // WAIT_BEFORE seconds is the time allocated for spinning the wheel and seeing the results.
|
---|
[9bd09b0] | 16 | if (game.timeToStart == 0) {
|
---|
[d0ef259] | 17 | game.timeToStart = game.COUNTDOWN_FROM + game.WAIT_BEFORE;
|
---|
| 18 |
|
---|
| 19 | game.magicNumber = Math.floor(Math.random() * 37);
|
---|
| 20 | game.winningBets = getWinningBets(game.magicNumber);
|
---|
| 21 |
|
---|
| 22 | setTimeout(() => {
|
---|
| 23 | updateGameWithWinners();
|
---|
| 24 | }, 6000)
|
---|
[9bd09b0] | 25 | }
|
---|
[ace7865] | 26 | else if (game.timeToStart == 10) {
|
---|
| 27 | game.status = '_2_spinning';
|
---|
| 28 | }
|
---|
[d0ef259] | 29 | else if (game.timeToStart == game.COUNTDOWN_FROM) {
|
---|
| 30 | resetGame();
|
---|
[ace7865] | 31 | }
|
---|
[9bd09b0] | 32 |
|
---|
| 33 | }, 1000);
|
---|
| 34 | })();
|
---|
| 35 |
|
---|
[e9f11ac] | 36 | if (game.status === undefined) {
|
---|
| 37 | game.status = '_1_ongoing_timer'; // statuses: _1_ongoing_timer, _2_spinning
|
---|
| 38 | game.timeToStart = 30; // in seconds
|
---|
| 39 | game.COUNTDOWN_FROM = 30;
|
---|
| 40 | game.WAIT_BEFORE = 20;
|
---|
| 41 | game.magicNumber = -1;
|
---|
| 42 | game.winningBets = [];
|
---|
| 43 | game.players = [];
|
---|
[9bd09b0] | 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * ********************* BEGIN OF REQUEST HANDLER *********************
|
---|
| 48 | */
|
---|
| 49 | export default async function handler(req, res) {
|
---|
| 50 | /**
|
---|
| 51 | * GET method
|
---|
| 52 | */
|
---|
| 53 | if (req.method === 'GET') {
|
---|
[ace7865] | 54 | /**
|
---|
| 55 | * /---------------------- GET ----------------------/
|
---|
[d0ef259] | 56 | * Place a bet.
|
---|
| 57 | * @action place_bet
|
---|
[ace7865] | 58 | * @param session_id
|
---|
[d0ef259] | 59 | * @param betAmount
|
---|
| 60 | * @param whichBets
|
---|
| 61 | * @param coinPlacedX
|
---|
| 62 | * @param coinPlacedY
|
---|
[ace7865] | 63 | */
|
---|
[d0ef259] | 64 | if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets && req.query?.coinPlacedX && req.query?.coinPlacedY) {
|
---|
[ace7865] | 65 | const session_id = req.query.session_id;
|
---|
| 66 |
|
---|
[d0ef259] | 67 | const { success, player } = getPlayer(session_id);
|
---|
[ace7865] | 68 |
|
---|
[d0ef259] | 69 | if (success && game.status.includes('_1_') && player.status.includes('_1_')) {
|
---|
| 70 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.betAmount}`).then(postgreRes => {
|
---|
[ace7865] | 71 | if (postgreRes.data?.success) {
|
---|
[d0ef259] | 72 | player.betAmount = parseInt(req.query.betAmount);
|
---|
| 73 | player.whichBets = req.query.whichBets.split(',');
|
---|
| 74 | player.status = '_2_placed_bet';
|
---|
| 75 | player.coinPlaced = {
|
---|
| 76 | x: req.query.coinPlacedX,
|
---|
| 77 | y: req.query.coinPlacedY,
|
---|
| 78 | },
|
---|
| 79 | player.credits = postgreRes.data?.credits;
|
---|
[e007fcd] | 80 | player.lastActivity = Date.now();
|
---|
[ace7865] | 81 | }
|
---|
| 82 | });
|
---|
| 83 | }
|
---|
[d0ef259] | 84 |
|
---|
| 85 | res.end();
|
---|
[ace7865] | 86 | }
|
---|
| 87 |
|
---|
[9bd09b0] | 88 | /**
|
---|
| 89 | * /---------------------- GET ----------------------/
|
---|
[d0ef259] | 90 | * Updates the state periodically
|
---|
| 91 | * @action update_state
|
---|
[9bd09b0] | 92 | * @param session_id
|
---|
| 93 | */
|
---|
[d0ef259] | 94 | if (req.query.action === 'update_state' && req.query?.session_id) {
|
---|
[9bd09b0] | 95 | const session_id = req.query.session_id;
|
---|
| 96 |
|
---|
[d0ef259] | 97 | const { success, player } = getPlayer(session_id);
|
---|
| 98 |
|
---|
| 99 | let extraAction = "";
|
---|
[e007fcd] | 100 | let extraAction2 = "";
|
---|
[d0ef259] | 101 | let magicNumber = -1;
|
---|
| 102 | let winningBets = [];
|
---|
| 103 |
|
---|
| 104 | if (success) {
|
---|
| 105 | if (game.timeToStart > game.COUNTDOWN_FROM && !player.gotResults) {
|
---|
| 106 | extraAction = "spin_wheel";
|
---|
| 107 | magicNumber = game.magicNumber;
|
---|
| 108 | winningBets = game.winningBets;
|
---|
| 109 |
|
---|
| 110 | player.gotResults = true;
|
---|
| 111 | }
|
---|
[e007fcd] | 112 | if (game.timeToStart > game.COUNTDOWN_FROM + game.WAIT_BEFORE - 15) {
|
---|
| 113 | extraAction2 = "keep_alert";
|
---|
| 114 | }
|
---|
[9bd09b0] | 115 | }
|
---|
[d0ef259] | 116 |
|
---|
[e9f11ac] | 117 | if (game.loaded !== undefined && game.loaded) {
|
---|
| 118 | update_game_to_database();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[d0ef259] | 121 | res.json({
|
---|
| 122 | success: true,
|
---|
| 123 | rouletteGame: {
|
---|
| 124 | game: restrictGameInfo(),
|
---|
| 125 | player: player,
|
---|
| 126 | },
|
---|
| 127 | extraAction: extraAction,
|
---|
[e007fcd] | 128 | extraAction2: extraAction2,
|
---|
[d0ef259] | 129 | magicNumber: magicNumber,
|
---|
| 130 | winningBets: winningBets,
|
---|
| 131 | })
|
---|
[9bd09b0] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * /---------------------- GET ----------------------/
|
---|
| 136 | * If the player is not in an existing room, create a room for them.
|
---|
| 137 | * If they are reconnecting, get the room they were in.
|
---|
| 138 | * @action get_player_info_on_enter
|
---|
| 139 | * @param session_id
|
---|
| 140 | */
|
---|
| 141 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 142 | const session_id = req.query.session_id;
|
---|
| 143 |
|
---|
| 144 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 145 | if (postgreRes.data?.success) {
|
---|
[e007fcd] | 146 | addPlayer(session_id, postgreRes.data?.displayName, postgreRes.data?.username);
|
---|
[9bd09b0] | 147 |
|
---|
| 148 | res.json({
|
---|
| 149 | success: true,
|
---|
| 150 | game: game,
|
---|
| 151 | displayName: postgreRes.data?.displayName,
|
---|
| 152 | session_id: postgreRes.data?.session_id,
|
---|
| 153 | credits: postgreRes.data?.credits,
|
---|
| 154 | })
|
---|
| 155 | }
|
---|
| 156 | else {
|
---|
| 157 | res.json({
|
---|
| 158 | success: false,
|
---|
| 159 | })
|
---|
| 160 | }
|
---|
| 161 | });
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | /**
|
---|
| 166 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 167 | */
|
---|