forked from RanvierMUD/bundle-example-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer-events.js
98 lines (85 loc) · 3.15 KB
/
player-events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const humanize = (sec) => { return require('humanize-duration')(sec, { round: true }); };
const { Broadcast: B, Logger, SkillErrors } = require('ranvier');
const Combat = require('@bundles/bundle-example-combat/lib/Combat');
const CombatErrors = require('@bundles/bundle-example-combat/lib/CombatErrors');
module.exports = {
listeners: {
useAbility: state => function (ability, args) {
if (!this.playerClass.hasAbility(ability.id)) {
return B.sayAt(this, 'Your class cannot use that ability.');
}
if (!this.playerClass.canUseAbility(this, ability.id)) {
return B.sayAt(this, 'You have not yet learned that ability.');
}
let target = null;
if (ability.requiresTarget) {
if (!args || !args.length) {
if (ability.targetSelf) {
target = this;
} else if (this.isInCombat()) {
target = [...this.combatants][0];
} else {
target = null;
}
} else {
try {
const targetSearch = args.split(' ').pop();
target = Combat.findCombatant(this, targetSearch);
} catch (e) {
if (
e instanceof CombatErrors.CombatSelfError ||
e instanceof CombatErrors.CombatNonPvpError ||
e instanceof CombatErrors.CombatInvalidTargetError ||
e instanceof CombatErrors.CombatPacifistError
) {
return B.sayAt(this, e.message);
}
Logger.error(e.message);
}
}
if (!target) {
return B.sayAt(this, `Use ${ability.name} on whom?`);
}
}
try {
ability.execute(args, this, target);
} catch (e) {
if (e instanceof SkillErrors.CooldownError) {
if (ability.cooldownGroup) {
return B.sayAt(this, `Cannot use ${ability.name} while ${e.effect.skill.name} is on cooldown.`);
}
return B.sayAt(this, `${ability.name} is on cooldown. ${humanize(e.effect.remaining)} remaining.`);
}
if (e instanceof SkillErrors.PassiveError) {
return B.sayAt(this, `That skill is passive.`);
}
if (e instanceof SkillErrors.NotEnoughResourcesError) {
return B.sayAt(this, `You do not have enough resources.`);
}
Logger.error(e.message);
B.sayAt(this, 'Huh?');
}
},
/**
* Handle player leveling up
*/
level: state => function () {
const abilities = this.playerClass.abilityTable;
if (!(this.level in this.playerClass.abilityTable)) {
return;
}
const newSkills = abilities[this.level].skills || [];
for (const abilityId of newSkills) {
const skill = state.SkillManager.get(abilityId);
B.sayAt(this, `<bold><yellow>You can now use skill: ${skill.name}.</yellow></bold>`);
skill.activate(this);
}
const newSpells = abilities[this.level].spells || [];
for (const abilityId of newSpells) {
const spell = state.SpellManager.get(abilityId);
B.sayAt(this, `<bold><yellow>You can now use spell: ${spell.name}.</yellow></bold>`);
}
}
}
};