diff options
author | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-12-05 20:38:29 +0200 |
---|---|---|
committer | JoelHMikael <joel.h.kronqvist@gmail.com> | 2021-12-05 20:38:29 +0200 |
commit | f76bd0750ee43bdda28f075de2744ed5fe115aba (patch) | |
tree | eac6ee8367ddb2f751b9d32a65f4744f8ccd3efa | |
parent | 1866a90a81d92b6190b55893f2341f9b4da8ec49 (diff) | |
download | LYLLRuoka-f76bd0750ee43bdda28f075de2744ed5fe115aba.tar.gz LYLLRuoka-f76bd0750ee43bdda28f075de2744ed5fe115aba.zip |
Improvements to server.js
Improved the readability of the server code by implementing async/await and making the logic based on a dictionary of build functions instead of a switch statement.
-rw-r--r-- | parse.js | 23 | ||||
-rw-r--r-- | server.js | 270 |
2 files changed, 63 insertions, 230 deletions
@@ -152,9 +152,7 @@ function parseDay(day) endOfLine = getNextChar(day, "\n", i); let unparsedIndexes = day.substring(i, endOfLine); shifts[shiftDesc] = parseLine(unparsedIndexes); - console.log(i); i = getToLineStartingWith(day, "RUOKAILUVUORO", i); - console.log(i); } while (i !== -1); return shifts; } @@ -175,13 +173,28 @@ function parseShift(data, weekdays = ["MAANANTAISIN", "TIISTAISIN", "KESKIVIIKKO else end = findExpression(data, weekdays[db.length + 1]); let unparsedDay = data.substring(i + 1, end); - console.log(unparsedDay); day = parseDay(unparsedDay); db.push(day); } return db; } -data = "MAANANTAISIN\n\n \n\nRUOKAILUVUORO I: ruokailu klo 10.50 - 11.30, oppitunti klo 11.40 - 13.00\n\nTE11 JUHO, GE21 PAHO, EA112 ANLE, MA72 JUMA, MA141 SAKO, ÄI82 MIAU, ÄI63 TUTA ja KAHDEN TUTKINNON OPINNOT 1., 2. ja 3. VUOSITASON RYHMÄT \n\n \n\nRUOKAILUVUORO II: ruokailu klo 11.40 - 12.20, oppitunti klo 11.00 - 11.40 ja klo 12.20 - 13.00\n\nGE42 MAME, KE82 SALE, PS53 SATU, MB42 SAHE, YH25 JAJU, RB46 ANSU, MB83 MATI, EA48 VIHU, FD41 MASI ja FI31 TEKE\n\n \n\nRUOKAILUVUORO III: ruokailu klo 12.20 - 13.00, oppitunti klo 11.00 - 12.20\n\nBI14 LAMI, BI12 LAMI, ENA27 SABE, RB37 KAHU, AT12 RIHO, AT13 ESRI, RB62 SASA, KU15 REOJ ja OP16 PIKS\n\n \n\n \n\nTIISTAISIN\n\n \n\nRUOKAILUVUORO I: ruokailu klo 10.50 - 11.20, oppitunti klo 11.30 - 12.50\n\nMA111 MATI, ÄI62 HAPA, KU41 REOJ, MA73 SAKO, PS82 JUNU, RB41 ANSU, BI41 PAHO, UE27 SATU ja RA41 SASA\n\n \n\nRUOKAILUVUORO II: ruokailu klo 11.40 - 12.10, oppitunti klo 11.00 - 11.40 ja klo 12.10 - 12.50\n\nUE13 TEKE, RB44 MAOI, HI31 JUSA, MA142 ESRI, KE81 SALE, PS51 ALMA, TE14 ANSA, TE12 ANSA, BI16 MAME ja BI13 MAME\n\n \n\nRUOKAILUVUORO III: ruokailu klo 12.20 - 12.50, oppitunti klo 11.00 - 12.20\n\nENA25 VIHU, TE17 JUHO, AT11 RIHO, FY12 JUMA, MB84 OLNU, ÄI85 TUTA ja MU41 MAMY\n\n \n\n \n\nKESKIVIIKKOISIN\n\n \n\nRUOKAILUVUORO I: ruokailu klo 10.50 - 11.30, oppitunti klo 11.40 - 13.00\n\nLP91 JUHO, LT91 ANSA, ÄI84 HAPA, AT14 ESRI, AT16 RIHO, GE61 MAME, HI32 HEAH, ÄI44 VETU ja PS52 JSAL\n\n \n\nRUOKAILUVUORO II: ruokailu klo 11.40 - 12.20, oppitunti klo 11.00 - 11.40 ja klo 12.20 - 13.00\n\nMB63 SAHE, UE51 SATU, KE33 SAKO, SC51 SABE, RB35 ANSU, EA46 KAHU, HI27 JAJU, ENA21 MASI ja MA143 MATI\n\n \n\nRUOKAILUVUORO III: ruokailu klo 12.20 - 13.00, oppitunti klo 11.00 - 12.20\n\nRB82 MAOI, KE13 SALE, PS15 ALMA, PS12 ALMA, OPO12 KIIK, OPO17 PIKS, EA51 ANLE ja ÄI61 TUTA\n\n \n\nTORSTAISIN\n\n \n\nRUOKAILUVUORO I: ruokailu klo 10.50 - 11.20, oppitunti klo 11.30 - 12.50\n\nLP22 JUHO, PS71 KAMA, ÄI48 HAPA, ÄI86 VETU, ÄI65 MIAU, MA74 RIHO, YH42 JUSA ja RB72 MAOI\n\n \n\nRUOKAILUVUORO II: ruokailu klo 11.40 - 12.10, oppitunti klo 11.00 - 11.40 ja klo 12.10 - 12.50\n\nMB64 SAHE, ENA24 ANLE, PS17 SATU, GE31 PAHO, SC101 SABE, MB43 JOTO, PC41 VIHU, PD51 VIHU, ENA22 KAHU ja FY72 JUMA\n\n \n\nRUOKAILUVUORO III: ruokailu klo 12.20 - 12.50, oppitunti klo 11.00 - 12.20\n\nFI13 ALMA, HI15 HEAH, HI12 HEAH, KU11 REOJ, LT23 ANSA, MU16 MAMY, MU14 MAMY ja BI23 MAME\n\n \n\nPERJANTAISIN\n\n \n\nRUOKAILUVUORO I: ruokailu klo 10.50 - 11.30, oppitunti klo 11.40 - 13.00\n\nOPO13 KIIK, OPO15 PIKS, ET21 ALMA, RA111 MAOI, RB111 MAOI, TE32 ANSA, FY111 JUMA, PC81 VIHU, TE22 JUHO, ÄI83 MIAU ja ÄI67 VETU\n\n \n\nRUOKAILUVUORO II: ruokailu klo 11.40 - 12.20, oppitunti klo 11.00 - 11.40 ja klo 12.20 - 13.00\n\nRB71 KAHU, AT17 RIHO, MB82 JOTO, MA71 MATI, MB41 ESRI, EA44 ANLE ja HI25 HEAH\n\n \n\nRUOKAILUVUORO III: ruokailu klo 12.20 - 13.00, oppitunti klo 11.00 - 12.20\n\nBI26 PAHO, FI11 JSAL, FI12 JSAL, PS14 TEKE, ENA26 SABE, MU12 MAMY, MU13 MAMY, KE32 SALE ja FD81 MASI"; -console.log(parseShift(data)); +/* + * DB structure: + * WEEKDAY + * FOOD SHIFTS + * COURSE INDEXES + * TEACHER INDEXES + */ + +function getShift(day, index, db) +{ + let shifts = db[day]; + for (const [key, val] of Object.entries(shifts)) + { + } +} + +exports.shift = parseShift; +exports.get = getShift; @@ -1,18 +1,14 @@ const http = require("http"); const fs = require("fs"); const url = require("url"); +const parse = require("./parse.js"); -let maxshifts = Infinity; 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 = [ + const shifts = [ //1: "Ruokailuvuoro 1", //2: @@ -21,105 +17,42 @@ function init() "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) + const build = { + "./index.html": buildMain, + "./index.css": buildDefault, + }; + const errorPath = "./404/index.html"; + + async 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; - - switch(path) + let data; + const args = { + "path": path, + "query": q.query, + "shifts": shifts + }; + try { - 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(); - } - ); + data = await build[path](args); } + catch (error) + { + if (!(error instanceof TypeError)) + console.log("ERROR!!! " + error); + data = await build404(errorPath, q.pathname); + } + 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; - if (path == "./") - path = "./index.html"; - - console.log(path); - - if (!allowedPaths.includes(path)) - path = path404; - - 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(); - } - ); - } -} function openFile(path) { @@ -130,154 +63,41 @@ function openFile(path) if (err) reject(err); resolve(data); - return data; }) }); } -function buildMain(query, path, maxshifts) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - data_string = data.toString("utf-8"); - if (query.index === undefined) - resolve(data_string.replace("\\(result\\)", "")); - resolve(data_string.replace("\\(result\\)", shifts[parseshift(query, maxshifts) - 1])); - } - ); - }); -} - - -function build404(path, attemptpath) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - data_string = data.toString("utf-8"); - resolve(data_string.replace("\\(path\\)", attemptpath)); - } - ); - }); -} - -function buildDefault(path) -{ - return new Promise((resolve, reject) => - { - openFile(path).then( - data => - { - resolve(data.toString("utf-8")); - } - ); - }); -} - -function parseshift(index, maxshifts) +async function buildMain(args) { - /*//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.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 1; + const path = args["path"]; + const query = args["query"]; + const shifts = args["shifts"]; + + 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]); } -function getCharAmount(s, c) +async function build404(path, attemptpath) { - let n = 0; - for (let c_i = 0; c_i < s.length; c_i++) - { - n += +(s[c_i] === c); - } - return n; + const data = await openFile(path); + const data_string = data.toString("utf-8"); + return data_string.replace("\\(path\\)", attemptpath); } -function getNextChar(s, i, c) +async function buildDefault(args) { - for (; i < s.length; i++) - { - if (s[i] === c) - return i; - } - return -1; + const path = args["path"]; + const data = await openFile(path); + return data.toString("utf-8"); } -function getToLineStartingWith(s, start, ss) +function parseshift(index) { - 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; - + return 1; // placeholder glue } -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(); |