Handling old data

This commit is contained in:
MrFry 2020-01-22 17:16:11 +01:00
parent bb23853043
commit 2f6f39c294
34 changed files with 41 additions and 94 deletions

95
utils/merger.js Normal file → Executable file
View file

@ -41,8 +41,7 @@ function Main () {
console.log('JSON data added')
} catch (e) {
console.log(e)
console.log('Trying with old format...')
dbs.push(ReadData(utils.ReadFile(params[i])).result)
return
}
}
PrintLN()
@ -155,98 +154,6 @@ function MergeDatabases (dbs) {
return db
}
/*
* Returns a question database from the given data.
* Parameter should be raw read file in string with "\n"-s
* TODO: ??? -s are not listed as errors, tho works correctly
* */
function ReadData (data) {
const d = data.split('\n')
const r = new classes.QuestionDB((x) => true, (x, y) => console.log(x, y))
var logs = []
var currSubj = '' // the current subjects name
var ExpectedIdentifier = ['+', '?']
let currQuestion = new classes.Question()
var i = -1
while (i < d.length) {
let currIdentifier
let skipped = 0
do {
if (skipped >= 1) { logs.push(i + ': ' + d[i]) }
i++
if (i >= d.length) {
if (currQuestion.IsComplete()) { r.AddQuestion(currSubj, currQuestion) }
return {
result: r,
logs: logs
}
}
currIdentifier = d[i][0]
skipped++
} while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length)
let currData = d[i].substring(1).trim()
if (currIdentifier === '+') {
if (currQuestion.IsComplete()) { r.AddQuestion(currSubj, currQuestion) }
currQuestion = new classes.Question()
currSubj = currData
ExpectedIdentifier = ['?']
continue
}
if (currIdentifier === '?') {
if (currQuestion.IsComplete()) {
r.AddQuestion(currSubj, currQuestion)
currQuestion = new classes.Question()
}
// overwriting is allowed here, bcus:
// ?????!>
currQuestion.Q = currData
ExpectedIdentifier = ['!', '?']
continue
}
if (currIdentifier === '!') {
// if dont have question continue
if (!currQuestion.HasQuestion()) { throw new Error('No question! (A)') }
// dont allow overwriting
// ?!!!!
if (!currQuestion.HasAnswer()) {
currData = currData.replace('A helyes válaszok: ', '')
currData = currData.replace('A helyes válasz: ', '')
currQuestion.A = currData
}
ExpectedIdentifier = ['?', '>', '+']
continue
}
if (currIdentifier === '>') {
// if dont have question or answer continue
if (!currQuestion.HasQuestion()) { throw new Error('No question! (I)') }
if (!currQuestion.HasAnswer()) { throw new Error('No asnwer! (I)') }
// dont allow overwriting
// ?!>>>
if (!currQuestion.HasImage()) {
try {
currQuestion.I = JSON.parse(currData)
} catch (e) {
currQuestion.I = currData.split(',')
}
}
ExpectedIdentifier = ['?', '+']
continue
}
}
return {
result: r,
logs: logs
}
}
function RemoveDuplicates (dataObj) {
for (var i = 0; i < dataObj.length; i++) { RemoveDuplFromSubject(dataObj.Subjects[i]) }
return dataObj