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

export class Guest {
  maxGuestCount = 12;
  categoryCount = 3;

  categories = {
    0: { description: "Beginner", level: 1 },
    1: { description: "Novice", level: 3 },
    2: { description: "Intermediate", level: 6 },
  };

  constructor(id, status, totalPlayed) {
    this.id = id;
    this.status = typeof status !== "undefined" ? status : false;
    const categoryId = Math.floor(id / (12 / this.categoryCount));
    const guestIdentifier = (id % (12 / this.categoryCount)) + 1;
    this.name = this.categories[categoryId].description + " " + guestIdentifier;
    this.level = this.categories[categoryId].level;
    this.totalPlayed = typeof totalPlayed !== "undefined" ? status : 0;
  }
  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=${"guest_" + this.id} ${type}>${this.name}</button>`;
  }
}

export class GuestManager extends Manager {
  constructor() {
    super("guest", []);
    console.log("Loading guest data.");
    let data = JSON.parse(localStorage.getItem(this.description));
    if (data === null || data.length === 0) {
      console.log("Guest data unavailable: " + data);
      this.generate();
      return;
    }
    for (let i = 0; i < data.length; i++) {
      let id = Number(data[i].id);
      let status = data[i].status;
      let totalPlayed = data[i].totalPlayed;
      this.listData.push(new Guest(id, status, totalPlayed));
    }
    console.log("Loaded " + this.listData.length + " players.");
    this.render();
  }
  generate() {
    console.log("Generating guest data.");
    for (let i = 0; i < new Guest(0, false).maxGuestCount; i++) {
      let p = new Guest(i, false, 0);
      this.listData.push(p);
    }
    console.log("Generated data for " + this.listData.length + " guest");
    this.store();
    this.render();
  }
  active(factor) {
    factor = typeof factor !== "undefined" ? factor : 1;
    let data = super.active();
    console.log("Normalize skill by a factor of " + factor + ".");
    let players = data.map(function (p) {
      p.skill = Math.ceil(p.skill / factor);
      return p;
    });
    return players;
  }
}