diff options
author | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-11-27 10:49:12 +0200 |
---|---|---|
committer | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-11-27 10:49:12 +0200 |
commit | fce2f30af2cf8c92810586654c6341fd469604bd (patch) | |
tree | 8c59c4fa7ff07885a62e66c2e61c2e88ff39fc3d /server.js | |
parent | 0925b5b6407e6464c738ecf98eb5b9a612a9b810 (diff) | |
download | LYLLRuoka-fce2f30af2cf8c92810586654c6341fd469604bd.tar.gz LYLLRuoka-fce2f30af2cf8c92810586654c6341fd469604bd.zip |
Broken shift parsing
Better WIP
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 184 |
1 files changed, 162 insertions, 22 deletions
@@ -2,21 +2,83 @@ const http = require("http"); const fs = require("fs"); const url = require("url"); -mainPage = "./index.html"; -allowedPaths = [mainPage, "./index.css"]; -path404 = "./404/index.html"; +let maxshifts = Infinity; -shifts = [ - //1: - "Ruokailuvuoro 1", - //2: - "Ruokailuvuoro 2", - //3: - "Ruokailuvuoro 3" -]; +function init() +{ + const mainPage = "./index.html"; + const allowedPaths = [mainPage, "./index.css"]; + const path404 = "./404/index.html"; + + const weekdays = [undefined, "MAANANTAI", "TIISTAI", "KESKIVIIKKO", "TORSTAI", "PERJANTAI", undefined]; + + shifts = [ + //1: + "Ruokailuvuoro 1", + //2: + "Ruokailuvuoro 2", + //3: + "Ruokailuvuoro 3" + ]; + + /*openFile("shifts.txt").then(data => + { + let shifts = 0; + let i = 0; + do + { + i = getToLineStartingWith(data.toString("utf-8"), i + 1, "RUOKAILUVUORO"); + shifts++; + } while (i !== -1) + maxshifts = shifts / 5; // 5 = number of days + });*/ + function server(req, res) + { + let q = url.parse(req.url, true); //true? + let path = "." + q.pathname; + if (path == "./") + path = "./index.html"; + + console.log(path); + if (!allowedPaths.includes(path)) + path = path404; -http.createServer((req, res) => + switch(path) + { + case mainPage: + buildMain(q.query, path, maxshifts).then( + (data) => + { + res.write(data); + res.end(); + } + ); + break; + case path404: + build404(path, q.pathname).then( + data => + { + res.write(data); + res.end(); + } + ); + break; + default: + buildDefault(path).then( + data => + { + res.write(data); + res.end(); + } + ); + } + } + + http.createServer(server).listen(8080); +} + +function server(req, res) { let q = url.parse(req.url, true); //true? let path = "." + q.pathname; @@ -31,7 +93,7 @@ http.createServer((req, res) => switch(path) { case mainPage: - buildMain(q.query, path).then( + buildMain(q.query, path, maxshifts).then( (data) => { res.write(data); @@ -57,7 +119,7 @@ http.createServer((req, res) => } ); } -}).listen(8080) +} function openFile(path) { @@ -73,7 +135,7 @@ function openFile(path) }); } -function buildMain(query, path) +function buildMain(query, path, maxshifts) { return new Promise((resolve, reject) => { @@ -83,7 +145,7 @@ function buildMain(query, path) data_string = data.toString("utf-8"); if (query.index === undefined) resolve(data_string.replace("\\(result\\)", "")); - resolve(data_string.replace("\\(result\\)", shifts[parseshift(query) - 1])); + resolve(data_string.replace("\\(result\\)", shifts[parseshift(query, maxshifts) - 1])); } ); }); @@ -117,18 +179,57 @@ function buildDefault(path) }); } -function parseshift(index) +function parseshift(index, maxshifts) { - //get index type - is_teacher = isNaN(parseInt(index[index.length - 1])); - is_course = !is_teacher; + /*//get index type + let is_teacher = isNaN(parseInt(index[index.length - 1])); + let is_course = !is_teacher; + + //get day of week + const d = new Date(); + let day = weekdays[d.getDay()]; + if (day === undefined) + day = weekdays[1]; + //read shiftfile openFile("shifts.txt").then( - data => {} + data => + { + data = data.toString("utf-8"); + // get to the position of the day + let i = getToLineStartingWith(data, 0, day); + if (i === -1) + return -1; + + // iterate through shifts + let shift = ""; + let shifts = 0; + while (shifts < maxshifts) // infinite loop if maxshift is infinity, FIX! + { + i = getToLineStartingWith(data, i, "RUOKAILUVUORO"); + let nextLineStart = getNextChar(data, i, "\n"); + shift = data.substring(i, nextLineStart); + + // Get to the line with the teachers & courses + i = nextLineStart; + while (!((nextLineStart - i) > 2)) + { + i = nextLineStart; + nextLineStart = getNextChar(data, i + 1, "\n"); + } + i++; + // Find whether the course is in the line or not + let parsed_line = data.substring(i + 1, nextLineStart).replaceAll(",", ""); + if (findExpression(parsed_line, index, i) !== -1) + return shift; + shifts++; + } + return -1; + } ); //iterate over lines, search for day //iterate shifts for course / teacher - //return the shift number + //return the shift number*/ return 1; } @@ -141,3 +242,42 @@ function getCharAmount(s, c) } return n; } + +function getNextChar(s, i, c) +{ + for (; i < s.length; i++) + { + if (s[i] === c) + return i; + } + return -1; +} + +function getToLineStartingWith(s, start, ss) +{ + let i = start; + do + { + if (s.substr(i, ss.length) === ss) + break; + i = getNextChar(s, i, "\n") + 1; + } while(i !== -1) + + if (i === -1) + return -1; + return i; + +} + +function findExpression(data, expr, start = 0) +{ + if (Number.isInteger(start) && (start < 0)) + throw new TypeError("Start must be a positive integer!"); + while ((data.substr(start, expr.length) !== expr) && (start + expr.length < data.length)) + start++; + if (data.substr(start, expr.length) !== expr) + return -1; + return start; +} + +init(); |