UNPKG

osrs-tools

Version:

A comprehensive TypeScript library for Old School RuneScape (OSRS) data and utilities, including quest data, skill requirements, and game item information

172 lines (171 loc) 5.57 kB
/** * Represents an Old School RuneScape account with various properties and methods to access skills, bosses, clues, and other relevant data. * The class is designed to be initialized from a JSON object that may come from an API response or other data source, and provides methods to retrieve specific information about the account. * It also includes a method to convert the account data back into a JSON format, which can be useful for saving or transmitting the account information. */ class OsrsAccount { #name; #combatLevel; #questPoints; #skills; #skillsDetail; #bosses; #clues; #bountyHunter; #lastManStanding; #pvpArena; #soulWarsZeal; #riftsClosed; #colosseumGlory; #collectionsLogged; #leaguePoints; #deadmanPoints; constructor(data) { this.#name = data.name; this.#combatLevel = data.combatLevel; this.#questPoints = data.questPoints; this.#skills = data.skills; this.#skillsDetail = data.skillsDetail; this.#bosses = data.bosses; this.#clues = data.clues; this.#bountyHunter = data.bountyHunter; this.#lastManStanding = data.lastManStanding; this.#pvpArena = data.pvpArena; this.#soulWarsZeal = data.soulWarsZeal; this.#riftsClosed = data.riftsClosed; this.#colosseumGlory = data.colosseumGlory; this.#collectionsLogged = data.collectionsLogged; this.#leaguePoints = data.leaguePoints; this.#deadmanPoints = data.deadmanPoints; } static fromJson(json) { const name = json.name || ""; const combatLevel = json.main?.combatLevel ?? json.combatLevel ?? 3; const questPoints = json.main?.questPoints ?? json.questPoints ?? 0; // Accept both lower and upper case skill keys const skills = {}; const skillsDetail = {}; const srcSkills = json.main?.skills || json.skills || {}; for (const key of Object.keys(srcSkills)) { skills[key.toLowerCase()] = { level: srcSkills[key].level }; skillsDetail[key.toLowerCase()] = { level: srcSkills[key].level, rank: srcSkills[key].rank, xp: srcSkills[key].xp, }; } const bosses = json.bosses ? { ...json.bosses } : undefined; const clues = json.clues ? { ...json.clues } : undefined; const bountyHunter = json.bountyHunter ? { ...json.bountyHunter } : undefined; const lastManStanding = json.lastManStanding; const pvpArena = json.pvpArena; const soulWarsZeal = json.soulWarsZeal; const riftsClosed = json.riftsClosed; const colosseumGlory = json.colosseumGlory; const collectionsLogged = json.collectionsLogged; const leaguePoints = json.leaguePoints; const deadmanPoints = json.deadmanPoints; return new OsrsAccount({ bosses, bountyHunter, clues, collectionsLogged, colosseumGlory, combatLevel, deadmanPoints, lastManStanding, leaguePoints, name, pvpArena, questPoints, riftsClosed, skills, skillsDetail, soulWarsZeal, }); } get name() { return this.#name; } get combatLevel() { return this.#combatLevel; } get questPoints() { return this.#questPoints; } get skills() { return this.#skills; } get skillsDetail() { return this.#skillsDetail; } get bosses() { return this.#bosses; } get clues() { return this.#clues; } get bountyHunter() { return this.#bountyHunter; } get lastManStanding() { return this.#lastManStanding; } get pvpArena() { return this.#pvpArena; } get soulWarsZeal() { return this.#soulWarsZeal; } get riftsClosed() { return this.#riftsClosed; } get colosseumGlory() { return this.#colosseumGlory; } get collectionsLogged() { return this.#collectionsLogged; } get leaguePoints() { return this.#leaguePoints; } get deadmanPoints() { return this.#deadmanPoints; } getSkill(skillName) { return this.#skills[skillName.toLowerCase()]; } getSkillDetail(skillName) { return this.#skillsDetail?.[skillName.toLowerCase()]; } getBossScore(bossName) { return this.#bosses?.[bossName]; } getClueScore(clueType) { return this.#clues?.[clueType]; } toString() { return `${this.#name}:\nCombat Level: ${this.#combatLevel}\nQuestPoints: ${this.#questPoints}`; } toJson() { return { bosses: this.#bosses, bountyHunter: this.#bountyHunter, clues: this.#clues, collectionsLogged: this.#collectionsLogged, colosseumGlory: this.#colosseumGlory, combatLevel: this.#combatLevel, deadmanPoints: this.#deadmanPoints, lastManStanding: this.#lastManStanding, leaguePoints: this.#leaguePoints, name: this.#name, pvpArena: this.#pvpArena, questPoints: this.#questPoints, riftsClosed: this.#riftsClosed, skills: this.#skills, skillsDetail: this.#skillsDetail, soulWarsZeal: this.#soulWarsZeal, }; } } export { OsrsAccount };