Pārlūkot izejas kodu

Weight news so that recent items are less likely to appear

tags/v0.1.0
Fen Dweller pirms 5 gadiem
vecāks
revīzija
f964b80055
Šim parakstam datu bāzē netika atrasta zināma atslēga GPG atslēgas ID: E80B35A6F11C3656
2 mainītis faili ar 37 papildinājumiem un 3 dzēšanām
  1. +24
    -3
      gorge.js
  2. +13
    -0
      util.js

+ 24
- 3
gorge.js Parādīt failu

@@ -48,6 +48,8 @@ let newsRemoveTimer;

const newsDelay = 8000;

const newsWeightFactors = [];

let buttonClicked = false;

const state = {
@@ -531,6 +533,7 @@ function setup() {
// prepare dynamic stuff

initializeData();
initializeNews();
createButtons();
createDisplays();
registerListeners();
@@ -541,6 +544,12 @@ function setup() {
updateAll();
}

function initializeNews() {
news.forEach(entry => {
newsWeightFactors.push(0);
});
}

const cache = {};

function initializeCaches() {
@@ -1116,16 +1125,28 @@ function clickPopup(text, type, location) {

function doNews() {
let options = [];
let weights = [];
let indices = [];
let index = 0;
news.forEach(entry => {
if (entry.condition(state)) {
if (entry.condition(state) && newsWeightFactors[index] != 1) {
options = options.concat(entry.lines);
weights.push(1 - newsWeightFactors[index])
indices.push(index);
}
index += 1;
});

const choice = Math.floor(Math.random() * options.length);
const choice = weightedSelect(weights);
showNews(options[choice](state));

for (let i = 0; i < newsWeightFactors.length; i++) {
newsWeightFactors[i] *= 0.9;
}

newsWeightFactors[indices[choice]] = 1;

newsShowTimer = setTimeout(() => {
doNews();
}, 8000);


+ 13
- 0
util.js Parādīt failu

@@ -53,3 +53,16 @@ function capitalize(string) {
function weekday() {
return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][(new Date()).getDay()];
}

function weightedSelect(weights) {
const total = weights.reduce((x,y) => x+y, 0);
const rand = Math.random() * total;
let counter = weights[0];
let index = 0;
while (counter < rand) {
index += 1;
counter += weights[index];
}

return index;
}

Notiek ielāde…
Atcelt
Saglabāt