source: pages/api/roulette/index.js@ e903234

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

Now you need to activate your account via email & also added mail sending after server crash

  • Property mode set to 100644
File size: 5.7 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 player.lastActivity = Date.now();
81 }
82 });
83 }
84
85 res.end();
86 }
87
88 /**
89 * /---------------------- GET ----------------------/
90 * Updates the state periodically
91 * @action update_state
92 * @param session_id
93 */
94 if (req.query.action === 'update_state' && req.query?.session_id) {
95 const session_id = req.query.session_id;
96
97 const { success, player } = getPlayer(session_id);
98
99 let extraAction = "";
100 let extraAction2 = "";
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 }
112 if (game.timeToStart > game.COUNTDOWN_FROM + game.WAIT_BEFORE - 15) {
113 extraAction2 = "keep_alert";
114 }
115 }
116
117 if (game.loaded !== undefined && game.loaded) {
118 update_game_to_database();
119 }
120
121 res.json({
122 success: true,
123 rouletteGame: {
124 game: restrictGameInfo(),
125 player: player,
126 },
127 extraAction: extraAction,
128 extraAction2: extraAction2,
129 magicNumber: magicNumber,
130 winningBets: winningBets,
131 })
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) {
146 addPlayer(session_id, postgreRes.data?.displayName, postgreRes.data?.username);
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 */
Note: See TracBrowser for help on using the repository browser.