Add group push, account summaries, and confirm answer choice.

This commit is contained in:
eTronic
2018-07-08 21:57:34 -04:00
parent 82b8809b90
commit 12dabdb80e
5 changed files with 147 additions and 19 deletions

View File

@@ -5,6 +5,7 @@ import { PUSH_SETTINGS } from './config';
export default class Pusher {
constructor(logger) {
this.pushClient = null;
this.groupPushClient = null;
this.logger = logger;
}
@@ -13,6 +14,14 @@ export default class Pusher {
user: PUSH_SETTINGS.PUSHOVER_USER,
token: PUSH_SETTINGS.PUSHOVER_TOKEN,
});
if (PUSH_SETTINGS.PUSHOVER_GROUP) {
this.groupPushClient = new Push({
user: PUSH_SETTINGS.PUSHOVER_GROUP,
token: PUSH_SETTINGS.PUSHOVER_TOKEN,
});
}
this.logger.log('Push notifications enabled!');
}
@@ -24,4 +33,17 @@ export default class Pusher {
priority: 0,
}, null);
}
sendAll(title, message) {
if (this.groupPushClient) {
this.groupPushClient.send({
message,
title,
sound: 'magic',
priority: 0,
}, null);
} else {
this.send(title, message);
}
}
}

View File

@@ -120,7 +120,7 @@ export default class Scraper {
const searchOutput = `[${searchWeights[0]}%, ${searchWeights[1]}%, ${searchWeights[2]}%]`;
this.broadcastMessage(`**Google Weights**\`\`\`${searchOutput}\`\`\``);
if (this.pusher) {
this.pusher.send(`HQ Question ${this.questionNumber}`, `Google Weights ${searchOutput}`);
this.pusher.sendAll(`HQ Question ${this.questionNumber}`, `Google Weights ${searchOutput}`);
}
this.googleWeightsPosted = true;
}
@@ -133,7 +133,7 @@ export default class Scraper {
this.broadcastMessage(`**Normalized Weights + Threshold Deltas**\`\`\`${weights}\t\t${delta}\t\t${suggestion}\`\`\``);
if (this.pusher && this.ticker === PUSH_SUGGESTION_SECONDS) {
this.pusher.send(`HQ Question ${this.questionNumber}`, `${suggestion} ${weights}`);
this.pusher.sendAll(`HQ Question ${this.questionNumber}`, `${suggestion} ${weights}`);
}
}

77
Summary.js Normal file
View File

@@ -0,0 +1,77 @@
import request from 'request';
import { accounts } from './accounts.json';
import { HEADERS, SERVER_URL } from './config';
export default class Summary {
constructor() {
this.logger = null;
this.totalBalance = parseFloat("0.00");
this.totalCash = parseFloat("0.00");
this.playersReturned = 0;
}
async run() {
console.log(`You have ${accounts.length} players available!`);
accounts.forEach(this.getPlayerSummary.bind(this));
const getSummary = setInterval(() => {
if (this.playersReturned === accounts.length) {
console.log(`\nTotal Available Cash: $${this.totalBalance.toFixed(2)}`);
console.log(`Total Cash Won Historically: $${this.totalCash.toFixed(2)}`);
clearInterval(getSummary);
}
}, 1000);
}
getPlayerSummary(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 } = result;
let gamesPlayed = 0;
let winCount = 0;
let winRatio = 0;
let formatString = '%s';
if (result.gamesPlayed) {
gamesPlayed = result.gamesPlayed;
winCount = result.winCount;
winRatio = Math.round(winCount / gamesPlayed * 100);
const getColor = (winRatio) => {
if (winRatio === 0) return '37'; // white
if (winRatio < 33) return '33'; // yellow
if (winRatio < 66) return '36'; // cyan
return '32'; // green
};
formatString = `\x1b[1m\x1b[${getColor(winRatio)}m%s\x1b[0m`;
}
console.log(`\n\nPlayer ${player.username}:`);
console.log('===================================');
console.log(` ${winRatio}% Won (${gamesPlayed} played with ${winCount} wins)`);
console.log(` ${lives} lives remaining!`);
const balance = result.leaderboard.unclaimed;
const { total } = result.leaderboard;
this.totalBalance += parseFloat(balance.replace('$',''));
this.totalCash += parseFloat(total.replace('$',''));
console.log(` Account Balance: ${balance}`);
console.log(` Total Cash Won: ${total}`);
console.log('===================================');
} catch (error) {
console.log('Error\n', error.toString('utf8'));
}
});
}
}

