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
|
import { Court, Guest, Regular } from "./data";
import { uniqueNamesGenerator, names, adjectives } from 'unique-names-generator';
const config = {
dictionaries: [adjectives, names],
style: 'capital',
length: 2
}
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());
}
incrementGames(list) {
for (let index = 0; index < this.data.length; index++) {
if (list.includes(this.data[index].name)) {
console.log("Added game count to " + this.data[index].name);
this.data[index].games += 1;
}
}
this.store();
}
}
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) {
const name = uniqueNamesGenerator(config);
console.log(name);
const firstName = name.split("_")[0];
const lastName = name.split("_")[1];
return new Regular(i, randomStatus(), randomLevel(), 0, firstName, lastName);
}
loadItem(data) {
const id = Number(data.id);
const level = Number(data.level);
const status = Boolean(Number(data.status));
const games = Number(data.games);
const firstName = data.firstName;
const 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;
}
document.getElementById("button-player-load").addEventListener("click", () => {
document.getElementById("player-load").click();
});
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);
});
}
|