},
isSameVar(v1, v2) {
- var e = v2.split(':');
- var vv = e[0];
- if (e.length === 1) {
- vv = 'main';
- }
- return vv.toLowerCase() === v1.toLowerCase();
+ let e = this.extractCoinsVariableAndValue(v2);
+ return e.variable === v1.toLowerCase();
},
addCoins: function (id, coins) {
if (this.addedCoins[id] === undefined) {
return this.addCoins(id, coins);
}
- this.addCoins(id, Math.max(coins, this.addedCoins[id]));
+
+ let currentCoins = this.extractCoinsVariableAndValue(this.addedCoins[id]);
+ let newValue = this.extractCoinsVariableAndValue(coins);
+
+ this.addCoins(id, newValue.variable + ':' + (Math.max(newValue.value, currentCoins.value)));
},
updateTotalCoins: function () {
setTimeout(function () {
$($this.fluidbook).trigger('fluidbook.gamify.coins.update', [$this.totalCoins]);
}, 1000);
-
+
},
- sumCoins: function (v) {
- var e = v.toString().toLowerCase().split(':');
+ extractCoinsVariableAndValue: function (coins) {
+ var e = coins.toString().toLowerCase().split(':');
var variable = e[0];
var value;
+ var action = 'coins';
if (e.length === 1) {
variable = 'main';
value = e[0];
} else {
value = e[1];
}
-
if (value === 'reset') {
- console.log('reset coins '+variable);
- this.resetCoins(variable);
+ action = 'reset';
+ value = '0';
+ }
+ return {variable: variable, action: action, value: parseInt(value)};
+ },
+
+ sumCoins: function (v) {
+ let coins = this.extractCoinsVariableAndValue(v);
+
+ if (coins.action === 'reset') {
+ console.log('reset coins ' + coins.variable);
+ this.resetCoins(coins.variable);
return false;
}
- if (this.totalCoins[variable] === undefined || this.totalCoins[variable] === null) {
- this.totalCoins[variable] = 0;
+ if (this.totalCoins[coins.variable] === undefined || this.totalCoins[coins.variable] === null) {
+ this.totalCoins[coins.variable] = 0;
}
- this.totalCoins[variable] += parseInt(value);
+ this.totalCoins[coins.variable] += coins.value;
return true;
},
if (variable === undefined) {
variable = 'main';
}
- variable=variable.toString().toLowerCase();
- var res= this.totalCoins[variable];
- if(res===undefined){
- res=0;
+ variable = variable.toString().toLowerCase();
+ var res = this.totalCoins[variable];
+ if (res === undefined) {
+ res = 0;
}
return res;
}