Changeset faff334 for pages/api


Ignore:
Timestamp:
07/18/22 13:59:55 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
22367db
Parents:
e903234
Message:

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

Location:
pages/api
Files:
4 edited

Legend:

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

    re903234 rfaff334  
    66import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
    77
    8 import { rooms, update_rooms_to_database } from '../postgre/index'
     8import { rooms, saveGameInHistory, update_rooms_to_database } from '../postgre/index'
    99
    1010/**
     
    3737
    3838      if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '5') {
    39         rooms[session_id] = {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
     39        rooms[session_id] = {...game, displayName: rooms[session_id].displayName, username: rooms[session_id].username, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
    4040
    4141        rooms[session_id].betOutcomeMessageShown = true;
     
    141141          }
    142142        });
     143
     144        saveGameInHistory('blackjack', rooms[session_id], rooms[session_id].username);
    143145
    144146        return ;
     
    201203            }
    202204          });
     205         
     206          saveGameInHistory('blackjack', rooms[session_id], rooms[session_id].username);
    203207        }
    204208        else {
  • pages/api/poker/tableSpecific.js

    re903234 rfaff334  
    1 import { tables } from '../postgre/index';
     1import { saveGameInHistory, tables } from '../postgre/index';
    22
    33import { deck } from './gameStates'
     
    209209
    210210            player.wonAmount = winnings;
     211
     212            saveGameInHistory('poker', table, player.username)
    211213        })
    212214
  • pages/api/postgre/index.js

    re903234 rfaff334  
    179179    /**
    180180     * /---------------------- GET ----------------------/
     181     * Get all the games played in the past.
     182     * @action get_games_history
     183     * @param session_id
     184     */
     185     if (req.query?.action === 'get_games_history' && req.query?.session_id) {
     186      const session_id = req.query.session_id
     187      const session = sessions.find(session => session.id === session_id)
     188
     189      let blackjackHistory = [];
     190      let rouletteHistory = [];
     191      let pokerHistory = [];
     192
     193      if (session) {
     194        // work
     195        pool.query('SELECT * FROM blackjack_history WHERE username = $1', [session.username], (error, blackjackResults) => {
     196          if (error) throw error;
     197
     198          if (blackjackResults.rows.length > 0) {
     199            blackjackHistory = blackjackResults.rows[0];
     200          }
     201         
     202          pool.query('SELECT * FROM roulette_history WHERE username = $1', [session.username], (error, rouletteResults) => {
     203            if (error) throw error;
     204
     205            if (rouletteResults.rows.length > 0) {
     206              rouletteHistory = rouletteResults.rows[0];
     207            }
     208           
     209            pool.query('SELECT * FROM poker_history WHERE username = $1', [session.username], (error, pokerResults) => {
     210              if (error) throw error;
     211
     212              if (pokerResults.rows.length > 0) {
     213                pokerHistory = pokerResults.rows[0];
     214              }
     215
     216              res.json({
     217                success: true,
     218                blackjack: JSON.parse(blackjackHistory.history ?? "[]"),
     219                roulette: JSON.parse(rouletteHistory.history ?? "[]"),
     220                poker: JSON.parse(pokerHistory.history ?? "[]"),
     221              })
     222            });
     223          });
     224        });
     225       
     226        return ;
     227      }
     228      else {
     229        res.json({
     230          success: false,
     231        })
     232      }
     233    }
     234
     235    /**
     236     * /---------------------- GET ----------------------/
     237     * /--------------------- ADMIN ----------------------/
     238     * Get all the games currently played.
     239     * @action get_live_games_as_admin
     240     * @param admin_id
     241     */
     242     if (req.query?.action === 'get_live_games_as_admin' && req.query?.admin_id) {
     243      const admin_id = req.query.admin_id
     244      const adminSession = adminSessions.find(adminSession => adminSession.id === admin_id)
     245
     246      if (adminSession) {
     247        let tmpRooms = [];
     248 
     249        for (let key in rooms) {
     250          if (key === "loaded") continue ;
     251
     252          tmpRooms.push(rooms[key]);
     253          tmpRooms[tmpRooms.length - 1].id = key;
     254        }
     255
     256        res.json({
     257          success: true,
     258          blackjack: tmpRooms,
     259          roulette: game,
     260          poker: tables,
     261        })
     262       
     263        return ;
     264      }
     265      else {
     266        res.json({
     267          success: false,
     268        })
     269      }
     270    }
     271
     272    /**
     273     * /---------------------- GET ----------------------/
    181274     * /--------------------- ADMIN ----------------------/
    182275     * Get complaints from the players and show them to the admin
     
    207300        return ;
    208301      }
    209 
    210       res.json({
    211         success: false,
    212       })
     302      else {
     303        res.json({
     304          success: false,
     305        })
     306      }
    213307    }
    214308
     
    11711265}
    11721266load_rooms_from_database();
     1267
     1268export async function saveGameInHistory(gameType, game, username) {
     1269  if (!username || !game || !gameType) return ;
     1270
     1271  if (gameType === 'blackjack') {
     1272    pool.query('SELECT * FROM blackjack_history WHERE username = $1', [username], (error, results) => {
     1273      if (error) throw error;
     1274 
     1275      if (results.rows.length > 0) {
     1276        let games = JSON.parse(results?.rows[0]?.history ?? []);
     1277
     1278        if (games.indexOf(game) === -1)
     1279          games.push(game);
     1280
     1281        pool.query('UPDATE blackjack_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => {
     1282          if (error) throw error;
     1283        });
     1284      }
     1285      else {
     1286        pool.query('INSERT INTO blackjack_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => {
     1287          if (error) throw error;
     1288        });
     1289      }
     1290    });
     1291  }
     1292
     1293  if (gameType === 'roulette') {
     1294    pool.query('SELECT * FROM roulette_history WHERE username = $1', [username], (error, results) => {
     1295      if (error) throw error;
     1296 
     1297      if (results.rows.length > 0) {
     1298        let games = JSON.parse(results?.rows[0]?.history ?? []);
     1299
     1300        if (games.indexOf(game) === -1)
     1301          games.push(game);
     1302
     1303        pool.query('UPDATE roulette_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => {
     1304          if (error) throw error;
     1305        });
     1306      }
     1307      else {
     1308        pool.query('INSERT INTO roulette_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => {
     1309          if (error) throw error;
     1310        });
     1311      }
     1312    });
     1313  }
     1314
     1315  if (gameType === 'poker') {
     1316    pool.query('SELECT * FROM poker_history WHERE username = $1', [username], (error, results) => {
     1317      if (error) throw error;
     1318 
     1319      if (results.rows.length > 0) {
     1320        let games = JSON.parse(results?.rows[0]?.history ?? []);
     1321
     1322        if (games.indexOf(game) === -1)
     1323          games.push(game);
     1324
     1325        pool.query('UPDATE poker_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => {
     1326          if (error) throw error;
     1327        });
     1328      }
     1329      else {
     1330        pool.query('INSERT INTO poker_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => {
     1331          if (error) throw error;
     1332        });
     1333      }
     1334    });
     1335  }
     1336}
  • pages/api/roulette/gameStates.js

    re903234 rfaff334  
    11import { calculateWinnings } from './calculateWinnings'
    22
    3 import { game } from '../postgre/index'
     3import { game, saveGameInHistory } from '../postgre/index'
    44
    55import axios from 'axios';
     
    7979            }
    8080        });
     81
     82        if (player.whichBets?.length > 0)
     83            saveGameInHistory('roulette', game, player.username)
    8184    }
    8285}
Note: See TracChangeset for help on using the changeset viewer.