Skip to content

Commit

Permalink
Merge pull request #151 from ChiefChippy2/master
Browse files Browse the repository at this point in the history
Fixes + CTW, Pet Consumables, SW Packages, Oscillation
  • Loading branch information
StavZ authored May 2, 2021
2 parents 107e18a + f192838 commit 72294ee
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Client extends EventEmitter {
*/
this.cache = requests.cache;
clients.push(this);
rateLimit.init(this.getKeyInfo(), this.options).then(()=>this.emit('ready'));
rateLimit.init(this.getKeyInfo(), this.options, this).then(()=>this.emit('ready'));
}
/**
* Private function - make request
Expand Down
2 changes: 1 addition & 1 deletion src/Private/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = class Requests {
async request (endpoint, options = {}) {
options.headers = {'API-Key': this.key, ...options.headers};
const res = await fetch(BASE_URL + endpoint, options);
if (res.status === 522) throw new Error(Errors.ERROR_STATUSTEXT.replace(/{statustext}/, '522 Connection Timed Out'));
if (res.status >= 500 && res.status < 528) throw new Error(Errors.ERROR_STATUSTEXT.replace(/{statustext}/, `Server Error : ${res.status} ${res.statusText}`));
const parsedRes = await res.json().catch(() => {
throw new Error(Errors.INVALID_RESPONSE_BODY);
});
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Boosters/Booster.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Booster {
* Stacked by ( if any )
* @type {string[]}
*/
this.stackers = Array.isArray(data.stacked) ? data.stacked : [];
this.stackers = Array.isArray(data.stacked) ? Array.from(data.stacked) : [];
/**
* Possibly expired booster
* Works by checking if date.length is negative
Expand Down
4 changes: 2 additions & 2 deletions src/structures/Friend.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Friend {
*/
constructor (data, uuid) {
/**
* Friend request sender's UUID ( Whoever sent the FR )
* Friend request sender's UUID ( Whoever sent the FR ). This can be either the friend's or the player's uuid.
* @type {string}
*/
this.sender = data.uuidSender;
/**
* Friend request receiver's UUID ( Whoever received the FR )
* Friend request receiver's UUID ( Whoever received the FR ). This can be either the friend's or the player's uuid.
* @type {string}
*/
this.receiver = data.uuidReceiver;
Expand Down
23 changes: 9 additions & 14 deletions src/structures/Guild/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const GuildMember = require('./GuildMember');
const GuildRank = require('./GuildRank');
const Color = require('../Color');
const Game = require('../Game');
const getGuildLevel = require('../../utils/getGuildLevel');
const {getGuildLevel, parseHistory} = require('../../utils/guildExp');
/**
* Guild class
*/
Expand Down Expand Up @@ -131,7 +131,7 @@ class Guild {
this.legacyRank = !isNaN(data.legacyRanking) ? parseInt(data.legacyRanking + 1, 10) : 0;
/**
* Experience history per day, resets at 5 am UTC
* @type {Array<{day:String, exp: number}>}
* @type {Array<import('../../utils/guildExp').ExpHistory>}
*/
this.expHistory = calculateExpHistory(data);
/**
Expand Down Expand Up @@ -190,19 +190,14 @@ function totalWeeklyGexp (data) {
* @return {Array}
*/
function calculateExpHistory (data) {
const array = [];
const days = Object.keys(data.members[0].expHistory);
for (const day in Object.keys(data.members[0].expHistory)) {
if (Object.prototype.hasOwnProperty.call(data.members[0].expHistory, day)) {
let gexp = 0;
for (const member in data.members) {
if (Object.prototype.hasOwnProperty.call(data.member, member)) {
gexp += (data.members[member].expHistory[days[day]] || 0);
}
}
array.push({ day: days[day], exp: gexp });
const finalObj = {};
for (const day of Object.keys(data.members[0].expHistory)) {
let gexp = 0;
for (const member of data.members) {
gexp += (member.expHistory[day] || 0);
}
finalObj[day] = gexp;
}
return array;
return parseHistory(finalObj);
}
module.exports = Guild;
35 changes: 1 addition & 34 deletions src/structures/Guild/GuildMember.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const {parseHistory} = require('../../utils/guildExp');
/**
* GuildMember class
*/
Expand Down Expand Up @@ -62,38 +63,4 @@ class GuildMember {
}
}

const dateRegExp = /(\d{4})-(\d{2})-(\d{2})/;
/**
* Parses exp history
* @param {object} historyData History data from the API
* @returns {Array<ExpHistory>} Array of ExpJistory
*/
function parseHistory(historyData) {
return Object.entries(historyData).map((x, index) => ({
day: x[0],
date: parseDate(x[0].match(dateRegExp).slice(1).map((x) => parseInt(x, 10))) || undefined,
exp: x[1] || 0,
totalExp: Object.values(historyData).slice(0, index + 1).reduce((pV, cV) => pV + cV, 0)
}));
}

/**
* Parses date
* Because hypixel's oscillation precises that exp resets at 5 am UTC, the hour is set accordingly
* @param {number[]} date Date from regexp
* @returns {Date} Parsed Date
*/
function parseDate(date) {
date[1] -= 1;
return new Date(Math.round(new Date(new Date().setUTCFullYear(...date)).setUTCHours(5, 0, 0) / 1000) * 1000);
}

/**
* @typedef {object} ExpHistory
* @property {string} day String Date ( unparsed )
* @property {Date} date Parsed Date
* @property {number} exp Experience of the day
* @property {number} totalExp Experience earned from day 0 to this day
*/

module.exports = GuildMember;
34 changes: 20 additions & 14 deletions src/structures/MiniGames/Arcade.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// IMPORTANT : a lot of the properties from the API seem to be nonsense

const divide = require('../../utils/divide');
const { weekAB, monthAB } = require('../../utils/oscillation');
const { removeSnakeCaseString } = require('../../utils/removeSnakeCase');

/**
Expand Down Expand Up @@ -28,15 +29,15 @@ class Arcade {
*/
this.coins = data.coins || 0;
/**
* Weekly coins, a + b
* Weekly coins
* @type {number}
*/
this.weeklyCoins = parseInt((data.weekly_coins_a || 0) + (data.weekly_coins_b || 0));
this.weeklyCoins = parseInt(data[`weekly_coins_${weekAB()}`] || 0);
/**
* Monthly coins, a + b
* Monthly coins
* @type {number}
*/
this.monthlyCoins = parseInt((data.monthly_coins_a || 0) + (data.monthly_coins_b || 0));
this.monthlyCoins = parseInt(data[`monthly_coins_${monthAB()}`] || 0);
/**
* Hints Disabled
* @type {Boolean}
Expand Down Expand Up @@ -144,7 +145,7 @@ class Arcade {
* Blocking dead ( previously known as DayOne ) stats
* @type {BlockingDead}
*/
this.blockingDead = new BaseGame(data, 'dayone').extend('headshots', data.headshots_dayone);
this.blockingDead = new BaseGame(data, 'dayone').extend('headshots', data.headshots_dayone || 0);
/**
* Galaxy Wars stats
* @type {GalaxyWars}
Expand All @@ -155,12 +156,17 @@ class Arcade {
* OITQ / One In The Quiver stats
* @type {OITQ}
*/
this.oitq = this.oneInTheQuiver = new BaseGame(data, 'oneinthequiver').extend('bountyKills', data.bounty_kills_oneinthequiver);
this.oitq = this.oneInTheQuiver = new BaseGame(data, 'oneinthequiver').extend('bountyKills', data.bounty_kills_oneinthequiver || 0);
/**
* Zombies
* @type {Zombies}
*/
this.zombies = new Zombies(data);
/**
* Capture The Wool
* @type {{kills: number, captures: number}}
*/
this.captureTheWool = { 'kills': data.arcade_ctw_slayer || 0, 'captures': data.arcade_ctw_oh_sheep || 0 };
}
}
/**
Expand All @@ -176,22 +182,22 @@ class BaseGame {
* Wins
* @type {?number}
*/
this.wins = parseInt(data['wins_' + gameName]) || null;
this.wins = parseInt(data['wins_' + gameName]) || 0;
/**
* Kills, only available in combat games
* @type {?number}
*/
this.kills = parseInt(data['kills_' + gameName]) || null;
this.kills = parseInt(data['kills_' + gameName]) || 0;
/**
* Deaths, only available in combat games
* @type {?number}
*/
this.deaths = parseInt(data['deaths_' + gameName]) || null;
this.deaths = parseInt(data['deaths_' + gameName]) || 0;
/**
* Rounds Played, only available in Santa says, Simon Says, and HITW
* @type {?number}
*/
this.roundsPlayed = parseInt(data['rounds_' + gameName]) || null;
this.roundsPlayed = parseInt(data['rounds_' + gameName]) || 0;
}
/**
* Extend BaseGame without creating a new class
Expand Down Expand Up @@ -235,15 +241,15 @@ class GalaxyWars {
*/
this.shotsFired = data.sw_shots_fired || 0;
/**
* Total weekly kills ( a + b )
* Total weekly kills
* @type {number}
*/
this.weeklyKills = parseInt((data.weekly_kills_a || 0) + (data.weekly_kills_b || 0));
this.weeklyKills = parseInt(data[`weekly_kills_${weekAB()}`] || 0);
/**
* Total Monthly kills ( a + b )
* Total Monthly kills
* @type {number}
*/
this.monthlyKills = parseInt((data.monthly_kills_a || 0) + (data.monthly_kills_b || 0));
this.monthlyKills = parseInt(data[`monthly_kills_${monthAB()}`] || 0);
/**
* Attacker Kills
* @type {number}
Expand Down
Loading

0 comments on commit 72294ee

Please sign in to comment.