aboutsummaryrefslogtreecommitdiff
path: root/src/court.mjs
blob: 713c488dd973c6c238dc702beb75d46f2b103df0 (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
import { Manager } from "./utils.mjs";

class Court {
  constructor(id, status) {
    this.id = id;
    this.status = typeof status !== "undefined" ? status : true;
    this.name = "Court " + (id + 1);
  }
  toggle() {
    this.status = !this.status;
    this.print();
  }
  print() {
    let status = this.status ? "active" : "inactive";
    console.log(this.name + " is currently " + status);
  }
  render() {
    let color = this.status ? "is-success" : "is-danger";
    let type = `class="button is-medium ${color}"`;
    return `<button id=${"court_" + this.id} ${type}>${this.name}</button>`;
  }
}

export class CourtManager extends Manager {
  maxCourtCount = 12;

  constructor() {
    super("court", []);
    console.log("Loading court data.");
    let data = JSON.parse(localStorage.getItem(this.description));
    if (data === null || data.length != this.maxCourtCount) {
      console.log("Court data unavailable: " + data);
      this.generate();
      return;
    }
    for (let i = 0; i < this.maxCourtCount; i++) {
      this.listData.push(new Court(data[i].id, data[i].status));
    }
    console.log("Loaded " + this.listData.length + " courts.");
    this.render();
    this.addBehaviour();
  }
  generate() {
    console.log("Generating court data.");
    for (let i = 0; i < this.maxCourtCount; i++) {
      this.listData.push(new Court(i));
    }
    console.log("Generated data for " + this.listData.length + " courts");
    this.store();
    this.render();
    this.addBehaviour();
  }
}