Skip to content

Commit

Permalink
Merge branch 'custom'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean O'Donohue committed Apr 3, 2019
2 parents 770725f + 82d0c71 commit 7933ba4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 46 deletions.
13 changes: 13 additions & 0 deletions lib/WebsocketStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class WebsocketStream extends TransportStream
{
attach(socket) {
super.attach(socket);
this.mask = false;

// websocket uses 'message' instead of the 'data' event net.Socket uses
socket.on('message', message => {
Expand Down Expand Up @@ -63,6 +64,18 @@ class WebsocketStream extends TransportStream
data
}));
}

executeToggleEcho() {
if (!this.writable) {
return;
}

this.mask = !this.mask;
this.socket.send(JSON.stringify({
type: 'ui',
data: {mask: this.mask}
}));
}
}

module.exports = WebsocketStream;
115 changes: 69 additions & 46 deletions player-events.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,94 @@
'use strict';

module.exports = {
listeners: {
attributeUpdate: state => function () {
updateAttributes.call(this);
},
module.exports = (srcPath) => {
return {
listeners: {
attributeUpdate: state => function () {
updateAttributes.call(this);
},

login: state => function () {
this.socket.command('sendData', 'quests', this.questTracker.serialize().active);
spawn: state => function () {
this._updated = 0;
this.socket.command('sendData', 'quests', this.questTracker.serialize().active);

const effects = this.effects.entries().filter(effect => !effect.config.hidden).map(effect => effect.serialize());
this.socket.command('sendData', 'effects', effects);
updateEffects.call(this);
updateAttributes.call(this);
},

updateAttributes.call(this);
},
combatantAdded: state => function () {
updateTargets.call(this);
},

combatantAdded: state => function () {
updateTargets.call(this);
},
combatantRemoved: state => function () {
updateTargets.call(this);
},

combatantRemoved: state => function () {
updateTargets.call(this);
},
updateTick: state => function () {
this._updated++;
if (this._updated % 20 === 0) {
updateEffects.call(this);
this._updated = 0;
}

updateTick: state => function () {
const effects = this.effects.entries().filter(effect => !effect.config.hidden).map(effect => ({
name: effect.name,
elapsed: effect.elapsed,
remaining: effect.remaining,
config: {
duration: effect.config.duration
if (!this.isInCombat()) {
return;
}
}));

if (effects.length) {
this.socket.command('sendData', 'effects', effects);
}
updateTargets.call(this);
},

if (!this.isInCombat()) {
return;
}
effectAdded: state => function (eff) {
if (eff.config.hidden) return;
updateEffects.call(this);
},

updateTargets.call(this);
},
effectRemoved: state => function () {
if (!this.effects.size) {
this.socket.command('sendData', 'effects', []);
}
updateEffects.call(this);
},

effectRemoved: state => function () {
if (!this.effects.size) {
this.socket.command('sendData', 'effects', []);
}
},
questProgress: state => function () {
this.socket.command('sendData', 'quests', this.questTracker.serialize().active);
},
}
};
};

questProgress: state => function () {
this.socket.command('sendData', 'quests', this.questTracker.serialize().active);
},
function updateEffects() {
const effectsMap = Array.from(this.effects.entries())
.filter(effect => !effect.config.hidden);

if (effectsMap.length) {
const effects = effectsMap
.map(effect => ({
name: effect.name,
elapsed: effect.elapsed,
remaining: effect.remaining,
config: {
duration: effect.config.duration
}
}));
this.socket.command('sendData', 'effects', effects);
}
};
}

function updateAttributes() {
// example of sending player data to a websocket client. This data is not sent to the default telnet socket
let attributes = {};
const attributes = {};
for (const [name, attribute] of this.attributes) {
attributes[name] = {
const attrData = {
current: this.getAttribute(name),
max: this.getMaxAttribute(name),
};
}

const type = ['health', 'energy', 'focus'].includes(name) ? 'pool' : 'stat';
attrData.type = type;
if (type === 'stat') {
attrData.base = this.getBaseAttribute(name);
}
attributes[name] = attrData;
}
this.socket.command('sendData', 'attributes', attributes);
}

Expand Down

0 comments on commit 7933ba4

Please sign in to comment.