diff --git a/config.json b/config.json index 6a7eff7..b3111e6 100644 --- a/config.json +++ b/config.json @@ -98,7 +98,7 @@ }, "hq": { "autoSplit": true, - "splitThreshold": 65, + "splitThreshold": 0.65, "autoCheckpoints": false, "checkpointThreshold": 2 } diff --git a/src/classes/SwagClient.js b/src/classes/SwagClient.js new file mode 100644 index 0000000..2b354c8 --- /dev/null +++ b/src/classes/SwagClient.js @@ -0,0 +1,63 @@ +import request from 'request'; + +export default class SwagClient { + constructor() { + this.apiUrl = 'api.playswagiq.com'; + this.appUrl = 'app.swagbucks.com'; + this.socketUrl = 'wss://api.playswagiq.com/sock/1'; + this.decryptionKey = '24fcc0515bffdcb56e3dfa48884dad7e020d985ae7450d605376b3547197b8b6'; + this.secret = 'f7Q3QGSUOYuH9wO9m8kVRGdS1ij0hWrb'; + this.headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + Connection: 'keep-alive', + Accept: '*/*', + 'User-Agent': 'SwagIQ-iOS/116', + 'Accept-Language': 'en-US;q=1', + 'Accept-Encoding': 'br, gzip, deflate', + }; + this.socketHeaders = { + 'Accept-Encoding': 'gzip', + 'User-Agent': 'SwagIQ-iOS/116', + }; + } + + async requestWithEncoding(url, headers, body) { + return new Promise((resolve, reject) => { + const bodyQueryString = queryString.stringify(body).replace('%2F', '/'); + const options = { + url, + headers: { + ...this.headers, + 'Content-Length': bodyQueryString.length, + }, + body: bodyQueryString, + }; + + const req = request.post(options); + + req.on('response', (res) => { + const chunks = []; + res.on('data', (chunk) => { + chunks.push(chunk); + }); + + res.on('end', () => { + const buffer = Buffer.concat(chunks); + const encoding = res.headers['content-encoding']; + + if (encoding === 'gzip') { + zlib.gunzip(buffer, (err, decoded) => { + resolve(parseResult(err, decoded && decoded.toString())); + }); + } else if (encoding === 'deflate') { + zlib.inflate(buffer, (err, decoded) => { + resolve(parseResult(err, decoded && decoded.toString())); + }); + } else { + resolve(parseResult(null, buffer.toString())); + } + }); + }); + }); + }; +} diff --git a/src/modules/hq-discord.js b/src/modules/hq-discord.js index b581917..8a491be 100644 --- a/src/modules/hq-discord.js +++ b/src/modules/hq-discord.js @@ -2,7 +2,9 @@ import { MessageEmbed, MessageAttachment } from 'discord.js'; import Client from '../classes/Client'; import { - discord, emuOnly, logs, logChannel, + discord, + emuOnly, + hq, } from '../../config'; import { getTokens } from '../utils/tokens'; import search from '../utils/google'; @@ -68,9 +70,23 @@ export default async (client, prize) => { m.edit(embed); }, 2000); setTimeout(() => { + const total = client.weights.hq.reduce((a, b) => a + b); const bestAnswer = client.weights.hq.indexOf(Math.max(...client.weights.hq)); - client.hqClients.forEach(c => c.submitAnswer(bestAnswer)); - }, 10000); + const bestAnswerPercentage = client.weights.hq[bestAnswer] / total; + const canAnswer = client.hqClients.filter(c => c.inTheGame); + const splitArr = [...canAnswer]; + if ((bestAnswerPercentage < hq.splitThreshold) && hq.autoSplit) { + const arr = [...client.weights.hq].splice(bestAnswer, 1); + const secondBestAnswer = arr.indexOf(Math.max(...arr)); + const secondBestAnswerPercentage = secondBestAnswer / total; + const answers = [bestAnswerPercentage, secondBestAnswerPercentage]; + answers.forEach((a, i) => { + const num = Math.round(canAnswer.length * a); + const clients = splitArr.slice(0, num <= splitArr.length ? num : splitArr.length); + clients.forEach(c => c.submitAnswer(i === 0 ? bestAnswer : secondBestAnswer)); + }); + } else client.hqClients.forEach(c => c.submitAnswer(bestAnswer)); + }, 11000); setTimeout(() => { clearInterval(i); }, 12000); @@ -114,9 +130,14 @@ export default async (client, prize) => { } }); incorrectUsers.forEach((u) => { - if (u.weight < 0) return; + if (u.weight < 0) { + client.db.updateWeight(u.id, modifier); + client.db.questionCorrect(u.id, true); + return false; + } client.db.updateWeight(u.id, -modifier); client.db.questionCorrect(u.id, false); + return true; }); } if (json.type === 'checkpoint') { @@ -141,7 +162,7 @@ export default async (client, prize) => { const image = genResults('HQ Trivia', winners, winnings, `${correctCount}/${questionCount}`); const attachment = new MessageAttachment(image); if (!emuOnly) channels.forEach(c => c.send(attachment)); - if (logs) client.channels.get(logChannel).send(attachment); + if (discord.logs) client.channels.get(discord.logChannel).send(attachment); client.activeGames.splice(client.activeGames.indexOf('hq-trivia'), 1); client.hqClients = []; });