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

Added complaints, managing credits, and lost connection screens

File:
1 edited

Legend:

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

    r1df3fde r433e0c5  
    275275    /**
    276276     * /---------------------- POST ----------------------/
     277     * Deposits money from credit card to game account.
     278     * @action register
     279     * @param session_id
     280     * @param data
     281     */
     282    if (body?.action === 'deposit') {
     283      // checks
     284      if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
     285        res.json({
     286          success: false,
     287          message: 'You are not logged in. Please log in first.',
     288        });
     289        return ;
     290      }
     291      if (body?.data?.name == "undefined" || body?.data?.name == "null" || body?.data?.name == "") {
     292        res.json({
     293          success: false,
     294          message: 'Name field cannot be empty',
     295        });
     296        return ;
     297      }
     298      if (body?.data?.card == "undefined" || body?.data?.card == "null" || body?.data?.card == "") {
     299        res.json({
     300          success: false,
     301          message: 'Card numbers field cannot be empty',
     302        });
     303        return ;
     304      }
     305      if (body?.data?.expire == "undefined" || body?.data?.expire == "null" || body?.data?.expire == "") {
     306        res.json({
     307          success: false,
     308          message: 'Expiration date field cannot be empty',
     309        });
     310        return ;
     311      }
     312      if (body?.data?.ccv == "undefined" || body?.data?.ccv == "null" || body?.data?.ccv == "") {
     313        res.json({
     314          success: false,
     315          message: 'CCV field cannot be empty',
     316        });
     317        return ;
     318      }
     319      if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") {
     320        res.json({
     321          success: false,
     322          message: 'Amount field cannot be empty',
     323        });
     324        return ;
     325      }
     326
     327      let session = sessions.find(session => session.id === body?.session_id)
     328
     329      if (session) {
     330        if (parseInt(body.data.amount) > 0) {
     331          session.credits = session.credits + parseInt(body.data.amount)
     332
     333          pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
     334            if (error) throw error;
     335           
     336            res.json({
     337              success: true,
     338              credits: session.credits
     339            })
     340          });
     341        }
     342      }
     343    }
     344
     345    /**
     346     * /---------------------- POST ----------------------/
     347     * Withdraws money from game account to personal account.
     348     * @action register
     349     * @param session_id
     350     * @param data
     351     */
     352     if (body?.action === 'withdraw') {
     353      // checks
     354      if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
     355        res.json({
     356          success: false,
     357          message: 'You are not logged in. Please log in first.',
     358        });
     359        return ;
     360      }
     361      if (body?.data?.citibank == "undefined" || body?.data?.citibank == "null" || body?.data?.citibank == "") {
     362        res.json({
     363          success: false,
     364          message: 'Bank name field cannot be empty',
     365        });
     366        return ;
     367      }
     368      if (body?.data?.iban == "undefined" || body?.data?.iban == "null" || body?.data?.iban == "") {
     369        res.json({
     370          success: false,
     371          message: 'IBAN code field cannot be empty',
     372        });
     373        return ;
     374      }
     375      if (body?.data?.bic == "undefined" || body?.data?.bic == "null" || body?.data?.bic == "") {
     376        res.json({
     377          success: false,
     378          message: 'BIC code field cannot be empty',
     379        });
     380        return ;
     381      }
     382      if (body?.data?.beneficiary == "undefined" || body?.data?.beneficiary == "null" || body?.data?.beneficiary == "") {
     383        res.json({
     384          success: false,
     385          message: 'Beneficiary name field cannot be empty',
     386        });
     387        return ;
     388      }
     389      if (body?.data?.address == "undefined" || body?.data?.address == "null" || body?.data?.address == "") {
     390        res.json({
     391          success: false,
     392          message: 'Bank address field cannot be empty',
     393        });
     394        return ;
     395      }
     396      if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") {
     397        res.json({
     398          success: false,
     399          message: 'Amount field cannot be empty',
     400        });
     401        return ;
     402      }
     403
     404      let session = sessions.find(session => session.id === body?.session_id)
     405
     406      if (session) {
     407        if (parseInt(body.data.amount) > 0) {
     408          session.credits = Math.max(session.credits - parseInt(body.data.amount), 0)
     409
     410          pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
     411            if (error) throw error;
     412
     413            res.json({
     414              success: true,
     415              credits: session.credits
     416            })
     417          });
     418        }
     419      }
     420    }
     421
     422    /**
     423     * /---------------------- POST ----------------------/
     424     * Sends a complaint.
     425     * @action complain
     426     * @param session_id
     427     * @param description
     428     */
     429     if (body?.action === 'complain') {
     430      // checks
     431      if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
     432        res.json({
     433          success: false,
     434          message: 'You are not logged in. Please log in first.',
     435        });
     436        return ;
     437      }
     438      if (body?.description == "undefined" || body?.description == "null" || body?.description == "") {
     439        res.json({
     440          success: false,
     441          message: 'You cannot submit an empty complaint.',
     442        });
     443        return ;
     444      }
     445
     446      let session = sessions.find(session => session.id === body.session_id)
     447
     448      if (session) {
     449        // date, by, description, answered
     450        const date = new Date();
     451        pool.query('INSERT INTO complaints (date, by, description, answered) VALUES ($1, $2, $3, $4)', [date, session.username, body.description, false], (error, complaintResults) => {
     452          if (error) throw error;
     453
     454          res.json({
     455            success: true,
     456          })
     457        });
     458      }
     459    }
     460
     461    /**
     462     * /---------------------- POST ----------------------/
    277463     * Checks if the entered account info is good, and registers a new user in the database if so.
    278464     * @action register
     
    539725}
    540726load_game_from_database();
    541  
     727
     728/**
     729 *  Blackjack game data
     730 */
     731export var rooms = []
     732 
     733export function update_rooms_to_database() {
     734  let tmpRooms = [];
     735 
     736  for (let key in rooms) {
     737    if (key === "loaded") continue ;
     738
     739    tmpRooms.push(rooms[key]);
     740    tmpRooms[tmpRooms.length - 1].id = key;
     741  }
     742
     743  pool.query('UPDATE blackjack SET data = $1 WHERE identifier = $2', [JSON.stringify(tmpRooms), 'blackjack_data'], (error, results) => {
     744    if (error) throw error;
     745  });
     746}
     747     
     748export async function load_rooms_from_database() {
     749  pool.query('SELECT data FROM blackjack WHERE identifier = $1', ['blackjack_data'], (error, results) => {
     750    if (error) throw error;
     751
     752    if (results?.rows[0]?.data) {
     753      const tmpRooms = JSON.parse(results.rows[0].data);
     754
     755      tmpRooms.forEach(room => {
     756        rooms[room.id] = {...room, id: ''}
     757      })
     758     
     759      rooms["loaded"] = true;
     760    }
     761  });
     762}
     763load_rooms_from_database();
     764 
Note: See TracChangeset for help on using the changeset viewer.