Skip to content
Permalink
Browse files

Sort in explore

Lots of other small fixes and improvements to the code.
  • Loading branch information...
Manuel-777 committed Feb 9, 2019
1 parent 57997ab commit 24db603efae765d3fa7392b6698451f1048dcd36
Showing with 148 additions and 57 deletions.
  1. +33 −0 shared/util.js
  2. +1 −1 window_background/http-api.js
  3. +112 −46 window_main/explore.js
  4. +2 −10 window_main/renderer.js
@@ -857,6 +857,39 @@ function get_deck_missing(deck) {
return missing;
}

//
function get_deck_missing_short(deck) {
var missing = {r: 0, c: 0, u: 0, m: 0};

deck.mainDeck.forEach(function(card) {
var grpid = card.id;
var quantity = card.quantity;
var rarity = cardsDb.get(grpid).rarity;

let add = get_wc_missing(grpid, quantity);

if (rarity == 'common') {missing.c += add;}
if (rarity == 'uncommon') {missing.u += add;}
if (rarity == 'rare') {missing.r += add;}
if (rarity == 'mythic') {missing.m += add;}
});

deck.sideboard.forEach(function(card) {
var grpid = card.id;
var quantity = card.quantity;
var rarity = cardsDb.get(grpid).rarity;

let add = get_wc_missing(grpid, quantity);

if (rarity == 'common') {missing.c += add;}
if (rarity == 'uncommon') {missing.u += add;}
if (rarity == 'rare') {missing.r += add;}
if (rarity == 'mythic') {missing.m += add;}
});

return missing;
}

