]> _ Git - fluidbook-html5.git/commitdiff
wait #5121 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Feb 2022 17:43:00 +0000 (18:43 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Feb 2022 17:43:00 +0000 (18:43 +0100)
js/libs/fluidbook/fluidbook.gamify.js
js/libs/fluidbook/fluidbook.links.js

index 9b1bdae7eef9836b9faa0f3f08a59bf342293bb3..1bad955cb77506eb51c144e37d22b0cf18759d4b 100644 (file)
@@ -1,6 +1,6 @@
 function FluidbookGamify(fluidbook) {
     this.fluidbook = fluidbook;
-    this.totalCoins = 0;
+    this.totalCoins = {main: 0};
     this.init();
 }
 
@@ -55,6 +55,42 @@ FluidbookGamify.prototype = {
         }
     },
 
+    resetCoins: function (variable) {
+        var $this = this;
+        if (variable === undefined) {
+            variable = 'main';
+        }
+
+        var addedCoins = {};
+        $.each(this.addedCoins, function (k, v) {
+            if (!$this.isSameVar(variable, v)) {
+                addedCoins[k] = v;
+            }
+        });
+        this.addedCoins = addedCoins;
+
+        var coinsLinksSeen = [];
+        $.each(this.coinsLinksSeen, function (k, uid) {
+            if ($this.fluidbook.settings.gamifyCoins[uid] !== undefined) {
+                if (!$this.isSameVar(variable, $this.fluidbook.settings.gamifyCoins[uid])) {
+                    coinsLinksSeen.push(uid);
+                }
+            }
+        });
+        this.coinsLinksSeen = coinsLinksSeen;
+        this.save();
+        this.updateTotalCoins();
+    },
+
+    isSameVar(v1, v2) {
+        var e = v2.split(':');
+        var vv = e[0];
+        if (e.length === 1) {
+            vv = 'main';
+        }
+        return vv.toLowerCase() === v1.toLowerCase();
+    },
+
     addCoins: function (id, coins) {
         this.addedCoins[id] = coins;
         this.save();
@@ -71,26 +107,73 @@ FluidbookGamify.prototype = {
     updateTotalCoins: function () {
         var $this = this;
         var formerValue = this.totalCoins;
-        this.totalCoins = 0;
+        this.totalCoins = {main: 0};
+        var stop = false;
         $.each(this.coinsLinksSeen, function (k, uid) {
             if ($this.fluidbook.settings.gamifyCoins[uid] !== undefined) {
-                $this.totalCoins += $this.fluidbook.settings.gamifyCoins[uid];
+                if ($this.sumCoins($this.fluidbook.settings.gamifyCoins[uid]) === false) {
+                    stop = true;
+                    return true;
+                }
             }
         });
+        if (stop) {
+            return;
+        }
         $.each(this.addedCoins, function (id, coins) {
-            $this.totalCoins += coins;
+            if ($this.sumCoins(coins) === false) {
+                stop = true;
+                return true;
+            }
         });
-        if (this.totalCoins !== formerValue) {
-            setTimeout(function () {
-                $($this.fluidbook).trigger('fluidbook.gamify.coins.update', [$this.totalCoins]);
-            }, 1000);
+        if (stop) {
+            return;
+        }
+
+        setTimeout(function () {
+            $($this.fluidbook).trigger('fluidbook.gamify.coins.update', [$this.totalCoins]);
+        }, 1000);
+        
+    },
+
+    sumCoins: function (v) {
+        var e = v.toString().toLowerCase().split(':');
+        var variable = e[0];
+        var value;
+        if (e.length === 1) {
+            variable = 'main';
+            value = e[0];
+        } else {
+            value = e[1];
         }
+
+        if (value === 'reset') {
+            console.log('reset coins '+variable);
+            this.resetCoins(variable);
+            return false;
+        }
+
+        if (this.totalCoins[variable] === undefined || this.totalCoins[variable] === null) {
+            this.totalCoins[variable] = 0;
+        }
+        this.totalCoins[variable] += parseInt(value);
+        return true;
     },
+
     save: function () {
         this.setToCache('gamify_coins_links_seen', this.coinsLinksSeen);
         this.setToCache('gamify_coins_added', this.addedCoins);
     },
-    getTotalCoins: function () {
-        return this.totalCoins;
+
+    getTotalCoins: function (variable) {
+        if (variable === undefined) {
+            variable = 'main';
+        }
+        variable=variable.toString().toLowerCase();
+        var res= this.totalCoins[variable];
+        if(res===undefined){
+            res=0;
+        }
+        return res;
     }
 }
\ No newline at end of file
index e87727974f9a66b023769269b32abaf5796b7125..8ddb88c13bdc2b244985d0c78a5e878ea0ff9731 100644 (file)
@@ -817,7 +817,6 @@ FluidbookLinks.prototype = {
         }
         if (usegsap) {
             to.duration = duration;
-            console.log(from,to);
             gsap.fromTo(linkElement, from, to);
         }
         this.fluidbook.networkControl.pause((to.delay + duration + 0.5) * 1000);