source: pages/api/roulette/index.js@ 433e0c5

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

Added complaints, managing credits, and lost connection screens

  • Property mode set to 100644
File size: 5.4 KB
Line 
1import axios from 'axios';
2
3import { game, update_game_to_database } from '../postgre/index'
4
5require('dotenv').config();
6
7import { resetGame, updateGameWithWinners, addPlayer, getPlayer, restrictGameInfo } from './gameStates'
8
9import { getWinningBets } from './calculateWinnings'
10
11(function() {
12 setInterval(() => {
13 game.timeToStart--;
14
15 // WAIT_BEFORE seconds is the time allocated for spinning the wheel and seeing the results.
16 if (game.timeToStart == 0) {
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)
25 }
26 else if (game.timeToStart == 10) {
27 game.status = '_2_spinning';
28 }
29 else if (game.timeToStart == game.COUNTDOWN_FROM) {
30 resetGame();
31 }
32
33 }, 1000);
34})();
35
36if (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 = [];
44}
45
46/**
47 * ********************* BEGIN OF REQUEST HANDLER *********************
48 */
49export default async function handler(req, res) {
50 /**
51 * GET method
52 */
53 if (req.method === 'GET') {
54 /**
55 * /---------------------- GET ----------------------/
56 * Place a bet.
57 * @action place_bet
58 * @param session_id
59 * @param betAmount
60 * @param whichBets
61 * @param coinPlacedX
62 * @param coinPlacedY
63 */
64 if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets && req.query?.coinPlacedX && req.query?.coinPlacedY) {
65 const session_id = req.query.session_id;
66
67 const { success, player } = getPlayer(session_id);
68
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 => {
71 if (postgreRes.data?.success) {
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;
80 }
81 });
82 }
83
84 res.end();
85 }
86
87 /**
88 * /---------------------- GET ----------------------/
89 * Updates the state periodically
90 * @action update_state
91 * @param session_id
92 */
93 if (req.query.action === 'update_state' && req.query?.session_id) {
94 const session_id = req.query.session_id;
95
96 const { success, player } = getPlayer(session_id);
97
98 let extraAction = "";
99 let magicNumber = -1;
100 let winningBets = [];
101
102 if (success) {
103 if (game.timeToStart > game.COUNTDOWN_FROM && !player.gotResults) {
104 extraAction = "spin_wheel";
105 magicNumber = game.magicNumber;
106 winningBets = game.winningBets;
107
108 player.gotResults = true;
109 }
110 }
111
112 if (game.loaded !== undefined && game.loaded) {
113 update_game_to_database();
114 }
115
116 res.json({
117 success: true,
118 rouletteGame: {
119 game: restrictGameInfo(),
120 player: player,
121 },
122 extraAction: extraAction,
123 magicNumber: magicNumber,
124 winningBets: winningBets,
125 })
126 }
127
128 /**
129 * /---------------------- GET ----------------------/
130 * If the player is not in an existing room, create a room for them.
131 * If they are reconnecting, get the room they were in.
132 * @action get_player_info_on_enter
133 * @param session_id
134 */
135 if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
136 const session_id = req.query.session_id;
137
138 axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
139 if (postgreRes.data?.success) {
140 addPlayer(session_id, postgreRes.data?.displayName);
141
142 res.json({
143 success: true,
144 game: game,
145 displayName: postgreRes.data?.displayName,
146 session_id: postgreRes.data?.session_id,
147 credits: postgreRes.data?.credits,
148 })
149 }
150 else {
151 res.json({
152 success: false,
153 })
154 }
155 });
156 }
157 }
158}
159/**
160 * ********************* END OF REQUEST HANDLER *********************
161 */
Note: See TracBrowser for help on using the repository browser.