aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--parse.js36
-rw-r--r--server.js37
2 files changed, 50 insertions, 23 deletions
diff --git a/parse.js b/parse.js
index e310148..6a88a19 100644
--- a/parse.js
+++ b/parse.js
@@ -3,7 +3,6 @@
let db = []; // first level array: days - second level array: shifts - third level dict: course/teacher - fourth level array: indexes
while (db.length < weekdays.length)
{
- console.log("\nParsing weekday " + weekdays[db.length] + ";");
i = findExpression(data, weekdays[db.length], i);
let end = i;
if (db.length === weekdays.length)
@@ -16,7 +15,6 @@
do
{
- console.log("Parsing shift " + (shifts + 1) + ";");
let teachers = [];
let courses = [];
i = findExpression(data, "RUOKAILUVUORO", i);
@@ -30,12 +28,10 @@
nextLineStart = getNextChar(data, i + 1, "\n");
}
let parsedLine = data.substring(i, nextLineStart).replaceAll(",", "").replaceAll("ja ", "");
- console.log(parsedLine);
let parse_i = 0;
let nextSpace = getNextChar(parsedLine, parse_i, " ");
while (parse_i !== -1)
{
- //console.log("Parsing the courses / teachers.");
courses.push(parsedLine.substring(parse_i, nextSpace));
parse_i = nextSpace + 1;
nextSpace = getNextChar(parsedLine, parse_i, " ");
@@ -116,7 +112,7 @@ 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(",", "");
+ line = line.replaceAll(",", "").replaceAll("ja ", "");
const getElement = list =>
{
@@ -182,19 +178,35 @@ function parseShift(data, weekdays = ["MAANANTAISIN", "TIISTAISIN", "KESKIVIIKKO
/*
* DB structure:
- * WEEKDAY
- * FOOD SHIFTS
- * COURSE INDEXES
- * TEACHER INDEXES
+ * list
+ * WEEKDAY - list
+ * FOOD SHIFTS - dict
+ * COURSE INDEXES - list
+ * TEACHER INDEXES - list
*/
-function getShift(day, index, db)
+function getShift(day, index, db) // day: int, 1 = monday; index: string of course/teacher; db: parsed shifts
{
- let shifts = db[day];
+ if ((typeof day !== "number") || isNaN(day) || (typeof index !== "string"))
+ return -1;
+
+ 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;
+
for (const [key, val] of Object.entries(shifts))
{
+ let indexes = val[+is_teacher];
+ for (let i = 0; i < indexes.length; i++)
+ {
+ if (indexes[i] === index)
+ return key;
+ }
}
+ return -1;
}
-exports.shift = parseShift;
+exports.build = parseShift;
exports.get = getShift;
diff --git a/server.js b/server.js
index 31e1e6f..591aeaf 100644
--- a/server.js
+++ b/server.js
@@ -4,7 +4,7 @@ const url = require("url");
const parse = require("./parse.js");
-function init()
+async function init()
{
const weekdays = [undefined, "MAANANTAI", "TIISTAI", "KESKIVIIKKO", "TORSTAI", "PERJANTAI", undefined];
@@ -23,9 +23,12 @@ function init()
};
const errorPath = "./404/index.html";
+ let shiftcont = await openFile("./shifts.txt");
+ const DB = await parse.build(shiftcont.toString("utf-8"));
+
async function server(req, res)
{
- let q = url.parse(req.url, true); //true?
+ let q = url.parse(req.url, true);
let path = "." + q.pathname;
if (path == "./")
path = "./index.html";
@@ -34,16 +37,15 @@ function init()
const args = {
"path": path,
"query": q.query,
- "shifts": shifts
+ "shifts": shifts,
+ "db": DB
};
- try
+ if (typeof build[path] === "function")
{
data = await build[path](args);
}
- catch (error)
+ else
{
- if (!(error instanceof TypeError))
- console.log("ERROR!!! " + error);
data = await build404(errorPath, q.pathname);
}
res.write(data);
@@ -70,14 +72,27 @@ function openFile(path)
async function buildMain(args)
{
const path = args["path"];
- const query = args["query"];
+ const index = args["query"].index.toUpperCase().replaceAll(".", "").replaceAll(" ", "");
const shifts = args["shifts"];
+ const DB = args["db"];
const data = await openFile(path);
const data_string = data.toString("utf-8");
- if (query.index === undefined)
- return data_string.replace("\\(result\\)", "");
- return data_string.replace("\\(result\\)", shifts[parseshift(query) - 1]);
+ let res;
+
+ const d = new Date();
+ const day = d.getDay();
+
+ if ((day === 0) || (day === 6))
+ res = `Maanantain ruoka: ${parse.get(day, query.index, DB)}`;
+ if ((index === undefined) || (index === ""))
+ res = "";
+ if (res === undefined)
+ res = parse.get(day, index, DB);
+ if (res === -1)
+ res = "Kyseiselle kurssille/opettajalle ei löydy ruokailua päivältä!";
+
+ return data_string.replace("\\(result\\)", res);
}
async function build404(path, attemptpath)