mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
233 lines
5.9 KiB
JavaScript
233 lines
5.9 KiB
JavaScript
/* ----------------------------------------------------------------------------
|
|
|
|
Question Server question file merger
|
|
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
const classes = require('./question-classes/classes.js')
|
|
const utils = require('./utils.js')
|
|
|
|
Main()
|
|
|
|
function Main () {
|
|
console.clear()
|
|
const params = GetParams()
|
|
console.log(params)
|
|
var db = []
|
|
|
|
PrintLN()
|
|
console.log(params[0] + ': ')
|
|
try {
|
|
db = ParseJSONData(utils.ReadFile(params[0]))
|
|
console.log('JSON data added')
|
|
} catch (e) {
|
|
console.log(e)
|
|
console.log('Trying with old format...')
|
|
db = ReadData(utils.ReadFile(params[0])).result
|
|
}
|
|
PrintLN()
|
|
|
|
PrintDB(db)
|
|
|
|
PrintLN()
|
|
|
|
RefactorDb(db)
|
|
|
|
utils.WriteFile(JSON.stringify(db), 'newData')
|
|
}
|
|
|
|
function RefactorDb (db) {
|
|
db.Subjects.forEach((subj) => {
|
|
subj.Questions.forEach((question) => {
|
|
if (question.I) {
|
|
question.data = {
|
|
type: 'image',
|
|
images: question.I
|
|
}
|
|
delete question.I
|
|
} else {
|
|
question.data = {
|
|
type: 'simple'
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------
|
|
|
|
function PrintLN () {
|
|
console.log('------------------------------------------------------')
|
|
}
|
|
|
|
function PrintDB (r, olds) {
|
|
console.log('Data subject count: ' + r.length)
|
|
var maxLength = 0
|
|
for (let i = 0; i < r.length; i++) {
|
|
if (maxLength < r.Subjects[i].Name.length) { maxLength = r.Subjects[i].Name.length }
|
|
}
|
|
|
|
let qcount = 0
|
|
|
|
for (let i = 0; i < r.length; i++) {
|
|
let line = i
|
|
if (line < 10) { line += ' ' }
|
|
|
|
line += ': '
|
|
var currLength = line.length + maxLength + 4
|
|
line += r.Subjects[i].Name
|
|
|
|
while (line.length < currLength) {
|
|
if (i % 4 === 0) { line += '.' } else { line += ' ' }
|
|
}
|
|
|
|
if (olds && olds.length > 0) {
|
|
// TODO: check if correct row! should be now, but well...
|
|
if (olds[i] < 10) { line += ' ' }
|
|
if (olds[i] < 100) { line += ' ' }
|
|
|
|
line += olds[i]
|
|
line += ' -> '
|
|
}
|
|
|
|
if (r.Subjects[i].length < 10) { line += ' ' }
|
|
if (r.Subjects[i].length < 100) { line += ' ' }
|
|
|
|
line += r.Subjects[i].length
|
|
qcount += r.Subjects[i].length
|
|
|
|
line += ' db'
|
|
|
|
console.log(line)
|
|
}
|
|
|
|
console.log('Total questions: ' + qcount)
|
|
|
|
PrintLN()
|
|
}
|
|
|
|
function GetParams () {
|
|
return process.argv.splice(2)
|
|
}
|
|
|
|
function ParseJSONData (data) {
|
|
var d = JSON.parse(data)
|
|
var r = new classes.QuestionDB((x) => true, (x, y) => console.log(x, y))
|
|
var rt = []
|
|
|
|
for (var i = 0; i < d.Subjects.length; i++) {
|
|
let s = new classes.Subject(d.Subjects[i].Name)
|
|
var j = 0
|
|
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
|
|
var currQ = d.Subjects[i].Questions[j]
|
|
s.AddQuestion(new classes.Question(currQ.Q, currQ.A, currQ.I))
|
|
}
|
|
rt.push({
|
|
name: d.Subjects[i].Name,
|
|
count: j
|
|
})
|
|
r.AddSubject(s)
|
|
}
|
|
return r
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|