diff options
| -rw-r--r-- | classes.txt | 8 | ||||
| -rw-r--r-- | index.css | 8 | ||||
| -rw-r--r-- | index.html | 6 | ||||
| -rw-r--r-- | parse.js | 84 | ||||
| -rw-r--r-- | server.js | 46 | 
5 files changed, 129 insertions, 23 deletions
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 @@ -22,8 +22,12 @@ body {  @media screen and (min-width: 700px)  { -	#foodshift, #food { -		width: 49%; +	#foodshift +	{ +		width: 58%; +	} +	#food { +		width: 41%;  	}  } @@ -16,9 +16,9 @@  		<main>  			<div id="foodshift">  				<form method="GET" class="float-block"> -					<label for="index">Opettaja / kurssi:</label> +					<label for="index">Opettaja/kurssi/luokka:</label>  					<br> -					<input type="text" name="index" placeholder="sahe // ÄI1.2"> +					<input type="text" name="index" placeholder="sahe // ÄI1.2 // B203">  					<br>  					<label for="day">Päivä:</label>  					<br> @@ -38,7 +38,7 @@  				<br>  				<div id="shift-result" class="float-block"> -					<h2>\(foodshift-header\)</h2> +					<h2>\(index-type\) \(shift-header\) ruokailuvuoro \(day\):</h2>  					<p>\(shift\)</p>  				</div>  			</div> @@ -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; @@ -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(`<option value=\"${day}\">`, `<option value=\"${day}\" selected>`);  	// get the food shift to res["shift"] +	const indexTypes = { +		"course": "Kurssin", +		"teacher": "Opettajan", +		"class": "Luokan" +	};  	res["shift"] = undefined; -	if ((day === 0) || (day === 6)) -		res["shift"] = `Maanantain ruoka: ${parse.get(day, query.index, DB)}`;  	if ((index === undefined) || (index === ""))  		res["shift"] = "";  	if (res["shift"] === undefined) -		res["shift"] = parse.get(day, index, DB); +	{ +		let shift = parse.get(day, index, DB); +		let key = Object.keys(shift)[0]; +		if (key !== undefined) +		{ +			res["shift"] = key; +			res["shift-header"] = `${shift[key][0]}/${shift[key][1]}`; +			if (shift[key][2] !== undefined) +				res["shift-header"] += `/${shift[key][2]}` +			res["index-type"] = "Kurssin"; +		} +		else +		{ +			res["shift"] = -1; +			res["shift-header"] = `${index}`; +			res["index-type"] = indexTypes[parse.indexType(index)]; +			if (res["index-type"] === undefined) +				res["index-type"] = ""; +		} +	}  	if (res["shift"] === -1) -		res["shift"] = "Kyseiselle kurssille/opettajalle ei löydy ruokailua päivältä!"; +		res["shift"] = "Kurssilla/opettajalla/luokalla ei ole ruokailua päivällä tai kurssia ei ole olemassa!";  	// get the day -	res["foodshift-header"] = `Kurssin (???)/(???) ruokailuvuoro ${["su", "ma", "ti", "ke", "to", "pe", "la"][day]}:` +	res["day"] = ["su", "ma", "ti", "ke", "to", "pe", "la"][day];  	if (res["shift"] === "")  		data_string = data_string.replace('<div id="shift-result" class="float-block">', '<div id="shift-result" class="float-block" style="display: none;">'); @@ -118,10 +144,8 @@ async function buildDefault(args)  function build_replace(s, dict)  { -	console.log(dict);  	for (const [key, val] of Object.entries(dict))  	{ -		console.log(`\\(${key}\\)`);  		s = s.replaceAll(`\\(${key}\\)`, val);  	}  | 