//
function get_deck_uniquestring(deck, side = true) {
if (!deck) return '';
@@ -291,7 +291,7 @@ function httpGetTopDecks(query, collection) {
heartbeatClear();
var _id = makeId(6);
collection = JSON.stringify(collection);
httpAsync.unshift({'reqId': _id, 'method': 'get_top_decks', 'method_path': '/get_courses_list.php', 'query': query, 'collection': collection});
httpAsync.unshift({'reqId': _id, 'method': 'get_top_decks', 'method_path': '/get_courses_list_short.php', 'query': query, 'collection': collection});
}

function httpGetTopLadderDecks() {
@@ -5,6 +5,7 @@ globals
selectAdd,
cardsDb,
mana,
orderedColorCodesCommon,
timeSince,
ipc_send,
getEventId,
@@ -23,6 +24,7 @@ let onlyOwned = false;
let exploreMode = 0;
let filteredMana = [];
let filteredranks = [];
let ownedWildcards = {c: 0, u: 0, r: 0, m: 0};

let ranks_list = ["Bronze", "Silver", "Gold", "Platinum", "Diamond", "Mythic"];

@@ -33,10 +35,15 @@ let raritySort = {c: 'common', u: 'uncommon', r: 'rare', m: 'mythic'};
let raritySortReversed = {common: 'c', uncommon: 'u', rare: 'r', mythic: 'm'};

function updateExplore() {
filterEvent = getEventId(document.getElementById("query_select").value);
filterEvent = getEventId(document.getElementById("query_select_filter").value);
ipc_send('request_explore', filterEvent);
}

function updateSort() {
filterSort = document.getElementById("query_select_sort").value;
open_explore_tab(null, 0);
}

function set_explore_mode(mode) {
exploreMode = mode;
}
@@ -48,13 +55,14 @@ function open_explore_tab(arg, loadMore) {
ladder = arg;
}
else {
console.log(arg);
explore = arg;
}
}

var mainDiv = document.getElementById("ux_0");
var dateNow, d;
let ownedWildcards = {c: economyHistory.wcCommon, u: economyHistory.wcUncommon, r: economyHistory.wcRare, m: economyHistory.wcMythic};
ownedWildcards = {c: economyHistory.wcCommon, u: economyHistory.wcUncommon, r: economyHistory.wcRare, m: economyHistory.wcMythic};

mainDiv.classList.remove("flex_item");
if (loadMore <= 0) {
@@ -63,16 +71,28 @@ function open_explore_tab(arg, loadMore) {

mainDiv.innerHTML = '';

if (filterSort == "By Winrate") sortFunction = sortByWinrate;
if (filterSort == "By Player") sortFunction = sortByPlayer;
if (filterSort == "By Boosters") sortFunction = sortByBoosters;
if (exploreMode == 1) {
ladder = add_booster_cost(ladder, exploreMode);
ladder.sort(sortFunction);
}
else {
explore = add_booster_cost(explore, exploreMode);
explore.sort(sortFunction);
}

d = document.createElement("div");
d.classList.add("list_fill");
mainDiv.appendChild(d);// goes down

// Search box
var icd = $('<div class="explore_buttons_container"></div>');

// Event filter
var input = $('<div class="query_explore" style="margin-left: 16px;"></div>');
var select = $('<select id="query_select"></select>');

var select = $('<select id="query_select_filter"></select>');
if (eventFilters == null) {
eventFilters = [];
eventFilters.push('All');
@@ -91,7 +111,7 @@ function open_explore_tab(arg, loadMore) {
i--;
}
else {
let evId = getReadableEvent(_deck.event)//.replace(/[0-9]/g, '');
let evId = getReadableEvent(_deck.event)
if (!eventFilters.includes(evId)) {
eventFilters.push(evId);
}
@@ -111,12 +131,22 @@ function open_explore_tab(arg, loadMore) {
select.appendTo(input);
selectAdd(select, updateExplore);
select.next('div.select-styled').text(getReadableEvent(filterEvent));
input.appendTo(icd);

// Sort filter
var input = $('<div class="query_explore" style="margin-left: 16px;"></div>');
var select = $('<select id="query_select_sort"></select>');
sortFilters = ["By Winrate", "By Player", "By Boosters"];
for (let i=0; i < sortFilters.length; i++) {
select.append('<option value="'+sortFilters[i]+'">'+sortFilters[i]+'</option>');
}
select.appendTo(input);
selectAdd(select, updateSort);
select.next('div.select-styled').text(filterSort);
input.appendTo(icd);

var manas = $('<div class="mana_filters_explore"></div>');
var ms = ["w", "u", "b", "r", "g"];
ms.forEach(function(s, i) {
orderedColorCodesCommon.forEach(function(s, i) {
var mi = [1, 2, 3, 4, 5];
var mf = "";
if (!filteredMana.includes(mi[i])) {
@@ -182,10 +212,10 @@ function open_explore_tab(arg, loadMore) {

//explore.forEach(function(_deck, index) {
if (exploreMode == 1) {
ladderLoadMore(loadMore, ownedWildcards);
ladderLoadMore(loadMore);
}
else {
exploreLoadMore(loadMore, ownedWildcards);
exploreLoadMore(loadMore);
}

if (loadMore == 0 && loadExplore-actuallyLoaded < 20) {
@@ -200,7 +230,7 @@ function open_explore_tab(arg, loadMore) {
})
}

function ladderLoadMore(loadMore, ownedWildcards) {
function ladderLoadMore(loadMore) {
let mainDiv = document.getElementById("ux_0");
var actuallyLoaded = loadExplore;
console.log(ladder);
@@ -209,7 +239,6 @@ function ladderLoadMore(loadMore, ownedWildcards) {
if (_deck == undefined) {
continue;
}
_deck.wildcards = get_deck_missing(_deck);

if (filteredMana.length > 0) {
let filterOut = false;
@@ -242,38 +271,30 @@ function ladderLoadMore(loadMore, ownedWildcards) {

let wc;
let n = 0;
let boosterCost = 0;
for (var key in raritySortReversed) {
if (_deck.wildcards.hasOwnProperty(key)) {
if (_deck.wildcards[key] > 0) {
let bc = rarityBooster[raritySortReversed[key]] * (_deck.wildcards[key] - ownedWildcards[raritySortReversed[key]]);

n++;
if (bc > boosterCost) {
boosterCost = bc;
}
wc = document.createElement("div");
wc.classList.add("wc_explore_cost");
wc.classList.add("wc_"+key);
wc.title = key.capitalize()+" wldcards needed.";
wc.innerHTML = ownedWildcards[raritySortReversed[key]] + '/' + _deck.wildcards[key];
flcf.appendChild(wc);
}
for (var key in raritySort) {
if (_deck.wildcards.hasOwnProperty(key) && _deck.wildcards[key] > 0) {
wc = document.createElement("div");
wc.classList.add("wc_explore_cost");
wc.classList.add("wc_"+raritySort[key]);
wc.title = raritySort[key].capitalize()+" wldcards needed.";
wc.innerHTML = ownedWildcards[key] + '/' + _deck.wildcards[key];
flcf.appendChild(wc);
n++;
}
}

if (n == 0) {
wc = document.createElement("div");
wc.classList.add("wc_complete");
flcf.appendChild(wc);
}

else if (onlyOwned) {
continue;
}
else {
let bo = document.createElement("div");
bo.classList.add("bo_explore_cost");
bo.innerHTML = boosterCost;
bo.innerHTML = _deck.wildcards.boosters;
bo.title = "Boosters needed (estimated)";
flcf.appendChild(bo);
}
@@ -283,9 +304,9 @@ function ladderLoadMore(loadMore, ownedWildcards) {
if (_deck.colors == undefined) {
_deck.colors = [];
}
if (_deck.wins == undefined) {
_deck.wins = 0;
_deck.losses = 0;
if (_deck.w == undefined) {
_deck.w = 0;
_deck.l = 0;
}

var tileGrpid = _deck.tile;
@@ -391,7 +412,7 @@ function ladderLoadMore(loadMore, ownedWildcards) {
}


function exploreLoadMore(loadMore, ownedWildcards) {
function exploreLoadMore(loadMore) {
let mainDiv = document.getElementById("ux_0");
var actuallyLoaded = loadExplore;
for (var loadEnd = loadExplore + loadMore; actuallyLoaded < loadEnd && loadExplore < explore.length; loadExplore++) {
@@ -432,18 +453,14 @@ function exploreLoadMore(loadMore, ownedWildcards) {
let n = 0;
let boosterCost = 0;
for (var key in raritySort) {
if (_deck.wildcards.hasOwnProperty(key)) {
n++;
let bc = rarityBooster[key] * (_deck.wildcards[key] - ownedWildcards[key]);
if (bc > boosterCost) {
boosterCost = bc;
}
if (_deck.wildcards.hasOwnProperty(key) && _deck.wildcards[key] > 0) {
wc = document.createElement("div");
wc.classList.add("wc_explore_cost");
wc.classList.add("wc_"+raritySort[key]);
wc.title = raritySort[key].capitalize()+" wldcards needed.";
wc.innerHTML = ownedWildcards[key] + '/' + _deck.wildcards[key];
flcf.appendChild(wc);
n++;
}
}
if (n == 0) {
@@ -457,16 +474,16 @@ function exploreLoadMore(loadMore, ownedWildcards) {
else {
let bo = document.createElement("div");
bo.classList.add("bo_explore_cost");
bo.innerHTML = boosterCost;
bo.innerHTML = _deck.wildcards.boosters;
bo.title = "Boosters needed (estimated)";
flcf.appendChild(bo);
}

actuallyLoaded++;

if (_deck.wins == undefined) {
_deck.wins = 0;
_deck.losses = 0;
if (_deck.w == undefined) {
_deck.w = 0;
_deck.l = 0;
}

var tileGrpid = _deck.tile;
@@ -527,8 +544,8 @@ function exploreLoadMore(loadMore, ownedWildcards) {
d = document.createElement("div");
d.classList.add("list_deck_record");

let colClass = getWinrateClass(1/(_deck.wins+_deck.losses)*_deck.wins);
d.innerHTML = `${_deck.wins}:${_deck.losses} <span class="${colClass}_bright">(${Math.round(100/(_deck.wins+_deck.losses)*_deck.wins)}%)</span>`;
let colClass = getWinrateClass(1/(_deck.w+_deck.l)*_deck.w);
d.innerHTML = `${_deck.w}:${_deck.l} <span class="${colClass}_bright">(${Math.round(100/(_deck.w+_deck.l)*_deck.w)}%)</span>`;
flr.appendChild(d);

d = document.createElement("div");
@@ -574,6 +591,55 @@ function open_course_request(courseId) {
ipc_send('request_course', courseId);
}

function add_booster_cost(list, mode) {
list.forEach((deck) => {
if (mode == 1) {
deck.wildcards = get_deck_missing_short(deck);
}
deck.wildcards.boosters = 0;
for (var key in raritySort) {
if (deck.wildcards.hasOwnProperty(key)) {
bc = rarityBooster[key] * (deck.wildcards[key] - ownedWildcards[key]);
if (bc > deck.wildcards.boosters)
deck.wildcards.boosters = bc;
}
}
});

return list;
}

function sortByWinrate(a, b) {
if (!b) return -1;
if (!a) return 1;

let awlrate = a.w / (a.w+a.l);
let bwlrate = b.w / (b.w+b.l);

if (awlrate > bwlrate) return -1;
if (awlrate < bwlrate) return 1;
return 0;
}

function sortByPlayer(a, b) {
if (!b) return -1;
if (!a) return 1;

if (a.player < b.player) return -1;
if (a.player > b.player) return 1;
return 0;
}

function sortByBoosters(a, b) {
if (!b.wildcards.boosters) return -1;
if (!a.wildcards.boosters) return 1;

if (a.wildcards.boosters < b.wildcards.boosters) return -1;
if (a.wildcards.boosters > b.wildcards.boosters) return 1;
return 0;
}


module.exports = {
open_explore_tab, update_explore_filters, set_explore_mode
}
Oops, something went wrong.

0 comments on commit 24db603

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