Show prize and potential winnings per question

This commit is contained in:
eTronic
2018-06-27 12:36:49 -04:00
parent 35cb911f9f
commit 289b0e2834

View File

@@ -17,7 +17,11 @@ let playersInGame = players.map((player) => new Account(player));
const removePlayer = (userId) => {
playersInGame = playersInGame.filter((player) => player.userId !== userId);
}
};
const toUSD = (currency) => {
return currency.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
};
request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
const result = JSON.parse(body);
@@ -26,6 +30,10 @@ request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
console.log('Game Not Active; Using debug server instead...');
}
const { broadcastId = 45589, socketUrl = 'wss://hqecho.herokuapp.com/' } = result.broadcast || {};
const { prize } = result || {};
const prizeInUSD = toUSD(prize || 0);
console.log(`Tonight's prize is ${prizeInUSD}!`);
const options = {
headers: {
Authorization: `Bearer ${INIT_TOKEN}`,
@@ -53,39 +61,41 @@ request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
};
const connectPlayer = (player) => {
let pTimeout = null;
const playerOptions = {
headers: {
Authorization: `Bearer ${player.accessToken}`,
},
};
const pws = new WebSocket(socketUrl, playerOptions);
const ping = (ws, reconnect) => {
try {
ws.ping();
pTimeout = setTimeout(() => {
ws.terminate();
if (!player.socket) {
let pTimeout = null;
const playerOptions = {
headers: {
Authorization: `Bearer ${player.accessToken}`,
},
};
const pws = new WebSocket(socketUrl, playerOptions);
const ping = (ws, reconnect) => {
try {
ws.ping();
pTimeout = setTimeout(() => {
ws.terminate();
console.log('\x1b[1m\x1b[33m%s\x1b[0m', `Player ${player.username} connection closed. Attempting to reconnect...`);
reconnect();
}, 2000);
} catch (error) {
console.log('\x1b[1m\x1b[33m%s\x1b[0m', `Player ${player.username} connection closed. Attempting to reconnect...`);
reconnect();
}, 2000);
} catch (error) {
console.log('\x1b[1m\x1b[33m%s\x1b[0m', `Player ${player.username} connection closed. Attempting to reconnect...`);
reconnect();
}
};
const pong = () => {
clearTimeout(pTimeout);
};
pws.on('pong', pong);
pws.on('open', () => {
setInterval(() => ping(pws, () => connectPlayer(player)), 10000);
console.log('\x1b[1m\x1b[36m%s\x1b[0m', `\nPlayer ${player.username} has connected to websocket`);
pws.send(JSON.stringify({ type:'subscribe', broadcastId }), () => {
console.log('\x1b[36m%s\x1b[0m', `\tPlayer ${player.username} has subscribed to game`);
player.activate(broadcastId, pws);
}
};
const pong = () => {
clearTimeout(pTimeout);
};
pws.on('pong', pong);
pws.on('open', () => {
setInterval(() => ping(pws, () => connectPlayer(player)), 10000);
console.log('\x1b[1m\x1b[36m%s\x1b[0m', `\nPlayer ${player.username} has connected to websocket`);
pws.send(JSON.stringify({ type:'subscribe', broadcastId }), () => {
console.log('\x1b[36m%s\x1b[0m', `\tPlayer ${player.username} has subscribed to game`);
player.activate(broadcastId, pws);
});
});
});
}
};
const connect = () => {
@@ -115,7 +125,7 @@ request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
}
if (type === 'questionSummary') {
const questionSummary = JSON.parse(data);
const { answerCounts } = questionSummary;
const { answerCounts, advancingPlayersCount } = questionSummary;
const index = answerCounts.findIndex(answer => answer.correct);
const correctAnswer = answerCounts[index];
@@ -130,12 +140,21 @@ request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
const answerText = correctAnswer.text || correctAnswer.answer || 'undefined';
console.log('\x1b[1m\x1b[32m%s\x1b[0m', `\nAnswer was ${index + 1}: ${answerText}`);
if (wrongPlayers.length > 0) {
console.log('\x1b[1m\x1b[31m%s\x1b[0m', `${wrongPlayers.length} players have been eliminated!`);
if (wrongPlayers.length === playersInGame.length) {
console.log('\x1b[1m\x1b[31m%s\x1b[0m', `All remaining ${wrongPlayers.length} players have been eliminated!`);
} else if (wrongPlayers.length > 0) {
console.log('\x1b[1m\x1b[33m%s\x1b[0m', `${wrongPlayers.length} players have been eliminated!`);
} else if (playersInGame.length > 0) {
console.log('\x1b[1m\x1b[32m%s\x1b[0m', 'No players have been eliminated!');
}
if (advancingPlayersCount && advancingPlayersCount > 0) {
const potentialWinnings = (prize / advancingPlayersCount);
const potentialPerPlayer = toUSD(potentialWinnings);
const potentialTotal = toUSD(potentialWinnings * (playersInGame.length - wrongPlayers.length));
console.log(`\nPotential winnings: ${potentialTotal} (${potentialPerPlayer} per player remaining)`);
}
wrongPlayers.forEach(userId => removePlayer(userId));
}
if (type !== 'question') return;