You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB
JavaScript

2 years ago
function randint (from, to) {
return from + Math.floor((Math.random() * (to-from)));
}
function choice (arr) {
let ri = randint(0, arr.length-1);
let ret = arr[ri];
arr.splice(ri, 1);
return ret;
}
2 years ago
function make_phrase (nn, terms) {
nn.querySelector(".actor").innerHTML = choice(terms['actor']);
nn.querySelector(".action").innerHTML = choice(terms['action']);
if (randint(0, 2) == 0) {
nn.querySelector(".prefix").innerHTML = choice(terms['prefix'])+'-';
2 years ago
} else {
nn.querySelector(".prefix").remove();
}
nn.querySelector(".sort").innerHTML = choice(terms['sort']);
nn.querySelector(".media").innerHTML = choice(terms['media']);
}
const sleep = delaysecs => new Promise(resolve => setTimeout(resolve, delaysecs*1000));
2 years ago
let limit = 25;
2 years ago
async function make_phrases () {
let terms = await (await fetch("terms.json")).json();
// console.log("terms", terms);
let call = document.querySelector("#call");
let template = call.querySelector(".phrase");
template.remove();
2 years ago
let count = 0;
2 years ago
while (true) {
let nn = template.cloneNode(true);
call.appendChild(nn);
make_phrase(nn, terms);
if (terms['actor'].length == 0 || terms['action'].length == 0 || terms['prefix'].length == 0 || terms['sort'].length == 0 || terms['media'].length == 0) {
console.log("out of terms, stopping");
break;
2 years ago
}
2 years ago
count += 1;
if (limit > 0 && count>=limit) { break };
2 years ago
await (sleep(1.0));
2 years ago
}
}
make_phrases();
2 years ago
// fetch("terms.json")
// .then(response => response.json())
// .then(terms => {
// console.log("terms", terms);
// let call = document.querySelector("#call");
// let template = call.querySelector(".phrase");
// for (var i=0; i<20; i++) {
// let nn = template.cloneNode(true);
// call.appendChild(nn);
// nn.querySelector(".actor").innerHTML = choice(terms['actor']);
// nn.querySelector(".action").innerHTML = choice(terms['action']);
// if (randint(0, 2) == 0) {
// nn.querySelector(".prefix").innerHTML = choice(terms['prefix']) + '-';
// } else {
// nn.querySelector(".prefix").remove();
// }
// nn.querySelector(".sort").innerHTML = choice(terms['sort']);
// nn.querySelector(".media").innerHTML = choice(terms['media']);
// }
// template.style.display = "none";
// });