const EventEmitter = require("events"); const Reward = require("./Reward"); class Quest extends EventEmitter { constructor(configuration, doneCallback) { super(); this.id = configuration.id; this.currentComplete = configuration.completedAmount; this.maximumComplete = configuration.maximumComplete; this.task = JSON.parse(JSON.stringify(configuration.task)); this.reward = new Reward({ distributionType: 2, baseReward: configuration.reward }); this.name = configuration.name || "Quest without a name"; this.finishedCount = 0; this.totalTasksCount = Object.values(this.task).reduce((pre, cur) => pre + cur.length, 0); this.doneCallback = doneCallback; } adjustTask(events, amount) { events.forEach(type => { const tasksInCategory = this.task[type.taskCategoryID]; if (!tasksInCategory) return tasksInCategory.forEach(task => { if (task.isFinished) return if (type.typeID === task.typeID && type.entityTypeID === task.entityTypeID) { task.amount += amount; if (task.requiredAmount <= task.amount) { task.amount = task.requiredAmount; task.isFinished = true; this.finishedCount++; }; }; }); }); if (this.finishedCount === this.totalTasksCount) { this.doneCallback(this.id); }; } getTasks() { const taskTypes = Object.keys(this.task); return taskTypes.map(t => this.task[t]); } getProgression() { const taskTypes = Object.keys(this.task); const progression = []; taskTypes.forEach(t => this.task[t].forEach((c, i) => { progression.push({ amount: c.amount, index: i, type: t }); })); return progression; } }; module.exports = Quest;