aboutsummaryrefslogtreecommitdiff
path: root/server.js
blob: d0792c5546ccb9c190c5c24f472c0299e3872d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const http	= require("http");
const fs	= require("fs");
const url	= require("url");

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 = [
		//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;

		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;
	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)
{
	return new Promise((resolve, reject) =>
	{
		fs.readFile(path, (err, data) =>
		{
			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)
{
	/*//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;
}

function getCharAmount(s, c)
{
	let n = 0;
	for (let c_i = 0; c_i < s.length; c_i++)
	{
		n += +(s[c_i] === 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();