]> _ Git - fluidbook-html5.git/commitdiff
wip #6222 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 28 Aug 2023 13:13:16 +0000 (15:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 28 Aug 2023 13:13:16 +0000 (15:13 +0200)
js/libs/fluidbook/fluidbook.gamify.js

index 1bad955cb77506eb51c144e37d22b0cf18759d4b..a9f597ab2e49776a4bb95fd680bb26125a19f5d6 100644 (file)
@@ -83,12 +83,8 @@ FluidbookGamify.prototype = {
     },
 
     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) {
@@ -101,7 +97,11 @@ FluidbookGamify.prototype = {
         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 () {
@@ -133,30 +133,40 @@ FluidbookGamify.prototype = {
         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;
     },
 
@@ -169,10 +179,10 @@ FluidbookGamify.prototype = {
         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;
     }