aboutsummaryrefslogtreecommitdiff
path: root/src/manage.js
blob: f8fe22095e952c69fbd65b181200b8c8c24b9507 (plain) (blame)
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
import { Court, Guest, Regular } from "./data";

export class Manager {
  constructor(description, maxCount) {
    this.description = description;
    this.maxCount = maxCount;
    this.data = [];
    this.count = this.data.length;
    let data = JSON.parse(localStorage.getItem(this.description));
    if (data === null || data.length === 0) {
      this.generate();
      return;
    }
    this.load(data);
  }
  generate() {
    for (let i = 0; i < this.maxCount; i++) {
      this.data.push(this.createItem(i));
    }
    console.log("Generated " + this.data.length + " " + this.description);
    this.store();
    this.render();
  }
  load(data) {
    for (let i = 0; i < data.length; i++) {
      this.data.push(this.loadItem(data[i]));
    }
    console.log("Loaded " + this.data.length + " " + this.description);
    this.render();
  }
  store() {
    let stringData = JSON.stringify(this.data);
    localStorage.setItem(this.data[0].description, stringData);
  }
  render() {
    let renderedHTML = this.data
      .map((i) => {
        return i.render();
      })
      .join("");
    let elementId = this.data[0].description + "_list";
    document.getElementById(elementId).innerHTML = renderedHTML;
    this.addBehaviour();
  }
  addBehaviour() {
    for (let idx = 0; idx < this.data.length; ++idx) {
      const elementId = this.data[0].description + "_" + this.data[idx].id;
      const p = document.getElementById(elementId);
      p.addEventListener("click", () => {
        this.data[idx].toggle();
        this.store();
        p.classList.toggle("secondary");
        p.classList.toggle("outline");
      });
    }
  }
  active() {
    return this.data.filter(function (c) {
      return c.status;
    });
  }
  inactive() {
    return this.data.filter(function (c) {
      return !c.status;
    });
  }
}

export class CourtManager extends Manager {
  constructor() {
    super("Court", 12);
  }
  createItem(i) {
    return new Court(i, randomStatus());
  }
  loadItem(data) {
    let id = Number(data.id);
    let status = Boolean(Number(data.status));
    return new Court(id, status);
  }
}

export function randomStatus() {
  return Math.random() < 0.5;
}

function normalize(data) {
  let factor = Number(document.getElementById("skill").value);
  console.log("Normalize skill by a factor of " + factor + ".");
  return data.map(function (p) {
    p.skill = Math.ceil(p.skill / factor);
    return p;
  });
}

export class GuestManager extends Manager {
  constructor(description, count) {
    if ( typeof description === "undefined" ) { description = "Guest"; };
    if ( typeof count === "undefined" ) { count = 18; };
    super(description, count);
  }
  createItem(i) {
    return new Guest(i, randomStatus());
  }
  loadItem(data) {
    let id = Number(data.id);
    let status = Boolean(Number(data.status));
    let games = data.games;
    return new Guest(id, status, games);
  }
  active() {
    return normalize(super.active());
  }
  update(list) {
    for (let index = 0; index < this.data.length; index++ ){
      if (this.data[index].name in list) {
        this.data[index].games += 1;
      }
    }
  }
}

export function randomLevel() {
  const maxLevel = 10;
  return Math.floor(Math.random() * maxLevel + 1);
}

export class RegularManager extends GuestManager {
  constructor() {
    super("Regular", 71);
    addLoadBehaviour(this);
  }
  createItem(i) {
    return new Regular(i, randomStatus(), randomLevel());
  }
  loadItem(data) {
    let id = Number(data.id);
    let level = Number(data.level);
    let status = Boolean(Number(data.status));
    let games = Number(data.games);
    let firstName = data.firstName;
    let lastName = data.lastName;
    return new Regular(id, status, level, games, firstName, lastName);
  }
  reset() {
    for (let index = 0; index < this.data.length; index++ ){
      this.data[index].games = 0;
      this.data[index].status = false;
    }
  }
}

function parseLineToPlayer(data) {
  const info = data.split(",");
  const id = Number(info[0].trim());
  const firstName = info[1].trim();
  const lastName = info[2].trim();
  const level = Number(info[3].trim());
  return new Regular(id, randomStatus(), level, 0, firstName, lastName);
}

function parseCsv(data) {
  let lines = data.split("\n");
  let players = [];
  for (let i = 1; i < lines.length; i++) {
    if (lines[i] == undefined || lines[i].trim() == "") {
      continue;
    }
    players.push(parseLineToPlayer(lines[i]));
  }
  console.log("Loaded " + players.length + " Players");
  for (let i = 0; i < players.length; i++) {
    players[i].print();
  }
  return players;
}

function addLoadBehaviour(p) {
  const f = document.getElementById("player-load");
  f.addEventListener("change", () => {
    const file = f.files[0];
    let importData;
    console.log("Loading data from " + file.name);
    const reader = new FileReader();
    reader.onload = function (e) {
      importData = reader.result;
      p.data = parseCsv(importData);
      p.store();
      p.render();
    };
    reader.readAsText(file);
  });
}