Skip to content

Commit

Permalink
Addresses code review from @tom-james-watson
Browse files Browse the repository at this point in the history
  • Loading branch information
benguaraldi committed Feb 25, 2022
1 parent 46e3680 commit ab807cc
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions lib/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,33 @@ export function getRandomItem(deck: Item[], played: Item[]): Item {
const [fromYear, toYear] =
periods[Math.floor(Math.random() * periods.length)];
const avoidPeople = Math.random() > 0.5;
let distance = 5;
if (played.length < 11) {
distance = 110 - 10 * played.length;
} else if (played.length > 40) {
distance = 1;
}
let item = deck[Math.floor(Math.random() * deck.length)];

for (let i = 0; i < 10000; i++) {
item = deck[Math.floor(Math.random() * deck.length)];
if (avoidPeople && item.instance_of.includes("human")) {
continue;
const candidates = deck.filter((candidate) => {
if (avoidPeople && candidate.instance_of.includes("human")) {
return false;
}
if (item.year < fromYear || item.year > toYear) {
continue;
if (candidate.year < fromYear || candidate.year > toYear) {
return false;
}
if (tooClose(item, played, distance)) {
continue;
if (tooClose(candidate, played)) {
return false;
}
return item;
}
return true;
});

if (candidates.length > 0) {
const item = { ...candidates[Math.floor(Math.random() * candidates.length)] };
} else {
throw new Error("No item candidates");
const item = { ...deck[Math.floor(Math.random() * deck.length)] };
}
return item;
}

function tooClose(item: Item, played: Item[], distance: number) {
function tooClose(item: Item, played: Item[]) {
let distance = (played.length < 40) ? 5 : 1;
if (played.length < 11)
distance = 110 - 10 * played.length;

for (let j = 0; j < played.length; j++) {
if (Math.abs(item.year - played[j].year) < distance) {
return true;
Expand All @@ -54,7 +55,7 @@ export function checkCorrect(
return i.id === item.id;
});

if (index !== correctIndex && item.year !== [...played, item][correctIndex].year) {
if (index !== correctIndex) {
return { correct: false, delta: correctIndex - index };
}

Expand Down

0 comments on commit ab807cc

Please sign in to comment.