Browse Source

Add/remove effects by ID, add client effect extending

tags/v0.10.6^2
kckckc 2 years ago
parent
commit
02b58b4a35
3 changed files with 79 additions and 24 deletions
  1. +29
    -7
      src/server/clientComponents/effects.js
  2. +36
    -16
      src/server/components/effects.js
  3. +14
    -1
      src/server/config/effects/effectTemplate.js

+ 29
- 7
src/server/clientComponents/effects.js View File

@@ -1,13 +1,17 @@
define([
'js/rendering/renderer'
], function (
renderer
) {
return {
type: 'effects',

effects: [],

effectBase: {

},

templates: {
},
@@ -17,12 +21,15 @@ define([
},

buildEffect: function (data) {
if (typeof data === 'string')
if (typeof data === 'string') {
//TODO: temporary while we work on effects
console.error('String type effects should be deprecated, this effect will be missing an id');
data = { type: data };
}
let template = this.templates[data.type] || {};

let effect = $.extend(true, {}, template, data);
let effect = $.extend(true, {}, this.effectBase, template, data);

effect.obj = this.obj;

@@ -39,8 +46,8 @@ define([
this.effects.push.apply(this.effects, blueprint.addEffects || []);
}
if (blueprint.removeEffects) {
blueprint.removeEffects.forEach(r => {
let effect = this.effects.find(e => e.type === r);
blueprint.removeEffects.forEach(removeId => {
let effect = this.effects.find(e => e.id === removeId);

if (!effect)
return;
@@ -48,7 +55,22 @@ define([
if (effect.destroy)
effect.destroy();

this.effects.spliceFirstWhere(e => e.type === r);
this.effects.spliceFirstWhere(e => e.id === removeId);
});
}
if (blueprint.extendEffects) {
blueprint.extendEffects.forEach(u => {
let effect = this.effects.find(e => e.id === u.id);

if (!effect)
return;

if (effect.extend)
effect.extend(u.data);
else {
for (let p in u.data)
effect[p] = u.data[p];
}
});
}
},


+ 36
- 16
src/server/components/effects.js View File

@@ -145,6 +145,8 @@ module.exports = {
if (!options.force && !this.canApplyEffect(options.type))
return;

// TODO: separate "stack ttl" flag/method
/*
if (!options.new) {
let exists = this.effects.find(e => e.type === options.type);
if (exists) {
@@ -160,6 +162,7 @@ module.exports = {
return exists;
}
}
*/

let typeTemplate = null;
if (options.type) {
@@ -179,14 +182,14 @@ module.exports = {
builtEffect.obj = this.obj;
builtEffect.id = this.nextId++;
builtEffect.noMsg = options.noMsg;
builtEffect.silent = options.silent;

if (builtEffect.init)
builtEffect.init(options.source);

this.effects.push(builtEffect);

if (!options.noMsg) {
if (!options.silent) {
this.obj.instance.syncer.queue('onGetBuff', {
type: options.type,
id: builtEffect.id
@@ -198,7 +201,7 @@ module.exports = {
text: '+' + options.type
}, -1);

this.obj.syncer.setArray(false, 'effects', 'addEffects', options.type);
this.obj.syncer.setArray(false, 'effects', 'addEffects', builtEffect.simplify());
}

this.obj.instance.eventEmitter.emit('onAddEffect', this.obj, builtEffect);
@@ -206,6 +209,22 @@ module.exports = {
return builtEffect;
},

syncExtend: function (id, data) {
let effect = this.effects.find(e => e.id === id);
if (!effect)
return;

//Never sync silent effects
if (effect.silent)
return;

//TODO: should this be for self?
this.obj.syncer.setArray(true, 'effects', 'extendEffects', {
id,
data
});
},

syncRemove: function (id, type, noMsg) {
if ((noMsg) || (!type))
return;
@@ -220,23 +239,24 @@ module.exports = {
text: '-' + type
}, -1);

this.obj.syncer.setArray(false, 'effects', 'removeEffects', type);
this.obj.syncer.setArray(false, 'effects', 'removeEffects', id);
},

removeEffect: function (checkEffect, noMsg) {
let effects = this.effects;
let eLen = effects.length;
for (let i = 0; i < eLen; i++) {
let effect = effects[i];
if (effect === checkEffect) {
this.destroyEffect(effect);
removeEffect: function (id, noMsg) {
if (noMsg) {
console.error('removeEffect: noMsg is deprecated!');
}
if (typeof id !== 'number') {
console.error('removeEffect: id should be a number');
id = id.id;
}

this.syncRemove(effect.id, effect.type, noMsg || effect.noMsg);
effects.splice(i, 1);
let effect = this.effects.find(e => e.id === id);
this.destroyEffect(effect);

return;
}
}
this.syncRemove(effect.id, effect.type);
this.effects.spliceWhere(e => e.id === id);
},
removeEffectByName: function (effectName, noMsg) {
let effects = this.effects;


+ 14
- 1
src/server/config/effects/effectTemplate.js View File

@@ -1,4 +1,15 @@
module.exports = {
syncExtend: function (data) {
let effects = this.obj.effects;
effects.syncExtend(this.id, data);
},

isFirstOfType: function () {
let effects = this.obj.effects;
let firstOfType = effects.find(f => f.type === this.type);
return (firstOfType.id === this);
},

save: function () {
if (!this.persist)
return null;
@@ -19,6 +30,8 @@ module.exports = {
},

simplify: function () {
return this.type;
return {
type: this.type
};
}
};

Loading…
Cancel
Save