Skip to content
Permalink
Browse files

Merge pull request #278 from lusbenjamin/count-modes

Add count mode filter to collection stats
  • Loading branch information...
Manuel-777 committed Apr 23, 2019
2 parents 2dddaf4 + 0987cfe commit 07f22347f796fbc7fc29e3e9c97ed184fdea6971
Showing with 65 additions and 20 deletions.
  1. +15 −9 shared/util.js
  2. +50 −11 window_main/collection.js
@@ -1370,13 +1370,22 @@ function get_set_code(set) {

//
class CountStats {
constructor(owned = 0, total = 0, unique = 0, complete = 0, wanted = 0, uniqueWanted = 0) {
constructor(
owned = 0,
total = 0,
unique = 0,
complete = 0,
wanted = 0,
uniqueWanted = 0,
uniqueOwned = 0
) {
this.owned = owned;
this.total = total;
this.unique = unique;
this.complete = complete; // all 4 copies of a card
this.wanted = wanted;
this.uniqueWanted = uniqueWanted;
this.uniqueOwned = uniqueOwned;
}

get percentage() {
@@ -1409,7 +1418,9 @@ class SetStats {
acc.owned += c.owned;
acc.total += c.total;
acc.unique += c.unique;
acc.complete += c.complete;
acc.wanted += c.wanted;
acc.uniqueOwned += c.uniqueOwned;
return acc;
});
}
@@ -1418,8 +1429,7 @@ class SetStats {
//
function get_collection_stats() {
const stats = {
complete: new SetStats("complete"),
singles: new SetStats("singles")
complete: new SetStats("complete")
};

for (var set in setsList) {
@@ -1440,23 +1450,21 @@ function get_collection_stats() {
// add to totals
stats[card.set][card.rarity].total += 4;
stats.complete[card.rarity].total += 4;
stats.singles[card.rarity].total += 1;
stats[card.set][card.rarity].unique += 1;
stats.complete[card.rarity].unique += 1;
stats.singles[card.rarity].unique += 1;

// add cards we own
if (cards[grpId] !== undefined) {
var owned = cards[grpId];
stats[card.set][card.rarity].owned += owned;
stats.complete[card.rarity].owned += owned;
stats.singles[card.rarity].owned += 1;
stats[card.set][card.rarity].uniqueOwned += 1;
stats.complete[card.rarity].uniqueOwned += 1;

// count complete sets we own
if (owned == 4) {
stats[card.set][card.rarity].complete += 1;
stats.complete[card.rarity].complete += 1;
stats.singles[card.rarity].complete += 1;
}
}

@@ -1465,12 +1473,10 @@ function get_collection_stats() {
let wanted = Math.max(...deckWantedCounts);
stats[card.set][card.rarity].wanted += wanted;
stats.complete[card.rarity].wanted += wanted;
stats.singles[card.rarity].wanted += Math.min(1, wanted);

// count unique cards we know we want across decks
stats[card.set][card.rarity].uniqueWanted += Math.min(1, wanted);
stats.complete[card.rarity].uniqueWanted += Math.min(1, wanted);
stats.singles[card.rarity].uniqueWanted += Math.min(1, wanted);
}
}
});
@@ -29,6 +29,12 @@ let filteredSets = [];
let filteredMana = [];
let orderedSets;

const ALL_CARDS = "All cards";
const SINGLETONS = "Singletons (at least one)";
const FULL_SETS = "Full sets (all 4 copies)";

let countMode = ALL_CARDS;

//
function openCollectionTab() {
orderedSets = [];
@@ -433,19 +439,31 @@ function printStats() {
completionLabel.innerHTML = "Sets Completion";
mainstats.appendChild(completionLabel);

// each set stats
orderedSets.forEach(set => {
let rs = renderSetStats(stats[set], setsList[set].code, set);
mainstats.appendChild(rs);
});
// Counting Mode Selector
const countModeDiv = createDivision(["stats_count_div"]);
const countModeSelect = createSelect(
countModeDiv,
[ALL_CARDS, SINGLETONS, FULL_SETS],
countMode,
selectedMode => {
countMode = selectedMode;
printStats();
},
"stats_count_select"
);
countModeSelect.style.margin = "12px auto auto auto";
countModeSelect.style.textAlign = "left";
mainstats.appendChild(countModeSelect);

// Complete collection sats
let rs = renderSetStats(stats.complete, "PW", "Complete collection");
mainstats.appendChild(rs);

// Singleton collection sats
rs = renderSetStats(stats.singles, "PW", "Singles");
mainstats.appendChild(rs);
// each set stats
orderedSets.forEach(set => {
let rs = renderSetStats(stats[set], setsList[set].code, set);
mainstats.appendChild(rs);
});

const substats = createDivision(["main_stats", "sub_stats"]);

@@ -543,6 +561,24 @@ function renderSetStats(setStats, setIconCode, setName) {

//
function renderCompletionDiv(countStats, image, title) {
let numerator, denominator;
switch (countMode) {
case SINGLETONS:
numerator = countStats.uniqueOwned;
denominator = countStats.unique;
break;
case FULL_SETS:
numerator = countStats.complete;
denominator = countStats.unique;
break;
default:
case ALL_CARDS:
numerator = countStats.owned;
denominator = countStats.total;
break;
}
const completionRatio = numerator / denominator;

const completionDiv = createDivision(["stats_set_completion"]);

let setIcon = createDivision(["stats_set_icon"]);
@@ -556,11 +592,14 @@ function renderCompletionDiv(countStats, image, title) {
const detailsDiv = createDivision(["stats_set_details"]);

const percentSpan = document.createElement("span");
percentSpan.innerHTML = Math.round(countStats.percentage) + "%";
percentSpan.innerHTML = completionRatio.toLocaleString([], {
style: "percent",
maximumSignificantDigits: 2
});
detailsDiv.appendChild(percentSpan);

const countSpan = document.createElement("span");
countSpan.innerHTML = countStats.owned + " / " + countStats.total;
countSpan.innerHTML = numerator + " / " + denominator;
detailsDiv.appendChild(countSpan);

const wantedSpan = document.createElement("span");
@@ -573,7 +612,7 @@ function renderCompletionDiv(countStats, image, title) {
completionDiv.appendChild(wrapperDiv);

let setBar = createDivision(["stats_set_bar"]);
setBar.style.width = countStats.percentage + "%";
setBar.style.width = Math.round(completionRatio * 100) + "%";

completionDiv.appendChild(setBar);
return completionDiv;

0 comments on commit 07f2234

Please sign in to comment.
You can’t perform that action at this time.