Skip to content

Commit 83ebcb2

Browse files
committed
Add Skyblock Government API and related structures
1 parent 4012452 commit 83ebcb2

File tree

8 files changed

+155
-0
lines changed

8 files changed

+155
-0
lines changed

src/API/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module.exports = {
1010
getStatus: require('./getStatus'),
1111
getWatchdogStats: require('./getWatchdogStats'),
1212
getEndedSkyblockAuctions: require('./skyblock/getEndedSkyblockAuctions'),
13+
1314
getSkyblockAuctions: require('./skyblock/getSkyblockAuctions'),
1415
getSkyblockAuctionsByPlayer: require('./skyblock/getSkyblockAuctionsByPlayer'),
1516
getSkyblockBazaar: require('./skyblock/getSkyblockBazaar'),
17+
getSkyblockGovernment: require('./skyblock/getSkyblockGovernment'),
1618
getSkyblockMember: require('./skyblock/getSkyblockMember'),
1719
getSkyblockNews: require('./skyblock/getSkyblockNews'),
1820
getSkyblockProfiles: require('./skyblock/getSkyblockProfiles')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = async function () {
2+
const GovernmentData = require('../../structures/Skyblock/Static/Government.js');
3+
4+
const res = await this._makeRequest('/resources/skyblock/election');
5+
if (res.raw) return res;
6+
7+
return new GovernmentData(res);
8+
};

src/Client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ class Client extends EventEmitter {
313313
* })
314314
* .catch(console.log);
315315
*/
316+
/**
317+
* Allows you to get SB government
318+
* @method
319+
* @name Client#getSkyblockGovernment
320+
* @param {MethodOptions} [options={}] Options
321+
* @return {Promise<Government>}
322+
*/
316323
/**
317324
* Allows you to get skyblock news
318325
* @method

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = {
2727
SkyblockMember: require('./structures/SkyBlock/SkyblockMember.js'),
2828
SkyblockInventoryItem: require('./structures/SkyBlock/SkyblockInventoryItem.js'),
2929
SkyblockPet: require('./structures/SkyBlock/SkyblockPet'),
30+
SkyblockGovernment: require('./structures/Skyblock/Static/Government.js'),
31+
SkyblockCandidate: require('./structures/Skyblock/Static/Candidate.js'),
3032

3133
/* Skyblock Auctions */
3234
BaseAuction: require('./structures/SkyBlock/Auctions/BaseAuction.js'),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Candidate class
3+
*/
4+
class Candidate {
5+
/**
6+
* Constructor
7+
* @param {Object} data data
8+
* @param {boolean} [isMayor=false] if this candidate is the current mayor
9+
*/
10+
constructor(data, isMayor = false) {
11+
/**
12+
* Mayor's name
13+
* @type {string}
14+
*/
15+
this.name = data.name;
16+
/**
17+
* Mayor's Key Benefit (in 1 word)
18+
* @type {string}
19+
*/
20+
this.keyBenefit = data.key;
21+
/**
22+
* Perks
23+
* @type {Record<'name'|'description',string>[]}
24+
*/
25+
this.perks = data.perks;
26+
/**
27+
* If this candidate is the current mayor
28+
* @type {boolean}
29+
*/
30+
this.isMayor = isMayor || false;
31+
/**
32+
* The number of votes received by this candidate
33+
* @type {number}
34+
*/
35+
this.votesReceived = parseInt(data.votes, 10) || 0;
36+
}
37+
}
38+
39+
module.exports = Candidate;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const Candidate = require('./Candidate');
2+
3+
/**
4+
* SB Government Class
5+
*/
6+
class GovernmentData {
7+
/**
8+
* constructor
9+
* @param {Object} data
10+
*/
11+
constructor(data) {
12+
/**
13+
* Last time this resource was updated
14+
* @type {number}
15+
*/
16+
this.lastUpdatedTimestamp = parseInt(data.lastUpdated, 10);
17+
/**
18+
* Last time this resource was updated, as Date
19+
* @type {Date|null}
20+
*/
21+
this.lastUpdatedAt = new Date(this.lastUpdatedTimestamp);
22+
const lastElectionResults = data.mayor.election.candidates.map((x) => new Candidate(x, x.name === data.mayor.name));
23+
/**
24+
* A map of last election results for each candidate
25+
* Sorted ascendingly by votes received
26+
* @type {Map<string, Candidate>}
27+
*/
28+
this.lastElectionResults = new Map(lastElectionResults.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x]));
29+
/**
30+
* The mayor
31+
* @type {Candidate}
32+
*/
33+
this.mayor = this.lastElectionResults.get(data.mayor.name);
34+
/**
35+
* The year the mayor will be running for
36+
* @type {number}
37+
*/
38+
this.runningYear = parseInt(data.mayor.election.year, 10) || 0;
39+
const thisElection = data.current.candidates.map((x) => new Candidate(x, x.name === data.mayor.name));
40+
/**
41+
* Current elections, valid for next year
42+
* Sorted ascendingly by votes received
43+
* RESULTS MIGHT BE TEMPORARY
44+
* @type {Map<string, Candidate>}
45+
*/
46+
this.currentElectionResults = new Map(thisElection.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x]));
47+
/**
48+
* The year the current election will be effective for
49+
* @type {number|null}
50+
*/
51+
this.currentElectionFor = parseInt(data.current.year, 10) || null;
52+
}
53+
}
54+
55+
module.exports = GovernmentData;

tests/Client#getSkyblockGoverment.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-undef */
2+
const { SkyblockGovernment } = require('../src/');
3+
const { client } = require('./Client.js');
4+
const { expect } = require('chai');
5+
6+
describe('Client#getSkyblockMember', async () => {
7+
let goverment;
8+
describe('Random (1)', async () => {
9+
it('expect not to throw', async () => {
10+
goverment = await client.getSkyblockGoverment();
11+
});
12+
it('should be an objecct', () => {
13+
expect(goverment).to.be.an('object');
14+
});
15+
it('required keys should exist', () => {
16+
expect(goverment).instanceOf(SkyblockGovernment);
17+
});
18+
});
19+
});

typings/index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,10 @@ declare module 'hypixel-api-reborn' {
861861
* @description Allows you to get list of products
862862
*/
863863
getSkyblockBazaar(options?: methodOptions): Promise<Product[]>;
864+
/**
865+
* @description Gets data of skyblock government
866+
*/
867+
getSkyblockGovernment(options?: methodOptions): Promise<GovernmentData>;
864868
/**
865869
* @description Allows you to get skyblock news
866870
*/
@@ -1836,6 +1840,25 @@ declare module 'hypixel-api-reborn' {
18361840
totalPrice: number;
18371841
orders: number;
18381842
}
1843+
class GovernmentData {
1844+
constructor(data: Record<string, unknown>);
1845+
lastUpdatedTimestamp: number;
1846+
lastUpdatedAt: Date | null;
1847+
lastElectionResults: Map<string, Candidate>;
1848+
mayor: Candidate;
1849+
runningYear: number;
1850+
currentElectionResults: Map<string, Candidate>;
1851+
currentElectionFor: number | null;
1852+
}
1853+
class Candidate {
1854+
constructor(data: Record<string, unknown>, isMayor?: boolean | undefined);
1855+
name: string;
1856+
keyBenefit: string;
1857+
perks: Record<'name' | 'description', string>[];
1858+
isMayor: boolean;
1859+
votesReceived: number;
1860+
toString(): string;
1861+
}
18391862
class WatchdogStats {
18401863
constructor(data: Record<string, unknown>);
18411864
byWatchdogTotal: number;

0 commit comments

Comments
 (0)