aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
blob: 5b326d7865107629ecc185c0011bb7edad12fd66 (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
import * as fs from "fs"
import * as html_to_pdf from "html-pdf-node"

import * as v from './variables.js'

let hinweise = "<ul>";
for (let hinweis of v.hinweise) {
  hinweise += `<li>${hinweis}</li>`;
}
hinweise += `</ul>`;

let date = new Date();
date = date.toLocaleDateString("de-DE", {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});

const ctage = {
  "Mo": [],
  "Di": [],
  "Mi": [],
  "Do": [],
  "Fr": []
}

const options = {
  format: 'A4',
  landscape: true,
  margin: {
    top: "0.5cm",
    right: "1cm",
    bottom: "1cm",
    left: "2cm",
  },
};

// returns time difference in 15-minute intervalls
function timediff(a, b) {
  const time = "1970-01-01 ";
  return (new Date(time + a) - new Date(time + b)) / 1000 / 60 / 15;
}

function gettimefromintervalls(x) {
  let y = 15 * x / 60 + 8
  let z = y.toString().padStart(2, '0');

  if (Math.round(y) == y) return `<th rowspan="4">${z}:00</th>`;

  return ""
}

function sortTage(tage) {
  for (let tag in tage) {
    tage[tag].sort((a, b) => {
      return timediff(a.von, b.von)
    });
  }

  return tage;
}

function runtimes(tage, tage_runtimes) {
  for (let tag in tage) {
    for (let eintrag in tage[tag]) {
      // could be redundant
      if (eintrag == tage[tag].length) break;

      let a = tage[tag][eintrag];
      let runtime = timediff(a.bis, a.von)

      tage_runtimes[tag].push({
        "name": a.name,
        "raum": a.raum,
        "runtime": runtime,
      });
    }
  }

  return tage_runtimes;
}

function bufferUp(tage) {
  tage = sortTage(tage);

  for (let tag in tage) {
    if (tage[tag][0].von != "08:00") {
      tage[tag].splice(0, 0, {
          "name": "BUFFER",
          "raum": "BUFFER",
          "von": "08:00",
          "bis": tage[tag][0].von,
      })
    }

    if (tage[tag][tage[tag].length - 1].bis != "19:00") {
      tage[tag].push({
          "name": "BUFFER",
          "raum": "BUFFER",
          "von": tage[tag][tage[tag].length - 1].bis,
          "bis": "19:00",
      })
    }
  }

  for (let tag in tage) {
    for (let eintrag in tage[tag]) {
      if (eintrag == tage[tag].length - 1) break;

      let a = tage[tag][eintrag];
      let b = tage[tag][eintrag - -1];

      let runtime = timediff(b.von, a.bis)

      if (runtime > 0) {
        tage[tag].push({
          "name": "BUFFER",
          "raum": "BUFFER",
          "von": a.bis,
          "bis": b.von,
        });
      }
    }
  }

  return tage;
}

function main() {
  let res = "";

  let tage = structuredClone(ctage);
  let tage_runtimes = structuredClone(ctage);

  for (let eintrag of v.eintraege) {
    for (let termin of eintrag.termine) {
      tage[termin.wochentag].push({
        "name": eintrag.name,
        "raum": termin.raum,
        "von": termin.von,
        "bis": termin.bis,
      })
    }
  }

  tage = bufferUp(tage, ctage);
  tage = sortTage(tage);
  tage_runtimes = runtimes(tage, tage_runtimes)

  let timeout = {}

  // 1h has 4 15 minute intervalls: 08 to 19 means 44 intervalls
  for (let i = 0; i < 44; i++) {
    let mensa = "";

    if (11 < i && i < 25) mensa = "mensa1";
    if (25 < i && i < 27) mensa = "mensa2";

    res += `<tr class="${mensa}">` + gettimefromintervalls(i)

    for (let day in tage_runtimes) {
      if (timeout[day] > 0) {
        timeout[day]--;
        continue;
      }

      let el = tage_runtimes[day][0];

      if (el.name == "BLOCKED" && el.raum == "BLOCKED") {
        res += `<td rowspan="${el.runtime}" class="block"></td>`;
      } else {
        let name = el.name == "BUFFER" ? "" : `<div><span>${el.name}</span>`;
        let raum = el.raum == "BUFFER" ? "" : `<span>${el.raum}</span>`;

        res += `<td rowspan="${el.runtime}">${name}${raum}</td>`;
      }

      timeout[day] = el.runtime - 1;
      tage_runtimes[day].splice(0, 1);
    }

    res += "</tr>"
  }

  // console.log(res)

  return res
}

let html = `
${v.style}
<p>Persönlicher Stundenplan von ${v.student} für das ${v.semester}. Stand: ${date}.</p>
<table>
  <tr>
    <th> Uhrzeit </th>
    <th class="tag"> Montag </th>
    <th class="tag"> Dienstag </th>
    <th class="tag"> Mittwoch </th>
    <th class="tag"> Donnerstag </th>
    <th class="tag"> Freitag </th>
  </tr>
  ${main()}
</table>
<br>
<u> Hinweise: </u>
${hinweise}
`;

html_to_pdf.generatePdf({content: html}, options).then(pdfBuffer => {
  // fs.writeFileSync("test.html", html)
  fs.writeFileSync(v.filename, pdfBuffer);
});