-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Skyblock Government API and related structures
- Loading branch information
Showing
8 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters