Skip to content

Commit

Permalink
Implementing queue-able widgets.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlackParade committed Mar 18, 2020
1 parent 3124641 commit 24411c8
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 53 deletions.
12 changes: 5 additions & 7 deletions src/net/data-parser/client-packet-data-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ export class ClientPacketDataParser extends DataParser {
return;
}

if(this.activePacketSize !== 0) {
// read packet data
const packetData = this.activeBuffer.readBytes(this.activePacketSize);
handlePacket(this.clientConnection.player, this.activePacketId, this.activePacketSize, packetData);
// read packet data
const packetData = this.activePacketSize !== 0 ? this.activeBuffer.readBytes(this.activePacketSize) : null;
handlePacket(this.clientConnection.player, this.activePacketId, this.activePacketSize, packetData);

if(clearBuffer) {
this.activeBuffer = null;
}
if(clearBuffer) {
this.activeBuffer = null;
}

this.activePacketId = null;
Expand Down
2 changes: 1 addition & 1 deletion src/net/incoming-packet-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { itemOnObjectPacket } from '@server/net/incoming-packets/item-on-object-
import { numberInputPacket } from '@server/net/incoming-packets/number-input-packet';
import { itemOnNpcPacket } from '@server/net/incoming-packets/item-on-npc-packet';

const ignore = [ 234, 160, 58 /* camera move */ ];
const ignore = [ 234, 160, 216, 13, 58 /* camera move */ ];

const packets: { [key: number]: incomingPacket } = {
75: chatPacket,
Expand Down
2 changes: 1 addition & 1 deletion src/net/incoming-packets/character-design-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export const characterDesignPacket: incomingPacket = (player: Player, packetId:
};

player.updateFlags.appearanceUpdateRequired = true;
player.closeActiveWidget();
player.closeActiveWidgets();
};
2 changes: 1 addition & 1 deletion src/net/incoming-packets/widgets-closed-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Player } from '../../world/actor/player/player';
import { RsBuffer } from '@server/net/rs-buffer';

export const widgetsClosedPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
player.closeActiveWidget(false);
player.closeActiveWidgets(false);
};
9 changes: 1 addition & 8 deletions src/plugins/dialogue/dialogue-option-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ const dialogueIds = [
64, 65, 66, 67, 241,
242, 243, 244, 228, 230,
232, 234,
158, 161, 175,
167, 171, 170,
168, 159, 177,
165, 164, 163,
160, 174, 169,
166, 157, 176,
173, 162, 172,
210, 211, 212, 213, 214,
];

