function FluidbookGamify(fluidbook) {
this.fluidbook = fluidbook;
- this.totalCoins = 0;
+ this.totalCoins = {main: 0};
this.init();
}
}
},
+ 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();
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