Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rooklift committed Aug 26, 2021
1 parent 8549ee4 commit 69b0e6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
10 changes: 8 additions & 2 deletions src/75_looker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//
// Note: Don't store the retrieved info in the node.table, because the logic
// there is already a bit convoluted with __touched, __ghost and whatnot (sadly).
//
// Note: format of entries in the DB is {type: "foo", moves: {}}
// where moves is a map of string --> something

function NewLooker() {
let looker = Object.create(null);
Expand Down Expand Up @@ -76,6 +79,9 @@ let looker_props = {

lookup: function(db_name, board) {

// When repeatedly called with the same params, this should
// return the same object (unless it changes of course).

if (typeof db_name !== "string" || !this.all_dbs[db_name]) {
return null;
}
Expand Down Expand Up @@ -133,7 +139,7 @@ let looker_props = {
// Create or recreate the info object. Recreation ensures that the infobox drawer can
// tell that it's a new object if it changes (and a redraw is needed).

let o = Object.create(null);
let o = {type: "chessdbcn", moves: {}};
db[fen] = o;

// Parse the data...
Expand Down Expand Up @@ -171,7 +177,7 @@ let looker_props = {
}

if (move && typeof val === "number") {
o[move] = val;
o.moves[move] = val;
}
}

Expand Down
43 changes: 25 additions & 18 deletions src/82_infobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ let infobox_props = {
info_list = SortedMoveInfo(node);
}

// A lookup_object should always have type (string) and moves (object).

let ltype = lookup_object ? lookup_object.type : null;
let lookup_moves = lookup_object ? lookup_object.moves : null;

// If we are using an online API, and the list has some "untouched" info, we
// may be able to sort them using the API info.

if (lookup_object) {
if (ltype === "chessdbcn") {

let touched_list = [];
let untouched_list = [];
Expand All @@ -44,10 +49,10 @@ let infobox_props = {
const b_is_best = 1;

untouched_list.sort((a, b) => {
if (typeof lookup_object[a.move] === "number" && typeof lookup_object[b.move] !== "number") return a_is_best;
if (typeof lookup_object[a.move] !== "number" && typeof lookup_object[b.move] === "number") return b_is_best;
if (typeof lookup_object[a.move] !== "number" && typeof lookup_object[b.move] !== "number") return 0;
return lookup_object[b.move] - lookup_object[a.move];
if (typeof lookup_moves[a.move] === "number" && typeof lookup_moves[b.move] !== "number") return a_is_best;
if (typeof lookup_moves[a.move] !== "number" && typeof lookup_moves[b.move] === "number") return b_is_best;
if (typeof lookup_moves[a.move] !== "number" && typeof lookup_moves[b.move] !== "number") return 0;
return lookup_moves[b.move] - lookup_moves[a.move];
});

info_list = touched_list.concat(untouched_list);
Expand Down Expand Up @@ -228,22 +233,24 @@ let infobox_props = {
}

if (config.looker_api) {
if (lookup_object && typeof lookup_object[info.move] === "number") {

let val = lookup_object[info.move];

if ((config.cp_pov === "b" && node.board.active === "w") || (config.cp_pov === "w" && node.board.active === "b")) {
val *= -1;
let api_string = "API: ?"; // Default.

if (ltype === "chessdbcn") {
if (typeof lookup_moves[info.move] === "number") {
let val = lookup_moves[info.move];
if ((config.cp_pov === "b" && node.board.active === "w") || (config.cp_pov === "w" && node.board.active === "b")) {
val *= -1;
}
let s = val.toFixed(2);
if (s !== "0.00" && s[0] !== "-") {
s = "+" + s;
}
api_string = `API: ${s}`;
}

let s = val.toFixed(2);
if (s !== "0.00" && s[0] !== "-") {
s = "+" + s;
}
extra_stat_strings.push(`API: ${s}`);
} else {
extra_stat_strings.push(`API: ?`);
}

extra_stat_strings.push(api_string);
}

if (extra_stat_strings.length > 0) {
Expand Down

0 comments on commit 69b0e6c

Please sign in to comment.