mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Actions.js refactoring
This commit is contained in:
parent
0d8c1c154a
commit
8a92e5651f
1 changed files with 418 additions and 455 deletions
869
utils/actions.js
869
utils/actions.js
|
@ -18,516 +18,479 @@ Question Server
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ProcessIncomingRequest: ProcessIncomingRequest,
|
ProcessIncomingRequest: ProcessIncomingRequest,
|
||||||
CheckData: CheckData,
|
CheckData: CheckData,
|
||||||
NLoad: NLoad,
|
NLoad: NLoad,
|
||||||
LoadJSON: LoadJSON,
|
LoadJSON: LoadJSON,
|
||||||
ProcessQA: ProcessQA
|
ProcessQA: ProcessQA
|
||||||
};
|
}
|
||||||
|
|
||||||
var staticFile = "public/data/static";
|
const staticFile = '../public/data/static'
|
||||||
var manFile = "public/man.html";
|
const dataFile = '../public/data.json'
|
||||||
var dataFile = "public/data.json";
|
const recDataFile = '../stats/recdata'
|
||||||
const recDataFile = "stats/recdata";
|
const versionFile = '../public/version'
|
||||||
const versionFile = "public/version";
|
const motdFile = '../public/motd'
|
||||||
const motdFile = "public/motd";
|
const qaFile = '../public/qa'
|
||||||
const qaFile = "public/qa";
|
|
||||||
|
|
||||||
var logger = require('./logger.js');
|
var logger = require('../utils/logger.js')
|
||||||
var utils = require('./utils.js');
|
var utils = require('../utils/utils.js')
|
||||||
|
|
||||||
class Question {
|
class Question {
|
||||||
constructor(q, a, i) {
|
constructor (q, a, i) {
|
||||||
this.Q = q;
|
this.Q = q
|
||||||
this.A = a;
|
this.A = a
|
||||||
this.I = i;
|
this.I = i
|
||||||
}
|
}
|
||||||
toString() {
|
toString () {
|
||||||
var r = "?" + this.Q + "\n!" + this.A;
|
var r = '?' + this.Q + '\n!' + this.A
|
||||||
if (this.I)
|
if (this.I) { r += '\n>' + this.I }
|
||||||
r += "\n>" + this.I;
|
return r
|
||||||
return r;
|
}
|
||||||
}
|
HasQuestion () {
|
||||||
HasQuestion() {
|
return this.Q !== undefined
|
||||||
return this.Q != undefined;
|
}
|
||||||
}
|
HasAnswer () {
|
||||||
HasAnswer() {
|
return this.A !== undefined
|
||||||
return this.A != undefined;
|
}
|
||||||
}
|
HasImage () {
|
||||||
HasImage() {
|
return this.I !== undefined
|
||||||
return this.I != undefined;
|
}
|
||||||
}
|
IsComplete () {
|
||||||
IsComplete() {
|
return this.HasQuestion() && this.HasAnswer()
|
||||||
return this.HasQuestion() && this.HasAnswer();
|
}
|
||||||
}
|
Compare (q2, i) {
|
||||||
Compare(q2, i) {
|
if (typeof q2 === 'string') {
|
||||||
if (typeof q2 == 'string') {
|
var qmatchpercent = Question.CompareString(this.Q, q2)
|
||||||
var qmatchpercent = Question.CompareString(this.Q, q2);
|
|
||||||
|
|
||||||
if (i == undefined || i.length == 0)
|
if (i == undefined || i.length == 0) { return qmatchpercent } else {
|
||||||
return qmatchpercent;
|
if (this.HasImage()) {
|
||||||
else {
|
const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(' '), i.join(' '))
|
||||||
if (this.HasImage()) {
|
: 0
|
||||||
const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(" "), i.join(" ")) :
|
return (qmatchpercent + imatchpercent) / 2
|
||||||
0;
|
} else {
|
||||||
return (qmatchpercent + imatchpercent) / 2;
|
qmatchpercent -= 30
|
||||||
} else {
|
if (qmatchpercent < 0) { return 0 } else { return qmatchpercent }
|
||||||
qmatchpercent -= 30;
|
}
|
||||||
if (qmatchpercent < 0)
|
}
|
||||||
return 0;
|
} else {
|
||||||
else
|
const qmatchpercent = Question.CompareString(this.Q, q2.Q)
|
||||||
return qmatchpercent;
|
const amatchpercent = Question.CompareString(this.A, q2.A)
|
||||||
}
|
if (this.I !== undefined) {
|
||||||
}
|
const imatchpercent = this.I === undefined ? Question.CompareString(this.I.join(' '), q2.I.join(
|
||||||
} else {
|
' ')) : 0
|
||||||
const qmatchpercent = Question.CompareString(this.Q, q2.Q);
|
return (qmatchpercent + amatchpercent + imatchpercent) / 3
|
||||||
const amatchpercent = Question.CompareString(this.A, q2.A);
|
} else {
|
||||||
if (this.I != undefined) {
|
return (qmatchpercent + amatchpercent) / 2
|
||||||
const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(" "), q2.I.join(
|
}
|
||||||
" ")) : 0;
|
}
|
||||||
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
|
}
|
||||||
} else {
|
|
||||||
return (qmatchpercent + amatchpercent) / 2;
|
// static CompareString (s1, s2) {
|
||||||
}
|
// // if (s1 == undefined || s2 == undefined)
|
||||||
}
|
// // return 0;
|
||||||
}
|
// s1 = SimplifyStringForComparison(s1).split(' ')
|
||||||
static CompareString(s1, s2) {
|
// s2 = SimplifyStringForComparison(s2).split(' ')
|
||||||
//if (s1 == undefined || s2 == undefined)
|
// var match = 0
|
||||||
// return 0;
|
// for (var i = 0; i < s1.length; i++) {
|
||||||
s1 = SimplifyStringForComparison(s1).split(" ");
|
// if (s2.includes(s1[i])) { match++ }
|
||||||
s2 = SimplifyStringForComparison(s2).split(" ");
|
// }
|
||||||
var match = 0;
|
// var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||||
for (var i = 0; i < s1.length; i++)
|
// var lengthDifference = Math.abs(s2.length - s1.length)
|
||||||
if (s2.includes(s1[i]))
|
// percent -= lengthDifference * 3
|
||||||
match++;
|
// if (percent < 0) { percent = 0 }
|
||||||
var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent
|
// return percent
|
||||||
var lengthDifference = Math.abs(s2.length - s1.length);
|
// }
|
||||||
percent -= lengthDifference * 3;
|
|
||||||
if (percent < 0)
|
|
||||||
percent = 0;
|
|
||||||
return percent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Subject {
|
class Subject {
|
||||||
constructor(n) {
|
constructor (n) {
|
||||||
this.Name = n;
|
this.Name = n
|
||||||
this.Questions = [];
|
this.Questions = []
|
||||||
}
|
}
|
||||||
get length() {
|
get length () {
|
||||||
return this.Questions.length;
|
return this.Questions.length
|
||||||
}
|
}
|
||||||
AddQuestion(q) {
|
AddQuestion (q) {
|
||||||
this.Questions.push(q);
|
this.Questions.push(q)
|
||||||
}
|
}
|
||||||
Search(q, img) {
|
Search (q, img) {
|
||||||
var r = [];
|
const minMatchAmmount = 90
|
||||||
for (var i = 0; i < this.length; i++) {
|
var r = []
|
||||||
let percent = this.Questions[i].Compare(q, img);
|
for (let i = 0; i < this.length; i++) {
|
||||||
if (percent > minMatchAmmount)
|
let percent = this.Questions[i].Compare(q, img)
|
||||||
r.push({
|
if (percent > minMatchAmmount) {
|
||||||
q: this.Questions[i],
|
r.push({
|
||||||
match: percent
|
q: this.Questions[i],
|
||||||
});
|
match: percent
|
||||||
}
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < r.length; i++)
|
for (let i = 0; i < r.length; i++) {
|
||||||
for (var j = i; j < r.length; j++)
|
for (var j = i; j < r.length; j++) {
|
||||||
if (r[i].match < r[j].match) {
|
if (r[i].match < r[j].match) {
|
||||||
var tmp = r[i];
|
var tmp = r[i]
|
||||||
r[i] = r[j];
|
r[i] = r[j]
|
||||||
r[j] = tmp;
|
r[j] = tmp
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return r;
|
return r
|
||||||
}
|
}
|
||||||
toString() {
|
toString () {
|
||||||
var r = [];
|
var r = []
|
||||||
for (var i = 0; i < this.Questions.length; i++)
|
for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) }
|
||||||
r.push(this.Questions[i].toString());
|
return '+' + this.Name + '\n' + r.join('\n')
|
||||||
return "+" + this.Name + "\n" + r.join("\n");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class QuestionDB {
|
class QuestionDB {
|
||||||
constructor() {
|
constructor () {
|
||||||
this.Subjects = [];
|
this.Subjects = []
|
||||||
}
|
}
|
||||||
get length() {
|
get length () {
|
||||||
return this.Subjects.length;
|
return this.Subjects.length
|
||||||
}
|
}
|
||||||
get activeIndexes() {
|
// get activeIndexes () {
|
||||||
var r = [];
|
// var r = []
|
||||||
for (var i = 0; i < this.length; i++) {
|
// for (var i = 0; i < this.length; i++) {
|
||||||
if (GM_getValue("Is" + i + "Active")) {
|
// if (GM_getValue('Is' + i + 'Active')) {
|
||||||
r.push(i);
|
// r.push(i)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return r;
|
// return r
|
||||||
}
|
// }
|
||||||
GetIfActive(ind) {
|
// GetIfActive (ind) {
|
||||||
return GM_getValue("Is" + ind + "Active");
|
// return GM_getValue('Is' + ind + 'Active')
|
||||||
}
|
// }
|
||||||
ChangeActive(i, value) {
|
// ChangeActive (i, value) {
|
||||||
GM_setValue("Is" + i + "Active", !!value);
|
// GM_setValue('Is' + i + 'Active', !!value)
|
||||||
}
|
// }
|
||||||
AddQuestion(subj, q) {
|
AddQuestion (subj, q) {
|
||||||
var i = 0;
|
var i = 0
|
||||||
while (i < this.Subjects.length && this.Subjects[i].Name != subj)
|
while (i < this.Subjects.length && this.Subjects[i].Name !== subj) { i++ }
|
||||||
i++;
|
if (i < this.Subjects.length) { this.Subjects[i].AddQuestion(q) } else {
|
||||||
if (i < this.Subjects.length)
|
const n = new Subject(subj)
|
||||||
this.Subjects[i].AddQuestion(q);
|
n.AddQuestion(q)
|
||||||
else {
|
this.Subjects.push(n)
|
||||||
const n = new Subject(subj);
|
}
|
||||||
n.AddQuestion(q);
|
}
|
||||||
this.Subjects.push(n);
|
Search (q, img) {
|
||||||
}
|
var r = []
|
||||||
}
|
for (let i = 0; i < this.length; i++) {
|
||||||
Search(q, img) {
|
if (this.GetIfActive(i)) { r = r.concat(this.Subjects[i].Search(q, img)) }
|
||||||
var r = [];
|
}
|
||||||
for (var i = 0; i < this.length; i++)
|
|
||||||
if (this.GetIfActive(i))
|
|
||||||
r = r.concat(this.Subjects[i].Search(q, img));
|
|
||||||
|
|
||||||
for (var i = 0; i < r.length; i++)
|
for (let i = 0; i < r.length; i++) {
|
||||||
for (var j = i; j < r.length; j++)
|
for (var j = i; j < r.length; j++) {
|
||||||
if (r[i].match < r[j].match) {
|
if (r[i].match < r[j].match) {
|
||||||
var tmp = r[i];
|
var tmp = r[i]
|
||||||
r[i] = r[j];
|
r[i] = r[j]
|
||||||
r[j] = tmp;
|
r[j] = tmp
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return r;
|
return r
|
||||||
}
|
}
|
||||||
AddSubject(subj) {
|
AddSubject (subj) {
|
||||||
var i = 0;
|
var i = 0
|
||||||
while (i < this.length && subj.Name != this.Subjects[i].Name)
|
while (i < this.length && subj.Name !== this.Subjects[i].Name) { i++ }
|
||||||
i++;
|
|
||||||
|
|
||||||
if (i < this.length) {
|
if (i < this.length) {
|
||||||
this.Subjects.concat(subj.Questions);
|
this.Subjects.concat(subj.Questions)
|
||||||
} else {
|
} else {
|
||||||
this.Subjects.push(subj);
|
this.Subjects.push(subj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
toString() {
|
toString () {
|
||||||
var r = [];
|
var r = []
|
||||||
for (var i = 0; i < this.Subjects.length; i++)
|
for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) }
|
||||||
r.push(this.Subjects[i].toString());
|
return r.join('\n\n')
|
||||||
return r.join("\n\n");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Process (d, file) {
|
||||||
|
try {
|
||||||
|
logger.Log('File: ' + file)
|
||||||
|
if (d.data.split('\n').length > 1) {
|
||||||
|
var oldFile = utils.ReadFile(file)
|
||||||
|
var newFile = oldFile + '\n'
|
||||||
|
if (d.data[0] === '+') { newFile += d.data } else { newFile += '+' + d.data }
|
||||||
|
|
||||||
function Process(d, file) {
|
var newRes = CheckData(newFile)
|
||||||
try {
|
var oldRes = CheckData(oldFile)
|
||||||
logger.Log("File: " + file);
|
|
||||||
if (d.data.split("\n").length > 1) {
|
|
||||||
var oldFile = utils.ReadFile(file);
|
|
||||||
var newFile = oldFile + "\n";
|
|
||||||
if (d.data[0] == '+')
|
|
||||||
newFile += d.data;
|
|
||||||
else
|
|
||||||
newFile += "+" + d.data;
|
|
||||||
|
|
||||||
var newRes = CheckData(newFile);
|
if (oldRes.count > 0) { logger.Log('\t\told public result: ' + oldRes.count, logger.GetColor('blue')) } else { logger.Log('\t\told public NLOD error, ' + oldRes.log, logger.GetColor('redbg'), true) }
|
||||||
var oldRes = CheckData(oldFile);
|
|
||||||
|
|
||||||
if (oldRes.count > 0)
|
if (newRes.count > 0) { logger.Log('\t\tnew file result: ' + newRes.count, logger.GetColor('blue')) } else { logger.Log('\t\tnew file NLOD error, ' + newRes.log, logger.GetColor('redbg'), true) }
|
||||||
logger.Log("\t\told public result: " + oldRes.count, logger.GetColor("blue"));
|
|
||||||
else
|
|
||||||
logger.Log("\t\told public NLOD error, " + oldRes.log, logger.GetColor("redbg"), true);
|
|
||||||
|
|
||||||
if (newRes.count > 0)
|
utils.WriteFile(newFile, file)
|
||||||
logger.Log("\t\tnew file result: " + newRes.count, logger.GetColor("blue"));
|
logger.Log('\t\tNew data written to: ' + file)
|
||||||
else
|
|
||||||
logger.Log("\t\tnew file NLOD error, " + newRes.log, logger.GetColor("redbg"), true);
|
|
||||||
|
|
||||||
utils.WriteFile(newFile, file);
|
return newRes.count - oldRes.count
|
||||||
logger.Log("\t\tNew data written to: " + file);
|
} else { logger.Log('\t\tNo new data') }
|
||||||
|
} catch (e) {
|
||||||
return newRes.count - oldRes.count;
|
logger.Log('\tError at processing data! File: ' + file, logger.GetColor('redbg'))
|
||||||
} else
|
logger.Log(e.toString(), logger.GetColor('redbg'))
|
||||||
logger.Log("\t\tNo new data");
|
}
|
||||||
|
return -1
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
Beep();
|
|
||||||
logger.Log("\tError at processing data! File: " + file, logger.GetColor("redbg"));
|
|
||||||
logger.Log(e.toString(), logger.GetColor("redbg"));
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ProcessIncomingRequest(data) {
|
function ProcessIncomingRequest (data) {
|
||||||
|
if (data === undefined) {
|
||||||
|
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (data == undefined) {
|
try {
|
||||||
logger.Log("\tRecieved data is undefined!", logger.GetColor("redbg"));
|
let towrite = logger.GetDateString() + '\n'
|
||||||
return;
|
towrite += '------------------------------------------------------------------------------\n'
|
||||||
}
|
towrite += data
|
||||||
|
towrite += '\n------------------------------------------------------------------------------\n'
|
||||||
|
utils.AppendToFile(towrite, recDataFile)
|
||||||
|
} catch (e) {
|
||||||
|
logger.log('Error writing recieved data.')
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let towrite = logger.GetDateString() + "\n";
|
var d = JSON.parse(data)
|
||||||
towrite += "------------------------------------------------------------------------------\n";
|
var dfile = utils.ReadFile(dataFile)
|
||||||
towrite += data
|
data = LoadJSON(dfile)
|
||||||
towrite += "\n------------------------------------------------------------------------------\n";
|
var allQuestions = []
|
||||||
utils.AppendToFile(towrite, recDataFile);
|
for (let i = 0; i < d.allData.length; i++) {
|
||||||
} catch (e) {
|
allQuestions.push(new Question(d.allData[i].Q, d.allData[i].A, d.allData[i].I))
|
||||||
logger.log("Error writing recieved data.");
|
}
|
||||||
}
|
var questions = []
|
||||||
|
for (let i = 0; i < d.data.length; i++) {
|
||||||
|
let q = new Question(d.data[i].Q, d.data[i].A, d.data[i].I)
|
||||||
|
questions.push(q)
|
||||||
|
data.AddQuestion(d.subj, q)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
logger.Log('\t' + d.subj)
|
||||||
var d = JSON.parse(data);
|
var msg = 'All / new count: ' + allQuestions.length + ' / ' + questions.length
|
||||||
var dfile = utils.ReadFile(dataFile);
|
if (d.version !== undefined) { msg += '. Version: ' + d.version }
|
||||||
var data = LoadJSON(dfile);
|
var color = logger.GetColor('green')
|
||||||
var allQuestions = [];
|
|
||||||
for ( var i = 0; i < d.allData.length; i++){
|
|
||||||
allQuestions.push(new Question(d.allData[i].Q, d.allData[i].A, d.allData[i].I));
|
|
||||||
}
|
|
||||||
var questions = [];
|
|
||||||
for ( var i = 0; i < d.data.length; i++){
|
|
||||||
let q = new Question(d.data[i].Q, d.data[i].A, d.data[i].I);
|
|
||||||
questions.push(q);
|
|
||||||
data.AddQuestion(d.subj, q);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Log("\t" + d.subj);
|
try {
|
||||||
var msg = "All / new count: " + allQuestions.length + " / " + questions.length;
|
data.version = utils.ReadFile(versionFile)
|
||||||
if (d.version != undefined)
|
data.motd = utils.ReadFile(motdFile)
|
||||||
msg += ". Version: " + d.version;
|
} catch (e) {
|
||||||
var color = logger.GetColor("green");
|
logger.Log('MOTD/Version writing/reading error!')
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if (data !== undefined && d.data.length > 0) {
|
||||||
data.version = utils.ReadFile(versionFile);
|
utils.WriteBackup()
|
||||||
data.motd = utils.ReadFile(motdFile);
|
utils.WriteFile(JSON.stringify(data), dataFile)
|
||||||
} catch (e) {
|
msg += ' - Data file written!'
|
||||||
Log("MOTD/Version writing/reading error!");
|
color = logger.GetColor('blue')
|
||||||
}
|
}
|
||||||
|
logger.Log('\t' + msg, color)
|
||||||
|
} catch (e) {
|
||||||
|
logger.Log('Couldnt parse JSON data, trying old format...', logger.GetColor('redbg'))
|
||||||
|
d = SetupData(data)
|
||||||
|
var qcount = -1
|
||||||
|
try {
|
||||||
|
var splitted = d.alldata.split('\n')
|
||||||
|
var count = 0
|
||||||
|
for (var i = 0; i < splitted.length; i++) {
|
||||||
|
if (splitted[i][0] === '?') { count++ }
|
||||||
|
}
|
||||||
|
qcount = count
|
||||||
|
} catch (e) { console.log('Error :c'); console.log(e) }
|
||||||
|
|
||||||
if (data != undefined && d.data.length > 0){
|
logger.Log('\tProcessing data: ' + d.subj + ' (' + d.type + '), count: ' + qcount, logger.GetColor('green'))
|
||||||
utils.WriteBackup();
|
if (d.subj === undefined) {
|
||||||
utils.WriteFile(JSON.stringify(data), dataFile);
|
logger.Log(JSON.stringify(d), logger.GetColor('red'))
|
||||||
msg += " - Data file written!";
|
return
|
||||||
var color = logger.GetColor("blue");
|
}
|
||||||
}
|
|
||||||
logger.Log("\t" + msg, color);
|
|
||||||
|
|
||||||
} catch (e) {
|
var newStatItems = Process(d, staticFile)
|
||||||
logger.Log("Couldnt parse JSON data, trying old format...", logger.GetColor("redbg"));
|
|
||||||
var d = SetupData(data);
|
|
||||||
var qcount = -1;
|
|
||||||
try {
|
|
||||||
var splitted = d.alldata.split("\n");
|
|
||||||
var count = 0;
|
|
||||||
for (var i = 0; i < splitted.length; i++)
|
|
||||||
if (splitted[i][0] == '?')
|
|
||||||
count ++;
|
|
||||||
qcount = count;
|
|
||||||
} catch (e) {console.log("Error :c"); console.log(e);}
|
|
||||||
|
|
||||||
logger.Log("\tProcessing data: " + d.subj + " (" + d.type + "), count: " + qcount, logger.GetColor("green"));
|
PrintNewCount(d, newStatItems, staticFile)
|
||||||
if (d.subj == undefined){
|
}
|
||||||
logger.Log(JSON.stringify(d), logger.GetColor("red"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var newItems = 0;
|
|
||||||
|
|
||||||
var newStatItems = Process(d, staticFile);
|
|
||||||
|
|
||||||
PrintNewCount(d, newStatItems, staticFile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function PrintNewCount(d, newItems, file) {
|
function PrintNewCount (d, newItems, file) {
|
||||||
if (newItems > 0) {
|
if (newItems > 0) {
|
||||||
var count = 0;
|
var count = 0
|
||||||
var splitted = d.alldata.split("\n");
|
var splitted = d.alldata.split('\n')
|
||||||
for (var i = 0; i < splitted.length; i++)
|
for (var i = 0; i < splitted.length; i++) {
|
||||||
if (splitted[i].startsWith("?"))
|
if (splitted[i].startsWith('?')) { count++ }
|
||||||
count++;
|
}
|
||||||
logger.Log("\t" + file + " All / New: " + count + " / " + newItems, logger.GetColor("cyan"));
|
logger.Log('\t' + file + ' All / New: ' + count + ' / ' + newItems, logger.GetColor('cyan'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function SetupData(data) {
|
function SetupData (data) {
|
||||||
var pdata = data.split("<#>");
|
var pdata = data.split('<#>')
|
||||||
if (pdata.length <= 0){
|
if (pdata.length <= 0) {
|
||||||
logger.Log("Data length is zero !", logger.GetColor("redbg"));
|
logger.Log('Data length is zero !', logger.GetColor('redbg'))
|
||||||
throw "No data recieved!";
|
throw 'No data recieved!'
|
||||||
}
|
}
|
||||||
|
|
||||||
var d = {}; // parsed data
|
var d = {} // parsed data
|
||||||
for (var i = 0; i < pdata.length; i++) {
|
for (var i = 0; i < pdata.length; i++) {
|
||||||
var td = pdata[i].split("<=>");
|
var td = pdata[i].split('<=>')
|
||||||
if (td.length == 2)
|
if (td.length === 2) { d[td[0]] = td[1] } else {
|
||||||
d[td[0]] = td[1];
|
logger.Log('Invalid parameter!', logger.GetColor('redbg'))
|
||||||
else {
|
throw 'Invalid parameter recieved!'
|
||||||
logger.Log("Invalid parameter!", logger.GetColor("redbg"));
|
}
|
||||||
throw "Invalid parameter recieved!";
|
}
|
||||||
}
|
return d
|
||||||
}
|
|
||||||
return d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function CheckData(data) {
|
function CheckData (data) {
|
||||||
try {
|
try {
|
||||||
var presult = NLoad(data);
|
var presult = NLoad(data)
|
||||||
return presult;
|
return presult
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log("Load error, " + e.toString(), logger.GetColor("redbg"), true);
|
logger.Log('Load error, ' + e.toString(), logger.GetColor('redbg'), true)
|
||||||
return {
|
return {
|
||||||
count: -1,
|
count: -1,
|
||||||
log: [e.toString()]
|
log: [e.toString()]
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function NLoad(resource) {
|
function NLoad (resource) {
|
||||||
var resultLog = [];
|
var resultLog = []
|
||||||
var count = -1;
|
var allCount = 0
|
||||||
var allCount = 0;
|
if (resource === undefined) { throw 'A megadott adat undefined!' }
|
||||||
if (resource == undefined)
|
resource = resource.split('\n') // splitting by enters
|
||||||
throw "A megadott adat undefined!";
|
if (resource.length === 1) { throw 'A megadott adat nem sorokból áll!' }
|
||||||
resource = resource.split("\n"); // splitting by enters
|
var data = [] // initializing data declared at the begining
|
||||||
if (resource.length == 1)
|
|
||||||
throw "A megadott adat nem sorokból áll!";
|
|
||||||
var data = []; // initializing data declared at the begining
|
|
||||||
|
|
||||||
function AddNewSubj(name) // adds a new subject to the data
|
function AddNewSubj (name) {
|
||||||
{
|
data.push({
|
||||||
data.push({
|
'questions': []
|
||||||
"questions": []
|
}) // ads aa new object, with an empty array in it
|
||||||
}); // ads aa new object, with an empty array in it
|
GetCurrSubj().name = name // sets the name for it
|
||||||
GetCurrSubj().name = name; // sets the name for it
|
GetCurrSubj().count = 0 // setting count to default
|
||||||
GetCurrSubj().count = 0; // setting count to default
|
// setting if its active only if its undefined, otherwise previous user setting shouldt be overwritten
|
||||||
// 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
|
function AddItem () // adds an item to the last subjects questions
|
||||||
{
|
{
|
||||||
GetCurrSubj().count++;
|
GetCurrSubj().count++
|
||||||
allCount++; // incrementing all count
|
allCount++ // incrementing all count
|
||||||
GetCurrSubj().questions.push({}); // adding a new empty object to the last item in the data
|
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
|
function GetLastItem () // returns the last item of the last item of data
|
||||||
{
|
{
|
||||||
var q = GetCurrSubj().questions.length; // questions length
|
var q = GetCurrSubj().questions.length // questions length
|
||||||
return GetCurrSubj().questions[q - 1];
|
return GetCurrSubj().questions[q - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
function GetCurrSubj() {
|
function GetCurrSubj () {
|
||||||
return data[data.length - 1];
|
return data[data.length - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// ? : question
|
// ? : question
|
||||||
// ! : answer
|
// ! : answer
|
||||||
// > : image JSON data
|
// > : image JSON data
|
||||||
// + : subject name
|
// + : subject name
|
||||||
|
|
||||||
// checking for name
|
// checking for name
|
||||||
for (var j = 0; j < resource.length; j++) // goes through resources
|
for (var j = 0; j < resource.length; j++) // goes through resources
|
||||||
{
|
{
|
||||||
if (resource[j][0] == '+') // if there is a name identifier
|
if (resource[j][0] == '+') // if there is a name identifier
|
||||||
{
|
{
|
||||||
break; // breaks, couse there will be a name
|
break // breaks, couse there will be a name
|
||||||
}
|
}
|
||||||
if (resource[j][0] == '?' || resource[j][0] == '!' || resource[j][0] == '>') // if it begins with another identifier:
|
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.
|
AddNewSubj('NONAME') // there is no name (for the first question at least), so setting it noname.
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
// else it does nothing, continues to check
|
// else it does nothing, continues to check
|
||||||
}
|
}
|
||||||
var jumped = 0; // the amount of lines it processed
|
var jumped = 0 // the amount of lines it processed
|
||||||
for (var i = 0; i < resource.length; i += jumped) // gouing through the resource
|
for (var i = 0; i < resource.length; i += jumped) // gouing through the resource
|
||||||
{
|
{
|
||||||
jumped = 0; // resetting it to 0
|
jumped = 0 // resetting it to 0
|
||||||
var currRawDataQ = resource[i]; // current question
|
var currRawDataQ = resource[i] // current question
|
||||||
var currRawDataA = resource[i + 1]; // current answer
|
var currRawDataA = resource[i + 1] // current answer
|
||||||
var currRawDataI = resource[i + 2]; // current image
|
var currRawDataI = resource[i + 2] // current image
|
||||||
if (currRawDataQ != undefined && currRawDataQ[0] == '?' && currRawDataA != undefined &&
|
if (currRawDataQ != undefined && currRawDataQ[0] == '?' && currRawDataA != undefined &&
|
||||||
currRawDataA[0] == '!') // if the current line is ? and the next is ! its a data
|
currRawDataA[0] == '!') // if the current line is ? and the next is ! its a data
|
||||||
{
|
{
|
||||||
AddItem();
|
AddItem()
|
||||||
GetLastItem().q = currRawDataQ.substr(1);
|
GetLastItem().q = currRawDataQ.substr(1)
|
||||||
GetLastItem().a = currRawDataA.substr(1);
|
GetLastItem().a = currRawDataA.substr(1)
|
||||||
jumped += 2;
|
jumped += 2
|
||||||
if (currRawDataI != undefined && currRawDataI[0] == '>') {
|
if (currRawDataI != undefined && currRawDataI[0] == '>') {
|
||||||
GetLastItem().i = currRawDataI.substr(1);
|
GetLastItem().i = currRawDataI.substr(1)
|
||||||
jumped++;
|
jumped++
|
||||||
}
|
}
|
||||||
} else if (currRawDataQ[0] == '+') // if its a new subject
|
} else if (currRawDataQ[0] == '+') // if its a new subject
|
||||||
{
|
{
|
||||||
AddNewSubj(currRawDataQ.substr(1));
|
AddNewSubj(currRawDataQ.substr(1))
|
||||||
jumped++;
|
jumped++
|
||||||
} else {
|
} else {
|
||||||
// this should be invalid question order
|
// this should be invalid question order
|
||||||
resultLog.push("Warning @ line " + i + ":" + currRawDataQ + " " + currRawDataA + " " +
|
resultLog.push('Warning @ line ' + i + ':' + currRawDataQ + ' ' + currRawDataA + ' ' +
|
||||||
currRawDataI);
|
currRawDataI)
|
||||||
jumped++;
|
jumped++
|
||||||
}
|
}
|
||||||
} // end of parsing all data
|
} // end of parsing all data
|
||||||
return {
|
return {
|
||||||
count: allCount,
|
count: allCount,
|
||||||
log: resultLog
|
log: resultLog
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// loading stuff
|
// loading stuff
|
||||||
function LoadJSON(resource) {
|
function LoadJSON (resource) {
|
||||||
var count = -1;
|
var count = -1
|
||||||
try {
|
try {
|
||||||
var d = JSON.parse(resource);
|
var d = JSON.parse(resource)
|
||||||
var r = new QuestionDB();
|
var r = new QuestionDB()
|
||||||
var rt = [];
|
var rt = []
|
||||||
var allCount = -1;
|
var allCount = -1
|
||||||
|
|
||||||
for (var i = 0; i < d.Subjects.length; i++) {
|
for (var i = 0; i < d.Subjects.length; i++) {
|
||||||
let s = new Subject(d.Subjects[i].Name);
|
let s = new Subject(d.Subjects[i].Name)
|
||||||
var j = 0;
|
var j = 0
|
||||||
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
|
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
|
||||||
var currQ = d.Subjects[i].Questions[j];
|
var currQ = d.Subjects[i].Questions[j]
|
||||||
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I));
|
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I))
|
||||||
}
|
}
|
||||||
rt.push({
|
rt.push({
|
||||||
name: d.Subjects[i].Name,
|
name: d.Subjects[i].Name,
|
||||||
count: j
|
count: j
|
||||||
});
|
})
|
||||||
allCount += j;
|
allCount += j
|
||||||
r.AddSubject(s);
|
r.AddSubject(s)
|
||||||
}
|
}
|
||||||
return r;
|
return r
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log("Error loading sutff", logger.GetColor("redbg"), true);
|
logger.Log('Error loading sutff', logger.GetColor('redbg'), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ProcessQA() {
|
function ProcessQA () {
|
||||||
if (!utils.FileExists(qaFile))
|
if (!utils.FileExists(qaFile)) { utils.WriteFile('', qaFile) }
|
||||||
utils.WriteFile("", qaFile);
|
|
||||||
|
|
||||||
let a = utils.ReadFile(qaFile).split("\n");
|
let a = utils.ReadFile(qaFile).split('\n')
|
||||||
let r = [];
|
let r = []
|
||||||
let ind = 0;
|
let ind = 0
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
if ( a[i] == "#")
|
if (a[i] == '#') { ind++ } else {
|
||||||
ind ++;
|
if (r[ind] == undefined) { r[ind] = {} }
|
||||||
else {
|
|
||||||
if (r[ind] == undefined)
|
|
||||||
r[ind] = {};
|
|
||||||
|
|
||||||
if (r[ind].q == undefined) {
|
if (r[ind].q == undefined) {
|
||||||
r[ind].q = a[i];
|
r[ind].q = a[i]
|
||||||
} else {
|
} else {
|
||||||
if (r[ind].a == undefined)
|
if (r[ind].a == undefined) { r[ind].a = [] }
|
||||||
r[ind].a = [];
|
|
||||||
|
|
||||||
r[ind].a.push(a[i]);
|
r[ind].a.push(a[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue