-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.js
79 lines (69 loc) · 2 KB
/
plan.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
const { createDates, createSlots } = require("./createTimes");
function createPlan(teams, startDate, startTime) {
// dummy team, to make sure we have an even amount of teams
if (teams.length % 2) {
teams.push(undefined);
}
const n = teams.length;
const dates = createDates(startDate, n - 1);
const slots = createSlots(startTime, n / 2);
const plan = new Plan(dates, slots);
plan.schedule(teams);
return plan;
}
class Plan {
constructor(termine, slots) {
this.termine = termine;
this.slots = slots;
this.init();
}
init() {
// create empty plan
const { rows, cols } = this;
this.plan = new Array(cols);
for (var i = 0; i < cols; i++) {
this.plan[i] = new Array(rows).fill("n/a");
}
}
// round robin
schedule(teams) {
let cycle = teams.slice();
const { rows, cols } = this;
for (var j = 0; j < cols; j++) {
for (var i = 0; i < rows; i++) {
const team1 = cycle[i];
const team2 = cycle[cols - i];
// single teams
if (team1 === undefined || team2 === undefined) {
this.plan[j][i] = team1 || team2;
}
// pairs of teams
else {
this.plan[j][i] = `${team1} ⟺ ${team2}`;
}
}
// rotate all elemets except the first one
cycle.splice(1, 0, cycle.pop());
}
}
get rows() {
return this.slots.length;
}
get cols() {
return this.termine.length;
}
print() {
const { termine, slots, plan, cols, rows } = this;
for (let j = 0; j < cols; j++) {
console.log(`### ${termine[j]} \n`);
for (let i = 0; i < rows; i++) {
console.log(`${slots[i]} : ${plan[j][i]} `);
}
console.log("\n");
}
}
}
module.exports = {
Plan,
createPlan
};