Commit cbd8e5e5 by Иван Кубота

seed, iterator, standard q category

parent 94941475
......@@ -16,14 +16,13 @@ const options = yargs
.option("n", { alias: "nolog", describe: "no text log in object", type: "boolean"})
.option("d", { alias: "dir", describe: "database dir", type: "string"})
.option("f", { alias: "fake", describe: "add fake images", type: "boolean"})
.option("c", { alias: "category", describe: "1 - product, 2 - standard", type: "number"})
.option("i", { alias: "id", describe: "return question with id. use it with `seed` arg", type: "number"})
.argv;
if(options.random){
options.m = options.multiple = Math.random()>24/(46+24);
options.p = options.photo = Math.random()>0.7;
}
const greeting = `Hello, ${options.name}!`;
options.c = options.category = options.c || 1;
......@@ -57,42 +56,88 @@ const sources = [
"js/controller/quizBits/radioPhoto.js"
];
const rand = function(a, b){
if(Array.isArray(a)){
return a[Math.random.seeded()*a.length|0];
}
if(typeof a === 'object' && 'max' in a){
b = a.max;
a = a.min || 0;
}
a = Math.ceil(a);
b = Math.floor(b);
return Math.floor(Math.random.seeded() * (b - a + 1)) + a;
return (r-a)*(b-a)|0;
};
var js = sources.map(a=>'./public/'+a).map(n=>fs.readFileSync(n)+'').join('\n\n');
if(options.dir)
process.env.KUS_DATA_DIR = options.dir;
const data = require("../db.js");
const body = 'const window = {};'+js+'; return {quizGenerator, initDataProvider, seeded: Math.random.seeded}';
const body = 'const window = {};'+js+'; return {quizGenerator, standardGenerator, initDataProvider, seeded: Math.random.seeded, rand}';
var ctx = new Function('',body)();
const rand = ctx.rand,
seeded = ctx.seeded;
//console.log(options);
data.after = function(){
if( options.seed )
ctx.seeded.setStringSeed( options.seed );
let countStdQ = Object.values(data.standardQuestions).length;
let state = {id: rand(0,countStdQ-1)};
if(options.random){
if(options.i){
const quiz = [];
let last = {}, cur, used = {};
for( let i = 0; i < 100; i++ ){
cur = {
m: seeded() > 24 / ( 46 + 24 ),
p: seeded() > 0.7,
c: 1
};
if( last.c === 1 && seeded() > 0.5 ){
cur = {
c: 2
};
let id;
do{
id = rand(0,countStdQ-1);
}while(id in used);
used[id] = true;
cur.id = id;
}
quiz.push(cur);
last = cur;
}
if( options.seed )
ctx.seeded.setStringSeed( options.i+options.seed );
state = quiz[options.i];
options.c = options.category = quiz[options.i].c;
if(options.c === 1){
options.m = options.multiple = quiz[ options.i ].m;
options.p = options.photo = quiz[ options.i ].p;
}
}else{
options.m = options.multiple = seeded() > 24 / ( 46 + 24 );
options.p = options.photo = seeded() > 0.7;
options.c = options.category =seeded() > 0.6 ? 2 : 1;
}
}
if( options.fake ){
for(var i = 0; i < 10; i++){
rand(Object.values(data.products)).image = 'https://robohash.org/'+Math.random().toString(36)
rand(Object.values(data.products)).image = 'https://robohash.org/'+seeded.toString(36)
}
}
ctx.initDataProvider( data );
if( options.seed )
ctx.seeded.setStringSeed( options.seed );
const result = ctx.quizGenerator( options.multiple ? 'checkbox' : 'radio', options.photo );
let result;
if(options.category === 1){
result = ctx.quizGenerator( options.multiple ? 'checkbox' : 'radio', options.photo );
}else{
result = ctx.standardGenerator(()=>state.id)
}
if( options.verbose ){
console.log( result.log.join( '\n' ) )
......
......@@ -51,6 +51,32 @@ const shuffle = function (a) {
}
return a;
};
const standardGenerator = function(random) {
const initialSeed = Math.random.seeded.getStringSeed();
const log = quizGenerator.log = ['Random seed: '+initialSeed];
const questionID = random(Object.values(dP.standardQuestions)),
q = dP.standardQuestions[questionID];
log.push('Creating standard question.');
log.push('It would be question number '+questionID);
log.push('');
log.push('Title '+q.title);
q.answers.forEach(
a =>
log.push((a.correct?'+':' ')+` > ${a.title}`));
return {
type: q.multiple ? 'checkbox': 'radio',
categoryId: 2,
productId: q.cardInfoID,
question: textFormat(q.title),
answers: shuffle(q.answers.map(a =>
a.correct ?
new Answer.Correct(a.title) :
new Answer.Wrong(a.title))),
image: !q.image?null:q.image,
log
};
};
const quizGenerator = function(type, photo, subType) {
return _quizGenerator(type, photo, subType);
},
......
......@@ -9,7 +9,8 @@ const store = new Store({
'productFilterByComponent': true,
'productFilterByTag': true,
'generateType': 'radio',
'generatePhoto': false
'generatePhoto': false,
generateCategory: 'product'
});
......
......@@ -3,7 +3,7 @@ const dataProvider = {
maxConnection: 0,
maxTagID: 0,
slice: {},
standardQuestions: {},
tags: [],
connections: {},
products: {}
......
......@@ -11,15 +11,17 @@ view.cmp.Answer = function(answer, type) {
view.page.Generate = function() {
const update = function() {
const photo = store.get('generatePhoto') === 'photo',
type = store.get('generateType');
type = store.get('generateType'),
category = store.get('generateCategory');
try{
seedInput.value = Math.random.seeded.getStringSeed();
const result = quizGenerator( type, photo );
const result = category === 'product' ? quizGenerator( type, photo ) : standardGenerator(({length})=>rand(0,length-1));
title.innerHTML = textFormat( result.question, true );
image.innerHTML = result.image ? `<img src="${result.image}" alt="img"/>` : '';
image.style.display = result.image ? 'block': 'none';
debug.value = result.log.join( '\n' )
D.removeChildren( answers );
D.appendChild( answers, result.answers.map( ( a ) => view.cmp.Answer( a, type ) ) );
......@@ -34,16 +36,28 @@ view.page.Generate = function() {
this.dom =
div({cls: 'generate-panel'},
div({cls: 'title-gradient'},
div({cls: 'generate-sub-menu'},
div({cls: 'generate-sub-menu'},
view.cmp.Menu({title: 'Единственный', id: 'radio', key: 'generateType'}),
view.cmp.Menu({title: 'Множественный', id: 'checkbox', key: 'generateType'})
view.cmp.Menu({title: 'Продукты', id: 'product', key: 'generateCategory'}),
view.cmp.Menu({title: 'Стандарты', id: 'standard', key: 'generateCategory'})
),
view.cmp.Switch({key: 'generateCategory'}, {
product: div({cls: 'generate-sub-menu'},
div({cls: 'generate-sub-menu'},
view.cmp.Menu({title: 'Единственный', id: 'radio', key: 'generateType'}),
view.cmp.Menu({title: 'Множественный', id: 'checkbox', key: 'generateType'})
),
div({cls: 'generate-sub-menu'},
view.cmp.Menu({title: 'С фото', id: 'photo', key: 'generatePhoto'}),
view.cmp.Menu({title: 'Без фото', id: 'noPhoto', key: 'generatePhoto'})
)
)
})
div({cls: 'generate-sub-menu'},
view.cmp.Menu({title: 'С фото', id: 'photo', key: 'generatePhoto'}),
view.cmp.Menu({title: 'Без фото', id: 'noPhoto', key: 'generatePhoto'})
)
)
),
div({cls: 'generate-controls'},
......@@ -74,6 +88,6 @@ view.page.Generate = function() {
);
store.sub(['generateType', 'generatePhoto'], update);
store.sub(['generateType', 'generatePhoto', 'generateCategory'], update);
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment