Changeset 285c3cc for pages/api


Ignore:
Timestamp:
06/16/22 17:01:08 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
9bd09b0
Parents:
ebf5e04
Message:

Removed double client-side api requests

Location:
pages/api
Files:
2 edited

Legend:

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

    rebf5e04 r285c3cc  
    255255        }
    256256
    257         const room = rooms[session_id];
    258 
    259         room.sideBet = parseInt(req.query.bet);
    260         room.sideBetName = req.query.betName;
    261         room.status = '_3_made_side_bet';
    262 
    263         rooms[session_id] = room;
    264 
    265         res.json({
    266           success: true,
    267           status: rooms[session_id].status,
    268         })
     257        axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
     258          if (postgreRes.data?.success) {
     259            const room = rooms[session_id];
     260
     261            room.sideBet = parseInt(req.query.bet);
     262            room.sideBetName = req.query.betName;
     263            room.status = '_3_made_side_bet';
     264
     265            rooms[session_id] = room;
     266
     267            res.json({
     268              success: true,
     269              status: rooms[session_id].status,
     270              credits: postgreRes.data?.credits,
     271            })
     272          }
     273        });
    269274       
    270275        return ;
     
    289294        if (parseInt(req.query.bet) <= 0) return ;
    290295
    291         const room = rooms[session_id];
    292 
    293         room.initialBet = parseInt(req.query.bet);
    294         room.status = '_2_made_initial_bet';
    295 
    296         rooms[session_id] = room;
    297 
    298         res.json({
    299           success: true,
    300           status: rooms[session_id].status,
    301         })
     296        axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
     297          if (postgreRes.data?.success) {
     298            const room = rooms[session_id];
     299
     300            room.initialBet = parseInt(req.query.bet);
     301            room.status = '_2_made_initial_bet';
     302
     303            rooms[session_id] = room;
     304
     305            res.json({
     306              success: true,
     307              status: rooms[session_id].status,
     308              credits: postgreRes.data?.credits,
     309            })
     310          }
     311          else {
     312            res.json({
     313              success: false,
     314            })
     315          }
     316        });
    302317
    303318        return ;
     
    337352      const session_id = req.query.session_id;
    338353
    339       if (rooms[session_id] !== undefined) {
    340 
    341       }
    342       else {
    343         createARoom(session_id);
    344       }
    345 
    346       let dealerCardsTmp = [];
    347       if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
    348         rooms[session_id].dealerCards.forEach((card, i) => {
    349           if (i === 0) {
    350             dealerCardsTmp.push(card);
     354      axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
     355        if (postgreRes.data?.success) {
     356          if (rooms[session_id] !== undefined) {
     357            // room exists
    351358          }
    352359          else {
    353             dealerCardsTmp.push('back');
    354           }
    355         })
    356       }
    357       else {
    358         dealerCardsTmp = rooms[session_id].dealerCards;
    359       }
    360 
    361       res.json({
    362         success: true,
    363         status: rooms[session_id].status,
    364         initialBet: rooms[session_id].initialBet,
    365         sideBet: rooms[session_id].sideBet,
    366         sideBetName: rooms[session_id].sideBetName,
    367         playerCards: rooms[session_id].playerCards,
    368         dealerCards: dealerCardsTmp,
    369         outcome: rooms[session_id].outcome,
    370         earnings: rooms[session_id].earnings,
    371       })
     360            createARoom(session_id);
     361          }
     362   
     363          let dealerCardsTmp = [];
     364          if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
     365            rooms[session_id].dealerCards.forEach((card, i) => {
     366              if (i === 0) {
     367                dealerCardsTmp.push(card);
     368              }
     369              else {
     370                dealerCardsTmp.push('back');
     371              }
     372            })
     373          }
     374          else {
     375            dealerCardsTmp = rooms[session_id].dealerCards;
     376          }
     377   
     378          res.json({
     379            success: true,
     380            status: rooms[session_id].status,
     381            initialBet: rooms[session_id].initialBet,
     382            sideBet: rooms[session_id].sideBet,
     383            sideBetName: rooms[session_id].sideBetName,
     384            playerCards: rooms[session_id].playerCards,
     385            dealerCards: dealerCardsTmp,
     386            outcome: rooms[session_id].outcome,
     387            earnings: rooms[session_id].earnings,
     388            displayName: postgreRes.data?.displayName,
     389            session_id: postgreRes.data?.session_id,
     390            credits: postgreRes.data?.credits,
     391          })
     392        }
     393        else {
     394          res.json({
     395            success: false,
     396          })
     397        }
     398      });
    372399    }
    373400  }
  • pages/api/postgre/index.js

    rebf5e04 r285c3cc  
    170170    /**
    171171     * /---------------------- GET ----------------------/
    172      * Returns the player's room, if the player was in one.
    173      * Same as the one below, but this one is used in a game-specific context.
    174      * @action get_player_info_on_enter
    175      * @param session_id
    176      */
    177     if (req.query?.action === 'get_player_info_on_enter' && req.query?.session_id) {
    178       const session_id = req.query.session_id
    179       const session = sessions.find(session => session.id === session_id)
    180 
    181       if (session) {
    182         res.json({
    183           success: true,
    184           displayName: session.displayName,
    185           session_id: session.id,
    186           credits: session.credits,
    187         })
    188         return ;
    189       }
    190 
    191       res.json({
    192         success: false,
    193       })
    194     }
    195 
    196     /**
    197      * /---------------------- GET ----------------------/
    198      * Returns the player's room, if the player was in one.
    199      * Same as the one above, but this one is used in a general context.
     172     * Checks if the player is logged in, and returns his session if so.
    200173     * @action check_if_logged_in
    201174     * @param session_id
Note: See TracChangeset for help on using the changeset viewer.