From 5eff94062faac65f402a6b04d545fb3225c07bf4 Mon Sep 17 00:00:00 2001 From: JoelHMikael Date: Sat, 11 Dec 2021 15:19:09 +0200 Subject: Class parsing Parsing in parse.js, implementation in server.js. Also changes to the returned information from parsing and minor ones to the GUI. --- classes.txt | 8 ++++++ index.css | 8 ++++-- index.html | 6 ++--- parse.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ server.js | 46 +++++++++++++++++++++++++-------- 5 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 classes.txt diff --git a/classes.txt b/classes.txt new file mode 100644 index 0000000..ce5663b --- /dev/null +++ b/classes.txt @@ -0,0 +1,8 @@ +ÄI7.1: A205 +FY5.2: B102 +MA8.4: B101 +EA5.2: B209 +FI2.6: B103 +EA5.6: B211 +BI4.2: A102 +HI2.4: B210 diff --git a/index.css b/index.css index 5c3834e..76b5df6 100644 --- a/index.css +++ b/index.css @@ -22,8 +22,12 @@ body { @media screen and (min-width: 700px) { - #foodshift, #food { - width: 49%; + #foodshift + { + width: 58%; + } + #food { + width: 41%; } } diff --git a/index.html b/index.html index 61bbda3..b12164d 100644 --- a/index.html +++ b/index.html @@ -16,9 +16,9 @@
- +
- +

@@ -38,7 +38,7 @@
-

\(foodshift-header\)

+

\(index-type\) \(shift-header\) ruokailuvuoro \(day\):

\(shift\)

diff --git a/parse.js b/parse.js index 6a88a19..819b3ed 100644 --- a/parse.js +++ b/parse.js @@ -103,6 +103,43 @@ function findExpression(data, expr, start = 0) return start; } +function parseCluttered(s) +{ + return s.replaceAll(".", "").replaceAll(" ", "").toUpperCase(); +} + +function parseClasses(classData, DB) +{ + classData = parseCluttered(classData) + "\n"; // newline so that loop can find last value + // parse data to dict + let classes = {}; + let i = 0; + while (i < classData.length) + { + let separator = getNextChar(classData, ":", i); + let lineEnd = getNextChar(classData, "\n", i); + let key = classData.substring(i, separator); + let val = classData.substring(separator + 1, lineEnd); + i = lineEnd + 1; + classes[key] = val; + } + + for (let day = 0; day < DB.length; day++) + { + let shifts = DB[day]; + for (const [key, val] of Object.entries(shifts)) + { + DB[day][key].push([]); + let indexes = val[0]; // use courses: the classes are paired to them and they are unique with a higher probability + for (let i = 0; i < indexes.length; i++) + { + DB[day][key][2].push(classes[indexes[i]]); + } + } + } + +} + function parseLine(line, toRemove = " ja KAHDEN TUTKINNON OPINNOT 1., 2. ja 3. VUOSITASON RYHMÄT ") { let i = 0; @@ -112,7 +149,24 @@ function parseLine(line, toRemove = " ja KAHDEN TUTKINNON OPINNOT 1., 2. ja 3. V if (line.substring(line.length - toRemove.length, line.length) === toRemove) line = line.substring(0, line.length - toRemove.length); - line = line.replaceAll(",", "").replaceAll("ja ", ""); + line = line.replaceAll(",", "").replaceAll("ja ", "").replaceAll(" + ", "+"); + + while (i < line.length) + { + if (line[i] === "+") + { + + nextSpace = getNextChar(line, " ", i); + let nextNextSpace = getNextChar(line, " ", nextSpace + 1); + if (nextNextSpace === -1) + nextNextSpace = line.length; + line = `${line.substring(0, i)} ${line.substring(nextSpace + 1, nextNextSpace)} ${line.substring(i + 1, line.length)}`; + i = nextNextSpace - 1; + } + i++; + } + nextSpace = 0; + i = 0; const getElement = list => { @@ -178,13 +232,23 @@ function parseShift(data, weekdays = ["MAANANTAISIN", "TIISTAISIN", "KESKIVIIKKO /* * DB structure: - * list * WEEKDAY - list * FOOD SHIFTS - dict * COURSE INDEXES - list * TEACHER INDEXES - list + * maybe: CLASSES - list */ +function getIndexType(index) +{ + if (/^[A-Za-zåäöÅÄÖ]{2,3}\d{2,3}$/.test(index)) + return "course"; + if (/^[A-Za-zåäöÅÄÖ]{4}$/.test(index)) + return "teacher"; + if (/^\w\d{3}$/.test(index)) + return "class"; +} + function getShift(day, index, db) // day: int, 1 = monday; index: string of course/teacher; db: parsed shifts { if ((typeof day !== "number") || isNaN(day) || (typeof index !== "string")) @@ -193,20 +257,26 @@ function getShift(day, index, db) // day: int, 1 = monday; index: string of cour let shifts = db[day - 1]; let _endOfIndex = parseInt(index.substring(2, 4)); - let is_teacher = _endOfIndex.toString() !== index.substring(2, 4); - let is_course = !is_teacher; + let type = getIndexType(index); + if (type === undefined) + return {}; + let type_index = +(type === "teacher") + (+(type === "class") * 2); + let res = {}; for (const [key, val] of Object.entries(shifts)) { - let indexes = val[+is_teacher]; + let indexes = val[type_index]; for (let i = 0; i < indexes.length; i++) { if (indexes[i] === index) - return key; + res[key] = [val[0][i], val[1][i], val[2][i]]; } } - return -1; + return res; } exports.build = parseShift; +exports.indexType = getIndexType; +exports.classes = parseClasses; exports.get = getShift; +exports.cluttered = parseCluttered; diff --git a/server.js b/server.js index 6dc41c5..662d319 100644 --- a/server.js +++ b/server.js @@ -15,9 +15,12 @@ async function init() }; const errorPath = "./404/index.html"; - let shiftcont = await openFile("./shifts.txt"); - shiftcont = shiftcont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which creates problems - const DB = await parse.build(shiftcont); + let shiftCont = await openFile("./shifts.txt"); + shiftCont = shiftCont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which creates problems + let classCont = await openFile("./classes.txt"); + classCont = classCont.toString("utf-8").replaceAll("\r", ""); + let DB = parse.build(shiftCont); + parse.classes(classCont, DB); async function server(req, res) { @@ -67,7 +70,7 @@ async function buildMain(args) const query = args["query"]; let index; if (typeof query.index === "string") - index = query.index.toUpperCase().replaceAll(".", "").replaceAll(" ", ""); + index = parse.cluttered(query.index); const DB = args["db"]; const data = await openFile(path); let data_string = data.toString("utf-8"); @@ -76,23 +79,46 @@ async function buildMain(args) const d = new Date(); let day = d.getDay(); + day = +((day === 0) || (day === 6)) + (+(!(day === 0) && !(day === 6)) * day); if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) > 0) && (parseInt(query.day) < 7)) day = parseInt(query.day); data_string = data_string.replace(`