some-random-actions-modification

This commit is contained in:
mrfry 2020-09-12 12:56:15 +02:00
parent c7fafa0789
commit 6ea0263b02
3 changed files with 100 additions and 98 deletions

@ -1 +1 @@
Subproject commit 7ac65348e41463670fc303fffc788f0baa03d288 Subproject commit ada93f0bb9b87863e04130e2db77ca52e8087058

@ -1 +1 @@
Subproject commit cbaaa7a376a9ac40e95b534c8a1181a017e95ddd Subproject commit f8d4bf2a414d2973582c08bfa8b8b8f19389b2e9

View file

@ -39,113 +39,115 @@ const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
var currWrites = 0 var currWrites = 0
function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
logger.DebugLog('Processing incoming request', 'actions', 1) return new Promise((resolve, reject) => {
if (recievedData === undefined) { logger.DebugLog('Processing incoming request', 'actions', 1)
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) if (recievedData === undefined) {
return logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
} reject(new Error('Recieved data is undefined!'))
try {
let towrite = logger.GetDateString() + '\n'
towrite += '------------------------------------------------------------------------------\n'
if (typeof recievedData === 'object') {
towrite += JSON.stringify(recievedData)
} else {
towrite += recievedData
} }
towrite += '\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, recDataFile)
logger.DebugLog('recDataFile written', 'actions', 1)
} catch (e) {
logger.log('Error writing recieved data.')
}
try { try {
let towrite = logger.GetDateString() + '\n'
towrite += '------------------------------------------------------------------------------\n'
if (typeof recievedData === 'object') {
towrite += JSON.stringify(recievedData)
} else {
towrite += recievedData
}
towrite += '\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, recDataFile)
logger.DebugLog('recDataFile written', 'actions', 1)
} catch (e) {
logger.log('Error writing recieved data.')
}
try {
// recievedData: { version: "", id: "", subj: "" quiz: {} } // recievedData: { version: "", id: "", subj: "" quiz: {} }
let d = recievedData let d = recievedData
// FIXME: if is for backwards compatibility, remove this sometime in the future // FIXME: if is for backwards compatibility, remove this sometime in the future
if (typeof d !== 'object') { if (typeof d !== 'object') {
d = JSON.parse(recievedData) d = JSON.parse(recievedData)
}
logger.DebugLog('recievedData JSON parsed', 'actions', 1)
logger.DebugLog(d, 'actions', 3)
let allQLength = d.quiz.length
let allQuestions = []
d.quiz.forEach((question) => {
logger.DebugLog('Question:', 'actions', 2)
logger.DebugLog(question, 'actions', 2)
let q = new classes.Question(question.Q, question.A, question.data)
logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3)
logger.DebugLog(q, 'actions', 3)
let sames = qdb.Search(q, d.subj)
logger.DebugLog('Same questions:', 'actions', 2)
logger.DebugLog('Length: ' + sames.length, 'actions', 2)
logger.DebugLog(sames, 'actions', 3)
// if it didnt find any question, or every found questions match is lower thatn 80
let isNew = sames.length === 0 || sames.every(searchResItem => {
return searchResItem.match < minMatchAmmountToAdd
})
logger.DebugLog('isNew: ' + isNew, 'actions', 2)
if (isNew) {
allQuestions.push(q)
} }
})
let color = logger.GetColor('green') logger.DebugLog('recievedData JSON parsed', 'actions', 1)
let msg = '' logger.DebugLog(d, 'actions', 3)
if (allQuestions.length > 0) { let allQLength = d.quiz.length
color = logger.GetColor('blue') let allQuestions = []
msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )`
allQuestions.forEach((q) => { d.quiz.forEach((question) => {
const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj) logger.DebugLog('Question:', 'actions', 2)
logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3) logger.DebugLog(question, 'actions', 2)
let q = new classes.Question(question.Q, question.A, question.data)
logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3)
logger.DebugLog(q, 'actions', 3) logger.DebugLog(q, 'actions', 3)
qdb.AddQuestion(sName, q)
let sames = qdb.Search(q, d.subj)
logger.DebugLog('Same questions:', 'actions', 2)
logger.DebugLog('Length: ' + sames.length, 'actions', 2)
logger.DebugLog(sames, 'actions', 3)
// if it didnt find any question, or every found questions match is lower thatn 80
let isNew = sames.length === 0 || sames.every(searchResItem => {
return searchResItem.match < minMatchAmmountToAdd
})
logger.DebugLog('isNew: ' + isNew, 'actions', 2)
if (isNew) {
allQuestions.push(q)
}
}) })
currWrites++ let color = logger.GetColor('green')
logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1) let msg = ''
if (currWrites >= writeAfter && !dryRun) { if (allQuestions.length > 0) {
currWrites = 0 color = logger.GetColor('blue')
try { msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )`
qdb.version = infos.version allQuestions.forEach((q) => {
qdb.motd = infos.motd const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj)
logger.DebugLog('version and motd set for data.json', 'actions', 3) logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3)
} catch (e) { logger.DebugLog(q, 'actions', 3)
logger.Log('MOTD/Version writing/reading error!') qdb.AddQuestion(sName, q)
})
currWrites++
logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
if (currWrites >= writeAfter && !dryRun) {
currWrites = 0
try {
qdb.version = infos.version
qdb.motd = infos.motd
logger.DebugLog('version and motd set for data.json', 'actions', 3)
} catch (e) {
logger.Log('MOTD/Version writing/reading error!')
}
logger.DebugLog('Writing data.json', 'actions', 1)
utils.WriteFile(JSON.stringify(qdb), dataFile)
logger.Log('\tData file written', color)
} else if (dryRun) {
logger.Log('\tDry run')
} }
logger.DebugLog('Writing data.json', 'actions', 1) } else {
utils.WriteFile(JSON.stringify(qdb), dataFile) msg += `No new data ( ${allQLength} )`
logger.Log('\tData file written', color)
} else if (dryRun) {
logger.Log('\tDry run')
} }
} else {
msg += `No new data ( ${allQLength} )` let subjRow = '\t' + d.subj
if (d.id) {
subjRow += ' ( CID: ' + logger.logHashed(d.id) + ')'
idStats.LogId(d.id, d.subj)
}
logger.Log(subjRow)
if (d.version !== undefined) { msg += '. Version: ' + d.version }
logger.Log('\t' + msg, color)
logger.DebugLog('New Questions:', 'actions', 2)
logger.DebugLog(allQuestions, 'actions', 2)
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
resolve(allQLength.length)
} catch (e) {
console.log(e)
logger.Log('Couldnt parse JSON data', logger.GetColor('redbg'))
reject(new Error('Couldnt parse JSON data'))
} }
})
let subjRow = '\t' + d.subj
if (d.id) {
subjRow += ' ( CID: ' + logger.logHashed(d.id) + ')'
idStats.LogId(d.id, d.subj)
}
logger.Log(subjRow)
if (d.version !== undefined) { msg += '. Version: ' + d.version }
logger.Log('\t' + msg, color)
logger.DebugLog('New Questions:', 'actions', 2)
logger.DebugLog(allQuestions, 'actions', 2)
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
return allQuestions.length
} catch (e) {
console.log(e)
logger.Log('Couldnt parse JSON data', logger.GetColor('redbg'))
return -1
}
} }
// loading stuff // loading stuff