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
|
const weekdayToNumber = require("./Functions/dateFuncs.js").weekdayToNumber;
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, c, i = 0)
{
if (!(Number.isInteger(i) && (i >= 0)))
return -1;
for (; i < s.length; i++)
{
if (s[i] === c)
return i;
}
return -1;
}
function getNextLine(s, i)
{
i = getNextChar(s, "\n", i);
i += +(i !== -1) * 1;
return i;
}
function getToLineStartingWith(s, ss, start = 0)
{
if (!(Number.isInteger(start) && (start >= 0)))
return -1
let i = start;
do
{
if (s.substring(i, i + ss.length) === ss)
break;
i = getNextLine(s, i);
} while(i !== -1)
return i;
}
function findExpression(data, expr, start = 0)
{
if (start == -1)
return -1;
if (!(Number.isInteger(start) && (start >= 0)))
throw new TypeError("Start must be a positive integer!");
if (typeof expr !== "string")
return -1;
while ((data.substring(start, start + expr.length) !== expr) && (start + expr.length < data.length))
start++;
if (data.substring(start, start + expr.length) !== expr)
return -1;
return start;
}
function parseCluttered(s)
{
if (!(typeof s === "string"))
return "";
return s.replaceAll(".", "").replaceAll(" ", "").toUpperCase();
}
async function writeShifts(data, DB)
{
let deletions = await Promise.all([
DB.query_raw("DELETE FROM shifts"),
DB.query_raw("DELETE FROM shiftnames")
]);
data = data.replace(/RUOKAILUVUOROT.*/, "");
const dbOperations = [];
const shiftRegex = /((?:MAANANTAI|TIISTAI|KESKIVIIKKO|TORSTAI|PERJANTAI)?.*)\s*(RUOKAILU.*)\s*(.*)/gmi;
const shifts = data.matchAll(shiftRegex);
let weekday;
let shiftId = 1;
for(const shift of shifts)
{
if (shift[1] !== "")
{
weekday = weekdayToNumber(shift[1]);
shiftId = 1;
}
dbOperations.push(
writeShift(weekday, shiftId, shift[2], shift[3], DB)
);
shiftId++;
}
await dbOperations;
return 0;
}
async function writeShift(weekday, shiftId, shiftLine, courseLine, DB)
{
const dbOperations = [];
// Shift names
dbOperations.push(
DB.execute(
"INSERT INTO shiftnames VALUE (?, ?, ?)",
[weekday, shiftId, shiftLine]
)
);
// Shift contents
const courseRegex = /([A-ZÅÄÖ]{2,3}\d{2,3})(?:\+([A-ZÅÄÖ]{2,3}\d{2,3}))?(?: ([A-ZÅÄÖ]{4}))?/gi;
const courses = courseLine.matchAll(courseRegex);
for(const course of courses)
{
const courseName1 = course[1];
const courseName2 = course[2]; // this exists if the course is like MA11+MA12 RIHO
const teacher = course[3] || null;
// Get the class
let className1 = await DB.execute(
"SELECT class FROM classes WHERE course=?",
[courseName1]
);
className1 = className1[0];
if (className1 !== undefined)
className1 = className1.class;
else
className1 = null;
let className2 = undefined;
if (courseName2 !== undefined) {
className2 = await DB.execute(
"SELECT class FROM classes WHERE course=?",
[courseName2]
);
className2 = className2[0];
if (className2 !== undefined)
className2 = className2.class;
else
className2 = null;
}
// Add the info
dbOperations.push(DB.execute(
`INSERT IGNORE INTO shifts VALUES (${weekday}, ${shiftId}, ?, ?, ?)`,
[courseName1, teacher, className1]
));
if (courseName2 !== undefined) {
dbOperations.push(DB.execute(
`INSERT IGNORE INTO shifts VALUES (${weekday}, ${shiftId}, ?, ?, ?)`,
[courseName2, teacher, className2]
));
}
}
await Promise.all(dbOperations);
return 0;
}
async function getShift(day, index, DB)
{
let shift = DB.execute("SELECT name FROM shiftnames WHERE day = ? and id = (SELECT shift FROM shifts WHERE (day = ?) AND (course = ? OR teacher = ? OR class = ?) LIMIT 1)", [day, day, index, index, index]);
let additional = DB.execute("SELECT course, teacher, class FROM shifts WHERE (day = ?) AND (course = ? OR teacher = ? OR class = ?)", [day, index, index, index]);
[shift, additional] = await Promise.all([shift, additional]);
if (shift.length !== 0)
{
shift.push(additional);
return shift;
}
return undefined;
}
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}R?$/.test(index))
return "class";
}
function randInt(start, stop)
{
return start + Math.floor(Math.random() * (stop - start));
}
async function getRandomIndex(day, DB, depth=0)
{
if (depth > 10)
return null;
let indexes = await DB.execute("SELECT course, teacher, class FROM shifts WHERE day = ? ORDER BY RAND() LIMIT 1", [day]);
indexes = Object.values(indexes[0] || [null, null, null]);
let start = randInt(0, indexes.length);
for (let test = 0; test < 3; test++)
{
let i = (start + test) % indexes.length;
if (indexes[i] !== null)
return indexes[i];
}
console.log("Warning: row without class/teacher/course in database!");
return getRandomIndex(day, DB, depth + 1);
}
exports.indexType = getIndexType;
exports.cluttered = parseCluttered;
exports.build = writeShifts;
exports.get = getShift;
exports.randomIndex = getRandomIndex;
exports.find = findExpression;
exports.getNextChar = getNextChar;
|