source: pages/api/roulette/gameStates.js@ faff334

main
Last change on this file since faff334 was faff334, checked in by anastasovv <simon@…>, 23 months ago

Ability for admin to watch live games and for user to see games history

  • Property mode set to 100644
File size: 3.4 KB
Line 
1import { calculateWinnings } from './calculateWinnings'
2
3import { game, saveGameInHistory } 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 if (player.whichBets?.length > 0)
83 saveGameInHistory('roulette', game, player.username)
84 }
85}
86
87export function addPlayer(session_id, name, username) {
88 if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
89 game.players.push({
90 lastActivity: Date.now(),
91 session_id: session_id,
92 username: username,
93 name: name,
94 whichBets: [],
95 coinPlaced: {
96 x: -1,
97 y: -1,
98 },
99 credits: -1,
100 betAmount: 0,
101 wonAmount: 0,
102 status: '_1_no_placed_bet',
103 outcome: 'none',
104 gotResults: false,
105 })
106 }
107 else {
108 game.players[game.players.map(e=>e.session_id).indexOf(session_id)].credits = -1;
109 }
110}
111
112export function getPlayer(session_id) {
113 const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
114
115 if (playerIdx !== -1) {
116 return {
117 success: true,
118 player: game.players[playerIdx],
119 }
120 }
121
122 return {
123 success: false,
124 player: {...samplePlayer},
125 };
126}
127
128export function restrictGameInfo() {
129 const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))
130
131 return {
132 ...game,
133 players: restrictedPlayers,
134 }
135}
Note: See TracBrowser for help on using the repository browser.