goodwin64

HWM - calculate profit in logs page

Jul 16th, 2023
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var transactions = $('center + .global_a_hover')
  2.     .innerText
  3.     .split('\n')
  4.     .map(s => s.trim())
  5.     .filter(Boolean);
  6.  
  7. var regexProfit = /(?<resNumber>\d+) шт. (?<resType>.+) за (?<goldSum>\d+) золота/gm;
  8.  
  9. var resources180 = new Set([
  10.     'Древесина', 'Руда'
  11. ])
  12.  
  13. var profits = transactions.reduce((acc, t) => {
  14.     const matched = regexProfit.exec(t);
  15.     if (matched) {
  16.         const resPrice = resources180.has(matched.groups.resType)
  17.             ? 180
  18.             : 360;
  19.         const resNumber = Number(matched.groups.resNumber);
  20.         const resSum = resPrice * resNumber;
  21.         const transactionProfit = resSum - Number(matched.groups.goldSum);
  22.         const profitPerRes = transactionProfit / resNumber;
  23.         acc.push({
  24.             transactionProfit,
  25.             resType: matched.groups.resType,
  26.             resNumber,
  27.             resSum,
  28.             profitPerRes,
  29.         });
  30.     }
  31.     return acc;
  32. }, []);
  33.  
  34. var totalProfit = profits.reduce((acc, p) => acc + p.transactionProfit, 0);
  35.  
  36. console.log('profits: ', totalProfit, profits);
  37.  
Advertisement
Add Comment
Please, Sign In to add comment