/**
* Handles a basic NPC/Player/Option/level-up dialogue choice/action.
* Handles a basic NPC/Player/Option/Text dialogue choice/action.
*/
export const action: widgetAction = (details) => {
const { player, childId } = details;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/skills/crafting/spinning-wheel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const buttonClicked: buttonAction = (details) => {
const product = widgetButtonIds.get(details.buttonId);

// Close the widget as it is no longer needed
details.player.closeActiveWidget();
details.player.closeActiveWidgets();

if (!details.player.skills.hasSkillLevel(Skill.CRAFTING, product.spinnable.requiredLevel)) {
details.player.sendMessage(`You need a crafting level of ${product.spinnable.requiredLevel} to craft ${gameCache.itemDefinitions.get(product.spinnable.output).name.toLowerCase()}.`, true);
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/skills/level-up-dialogue-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { widgetAction } from '@server/world/actor/player/action/widget-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';

const widgetIds = [
158, 161, 175,
167, 171, 170,
168, 159, 177,
165, 164, 163,
160, 174, 169,
166, 157, 176,
173, 162, 172,
];

/**
* Handles a level-up dialogue action.
*/
export const action: widgetAction = (details) => {
const { player } = details;
player.closeActiveWidgets();
};

export default new RunePlugin({ type: ActionType.WIDGET_ACTION, widgetIds, action, cancelActions: false });
18 changes: 3 additions & 15 deletions src/world/actor/player/action/dialogue-action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Player } from '@server/world/actor/player/player';
import { gameCache } from '@server/game-server';
import { Npc } from '@server/world/actor/npc/npc';
import { skillDetails } from '@server/world/actor/skills';

export const dialogueWidgetIds = {
PLAYER: [ 64, 65, 66, 67 ],
Expand All @@ -17,7 +16,6 @@ const lineConstraints = {
PLAYER: [ 1, 4 ],
NPC: [ 1, 4 ],
OPTIONS: [ 2, 5 ],
LEVEL_UP: [ 2, 2 ],
TEXT: [ 1, 5 ]
};

Expand Down Expand Up @@ -54,7 +52,7 @@ export enum DialogueEmote {
ANGRY_4 = 617
}

export type DialogueType = 'PLAYER' | 'NPC' | 'OPTIONS' | 'LEVEL_UP' | 'TEXT';
export type DialogueType = 'PLAYER' | 'NPC' | 'OPTIONS' | 'TEXT';

export interface DialogueOptions {
type: DialogueType;
Expand Down Expand Up @@ -94,24 +92,14 @@ export class DialogueAction {
throw 'NPC not supplied.';
}

if(options.type === 'LEVEL_UP' && options.skillId === undefined) {
throw 'Skill ID not supplied.';
}

this._action = null;

let widgetIndex = options.lines.length - 1;
if(options.type === 'OPTIONS') {
widgetIndex--;
}

let widgetId = -1;

if(options.type === 'LEVEL_UP') {
widgetId = skillDetails.map(skill => !skill || !skill.advancementWidgetId ? -1 : skill.advancementWidgetId)[options.skillId];
} else {
widgetId = dialogueWidgetIds[options.type][widgetIndex];
}
const widgetId = dialogueWidgetIds[options.type][widgetIndex];

if(widgetId === undefined || widgetId === null || widgetId === -1) {
return Promise.resolve(this);
Expand All @@ -137,7 +125,7 @@ export class DialogueAction {
} else if(options.type === 'OPTIONS') {
this.p.outgoingPackets.updateWidgetString(widgetId, 0, options.title);
textOffset = 1;
} else if(options.type === 'LEVEL_UP' || options.type === 'TEXT') {
} else if(options.type === 'TEXT') {
textOffset = 0;
}

Expand Down
6 changes: 3 additions & 3 deletions src/world/actor/player/action/item-selection-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ export async function itemSelectionAction(player: Player, type: 'COOKING' | 'MAK
interactionSub.unsubscribe();

if(input < 1 || input > 2147483647) {
player.closeActiveWidget();
player.closeActiveWidgets();
reject('Invalid User Amount Input');
} else {
player.closeActiveWidget();
player.closeActiveWidgets();
resolve({itemId, amount: input} as ItemSelection);
}
});
} else {
actionsSub.unsubscribe();
interactionSub.unsubscribe();
player.closeActiveWidget();
player.closeActiveWidgets();
resolve({itemId, amount} as ItemSelection);
}
});
Expand Down
63 changes: 57 additions & 6 deletions src/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,15 @@ export class Player extends Actor {
questData.completion.modelRotationX || 0, questData.completion.modelRotationY || 0,
questData.completion.modelZoom || 0);

questData.completion.onComplete(this);

this.activeWidget = {
widgetId: widgets.questReward,
type: 'SCREEN',
closeOnWalk: true
};

this.modifyWidget(widgets.questTab, { childId: questData.questTabId, textColor: colors.green });

questData.completion.onComplete(this);
}

