SHARE
TWEET

Untitled

a guest Oct 12th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports.tradeItems = (req, res) => {
  2.    
  3.     const config = require('../config');
  4.     const points = config.app.inventoryPoints;
  5.    
  6.     let idBuyer = req.params.id;
  7.     let wanted = req.body.wanted;
  8.     let recipient = req.body.recipient;
  9.     let payment = req.body.payment;
  10.  
  11.     let wantedPoints = 0;
  12.     let paymentPoints = 0;
  13.  
  14.  
  15.     Object.keys(wanted).forEach((key) => {
  16.         if (!points[key]) {
  17.             //error
  18.             return;
  19.         }
  20.         wantedPoints += wanted[key] * points[key];
  21.     });
  22.  
  23.     Object.keys(payment).forEach((key) => {
  24.         if (!points[key]) {
  25.             //error
  26.         }
  27.         paymentPoints += payment[key] * points[key];
  28.     });
  29.  
  30.     if (wantedPoints != paymentPoints) {
  31.         res.status(412).send('ops');
  32.         return;
  33.     }
  34.  
  35.     let pBuyer = new Promise((resolve, reject) => {
  36.         Survivor.findById(idBuyer, (err, survivor) => {
  37.             if (err)
  38.                 return reject(err);
  39.             return resolve(survivor);
  40.         });
  41.     });
  42.  
  43.     let pRecipient = new Promise((resolve, reject) => {
  44.         Survivor.findById({
  45.             _id: recipient.id,
  46.             name: recipient.name
  47.         }, (err, survivor) => {
  48.             if (err)
  49.                 return reject(err);
  50.             return resolve(survivor);
  51.         });
  52.     });
  53.  
  54.     Promise.all([pBuyer, pRecipient])
  55.     .then((data) => {
  56.         let survivorBuyer = data[0];
  57.         let survivorRecipient = data[1];
  58.  
  59.         Object.keys(payment).forEach((key) => {
  60.             if (payment[key] > survivorBuyer.inventory[key]) {
  61.                 //error
  62.                 return;
  63.             }
  64.         });
  65.  
  66.         Object.keys(wanted).forEach((key) => {
  67.             if (wanted[key] > survivorRecipient.inventory[key]) {
  68.                 //error
  69.                 return;
  70.             }
  71.         });
  72.  
  73.         Object.keys(wanted).forEach((key) => {
  74.             survivorBuyer.inventory[key] += wanted[key];
  75.             survivorRecipient.inventory[key] -= wanted[key];
  76.         });
  77.  
  78.         Object.keys(payment).forEach((key) => {
  79.             survivorRecipient.inventory[key] += payment[key];
  80.             survivorBuyer.inventory[key] -= payment[key];
  81.         });
  82.  
  83.         survivorBuyer.save((err) => {
  84.  
  85.             survivorRecipient.save((err) => {
  86.  
  87.                 res.status(200).send('ok');
  88.             });
  89.  
  90.         });
  91.     })
  92.     .catch((data) => {
  93.  
  94.     })
  95. }
RAW Paste Data
Top