mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
235 lines
7.4 KiB
JavaScript
235 lines
7.4 KiB
JavaScript
/* ----------------------------------------------------------------------------
|
|
Question Server
|
|
GitLab: <https://gitlab.com/MrFry/question-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/>.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
module.exports = {
|
|
ProcessIncomingRequest: ProcessIncomingRequest,
|
|
CheckData: CheckData,
|
|
NLoad: NLoad,
|
|
LoadJSON: LoadJSON,
|
|
ProcessQA: ProcessQA
|
|
}
|
|
|
|
const dataFile = './public/data.json'
|
|
const recDataFile = './stats/recdata'
|
|
const versionFile = './public/version'
|
|
const motdFile = './public/motd'
|
|
const qaFile = './public/qa'
|
|
|
|
var logger = require('../utils/logger.js')
|
|
var utils = require('../utils/utils.js')
|
|
const classes = require('./question-classes/classes.js')
|
|
|
|
function ProcessIncomingRequest (data) {
|
|
if (data === undefined) {
|
|
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
|
return
|
|
}
|
|
|
|
try {
|
|
let towrite = logger.GetDateString() + '\n'
|
|
towrite += '------------------------------------------------------------------------------\n'
|
|
towrite += data
|
|
towrite += '\n------------------------------------------------------------------------------\n'
|
|
utils.AppendToFile(towrite, recDataFile)
|
|
} catch (e) {
|
|
logger.log('Error writing recieved data.')
|
|
}
|
|
|
|
try {
|
|
var d = JSON.parse(data)
|
|
var allQuestions = []
|
|
for (let i = 0; i < d.allData.length; i++) {
|
|
allQuestions.push(new classes.Question(d.allData[i].Q, d.allData[i].A, d.allData[i].I))
|
|
}
|
|
|
|
let color = logger.GetColor('green')
|
|
let questions = []
|
|
let msg = ''
|
|
if (d.data.length > 0) {
|
|
let qdb = LoadJSON(utils.ReadFile(dataFile))
|
|
d.data.forEach((x) => {
|
|
let q = new classes.Question(x.Q, x.A, x.I)
|
|
questions.push(q)
|
|
qdb.AddQuestion(d.subj, q)
|
|
})
|
|
|
|
try {
|
|
data.version = utils.ReadFile(versionFile)
|
|
data.motd = utils.ReadFile(motdFile)
|
|
} catch (e) {
|
|
logger.Log('MOTD/Version writing/reading error!')
|
|
}
|
|
|
|
msg += 'All / new count: ' + allQuestions.length + ' / ' + questions.length
|
|
if (qdb !== undefined && d.data.length > 0) {
|
|
utils.WriteBackup()
|
|
utils.WriteFile(JSON.stringify(qdb), dataFile)
|
|
msg += ' - Data file written!'
|
|
color = logger.GetColor('blue')
|
|
}
|
|
}
|
|
|
|
logger.Log('\t' + d.subj)
|
|
if (d.version !== undefined) { msg += '. Version: ' + d.version }
|
|
|
|
logger.Log('\t' + msg, color)
|
|
} catch (e) {
|
|
logger.Log('Couldnt parse JSON data', logger.GetColor('redbg'))
|
|
}
|
|
}
|
|
|
|
function CheckData (data) {
|
|
try {
|
|
var presult = NLoad(data)
|
|
return presult
|
|
} catch (e) {
|
|
logger.Log('Load error, ' + e.toString(), logger.GetColor('redbg'), true)
|
|
return {
|
|
count: -1,
|
|
log: [e.toString()]
|
|
}
|
|
}
|
|
}
|
|
|
|
function NLoad (resource) {
|
|
var resultLog = []
|
|
var allCount = 0
|
|
if (resource === undefined) { throw new Error('A megadott adat undefined!') }
|
|
resource = resource.split('\n') // splitting by enters
|
|
if (resource.length === 1) { throw new Error('A megadott adat nem sorokból áll!') }
|
|
var data = [] // initializing data declared at the begining
|
|
|
|
function AddNewSubj (name) {
|
|
data.push({
|
|
'questions': []
|
|
}) // ads aa new object, with an empty array in it
|
|
GetCurrSubj().name = name // sets the name for it
|
|
GetCurrSubj().count = 0 // setting count to default
|
|
// setting if its active only if its undefined, otherwise previous user setting shouldt be overwritten
|
|
}
|
|
|
|
function AddItem () { // adds an item to the last subjects questions
|
|
GetCurrSubj().count++
|
|
allCount++ // incrementing all count
|
|
GetCurrSubj().questions.push({}) // adding a new empty object to the last item in the data
|
|
}
|
|
|
|
function GetLastItem () { // returns the last item of the last item of data
|
|
var q = GetCurrSubj().questions.length // questions length
|
|
return GetCurrSubj().questions[q - 1]
|
|
}
|
|
|
|
function GetCurrSubj () {
|
|
return data[data.length - 1]
|
|
}
|
|
|
|
// ? : question
|
|
// ! : answer
|
|
// > : image JSON data
|
|
// + : subject name
|
|
|
|
// checking for name
|
|
for (var j = 0; j < resource.length; j++) { // goes through resources
|
|
if (resource[j][0] === '+') { // if there is a name identifier
|
|
break // breaks, couse there will be a name
|
|
}
|
|
if (resource[j][0] === '?' || resource[j][0] === '!' || resource[j][0] === '>') { // if it begins with another identifier:
|
|
AddNewSubj('NONAME') // there is no name (for the first question at least), so setting it noname.
|
|
break
|
|
}
|
|
// else it does nothing, continues to check
|
|
}
|
|
var jumped = 0 // the amount of lines it processed
|
|
for (var i = 0; i < resource.length; i += jumped) { // gouing through the resource
|
|
jumped = 0 // resetting it to 0
|
|
var currRawDataQ = resource[i] // current question
|
|
var currRawDataA = resource[i + 1] // current answer
|
|
var currRawDataI = resource[i + 2] // current image
|
|
if (currRawDataQ !== undefined && currRawDataQ[0] === '?' && currRawDataA !== undefined && currRawDataA[0] === '!') { // if the current line is ? and the next is ! its a data
|
|
AddItem()
|
|
GetLastItem().q = currRawDataQ.substr(1)
|
|
GetLastItem().a = currRawDataA.substr(1)
|
|
jumped += 2
|
|
if (currRawDataI !== undefined && currRawDataI[0] === '>') {
|
|
GetLastItem().i = currRawDataI.substr(1)
|
|
jumped++
|
|
}
|
|
} else if (currRawDataQ[0] === '+') { // if its a new subject
|
|
AddNewSubj(currRawDataQ.substr(1))
|
|
jumped++
|
|
} else {
|
|
// this should be invalid question order
|
|
resultLog.push('Warning @ line ' + i + ':' + currRawDataQ + ' ' + currRawDataA + ' ' + currRawDataI)
|
|
jumped++
|
|
}
|
|
} // end of parsing all data
|
|
return {
|
|
count: allCount,
|
|
log: resultLog
|
|
}
|
|
}
|
|
|
|
// loading stuff
|
|
function LoadJSON (resource) {
|
|
try {
|
|
var d = JSON.parse(resource)
|
|
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
|
|
} catch (e) {
|
|
logger.Log('Error loading sutff', logger.GetColor('redbg'), true)
|
|
}
|
|
}
|
|
|
|
function ProcessQA () {
|
|
if (!utils.FileExists(qaFile)) { utils.WriteFile('', qaFile) }
|
|
|
|
let a = utils.ReadFile(qaFile).split('\n')
|
|
let r = []
|
|
let ind = 0
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (a[i] === '#') { ind++ } else {
|
|
if (r[ind] === undefined) { r[ind] = {} }
|
|
|
|
if (r[ind].q === undefined) {
|
|
r[ind].q = a[i]
|
|
} else {
|
|
if (r[ind].a === undefined) { r[ind].a = [] }
|
|
|
|
r[ind].a.push(a[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|