playerQuest.stage = stage;
Expand All @@ -487,7 +487,7 @@ export class Player extends Actor {
public modifyWidget(widgetId: number, options: { childId?: number, text?: string, hidden?: boolean, textColor?: number }): void {
const { childId, text, hidden, textColor } = options;

if(childId) {
if(childId !== undefined) {
if(text !== undefined) {
this.outgoingPackets.updateWidgetString(widgetId, childId, text);
}
Expand Down Expand Up @@ -614,6 +614,10 @@ export class Player extends Actor {
this.updateCarryWeight();
}

/**
* Updates the player's carry weight based off of their held items (inventory + equipment).
* @param force Whether or not to force send an updated carry weight to the game client.
*/
public updateCarryWeight(force: boolean = false): void {
const oldWeight = this._carryWeight;
this._carryWeight = Math.round(this.inventory.weight() + this.equipment.weight());
Expand All @@ -623,6 +627,11 @@ export class Player extends Actor {
}
}

/**
* Updates a player's client settings based off of which setting button they've clicked.
* @param buttonId The ID of the setting button.
* @TODO refactor to better match the 400+ widget system
*/
public settingChanged(buttonId: number): void {
const settingsMappings = {
0: {setting: 'runEnabled', value: !this.settings['runEnabled']},
Expand Down Expand Up @@ -663,6 +672,9 @@ export class Player extends Actor {
this.settings[config.setting] = config.value;
}

/**
* Updates the player's combat bonuses based off of their equipped items.
*/
public updateBonuses(): void {
this.clearBonuses();

Expand Down Expand Up @@ -711,15 +723,46 @@ export class Player extends Actor {
};
}

public closeActiveWidget(notifyClient: boolean = true): void {
/**
* Queues up a widget to be displayed when the active widget is closed.
* If there is no active widget, the provided widget will be automatically displayed.
* @param widget The widget to queue.
*/
public queueWidget(widget: PlayerWidget): void {
if(this.activeWidget === null) {
this.activeWidget = widget;
} else {
this.queuedWidgets.push(widget);
console.log(this.queuedWidgets);
}
}

/**
* Closes the currently active widget or widget pair.
* @param notifyClient [optional] Whether or not to notify the game client that widgets should be cleared. Defaults to true.
*/
public closeActiveWidgets(notifyClient: boolean = true): void {
if(notifyClient) {
this.activeWidget = null;
if(this.queuedWidgets.length !== 0) {
this.activeWidget = this.queuedWidgets.shift();
} else {
this.activeWidget = null;
}
} else {
this.actionsCancelled.next(true);
this._activeWidget = null;

if(this.queuedWidgets.length !== 0) {
this.activeWidget = this.queuedWidgets.shift();
} else {
this.actionsCancelled.next(true);
}
}
}

/**
* Checks to see if the player has the specified widget ID open on their screen or not.
* @param widgetId The ID of the widget to look for.
*/
public hasWidgetOpen(widgetId: number): boolean {
return this.activeWidget && this.activeWidget.widgetId === widgetId;
}
Expand Down Expand Up @@ -785,6 +828,10 @@ export class Player extends Actor {

public set activeWidget(value: PlayerWidget) {
if(value !== null) {
if(value.beforeOpened !== undefined) {
value.beforeOpened();
}

if(value.type === 'SCREEN') {
this.outgoingPackets.showScreenWidget(value.widgetId);
} else if(value.type === 'CHAT') {
Expand All @@ -794,6 +841,10 @@ export class Player extends Actor {
} else if(value.type === 'SCREEN_AND_TAB') {
this.outgoingPackets.showScreenAndTabWidgets(value.widgetId, value.secondaryWidgetId);
}

if(value.afterOpened !== undefined) {
value.afterOpened();
}
} else {
this.outgoingPackets.closeActiveWidgets();
}
Expand Down
45 changes: 35 additions & 10 deletions src/world/actor/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,52 @@ export class Skills {

if(currentLevel !== finalLevel) {
this.setLevel(skillId, finalLevel);
this.actor.playGraphics({ id: 199, delay: 0, height: 125 });
// this.actor.playGraphics({ id: 199, delay: 0, height: 125 });

if(this.actor instanceof Player) {
const achievementDetails = skillDetails[skillId];
if(!achievementDetails) {
return;
}

this.actor.outgoingPackets.chatboxMessage(`Congratulations, you just advanced a ${achievementDetails.name.toLowerCase()} level.`);

if(achievementDetails.advancementWidgetId) {
dialogueAction(this.actor, { type: 'LEVEL_UP', skillId, lines: [
`<col=000080>Congratulations, you just advanced ${startsWithVowel(achievementDetails.name) ? 'an' : 'a'} ` +
`${achievementDetails.name.toLowerCase()} level.</col>`,
`Your ${achievementDetails.name.toLowerCase()} level is now ${finalLevel}.` ] }).then(d => d.close());
// @TODO sounds
}
this.actor.sendMessage(`Congratulations, you just advanced a ${achievementDetails.name.toLowerCase()} level.`);
this.showLevelUpDialogue(skillId, finalLevel);
}
}
}

public showLevelUpDialogue(skillId: number, level: number): void {
if(!(this.actor instanceof Player)) {
return;
}

const player = this.actor as Player;
const achievementDetails = skillDetails[skillId];
const widgetId = achievementDetails.advancementWidgetId;

if(!widgetId) {
return;
}

const skillName = achievementDetails.name.toLowerCase();

player.queueWidget({
widgetId,
type: 'CHAT',
closeOnWalk: true,
beforeOpened: () => {
player.modifyWidget(widgetId, { childId: 0,
text: `<col=000080>Congratulations, you just advanced ${startsWithVowel(skillName) ? 'an' : 'a'} ${skillName} level.</col>` });
player.modifyWidget(widgetId, { childId: 1,
text: `Your ${skillName} level is now ${level}.` });
},
afterOpened: () => {
player.playGraphics({ id: 199, delay: 0, height: 125 });
// @TODO sounds
}
});
}

public setExp(skillId: number, exp: number): void {
this._values[skillId].exp = exp;
}
Expand Down
2 changes: 2 additions & 0 deletions src/world/config/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ export interface PlayerWidget {
disablePlayerMovement?: boolean;
closeOnWalk?: boolean;
forceClosed?: Function;
beforeOpened?: Function;
afterOpened?: Function;
}

0 comments on commit 24411c8

Please sign in to comment.