aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
blob: 4792319d479a1dfc105d07885ea5e7c3367764db (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
import { c } from './files.js'

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

// 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;
}

export 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;
}

export function prepare(forICS = false) {
  let tage = structuredClone(ctage);
  let tage_runtimes = structuredClone(ctage);

  for (let eintrag of c.eintraege) {
    let eintragName = Object.keys(eintrag)[0];
    eintrag = eintrag[eintragName]

    for (let veranstaltung in eintrag) {
      for (let termin of eintrag[veranstaltung]) {
        tage[termin[0]].push({
          "name": `${eintragName.toString()} / ${veranstaltung.toString()}`,
          "raum": termin[3],
          "von": termin[1].replace("h", ":"),
          "bis": termin[2].replace("h", ":"),
        })
      }
    }
  }

  if (forICS) {
    return tage = sortTage(tage);
  }

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

  return runtimes(tage, tage_runtimes)
}