osrs-tools
Version:
A comprehensive TypeScript library for Old School RuneScape (OSRS) data and utilities, including quest data, skill requirements, and game item information
100 lines (99 loc) • 3.05 kB
JavaScript
import { Skill, SKILL_METADATA } from '../model/account/Skill';
/**
* Utility functions for working with skills and skill metadata.
* Provides helper methods for validation, categorization, and metadata lookup.
*/
/**
* Get metadata for a specific skill.
* @param skill - The skill to get metadata for
* @returns The skill's metadata or undefined if not found
*/
export function getSkillMetadata(skill) {
return SKILL_METADATA[skill];
}
/**
* Check if a skill is valid.
* @param skillName - The skill name to validate
* @returns true if the skill exists, false otherwise
*/
export function isValidSkill(skillName) {
return skillName in SKILL_METADATA;
}
/**
* Get all skills in a specific category.
* @param category - The category to filter by
* @returns Array of skills in that category
*/
export function getSkillsByCategory(category) {
return Object.entries(SKILL_METADATA)
.filter(([_, meta]) => meta.category === category)
.map(([skill, _]) => skill);
}
/**
* Check if a skill level is valid.
* @param skill - The skill to check
* @param level - The level to validate
* @returns true if the level is between 1 and the skill's max level
*/
export function isValidLevel(skill, level) {
const meta = getSkillMetadata(skill);
if (!meta)
return false;
return level >= 1 && level <= meta.maxLevel;
}
/**
* Check if a skill can be boosted.
* @param skill - The skill to check
* @returns true if the skill is boostable
*/
export function isBoostable(skill) {
return getSkillMetadata(skill)?.isBoostable ?? false;
}
/**
* Get the max level for a skill.
* @param skill - The skill to check
* @returns The maximum level for this skill, or 99 as default
*/
export function getMaxLevel(skill) {
return getSkillMetadata(skill)?.maxLevel ?? 99;
}
/**
* Get category for a skill.
* @param skill - The skill to check
* @returns The category of the skill or undefined if not found
*/
export function getSkillCategory(skill) {
return getSkillMetadata(skill)?.category;
}
/**
* Get all available skills.
* @returns Array of all skills in the game
*/
export function getAllSkills() {
return Object.keys(SKILL_METADATA);
}
/**
* Get all combat-related skills.
* @returns Array of combat category skills
*/
export function getCombatSkills() {
return getSkillsByCategory("combat");
}
/**
* Get the total combat level from individual combat skills.
* Combat level formula: (Attack + Defence + 2*Strength + 2*Magic + 2*Ranged + Prayer) / 4 + 1
* This is a helper; actual calculation depends on account implementation.
* @param skillLevels - Object mapping skill names to levels
* @returns Calculated combat level
*/
export function calculateCombatLevel(skillLevels) {
const get = (skill) => skillLevels[skill] ?? 0;
const base = (get(Skill.Attack) +
get(Skill.Defence) +
2 * get(Skill.Strength) +
2 * get(Skill.Magic) +
2 * get(Skill.Ranged) +
get(Skill.Prayer)) /
4;
return Math.floor(base) + 1;
}