source: pages/api/roulette/gameStates.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: 3.3 KB
Line 
1import { calculateWinnings } from './calculateWinnings'
2
3import { game } from '../postgre/index'
4
5import axios from 'axios';
6
7export 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}
22
23export function resetGame() {
24 game.magicNumber = -1;
25 game.winningBets = [];
26 game.status = '_1_ongoing_timer';
27
28 let inactivePlayers = []
29
30 game.players.forEach(player => {
31 player.whichBets = [];
32 player.betAmount = 0;
33 player.wonAmount = 0;
34 player.coinPlaced = {
35 x: -1,
36 y: -1,
37 },
38 player.outcome = 'none';
39 player.status = '_1_no_placed_bet';
40 player.gotResults = false;
41
42 const d = Date.now();
43
44 if (d - player.lastActivity > 200000) {
45 inactivePlayers.push(player);
46 }
47 })
48
49 for (let i = 0; i < inactivePlayers.length; i++) {
50 if (game.players.indexOf(inactivePlayers[i]) !== -1) {
51 game.players.splice(game.players.indexOf(inactivePlayers[i]), 1);
52 }
53 }
54}
55
56export function updateGameWithWinners() {
57 for (let i = 0; i < game.players.length; i++) {
58 const player = game.players[i];
59
60 let playerWon = false;
61 player.whichBets.forEach(bet => {
62 if (game.winningBets.indexOf(bet) !== -1) {
63 playerWon = true;
64 }
65 })
66
67 if (playerWon) {
68 player.outcome = 'won';
69 }
70 else {
71 player.outcome = 'lost';
72 }
73
74 player.wonAmount = calculateWinnings(player);
75
76 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 => {
77 if (postgreRes.data?.success) {
78 player.credits = postgreRes.data?.credits;
79 }
80 });
81 }
82}
83
84export function addPlayer(session_id, name, username) {
85 if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
86 game.players.push({
87 lastActivity: Date.now(),
88 session_id: session_id,
89 username: username,
90 name: name,
91 whichBets: [],
92 coinPlaced: {
93 x: -1,
94 y: -1,
95 },
96 credits: -1,
97 betAmount: 0,
98 wonAmount: 0,
99 status: '_1_no_placed_bet',
100 outcome: 'none',
101 gotResults: false,
102 })
103 }
104 else {
105 game.players[game.players.map(e=>e.session_id).indexOf(session_id)].credits = -1;
106 }
107}
108
109export function getPlayer(session_id) {
110 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
111
112 if (playerIdx !== -1) {
113 return {
114 success: true,
115 player: game.players[playerIdx],
116 }
117 }
118
119 return {
120 success: false,
121 player: {...samplePlayer},
122 };
123}
124
125export function restrictGameInfo() {
126 const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))
127
128 return {
129 ...game,
130 players: restrictedPlayers,
131 }
132}
Note: See TracBrowser for help on using the repository browser.