View File

@@ -1,6 +1,9 @@
import prompts from 'prompts';
import Trivia from './trivia';
import Creator from './Creator';
import Summary from './Summary';
import * as config from './config';
import { LOG_TYPES } from './constants';
@@ -8,7 +11,7 @@ const choices = [
{ title: 'Play Trivia', select: () => new Trivia().run() },
{ title: 'Create Account', select: () => console.log('Not available yet!') },
{ title: 'Update Weekly Lives', select: () => console.log('Not available yet!') },
{ title: 'Show Player Summaries', select: () => console.log('Not available yet!') },
{ title: 'Show Player Summaries', select: () => new Summary().run() },
{ title: 'Cashout', select: () => console.log('Not available yet!') },
];

View File

@@ -182,6 +182,7 @@ export default class Trivia {
this.pusher = new Pusher(this.logger);
this.pusher.connect();
this.pusher.send('HQ Game Started', `${this.players.length} of your players are playing for ${toUSD(this.game.prize)} tonight!`);
this.pusher.sendAll('HQ Game Started', `Tonight's game is for ${toUSD(this.game.prize)}!`);
if (ENABLE_DISCORD) {
this.scraper.pusher = this.pusher;
@@ -319,6 +320,11 @@ export default class Trivia {
this.autoSend.call(this, questionId, answers, remainingPlayers);
}, timeLeftMs - TIME_EXPIRE_BUFFER);
} else {
let ticker = 1;
const staggerInterval = setInterval(() => {
ticker++;
}, 1000);
let previousChoices = null;
read({
prompt: 'My Answer Choice:',
default: '1,2,3',
@@ -333,22 +339,42 @@ export default class Trivia {
return;
}
const choices = result.trim().split(/,| |;/);
switch (choices.length) {
case 1:
this.sendAnswers(questionId, answers, [choices[0]], players);
break;
case 2:
this.sendAnswers(questionId, answers, [choices[0], choices[1]], players);
break;
case 3:
this.sendAnswers(questionId, answers, [choices[0], choices[1], choices[2]], players);
break;
default:
this.logger.log('You sent too many answer choices!', LOG_TYPES.ERROR);
this.autoSend(questionId, answers);
break;
}
previousChoices = result;
read({
prompt: 'Confirm Choice:',
default: previousChoices || '1,2,3',
input: process.stdin,
output: process.stdout,
timeout: timeLeftMs - (ticker * 1000) - TIME_EXPIRE_BUFFER,
}, (error, result) => {
if (error) {
process.stdout.write('\n');
this.logger.log('Time buffer expired. Auto-sending answers!');
this.autoSend(questionId, answers, players);
return;
}
let choices = result.trim().split(/,| |;/);
if (!choices && previousChoices) {
choices = previousChoices.trim().split(/,| |;/);
}
switch (choices.length) {
case 1:
this.sendAnswers(questionId, answers, [choices[0]], players);
break;
case 2:
this.sendAnswers(questionId, answers, [choices[0], choices[1]], players);
break;
case 3:
this.sendAnswers(questionId, answers, [choices[0], choices[1], choices[2]], players);
break;
default:
this.logger.log('You sent too many answer choices!', LOG_TYPES.ERROR);
this.autoSend(questionId, answers, players);
break;
}
});
});
}
}