This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauto.js
296 lines (253 loc) · 10.4 KB
/
auto.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
var path = require('path');
var request = require('request');
var express = require('express');
var compression = require('compression');
var config;
try {
config = require('./config.js');
} catch (err) { // If there is no config file
config = {};
config.requestAB = process.env.REQUESTAB;
config.requestAT = process.env.REQUESTAT;
config.PCRToken = process.env.PCRTOKEN;
config.KeenIOID = process.env.KEEN_PROJECT_ID;
config.KeenIOWriteKey = process.env.KEEN_WRITE_KEY;
config.autotestKey = process.env.AUTOTESTKEY;
}
var app = express();
// Set express settings
app.use(compression());
app.use('/js/plugins', express.static(path.join(__dirname, 'public/js/plugins'), { maxAge: 2628000000 }));
app.use('/js', express.static(path.join(__dirname, 'public/js'), { maxAge: 0 }));
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 2628000000 }));
console.log('Express initialized');
// Start the server
app.listen(process.env.PORT || 3001, function(){
console.log("Node app is running. Better go catch it.");
});
// Handle main page requests
app.get('/auto/', function(req, res) {
res.sendFile(path.join(__dirname+'/views/auto.html'));
});
// Handle main page requests
app.get('/auto/request', function(req, res) {
console.log(req.query.course);
var courses = req.query.course;
// console.log();
return res.send(autoSched(courses));
});
var deptList = ["AAMW", "ACCT", "AFRC", "AFST", "ALAN", "AMCS", "ANCH", "ANEL", "ANTH", "ARAB", "ARCH", "ARTH", "ASAM", "ASTR", "BCHE", "BDS", "BE", "BENF", "BENG", "BEPP", "BIBB", "BIOE", "BIOL", "BIOM", "BIOT", "BMB", "BMIN", "BSTA", "CAMB", "CBE", "CHEM", "CHIN", "CIMS", "CIS", "CIT", "CLST", "COGS", "COML", "COMM", "CPLN", "CRIM", "DEMG", "DENT", "DPED", "DPRD", "DRST", "DTCH", "DYNM", "EALC", "EAS", "ECON", "EDUC", "EEUR", "ENGL", "ENGR", "ENM", "ENMG", "ENVS", "EPID", "ESE", "FNAR", "FNCE", "FOLK", "FREN", "GAFL", "GAS", "GCB", "GEOL", "GREK", "GRMN", "GSWS", "GUJR", "HCIN", "HCMG", "HEBR", "HIND", "HIST", "HPR", "HSOC", "HSPV", "HSSC", "IMUN", "INTG", "INTL", "INTR", "INTS", "IPD", "ITAL", "JPAN", "JWST", "KORN", "LALS", "LARP", "LATN", "LAW", "LAWM", "LGIC", "LGST", "LING", "LSMP", "MATH", "MCS", "MEAM", "MED", "MGEC", "MGMT", "MKTG", "MLA", "MLYM", "MMP", "MSCI", "MSE", "MSSP", "MTR", "MUSA", "MUSC", "NANO", "NELC", "NETS", "NGG", "NPLD", "NSCI", "NURS", "OIDD", "PERS", "PHIL", "PHRM", "PHYS", "PPE", "PREC", "PRTG", "PSCI", "PSYC", "PUBH", "PUNJ", "REAL", "REG", "RELS", "ROML", "RUSS", "SAST", "SCND", "SKRT", "SLAV", "SOCI", "SPAN", "STAT", "STSC", "SWRK", "TAML", "TELU", "THAR", "TURK", "URBS", "URDU", "VBMS", "VCSN", "VCSP", "VIPR", "VISR", "VLST", "VMED", "VPTH", "WH", "WHCP", "WHG", "WRIT", "YDSH"];
var meetData = [];
for (var dept in deptList) { if (deptList.hasOwnProperty(dept)) {
try {
var thedept = deptList[dept];
var deptData = require('./Data/2018AMeet/'+thedept+'.json');
var secs = Object.keys(deptData);
for (var i = 0; i < secs.length; i++) {
var meetData = meetData.concat(deptData[secs[i]]);
}
} catch(err) {
}
}}
// var args = process.argv;
var courses = ['cis-110', 'meam-348','cis-120', 'math114', 'econ001', 'chem102', 'meam545', 'meam101', 'meam201', 'psyc001'];
autoSched(courses);
function autoSched(courses) {
var allData = [];
// Pull relevant meeting time info for the classes
for (var i = 0; i < courses.length; i++) {
var thisc = FormatID(courses[i]);
courses[i] = thisc[0] + '-' + thisc[1];
var toAdd = meetData.filter(function(sec) { // Find all sections of the course
return sec.course === courses[i];
});
allData = allData.concat(toAdd);
}
return run(allData);
}
function run(allData) {
var coursesAdded = [];
var datasched = {}; // Create object with each required course section type
// {
// MEAM-101-LAB: [MEAM-101-101, MEAM-101-102 ...],
// ;..
// }
allData.forEach(function(thissect) {
if (thissect.open) {
var thiscourseact = thissect.course + '-' + thissect.actType;
if (!datasched[thiscourseact]) {
datasched[thiscourseact] = [];
}
datasched[thiscourseact].push(thissect);
}
});
var d, twodarr;
d = regenData(datasched);
raw_twodarr = d[0]; // [MEAM-101-LEC, MEAM-101-LAB]
raw_datasched = d[1];//[1, 4]
raw_types = raw_twodarr.map(function(a) {
return a[0];
});
var typekey = ['LEC', 'LAB', 'REC'];
datasched = raw_datasched;
twodarr = raw_twodarr;
types = raw_types;
for (var h = 0; h < raw_types.length; h++) {
var anchor = datasched[types[h]][0];
if (!anchor) {
console.log('noanch', types[h]);
}
console.log(types[h]);
d = regenData(datasched);
twodarr = d[0];
datasched = d[1];
typeNumObj = d[2];
var hold;
for (var i = h+1; i < twodarr.length; i++) {
indtorem = CompareAnchorToType(i);
if (indtorem.length === twodarr[i][1]) { // No sections work with this anchor, move to next anchor index and try again.
// anchorind++;
console.log('full', types[h]);
datasched[types[h]].splice(0, 1);
i = twodarr.length;
h--;
hold = true;
} else {
var typeToRem = raw_twodarr[i][0];
datasched[typeToRem] = datasched[typeToRem].filter(function(a,e) {
return (indtorem.indexOf(e) === -1);
});
RemoveNonAsscSections(anchor);
d = regenData(datasched);
twodarr = d[0];
datasched = d[1];
}
}
if (hold) {
hold = false;
} else {
coursesAdded.push(anchor.idDashed);
}
}
console.log(coursesAdded);
return coursesAdded;
function CompareAnchorToType(i) {
var indtorem = [];
var type = raw_twodarr[i][0];
var numoftype = typeNumObj[type];
for (var j = 0; j < numoftype; j++) {
var ameet = datasched[type][j];
if (ameet) {
if (anchor.course !== ameet.course) {
var isover = Overlap(ameet, anchor);
if (isover) {
console.log('conflict', ameet.idDashed);
indtorem.push(j);
}
}
}
}
return indtorem;
}
function RemoveNonAsscSections(anchor) {
var all_anchor_assc = [anchor.assclec, anchor.assclab, anchor.asscrec];
for (var g = 0; g < 3; g++) {
var thisassc = all_anchor_assc[g];
if (thisassc.length) {
thisassclist = thisassc.map(function(a) {
return a.subject + '-' + a.course_id + '-' + a.section_id;
});
var thisassctype = anchor.course + '-' + typekey[g];
var dataschedassc = datasched[thisassctype].map(function(a) {
return a.idDashed;
});
datasched[thisassctype] = datasched[thisassctype].filter(function(a,e) {
return (thisassclist.indexOf(dataschedassc[e]) > -1);
});
}
}
}
}
function regenData(datasched) {
var types = Object.keys(datasched);
var nums = [];
for (var i = 0; i < types.length; i++) {
nums[i] = datasched[types[i]].length;
} // Number of each sections of a specific type
var twodarr = [];
for (i = 0; i < types.length; i++) {
twodarr[i] = [types[i], nums[i]];
}
twodarr.sort(function(a,b) {
return a[1] - b[1];
});
// Sorted double list of types and number of available sections of those types (low to hi)
typeNumObj = {}; // Objectified
for (i = 0; i < twodarr.length; i++) {
typeNumObj[twodarr[i][0]] = twodarr[i][1];
}
return [twodarr, datasched, typeNumObj];
}
function Overlap(block1, block2) {
// console.log(block1)
var meet1 = block1.meetblk;
var meet2 = block2.meetblk;
var over = false;
for (var i = 0; i < meet1.length; i++) {
for (var j = 0; j < meet2.length; j++) {
// console.log(1, meet1[i])
// console.log(2, meet2[j])
if (meet1[i].meetday === meet2[j].meetday) {
over = check(meet1[i], meet2[j]);
if (over) {return over;}
}
}
}
return over;
}
function check(block1, block2) {
// Thank you to Stack Overflow user BC. for the function this is based on.
// http://stackoverflow.com/questions/5419134/how-to-detect-if-two-divs-touch-with-jquery
var y1 = block1.starthr;
var b1 = block1.endhr;
var y2 = block2.starthr;
var b2 = block2.endhr;
// This checks if the top of block 2 is lower down (higher value) than the bottom of block 1...
// or if the top of block 1 is lower down (higher value) than the bottom of block 2.
// In this case, they are not overlapping, so return false
if (b1 <= y2 || b2 <= y1) {
return false;
} else {
return true;
}
}
function requestAsync(url) {
return new Promise(function(resolve, reject) {
request({
uri: url,
method: "GET",headers: {"Authorization-Bearer": config.requestAB2, "Authorization-Token": config.requestAT2}, // Send authorization headers
}, function(err, res, body) {
if (err) { return reject(err); }
return resolve([res, body]);
});
});
}
function FormatID(rawParam) {
var searchParam = rawParam.replace(/ /g, "").replace(/-/g, "").replace(/:/g, ""); // Remove spaces, dashes, and colons
var retArr = ['', '', ''];
if (isFinite(searchParam[2])) { // If the third character is a number (e.g. BE100)
splitTerms(2);
} else if (isFinite(searchParam[3])) { // If the fourth character is a number (e.g. CIS110)
splitTerms(3);
} else if (isFinite(searchParam[4])) { // If the fifth character is a number (e.g. MEAM110)
splitTerms(4);
} else {
retArr[0] = searchParam;
}
function splitTerms(n) {
retArr[0] = searchParam.substr(0, n).toUpperCase();
retArr[1] = searchParam.substr(n, 3);
retArr[2] = searchParam.substr(n+3, 3);
}
return retArr;
}