Files
tasJS/Cashout.js
2018-07-11 12:55:05 -04:00

110 lines
3.0 KiB
JavaScript

import prompts from 'prompts';
import request from 'request';
import { accounts } from './accounts.json';
import { CASHOUT_MIN, CATCHALL_DOMAIN, HEADERS, SERVER_URL } from './config';
export default class Cashout {
constructor() {
this.logger = null;
this.playersUpdated = 0;
this.playersReturned = 0;
this.totalBalance = 0;
this.fundedPlayers = [];
}
async run() {
console.log(`You have ${accounts.length} players available!\n`);
accounts.forEach(this.getFundedPlayers.bind(this));
const getSummary = setInterval(async () => {
if (this.playersReturned === accounts.length) {
clearInterval(getSummary);
console.log(`${this.fundedPlayers.length} can be cashed out for a total of $${this.totalBalance.toFixed(2)}!`);
if (this.fundedPlayers.length === 0) return;
const getConfirm = async () => {
const response = await prompts([
{
type: 'text',
name: 'confirm',
message: 'Confirm Cashout (y/n):',
},
]);
return response;
};
const confirmation = await getConfirm();
if (confirmation.confirm.toLowerCase() !== 'y') {
return;
}
this.playersReturned = 0;
this.cashoutPlayers();
}
}, 1000);
}
getFundedPlayers(player) {
const { username } = player;
request({
url: `https://${SERVER_URL}/users/me`,
headers: {
...HEADERS,
authorization: `Bearer ${player.accessToken}`,
}
}, (error, response, body) => {
this.playersReturned++;
try {
const result = JSON.parse(body);
const { lives, leaderboard } = result;
const { unclaimed } = leaderboard;
const balance = parseFloat(unclaimed.replace('$',''));
if (balance > CASHOUT_MIN) {
console.log(`${username}\thas balance of ${unclaimed} available for withdrawal.`);
this.totalBalance += balance;
this.fundedPlayers.push(player);
}
} catch (error) {
console.log('Error\n', error.toString('utf8'));
}
});
}
cashoutPlayers() {
this.fundedPlayers.forEach(player => {
const { username } = player;
const email = `${player.username}@${CATCHALL_DOMAIN}`;
request({
url: `https://${SERVER_URL}/users/me/payouts`,
method: 'POST',
headers: {
...HEADERS,
authorization: `Bearer ${player.accessToken}`,
},
body: JSON.stringify({
email,
}),
}, (error, response, body) => {
this.playersReturned++;
try {
console.log(`${username}\thas cashed out to ${email}!`);
} catch (error) {
console.log('Error\n', error.toString('utf8'));
}
});
});
const getSummary = setInterval(() => {
if (this.playersReturned === this.fundedPlayers.length) {
clearInterval(getSummary);
console.log(`${this.fundedPlayers.length} players have cashed out! Enjoy!`);
}
}, 1000);
}
}