[433e0c5] | 1 | import { calculateWinnings } from './calculateWinnings'
|
---|
| 2 |
|
---|
[faff334] | 3 | import { game, saveGameInHistory } from '../postgre/index'
|
---|
[433e0c5] | 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 |
|
---|
[e007fcd] | 28 | let inactivePlayers = []
|
---|
| 29 |
|
---|
[433e0c5] | 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;
|
---|
[e007fcd] | 41 |
|
---|
| 42 | const d = Date.now();
|
---|
| 43 |
|
---|
| 44 | if (d - player.lastActivity > 200000) {
|
---|
| 45 | inactivePlayers.push(player);
|
---|
| 46 | }
|
---|
[433e0c5] | 47 | })
|
---|
[e007fcd] | 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 | }
|
---|
[433e0c5] | 54 | }
|
---|
| 55 |
|
---|
| 56 | export 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 | });
|
---|
[faff334] | 81 |
|
---|
[22367db] | 82 | if (player.whichBets?.length > 0) {
|
---|
| 83 | game.finished = new Date().toGMTString();
|
---|
| 84 | saveGameInHistory('roulette', game, player.username)
|
---|
| 85 | }
|
---|
[433e0c5] | 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[e007fcd] | 89 | export function addPlayer(session_id, name, username) {
|
---|
[433e0c5] | 90 | if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
|
---|
| 91 | game.players.push({
|
---|
[e007fcd] | 92 | lastActivity: Date.now(),
|
---|
[433e0c5] | 93 | session_id: session_id,
|
---|
[e007fcd] | 94 | username: username,
|
---|
[433e0c5] | 95 | name: name,
|
---|
| 96 | whichBets: [],
|
---|
| 97 | coinPlaced: {
|
---|
| 98 | x: -1,
|
---|
| 99 | y: -1,
|
---|
| 100 | },
|
---|
| 101 | credits: -1,
|
---|
| 102 | betAmount: 0,
|
---|
| 103 | wonAmount: 0,
|
---|
| 104 | status: '_1_no_placed_bet',
|
---|
| 105 | outcome: 'none',
|
---|
| 106 | gotResults: false,
|
---|
| 107 | })
|
---|
| 108 | }
|
---|
[55701f0] | 109 | else {
|
---|
| 110 | game.players[game.players.map(e=>e.session_id).indexOf(session_id)].credits = -1;
|
---|
| 111 | }
|
---|
[433e0c5] | 112 | }
|
---|
| 113 |
|
---|
| 114 | export function getPlayer(session_id) {
|
---|
| 115 | const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
|
---|
| 116 |
|
---|
| 117 | if (playerIdx !== -1) {
|
---|
| 118 | return {
|
---|
| 119 | success: true,
|
---|
| 120 | player: game.players[playerIdx],
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return {
|
---|
| 125 | success: false,
|
---|
| 126 | player: {...samplePlayer},
|
---|
| 127 | };
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | export function restrictGameInfo() {
|
---|
| 131 | const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))
|
---|
| 132 |
|
---|
| 133 | return {
|
---|
| 134 | ...game,
|
---|
| 135 | players: restrictedPlayers,
|
---|
| 136 | }
|
---|
| 137 | }
|
---|