Skip to content

Commit c8c999e

Browse files
Merge pull request #162 from rune-js/skills-by-name
Adding support for selecting player skill info via skill name.
2 parents 99f5488 + c0e4553 commit c8c999e

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

src/plugins/commands/give-item-command.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ActionType, RunePlugin } from '@server/plugins/plugin';
22
import { commandAction } from '@server/world/actor/player/action/input-command-action';
33
import { cache } from '@server/game-server';
4+
import { itemIds } from '@server/world/config/item-ids';
45

56
const action: commandAction = (details) => {
67
const { player, args } = details;
@@ -12,7 +13,20 @@ const action: commandAction = (details) => {
1213
return;
1314
}
1415

15-
const itemId: number = args.itemId as number;
16+
const itemSearch: string = args.itemSearch as string;
17+
let itemId: number;
18+
19+
if(itemSearch.match(/^[0-9]+$/)) {
20+
itemId = parseInt(itemSearch, 10);
21+
} else {
22+
// @TODO nested item ids
23+
itemId = itemIds[itemSearch];
24+
}
25+
26+
if(isNaN(itemId)) {
27+
throw new Error(`Item name not found.`);
28+
}
29+
1630
let amount: number = args.amount as number;
1731

1832
if(amount > 2000000000) {
@@ -52,8 +66,8 @@ export default new RunePlugin({
5266
commands: [ 'give', 'item', 'spawn' ],
5367
args: [
5468
{
55-
name: 'itemId',
56-
type: 'number'
69+
name: 'itemSearch',
70+
type: 'string'
5771
},
5872
{
5973
name: 'amount',

src/plugins/commands/spawn-scenery-command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ const spawnSceneryAction: commandAction = (details) => {
1616
if(locationObjectSearch.match(/^[0-9]+$/)) {
1717
locationObjectId = parseInt(locationObjectSearch, 10);
1818
} else {
19+
// @TODO nested object ids
1920
locationObjectId = objectIds[locationObjectSearch];
2021
}
2122

2223
if(isNaN(locationObjectId)) {
23-
throw `Location object name not found.`;
24+
throw new Error(`Location object name not found.`);
2425
}
2526

2627
const objectType = args.objectType as number;

src/plugins/skills/firemaking-plugin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { itemOnItemAction } from '@server/world/actor/player/action/item-on-item-action';
22
import { world } from '@server/game-server';
3-
import { Skill } from '@server/world/actor/skills';
43
import { loopingAction } from '@server/world/actor/player/action/action';
54
import { LocationObject } from '@runejs/cache-parser';
65
import { Player } from '@server/world/actor/player/player';
@@ -52,7 +51,7 @@ const lightFire = (player: Player, position: Position, worldItemLog: WorldItem,
5251

5352
player.playAnimation(null);
5453
player.sendMessage(`The fire catches and the logs begin to burn.`);
55-
player.skills.addExp(Skill.FIREMAKING, burnExp);
54+
player.skills.addExp('firemaking', burnExp);
5655

5756
if(!player.walkingQueue.moveIfAble(-1, 0)) {
5857
if(!player.walkingQueue.moveIfAble(1, 0)) {
@@ -93,7 +92,8 @@ const action: itemOnItemAction = (details) => {
9392
player.removeItem(removeFromSlot);
9493
const worldItemLog = world.spawnWorldItem(log, player.position, player, 300);
9594

96-
if(player.metadata['lastFire'] && Date.now() - player.metadata['lastFire'] < 1200 && canChain(skillInfo.requiredLevel, player.skills.values[Skill.WOODCUTTING].level)) {
95+
if(player.metadata['lastFire'] && Date.now() - player.metadata['lastFire'] < 1200 &&
96+
canChain(skillInfo.requiredLevel, player.skills.getSkillLevel('firemaking'))) {
9797
lightFire(player, position, worldItemLog, skillInfo.burnExp);
9898
} else {
9999
player.sendMessage(`You attempt to light the logs.`);
@@ -121,7 +121,7 @@ const action: itemOnItemAction = (details) => {
121121
player.playAnimation(animationIds.lightingFire);
122122
}
123123

124-
canLightFire = elapsedTicks > 10 && canLight(skillInfo.requiredLevel, player.skills.values[Skill.WOODCUTTING].level);
124+
canLightFire = elapsedTicks > 10 && canLight(skillInfo.requiredLevel, player.skills.getSkillLevel('firemaking'));
125125

126126
if(!canLightFire && (elapsedTicks === 0 || elapsedTicks % 4 === 0)) {
127127
player.playSound(soundIds.lightingFire, 10, 0);
@@ -134,4 +134,8 @@ const action: itemOnItemAction = (details) => {
134134
}
135135
};
136136

137-
export default new RunePlugin({ type: ActionType.ITEM_ON_ITEM_ACTION, items: logs.map(log => ({item1: itemIds.tinderbox, item2: log.logId})), action });
137+
export default new RunePlugin({
138+
type: ActionType.ITEM_ON_ITEM_ACTION,
139+
items: logs.map(log => ({ item1: itemIds.tinderbox, item2: log.logId })),
140+
action
141+
});

src/world/actor/skills.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export enum Skill {
2929
CONSTRUCTION = 22
3030
}
3131

32+
export type SkillName = 'attack' | 'defence' | 'strength' | 'hitpoints' | 'ranged' | 'prayer' | 'magic' | 'cooking' |
33+
'woodcutting' | 'fletching' | 'fishing' | 'firemaking' | 'crafting' | 'smithing' | 'mining' | 'herblore' |
34+
'agility' | 'thieving' | 'slayer' | 'farming' | 'runecrafting' | 'construction';
35+
3236
export interface SkillDetail {
3337
readonly name: string;
3438
readonly advancementWidgetId?: number;
@@ -89,12 +93,12 @@ export class Skills {
8993
* Also add a new method to get the unboostedLevel incase it's ever needed
9094
* Then think about some way to reliably and easily fade those boosts out over time
9195
*/
92-
public getSkillLevel(skillId: number, level: number): number {
93-
return this.values[skillId].level;
96+
public getSkillLevel(skill: number | SkillName): number {
97+
return this.get(skill).level;
9498
}
9599

96-
public hasSkillLevel(skillId: number, level: number): boolean {
97-
return this.values[skillId].level >= level;
100+
public hasSkillLevel(skill: number | SkillName, level: number): boolean {
101+
return this.get(skill).level >= level;
98102
}
99103

100104
public getLevelForExp(exp: number): number {
@@ -112,8 +116,8 @@ export class Skills {
112116
return 99;
113117
}
114118

115-
public addExp(skillId: number, exp: number): void {
116-
const currentExp = this._values[skillId].exp;
119+
public addExp(skill: number | SkillName, exp: number): void {
120+
const currentExp = this.get(skill).exp;
117121
const currentLevel = this.getLevelForExp(currentExp);
118122
let finalExp = currentExp + (exp * serverConfig.expRate);
119123
if(finalExp > 200000000) {
@@ -122,34 +126,35 @@ export class Skills {
122126

123127
const finalLevel = this.getLevelForExp(finalExp);
124128

125-
this.setExp(skillId, finalExp);
129+
this.setExp(skill, finalExp);
126130

127131
if(this.actor instanceof Player) {
128-
this.actor.outgoingPackets.updateSkill(skillId, finalLevel, finalExp);
132+
this.actor.outgoingPackets.updateSkill(this.getSkillId(skill), finalLevel, finalExp);
129133
}
130134

131135
if(currentLevel !== finalLevel) {
132-
this.setLevel(skillId, finalLevel);
136+
this.setLevel(skill, finalLevel);
133137

134138
if(this.actor instanceof Player) {
135-
const achievementDetails = skillDetails[skillId];
139+
const achievementDetails = skillDetails[this.getSkillId(skill)];
136140
if(!achievementDetails) {
137141
return;
138142
}
139143

140-
this.actor.sendMessage(`Congratulations, you just advanced a ${achievementDetails.name.toLowerCase()} level.`);
141-
this.showLevelUpDialogue(skillId, finalLevel);
144+
this.actor.sendMessage(`Congratulations, you just advanced a ` +
145+
`${ achievementDetails.name.toLowerCase() } level.`);
146+
this.showLevelUpDialogue(skill, finalLevel);
142147
}
143148
}
144149
}
145150

146-
public showLevelUpDialogue(skillId: number, level: number): void {
151+
public showLevelUpDialogue(skill: number | SkillName, level: number): void {
147152
if(!(this.actor instanceof Player)) {
148153
return;
149154
}
150155

151156
const player = this.actor as Player;
152-
const achievementDetails = skillDetails[skillId];
157+
const achievementDetails = skillDetails[this.getSkillId(skill)];
153158
const widgetId = achievementDetails.advancementWidgetId;
154159

155160
if(!widgetId) {
@@ -164,9 +169,10 @@ export class Skills {
164169
closeOnWalk: true,
165170
beforeOpened: () => {
166171
player.modifyWidget(widgetId, { childId: 0,
167-
text: `<col=000080>Congratulations, you just advanced ${startsWithVowel(skillName) ? 'an' : 'a'} ${skillName} level.</col>` });
172+
text: `<col=000080>Congratulations, you just advanced ${ startsWithVowel(skillName) ? 'an' : 'a' } ` +
173+
`${ skillName } level.</col>` });
168174
player.modifyWidget(widgetId, { childId: 1,
169-
text: `Your ${skillName} level is now ${level}.` });
175+
text: `Your ${skillName} level is now ${ level }.` });
170176
},
171177
afterOpened: () => {
172178
player.playGraphics({ id: gfxIds.levelUpFireworks, delay: 0, height: 125 });
@@ -175,14 +181,34 @@ export class Skills {
175181
});
176182
}
177183

178-
public setExp(skillId: number, exp: number): void {
184+
public setExp(skill: number | SkillName, exp: number): void {
185+
const skillId = this.getSkillId(skill);
179186
this._values[skillId].exp = exp;
180187
}
181188

182-
public setLevel(skillId: number, level: number): void {
189+
public setLevel(skill: number | SkillName, level: number): void {
190+
const skillId = this.getSkillId(skill);
183191
this._values[skillId].level = level;
184192
}
185193

194+
public getSkillId(skill: number | SkillName) : number {
195+
if(typeof skill === 'number') {
196+
return skill;
197+
} else {
198+
const skillName = skill.toString().toUpperCase();
199+
return Skill[skillName].valueOf();
200+
}
201+
}
202+
203+
public get(skill: number | SkillName): SkillValue {
204+
if(typeof skill === 'number') {
205+
return this._values[skill];
206+
} else {
207+
const skillName = skill.toString().toUpperCase();
208+
return this._values[Skill[skillName].valueOf()];
209+
}
210+
}
211+
186212
public get values(): SkillValue[] {
187213
return this._values;
188214
}

src/world/world.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,16 @@ export class World {
218218
}
219219

220220
const position = new Position(newObject.x, newObject.y, newObject.level);
221+
const chunk = this.chunkManager.getChunkForWorldPosition(position);
221222

223+
this.deleteAddedLocationObjectMarker(oldObject, position, chunk);
222224
this.addLocationObject(newObject, position);
223225

224226
if(respawnTicks !== -1) {
225-
schedule(respawnTicks).then(() => this.addLocationObject(oldObject, position));
227+
schedule(respawnTicks).then(() => {
228+
this.deleteAddedLocationObjectMarker(newObject as LocationObject, position, chunk);
229+
this.addLocationObject(oldObject, position);
230+
});
226231
}
227232
}
228233

0 commit comments

Comments
 (0)