osrs-tools
Version:
A comprehensive TypeScript library for Old School RuneScape (OSRS) data and utilities, including quest data, skill requirements, and game item information
129 lines (128 loc) • 5.16 kB
JavaScript
/**
* SlayerMaster class represents a Slayer Master in the game.
* It encapsulates the properties and methods related to a Slayer Master,
* including the tasks they assign, their location, level requirements,
* and the ability to generate random assignments.
* Reference: https://oldschool.runescape.wiki/w/Duradel
*
*/
class SlayerMaster {
name; // Name of the Slayer Master
totalWeight; // Total weight of the tasks assigned to this Slayer Master. This is used to determine the likelihood of each task being assigned.
location = ''; // Location of the Slayer Master (optional, can be set later if needed)
minimumCombatLevel = 0; // Level requirement to access this Slayer Master (optional, can be set later if needed)
tasks; // Array of tasks assigned to this Slayer Master. This holds the actual tasks that players can be assigned to kill.
wikiUrl = ''; // URL for the Slayer Master (optional, can be set later if needed)
taskPoints; // Mapping of task intervals to points
eliteDiaryTaskPoints; // Mapping of task intervals to points
// Constructor implementation
constructor(nameOrOptions, tasks, location, levelRequirement, url, taskPoints, eliteDiaryTaskPoints) {
// Handle options object pattern (modern API)
if (typeof nameOrOptions === 'object' && !Array.isArray(nameOrOptions)) {
const options = nameOrOptions;
this.name = options.name;
this.tasks = options.tasks;
this.location = options.location || '';
this.minimumCombatLevel = options.minimumCombatLevel || 0;
this.wikiUrl = options.wikiUrl || '';
this.taskPoints = options.pointsTable || {};
this.eliteDiaryTaskPoints = options.eliteDiaryTaskPoints;
}
else {
// Handle positional arguments pattern (legacy API)
this.name = nameOrOptions;
this.tasks = tasks || [];
this.location = location || '';
this.minimumCombatLevel = levelRequirement || 0;
this.wikiUrl = url || '';
this.taskPoints = taskPoints || {};
this.eliteDiaryTaskPoints = eliteDiaryTaskPoints;
}
// Initialize tasks
this.tasks.forEach(task => {
task.weight = task.weight || 0; // Default weight to 0 if not provided
});
this.totalWeight = this.calculateTotalWeight();
}
/**
* Calculate the total weight of all tasks assigned to this Slayer Master.
* @returns {number} The total weight of all tasks assigned to this Slayer Master.
*/
calculateTotalWeight() {
if (this.tasks.length === 0)
return 0; // Return 0 if there are no tasks
return this.tasks.reduce((total, task) => {
return total + (task.weight || 0); // Sum up the weights of all tasks
}, 0);
throw new Error('Method not implemented.');
}
/**
* Get the points awarded for completing a task based on the interval.
* @param {number} taskInterval - The interval of the task (e.g., 1 for every task, 10 for every 10th task, etc.).
* @returns {number} The points awarded for the given task interval.
*/
getPointsForTaskInterval(taskInterval) {
return this.taskPoints[taskInterval] || 0; // Return the points for the interval or 0 if not defined
}
/**
* Get the location of the Slayer Master
*/
getLocation() {
return this.location;
}
/**
* Get the minimum combat level of the Slayer Master
*/
getMinimumCombatLevel() {
return this.minimumCombatLevel;
}
/**
* Get the wiki URL of the Slayer Master
*/
getWikiUrl() {
return this.wikiUrl;
}
/**
* Get a random task based on the weighting system of the tasks.
* Tasks with higher weights have a higher chance of being selected.
* @returns {Task | null} A randomly selected task or null if no tasks are available.
*/
getRandomTask() {
if (this.tasks.length === 0)
return null; // Return null if there are no tasks
const randomWeight = Math.random() * this.totalWeight; // Generate a random number between 0 and totalWeight
let cumulativeWeight = 0;
for (const task of this.tasks) {
cumulativeWeight += task.weight || 0; // Add the task's weight to the cumulative weight
if (randomWeight <= cumulativeWeight) {
return task; // Return the task if the random weight falls within its range
}
}
return null; // Fallback in case no task is selected (shouldn't happen if weights are correct)
}
/**
* Get the total weight of tasks
*/
getTotalWeight() {
return this.totalWeight;
}
/**
* Get Name of Slayer Master
*/
getName() {
return this.name;
}
/**
* Get Tasks
*/
getTasks() {
return this.tasks;
}
/**
* toString function
*/
toString() {
return `SlayerMaster :) WIP SlayerMaster:{\nname: ${this.name}\n}`;
}
}
export { SlayerMaster };