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