Skip to content

Commit

Permalink
Add Skyblock Government API and related structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Feb 21, 2024
1 parent 4012452 commit 83ebcb2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/API/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ module.exports = {
getStatus: require('./getStatus'),
getWatchdogStats: require('./getWatchdogStats'),
getEndedSkyblockAuctions: require('./skyblock/getEndedSkyblockAuctions'),

getSkyblockAuctions: require('./skyblock/getSkyblockAuctions'),
getSkyblockAuctionsByPlayer: require('./skyblock/getSkyblockAuctionsByPlayer'),
getSkyblockBazaar: require('./skyblock/getSkyblockBazaar'),
getSkyblockGovernment: require('./skyblock/getSkyblockGovernment'),
getSkyblockMember: require('./skyblock/getSkyblockMember'),
getSkyblockNews: require('./skyblock/getSkyblockNews'),
getSkyblockProfiles: require('./skyblock/getSkyblockProfiles')
Expand Down
8 changes: 8 additions & 0 deletions src/API/skyblock/getSkyblockGovernment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = async function () {
const GovernmentData = require('../../structures/Skyblock/Static/Government.js');

const res = await this._makeRequest('/resources/skyblock/election');
if (res.raw) return res;

return new GovernmentData(res);
};
7 changes: 7 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ class Client extends EventEmitter {
* })
* .catch(console.log);
*/
/**
* Allows you to get SB government
* @method
* @name Client#getSkyblockGovernment
* @param {MethodOptions} [options={}] Options
* @return {Promise<Government>}
*/
/**
* Allows you to get skyblock news
* @method
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports = {
SkyblockMember: require('./structures/SkyBlock/SkyblockMember.js'),
SkyblockInventoryItem: require('./structures/SkyBlock/SkyblockInventoryItem.js'),
SkyblockPet: require('./structures/SkyBlock/SkyblockPet'),
SkyblockGovernment: require('./structures/Skyblock/Static/Government.js'),
SkyblockCandidate: require('./structures/Skyblock/Static/Candidate.js'),

/* Skyblock Auctions */
BaseAuction: require('./structures/SkyBlock/Auctions/BaseAuction.js'),
Expand Down
39 changes: 39 additions & 0 deletions src/structures/SkyBlock/Static/Candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Candidate class
*/
class Candidate {
/**
* Constructor
* @param {Object} data data
* @param {boolean} [isMayor=false] if this candidate is the current mayor
*/
constructor(data, isMayor = false) {
/**
* Mayor's name
* @type {string}
*/
this.name = data.name;
/**
* Mayor's Key Benefit (in 1 word)
* @type {string}
*/
this.keyBenefit = data.key;
/**
* Perks
* @type {Record<'name'|'description',string>[]}
*/
this.perks = data.perks;
/**
* If this candidate is the current mayor
* @type {boolean}
*/
this.isMayor = isMayor || false;
/**
* The number of votes received by this candidate
* @type {number}
*/
this.votesReceived = parseInt(data.votes, 10) || 0;
}
}

module.exports = Candidate;
55 changes: 55 additions & 0 deletions src/structures/SkyBlock/Static/Government.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Candidate = require('./Candidate');

/**
* SB Government Class
*/
class GovernmentData {
/**
* constructor
* @param {Object} data
*/
constructor(data) {
/**
* Last time this resource was updated
* @type {number}
*/
this.lastUpdatedTimestamp = parseInt(data.lastUpdated, 10);
/**
* Last time this resource was updated, as Date
* @type {Date|null}
*/
this.lastUpdatedAt = new Date(this.lastUpdatedTimestamp);
const lastElectionResults = data.mayor.election.candidates.map((x) => new Candidate(x, x.name === data.mayor.name));
/**
* A map of last election results for each candidate
* Sorted ascendingly by votes received
* @type {Map<string, Candidate>}
*/
this.lastElectionResults = new Map(lastElectionResults.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x]));
/**
* The mayor
* @type {Candidate}
*/
this.mayor = this.lastElectionResults.get(data.mayor.name);
/**
* The year the mayor will be running for
* @type {number}
*/
this.runningYear = parseInt(data.mayor.election.year, 10) || 0;
const thisElection = data.current.candidates.map((x) => new Candidate(x, x.name === data.mayor.name));
/**
* Current elections, valid for next year
* Sorted ascendingly by votes received
* RESULTS MIGHT BE TEMPORARY
* @type {Map<string, Candidate>}
*/
this.currentElectionResults = new Map(thisElection.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x]));
/**
* The year the current election will be effective for
* @type {number|null}
*/
this.currentElectionFor = parseInt(data.current.year, 10) || null;
}
}

module.exports = GovernmentData;
19 changes: 19 additions & 0 deletions tests/Client#getSkyblockGoverment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-undef */
const { SkyblockGovernment } = require('../src/');
const { client } = require('./Client.js');
const { expect } = require('chai');

describe('Client#getSkyblockMember', async () => {
let goverment;
describe('Random (1)', async () => {
it('expect not to throw', async () => {
goverment = await client.getSkyblockGoverment();
});
it('should be an objecct', () => {
expect(goverment).to.be.an('object');
});
it('required keys should exist', () => {
expect(goverment).instanceOf(SkyblockGovernment);
});
});
});
23 changes: 23 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ declare module 'hypixel-api-reborn' {
* @description Allows you to get list of products
*/
getSkyblockBazaar(options?: methodOptions): Promise<Product[]>;
/**
* @description Gets data of skyblock government
*/
getSkyblockGovernment(options?: methodOptions): Promise<GovernmentData>;
/**
* @description Allows you to get skyblock news
*/
Expand Down Expand Up @@ -1836,6 +1840,25 @@ declare module 'hypixel-api-reborn' {
totalPrice: number;
orders: number;
}
class GovernmentData {
constructor(data: Record<string, unknown>);
lastUpdatedTimestamp: number;
lastUpdatedAt: Date | null;
lastElectionResults: Map<string, Candidate>;
mayor: Candidate;
runningYear: number;
currentElectionResults: Map<string, Candidate>;
currentElectionFor: number | null;
}
class Candidate {
constructor(data: Record<string, unknown>, isMayor?: boolean | undefined);
name: string;
keyBenefit: string;
perks: Record<'name' | 'description', string>[];
isMayor: boolean;
votesReceived: number;
toString(): string;
}
class WatchdogStats {
constructor(data: Record<string, unknown>);
byWatchdogTotal: number;
Expand Down

0 comments on commit 83ebcb2

Please sign in to comment.