Permalink
Browse files

refactored collection stats

this also fixes a division by zero bug
  • Loading branch information...
NorthFury committed Oct 6, 2018
1 parent f86a65b commit 278bf01ec7bc3b0f4cb4ecacc64e1d5286a65bee
Showing with 96 additions and 147 deletions.
  1. +54 −58 shared/util.js
  2. +42 −89 window_main/renderer.js
@@ -364,75 +364,71 @@ function get_set_code(set) {
return s;
}
//
class CountStats {
constructor(owned = 0, total = 0) {
this.owned = owned;
this.total = total;
}
get percentage() {
if (this.total) {
return this.owned / this.total * 100;
} else {
return 100;
}
}
}
//
class SetStats {
constructor(set) {
this.set = set;
this.common = new CountStats();
this.uncommon = new CountStats();
this.rare = new CountStats();
this.mythic = new CountStats();
}
get all() {
return [new CountStats(), this.common, this.uncommon, this.rare, this.mythic].reduce((acc, c) => {
acc.owned += c.owned;
acc.total += c.total;
return acc;
});
}
}
//
function get_collection_stats() {
var stats = {
ownedCommon: 0,
ownedUncommon: 0,
ownedRare: 0,
ownedMythic: 0,
totalCommon: 0,
totalUncommon: 0,
totalRare: 0,
totalMythic: 0,
totalCards: 0,
ownedCards: 0,
ownedSinglesCommon: 0,
ownedSinglesUncommon: 0,
ownedSinglesRare: 0,
ownedSinglesMythic: 0,
totalSinglesCommon: 0,
totalSinglesUncommon: 0,
totalSinglesRare: 0,
totalSinglesMythic: 0,
totalSingles: 0,
ownedSingles: 0
const stats = {
complete: new SetStats("complete"),
singles: new SetStats("singles")
};
for (var set in setsList) {
stats[set] = {
totalCards: 0,
ownedCards: 0,
ownedCommon: 0,
ownedUncommon: 0,
ownedRare: 0,
ownedMythic: 0,
totalCommon: 0,
totalUncommon: 0,
totalRare: 0,
totalMythic: 0
};
stats[set] = new SetStats(set);
}
Object.keys(cardsDb.cards).forEach(function(grpId) {
card = cardsDb.get(grpId);
if (card.rarity !== "token" && card.rarity !== "land" && card.set !== "Oath of the Gatewatch" && card.dfc != "DFC_Front" && card.dfc != "SplitCard") {
Object.keys(cardsDb.cards).forEach(function (grpId) {
const card = cardsDb.get(grpId);
if (card.rarity !== "token" && card.rarity !== "land" && card.set !== "Oath of the Gatewatch" && card.dfc != "DFC_Front" && card.dfc != "SplitCard") {
// add to totals
//console.log(card, card.set);
stats[card.set].totalCards += 4;
stats.totalCards += 4;
stats.totalSingles += 1;
if (card.rarity == 'common') { stats.totalCommon += 4; stats.totalSinglesCommon += 1; stats[card.set].totalCommon += 4; }
if (card.rarity == 'uncommon') { stats.totalUncommon += 4; stats.totalSinglesUncommon += 1; stats[card.set].totalUncommon += 4; }
if (card.rarity == 'rare') { stats.totalRare += 4; stats.totalSinglesRare += 1; stats[card.set].totalRare += 4; }
if (card.rarity == 'mythic') { stats.totalMythic += 4; stats.totalSinglesMythic += 1; stats[card.set].totalMythic += 4; }
// add cards we own
stats[card.set][card.rarity].total += 4;
stats.complete[card.rarity].total += 4;
stats.singles[card.rarity].total += 1;
// add cards we own
if (cards[grpId] !== undefined) {
var add = cards[grpId];
stats[card.set].ownedCards += add;
stats.ownedCards += add;
stats.ownedSingles += 1;
if (card.rarity == 'common') { stats.ownedCommon += add; stats.ownedSinglesCommon += 1; stats[card.set].ownedCommon += add; }
if (card.rarity == 'uncommon') { stats.ownedUncommon += add; stats.ownedSinglesUncommon += 1; stats[card.set].ownedUncommon += add; }
if (card.rarity == 'rare') { stats.ownedRare += add; stats.ownedSinglesRare += 1; stats[card.set].ownedRare += add; }
if (card.rarity == 'mythic') { stats.ownedMythic += add; stats.ownedSinglesMythic += 1; stats[card.set].ownedMythic += add; }
var owned = cards[grpId];
stats[card.set][card.rarity].owned += owned;
stats.complete[card.rarity].owned += owned;
stats.singles[card.rarity].owned += 1;
}
}
});
}
});
return stats;
return stats;
}
//
@@ -2260,107 +2260,30 @@ function exportCollection() {
//
function printStats() {
$('.moving_ux').animate({'left': '-100%'}, 250, 'easeInOutCubic');
$('.moving_ux').animate({ 'left': '-100%' }, 250, 'easeInOutCubic');
$("#ux_1").html('');
stats = get_collection_stats();
const stats = get_collection_stats();
var top = $('<div class="decklist_top"><div class="button back"></div><div class="deck_name">Collection Statistics</div></div>');
const top = $('<div class="decklist_top"><div class="button back"></div><div class="deck_name">Collection Statistics</div></div>');
change_background("http://www.artofmtg.com/wp-content/uploads/2018/04/Urzas-Tome-Dominaria-MtG-Art.jpg");
flex = $('<div class="flex_item"></div>');
mainstats = $('<div class="main_stats"></div>');
var label = $('<label>Sets Completion</label>');
label.appendTo(mainstats);
const flex = $('<div class="flex_item"></div>');
const mainstats = $('<div class="main_stats"></div>');
$('<label>Sets Completion</label>').appendTo(mainstats);
// each set stats
for (let set in setsList) {
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_set_icon" style="background-image: url(../images/sets/'+setsList[set].code+'.png)"><span>'+set+' <i>('+stats[set].ownedCards+'/'+stats[set].totalCards+', '+Math.round(stats[set].ownedCards/stats[set].totalCards*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats[set].ownedCards/stats[set].totalCards*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(mainstats);
setdiv.click(function() {
var substats = $(".sub_stats");
substats.html('');
$('<label>'+set+' Completion</label>').appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_common.png)"><span>Commons <i>('+stats[set].ownedCommon+'/'+stats[set].totalCommon+', '+Math.round(stats[set].ownedCommon/stats[set].totalCommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats[set].ownedCommon/stats[set].totalCommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_uncommon.png)"><span>Uncommons <i>('+stats[set].ownedUncommon+'/'+stats[set].totalUncommon+', '+Math.round(stats[set].ownedUncommon/stats[set].totalUncommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats[set].ownedUncommon/stats[set].totalUncommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_rare.png)"><span>Rares <i>('+stats[set].ownedRare+'/'+stats[set].totalRare+', '+Math.round(stats[set].ownedRare/stats[set].totalRare*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats[set].ownedRare/stats[set].totalRare*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
if (stats[set].totalMythic == 0) stats[set].totalMythic = 1;
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_mythic.png)"><span>Mythics <i>('+stats[set].ownedMythic+'/'+stats[set].totalMythic+', '+Math.round(stats[set].ownedMythic/stats[set].totalMythic*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats[set].ownedMythic/stats[set].totalMythic*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
});
renderSetStats(stats[set], setsList[set].code, set).appendTo(mainstats);
}
// Complete collection sats
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_set_icon" style="background-image: url(../images/sets/pw.png)"><span>Complete collection <i>('+stats.ownedCards+'/'+stats.totalCards+', '+Math.round(stats.ownedCards/stats.totalCards*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedCards/stats.totalCards*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(mainstats);
setdiv.click(function() {
var substats = $(".sub_stats");
substats.html('');
$('<label>Complete collection completion</label>').appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_common.png)"><span>Commons <i>('+stats.ownedCommon+'/'+stats.totalCommon+', '+Math.round(stats.ownedCommon/stats.totalCommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedCommon/stats.totalCommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_uncommon.png)"><span>Uncommons <i>('+stats.ownedUncommon+'/'+stats.totalUncommon+', '+Math.round(stats.ownedUncommon/stats.totalUncommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedUncommon/stats.totalUncommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_rare.png)"><span>Rares <i>('+stats.ownedRare+'/'+stats.totalRare+', '+Math.round(stats.ownedRare/stats.totalRare*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedRare/stats.totalRare*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_mythic.png)"><span>Mythics <i>('+stats.ownedMythic+'/'+stats.totalMythic+', '+Math.round(stats.ownedMythic/stats.totalMythic*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedMythic/stats.totalMythic*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
});
renderSetStats(stats.complete, "pw", "Complete collection").appendTo(mainstats);
// Singleton collection sats
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_set_icon" style="background-image: url(../images/sets/pw.png)"><span>Singles <i>('+stats.ownedSingles+'/'+stats.totalSingles+', '+Math.round(stats.ownedSingles/stats.totalSingles*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedSingles/stats.totalSingles*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(mainstats);
renderSetStats(stats.singles, "pw", "Singles").appendTo(mainstats);
setdiv.click(function() {
var substats = $(".sub_stats");
substats.html('');
$('<label>Singles completion</label>').appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_common.png)"><span>Commons <i>('+stats.ownedSinglesCommon+'/'+stats.totalSinglesCommon+', '+Math.round(stats.ownedSinglesCommon/stats.totalSinglesCommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedSinglesCommon/stats.totalSinglesCommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_uncommon.png)"><span>Uncommons <i>('+stats.ownedSinglesUncommon+'/'+stats.totalSinglesUncommon+', '+Math.round(stats.ownedSinglesUncommon/stats.totalSinglesUncommon*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedSinglesUncommon/stats.totalSinglesUncommon*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_rare.png)"><span>Rares <i>('+stats.ownedSinglesRare+'/'+stats.totalSinglesRare+', '+Math.round(stats.ownedSinglesRare/stats.totalSinglesRare*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedSinglesRare/stats.totalSinglesRare*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
var setdiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_rarity_icon" style="background-image: url(../images/wc_mythic.png)"><span>Mythics <i>('+stats.ownedSinglesMythic+'/'+stats.totalSinglesMythic+', '+Math.round(stats.ownedSinglesMythic/stats.totalSinglesMythic*100)+'%)</i></span></div>').appendTo(setdiv);
$('<div class="stats_set_bar" style="width: '+stats.ownedSinglesMythic/stats.totalSinglesMythic*100+'%"></div>').appendTo(setdiv);
setdiv.appendTo(substats);
});
substats = $('<div class="main_stats sub_stats"></div>');
const substats = $('<div class="main_stats sub_stats"></div>');
flex.append(mainstats);
flex.append(substats);
@@ -2370,8 +2293,38 @@ function printStats() {
//
$(".back").click(function () {
change_background("default");
$('.moving_ux').animate({'left': '0px'}, 250, 'easeInOutCubic');
$('.moving_ux').animate({ 'left': '0px' }, 250, 'easeInOutCubic');
});
}
//
function renderSetStats(setStats, setIconCode, setName) {
const setDiv = renderCompletionDiv(setStats.all, 'sets/' + setIconCode + '.png', setName);
setDiv.click(function () {
const substats = $(".sub_stats");
substats.html('');
$('<label>' + setName + ' completion</label>').appendTo(substats);
["common", "uncommon", "rare", "mythic"].forEach(rarity => {
const countStats = setStats[rarity];
if (countStats.total > 0) {
const capitalizedRarity = rarity[0].toUpperCase() + rarity.slice(1) + 's';
renderCompletionDiv(countStats, 'wc_' + rarity + '.png', capitalizedRarity).appendTo(substats);
}
});
});
return setDiv;
}
//
function renderCompletionDiv(countStats, image, title) {
const completionDiv = $('<div class="stats_set_completion"></div>');
$('<div class="stats_set_icon" style="background-image: url(../images/' + image + ')"><span>' + title + ' <i>(' + countStats.owned + '/' + countStats.total + ', ' + Math.round(countStats.percentage) + '%)</i></span></div>')
.appendTo(completionDiv);
$('<div class="stats_set_bar" style="width: ' + countStats.percentage + '%"></div>')
.appendTo(completionDiv);
return completionDiv;
}
function sortCollection(alg) {

0 comments on commit 278bf01

Please sign in to comment.