Changeset 433e0c5 for pages/api/roulette


Ignore:
Timestamp:
07/15/22 14:45:30 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
55701f0
Parents:
1df3fde
Message:

Added complaints, managing credits, and lost connection screens

Location:
pages/api/roulette
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • pages/api/roulette/index.js

    r1df3fde r433e0c5  
    55require('dotenv').config();
    66
    7 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 }
     7import { resetGame, updateGameWithWinners, addPlayer, getPlayer, restrictGameInfo } from './gameStates'
    228
    23 function getWinningBets(magicNumber) {
    24     let winningBets = [];
    25     winningBets.push(magicNumber);
    26    
    27     if (magicNumber != 0) {
    28         if ((magicNumber <= 9 && magicNumber % 2 == 1) || (magicNumber > 10 && magicNumber <= 18 && magicNumber % 2 == 0) ||
    29             (magicNumber > 19 && magicNumber <= 27 && magicNumber % 2 == 1) || (magicNumber > 27 && magicNumber % 2 == 0)) {
    30 
    31             winningBets.push('Red');
    32         }
    33         else {
    34             winningBets.push('Black');
    35         }
    36 
    37         if (magicNumber % 2 === 0)      winningBets.push('Even');
    38         else                            winningBets.push('Odd');
    39 
    40         if (magicNumber <= 12)          winningBets.push('1-12');
    41         else if (magicNumber <= 24)     winningBets.push('13-24');
    42         else                            winningBets.push('25-36');
    43 
    44         if (magicNumber <= 18)          winningBets.push('1-18');
    45         else                            winningBets.push('19-36');
    46 
    47         if (magicNumber % 3 === 0)      winningBets.push('Remainder3');
    48         else if (magicNumber % 3 === 2) winningBets.push('Remainder2');
    49         else                            winningBets.push('Remainder1');
    50     }
    51 
    52     return winningBets;
    53 }
    54 
    55 function updateGameWithWinners() {
    56     for (let i = 0; i < game.players.length; i++) {
    57         const player = game.players[i];
    58 
    59         let playerWon = false;
    60         player.whichBets.forEach(bet => {
    61             if (game.winningBets.indexOf(bet) !== -1) {
    62                 playerWon = true;
    63             }
    64         })
    65 
    66         if (playerWon) {
    67             player.outcome = 'won';
    68         }
    69         else {
    70             player.outcome = 'lost';
    71         }
    72 
    73         player.wonAmount = calculateWinnings(player);
    74 
    75         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 => {
    76             if (postgreRes.data?.success) {
    77                 player.credits = postgreRes.data?.credits;
    78             }
    79         });
    80     }
    81 }
    82 
    83 function resetGame() {
    84     game.magicNumber = -1;
    85     game.winningBets = [];
    86     game.status = '_1_ongoing_timer';
    87 
    88     game.players.forEach(player => {
    89         player.whichBets = [];
    90         player.betAmount = 0;
    91         player.wonAmount = 0;
    92         player.coinPlaced = {
    93             x: -1,
    94             y: -1,               
    95         },
    96         player.outcome = 'none';
    97         player.status = '_1_no_placed_bet';
    98         player.gotResults = false;
    99     })
    100 }
    101 
    102 function calculateWinnings(player) {
    103     if (player.outcome === 'lost') return 0;
    104 
    105     let bets = player.whichBets;
    106     let bet = player.betAmount;
    107 
    108     if (bets[0] === 'Even' || bets[0] === 'Odd') return 2 * bet;
    109     else if (bets[0] === 'Red' || bets[0] === 'Black') return 2 * bet;
    110     else if (bets[0].includes('Remainder')) return 3 * bet;
    111     else if (bets[0] === '1-12' || bets[0] === '13-24' || bets[0] === '25-36') return 3 * bet;
    112     else if (bets[0] === '1-18' || bets[0] === '19-36') return 2 * bet;
    113     else if (bets.length === 4) return 9 * bet;
    114     else if (bets.length === 2) return 18 * bet;
    115     else if (bets.length === 1) return 36 * bet;
    116 
    117     return 0;
    118 }
     9import { getWinningBets } from './calculateWinnings'
    11910
    12011(function() {
     
    15142    game.winningBets = [];
    15243    game.players = [];
    153 }
    154 
    155 function addPlayer(session_id, name) {
    156     if (game.players.map(e=>e.session_id).indexOf(session_id) === -1) {
    157         game.players.push({
    158             session_id: session_id,
    159             name: name,
    160             whichBets: [],
    161             coinPlaced: {
    162                 x: -1,
    163                 y: -1,               
    164             },
    165             credits: -1,
    166             betAmount: 0,
    167             wonAmount: 0,
    168             status: '_1_no_placed_bet',
    169             outcome: 'none',
    170             gotResults: false,
    171         })
    172     }
    173 }
    174 
    175 export function getPlayer(session_id) {
    176     const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
    177 
    178     if (playerIdx !== -1) {
    179         return {
    180             success: true,
    181             player: game.players[playerIdx],
    182         }
    183     }
    184 
    185     return {
    186         success: false,
    187         player: {...samplePlayer},
    188     };
    189 }
    190 
    191 export function restrictGameInfo() {
    192     const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))
    193 
    194     return {
    195         ...game,
    196         players: restrictedPlayers,
    197     }
    19844}
    19945
Note: See TracChangeset for help on using the changeset viewer.