Commit 9eb23466 by Иван Кубота

Merge branch 'master' into m2

parents 65638767 09075ed1
...@@ -16,10 +16,16 @@ const options = yargs ...@@ -16,10 +16,16 @@ const options = yargs
.option("n", { alias: "nolog", describe: "no text log in object", type: "boolean"}) .option("n", { alias: "nolog", describe: "no text log in object", type: "boolean"})
.option("d", { alias: "dir", describe: "database dir", type: "string"}) .option("d", { alias: "dir", describe: "database dir", type: "string"})
.option("f", { alias: "fake", describe: "add fake images", type: "boolean"}) .option("f", { alias: "fake", describe: "add fake images", type: "boolean"})
.option("c", { alias: "category", describe: "1 - product, 2 - standard", type: "string"})
.option("i", { alias: "id", describe: "return question with id. use it with `seed` arg", type: "number"})
.argv; .argv;
let categoryFixed = false;
if(options.c)
categoryFixed = true;
options.c = options.category = (parseInt(options.c)|0) || 1;
const greeting = `Hello, ${options.name}!`;
...@@ -53,42 +59,89 @@ const sources = [ ...@@ -53,42 +59,89 @@ const sources = [
"js/controller/quizBits/radioPhoto.js" "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'); var js = sources.map(a=>'./public/'+a).map(n=>fs.readFileSync(n)+'').join('\n\n');
if(options.dir) if(options.dir)
process.env.KUS_DATA_DIR = options.dir; process.env.KUS_DATA_DIR = options.dir;
const data = require("../db.js"); 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)(); var ctx = new Function('',body)();
const rand = ctx.rand,
seeded = ctx.seeded;
//console.log(options); //console.log(options);
data.after = function(){ 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 ){ if( options.fake ){
for(var i = 0; i < 10; i++){ 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 ); 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 ){ if( options.verbose ){
console.log( result.log.join( '\n' ) ) console.log( result.log.join( '\n' ) )
......
cd /home/forge/kus-quizard cd /home/forge/kus-quizard
node . node . -r -n
\ No newline at end of file \ No newline at end of file
...@@ -51,6 +51,32 @@ const shuffle = function (a) { ...@@ -51,6 +51,32 @@ const shuffle = function (a) {
} }
return 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) { const quizGenerator = function(type, photo, subType) {
return _quizGenerator(type, photo, subType); return _quizGenerator(type, photo, subType);
}, },
......
...@@ -9,7 +9,8 @@ const store = new Store({ ...@@ -9,7 +9,8 @@ const store = new Store({
'productFilterByComponent': true, 'productFilterByComponent': true,
'productFilterByTag': true, 'productFilterByTag': true,
'generateType': 'radio', 'generateType': 'radio',
'generatePhoto': false 'generatePhoto': false,
generateCategory: 'product'
}); });
......
...@@ -3,7 +3,7 @@ const dataProvider = { ...@@ -3,7 +3,7 @@ const dataProvider = {
maxConnection: 0, maxConnection: 0,
maxTagID: 0, maxTagID: 0,
slice: {}, slice: {},
standardQuestions: {},
tags: [], tags: [],
connections: {}, connections: {},
products: {} products: {}
......
...@@ -11,15 +11,17 @@ view.cmp.Answer = function(answer, type) { ...@@ -11,15 +11,17 @@ view.cmp.Answer = function(answer, type) {
view.page.Generate = function() { view.page.Generate = function() {
const update = function() { const update = function() {
const photo = store.get('generatePhoto') === 'photo', const photo = store.get('generatePhoto') === 'photo',
type = store.get('generateType'); type = store.get('generateType'),
category = store.get('generateCategory');
try{ try{
seedInput.value = Math.random.seeded.getStringSeed(); 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 ); title.innerHTML = textFormat( result.question, true );
image.innerHTML = result.image ? `<img src="${result.image}" alt="img"/>` : ''; image.innerHTML = result.image ? `<img src="${result.image}" alt="img"/>` : '';
image.style.display = result.image ? 'block': 'none';
debug.value = result.log.join( '\n' ) debug.value = result.log.join( '\n' )
D.removeChildren( answers ); D.removeChildren( answers );
D.appendChild( answers, result.answers.map( ( a ) => view.cmp.Answer( a, type ) ) ); D.appendChild( answers, result.answers.map( ( a ) => view.cmp.Answer( a, type ) ) );
...@@ -34,7 +36,15 @@ view.page.Generate = function() { ...@@ -34,7 +36,15 @@ view.page.Generate = function() {
this.dom = this.dom =
div({cls: 'generate-panel'}, div({cls: 'generate-panel'},
div({cls: 'title-gradient'}, div({cls: 'title-gradient'},
div({cls: 'generate-sub-menu'},
div({cls: 'generate-sub-menu'}, div({cls: 'generate-sub-menu'},
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'}, div({cls: 'generate-sub-menu'},
view.cmp.Menu({title: 'Единственный', id: 'radio', key: 'generateType'}), view.cmp.Menu({title: 'Единственный', id: 'radio', key: 'generateType'}),
view.cmp.Menu({title: 'Множественный', id: 'checkbox', key: 'generateType'}) view.cmp.Menu({title: 'Множественный', id: 'checkbox', key: 'generateType'})
...@@ -45,6 +55,10 @@ view.page.Generate = function() { ...@@ -45,6 +55,10 @@ view.page.Generate = function() {
view.cmp.Menu({title: 'Без фото', id: 'noPhoto', key: 'generatePhoto'}) view.cmp.Menu({title: 'Без фото', id: 'noPhoto', key: 'generatePhoto'})
) )
) )
})
)
), ),
div({cls: 'generate-controls'}, div({cls: 'generate-controls'},
D.input({ D.input({
...@@ -74,6 +88,6 @@ view.page.Generate = function() { ...@@ -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