Skip to content

Commit

Permalink
Inherit scores for "Don't know" and "Don't care"
Browse files Browse the repository at this point in the history
  • Loading branch information
Androbin committed Jul 23, 2018
1 parent 36e568f commit 8217b0c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/data/dataFileNames.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"main":"firstModule.json","others":["RfEsZpNK.791f2cda554d18a95fc3734bbe65f88b.json","sArkxak3.bda40d280ffd6baa48d38117905bd5ab.json","2qVqSwId.6c0be3e0610de0d44ac1b294da16d4fd.json","vQrgRbIS.80594ce9bebcfa2809538046c647dde3.json","U77gcZNf.8857dc7179e0813487e12b85611b3b9b.json","RqGMsFtz.aeed2715e184fe51657af9db9ba6f965.json"]}
{"main":"firstModule.json","others":["RfEsZpNK.791f2cda554d18a95fc3734bbe65f88b.json","sArkxak3.bda40d280ffd6baa48d38117905bd5ab.json","2qVqSwId.b86995173d611d4e9c0524bce3ecf6e5.json","vQrgRbIS.80594ce9bebcfa2809538046c647dde3.json","U77gcZNf.8857dc7179e0813487e12b85611b3b9b.json","RqGMsFtz.aeed2715e184fe51657af9db9ba6f965.json"]}
79 changes: 69 additions & 10 deletions app/data/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ANALYTICS_STRINGS,
EVENTS,
SCORES,
INHERITING_KEYS,
} from '../utils/constants';
import logTiming from '../utils/logTiming';

Expand Down Expand Up @@ -99,6 +100,18 @@ const store = {
});
},

computeValue(currentScoreKey, newScoreKey, oldScoreKey) {
if (INHERITING_KEYS.includes(newScoreKey)) {
return newScoreKey;
} else if (INHERITING_KEYS.includes(oldScoreKey)) {
if (currentScoreKey == oldScoreKey) {
return SCORES.LEVEL_0.key;
}
}

return currentScoreKey || SCORES.LEVEL_0.key;
},

getItem(idOrItem) {
if (typeof idOrItem === `string`) {
return this.itemCache[idOrItem];
Expand Down Expand Up @@ -221,11 +234,28 @@ const store = {
if (!item) return;
const updateDom = (typeof options.updateDom !== `undefined`) ? options.updateDom : true;

const oldScoreKey = item.scoreKey;

// caution: update the score summary before updating the item
const updatedItems = this.updateScoreSummary(item, scoreKey, item.scoreKey);
const updatedItems = this.updateScoreSummary(item, scoreKey, oldScoreKey);

this.updateItem(item, { scoreKey }, options);

const updateChildren = (parent) => {
const children = this.getChildrenOf(parent.id);
if (!children) return;

children.forEach((child) => {
const newValue = this.computeValue(child.scoreKey, scoreKey, oldScoreKey);
this.updateItem(child, { scoreKey: newValue }, options);
updateChildren(child);
});
};

if (INHERITING_KEYS.includes(scoreKey) || INHERITING_KEYS.includes(oldScoreKey)) {
updateChildren(item);
}

if (updateDom) {
updatedItems.forEach((updatedItem) => {
this.triggerListener(`PIE-${updatedItem.id}`);
Expand All @@ -241,23 +271,52 @@ const store = {
oldScoreKey = oldScoreKey || SCORES.LEVEL_0.key;
const updatedItems = [];

const updateParentScore = (child) => {
const parent = this.getParent(child);
if (!parent) return; // we're at the top
const updateSummary = (parent, inherit) => {
const children = this.getChildrenOf(parent.id);
if (!children) return; // we're at the bottom
updatedItems.push(parent);

this.scoreSummary[parent.id] = this.scoreSummary[parent.id] || {};
this.scoreSummary[parent.id][newScoreKey] = this.scoreSummary[parent.id][newScoreKey] || 0;
this.scoreSummary[parent.id][newScoreKey] += 1;
Object.keys(SCORES).forEach((scoreKey) => {
this.scoreSummary[parent.id][scoreKey] = children
.map((child) => {
this.scoreSummary[child.id] = this.scoreSummary[child.id] || {};
const scoreSummary = this.scoreSummary[child.id][scoreKey] || 0;
let newValue = child.scoreKey || SCORES.LEVEL_0.key;

if (child == item) {
newValue = newScoreKey;
} else if (inherit) {
newValue = this.computeValue(child.scoreKey, newScoreKey, oldScoreKey);
}

return newValue == scoreKey ? scoreSummary + 1 : scoreSummary;
})
.reduce((a, b) => a + b, 0);
});
};
const updateChildrenScore = (parent) => {
const children = this.getChildrenOf(parent.id);
if (!children) return; // we're at the bottom

if (!newItem) {
this.scoreSummary[parent.id][oldScoreKey] -= 1;
}
children.forEach((child) => {
updateChildrenScore(child);
});

updateSummary(parent, true);
};
const updateParentScore = (child) => {
const parent = this.getParent(child);
if (!parent) return; // we're at the top

updateSummary(parent, false);
updateParentScore(parent);
};

// we don't update the item directly, only its ancestors
if (INHERITING_KEYS.includes(newScoreKey) || INHERITING_KEYS.includes(oldScoreKey)) {
updateChildrenScore(item);
}

updateParentScore(item);

return updatedItems;
Expand Down
2 changes: 2 additions & 0 deletions app/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export const SCORES = {
},
};

export const INHERITING_KEYS = [SCORES.LEVEL_1.key, SCORES.LEVEL_4.key];

export const KEYS = {
DOWN: 40,
ENTER: 13,
Expand Down

0 comments on commit 8217b0c

Please sign in to comment.