summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/ui/components/server-listing.js
blob: 2af56ac2b876ba031e1120e2f6f96d40545307c0 (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
/**
 * Class for handling the server list
 */
export default class ServerListing {
  /**
   * Creates reference to container
   * @param {Interface} iface Interface for comm. with other objects
   * @param {string} serverListId ID of the server list div
   * @param {string} refreshBtnId ID of the refresh btn
   */
  constructor(iface, serverListId, refreshBtnId) {
    this.ids = {serverListId, refreshBtnId};

    iface.addObject(this, 'serverListing', ['flushElements', 'addElements']);
    this.iface = iface;
  }

  /**
   * Initializes Server List DOM Element
   */
  initialize() {
    this.serverListing = document.getElementById(this.ids.serverListId);
    this.refreshBtn = document.getElementById(this.ids.refreshBtnId);
    this.registerEvents();
  }

  /**
   * Registers events associated with server list UI
   */
  registerEvents() {
    this.registerRefreshEvent();
  }

  /**
   * Registers event for pushing the refresh button
   */
  registerRefreshEvent() {
    this.refreshBtn.addEventListener('click', () => {
      this.iface.callMethod('listServers', 'listServers');
    });
  }

  /**
   * Removes all elements currently in the server listing
   */
  flushElements() {
    this.serverListing.innerHTML = '';
  }

  /**
   * Populates servers from a given array of games
   * @param {array} array Array of available games
   */
  addElements(array) {
    for (let server of array) {
      const name = server['name'];
      const playerAmount = server['userCount'];

      let serverDiv = document.createElement('div');
      let nameSpan = document.createElement('span');
      let rightAlignDiv = document.createElement('div');
      let onlineDot = document.createElement('div');
      let playerCountSpan = document.createElement('span');
      let playerCountStaticSpan = document.createElement('span');
      let joinButton = document.createElement('button');
      serverDiv.className = 'server';
      nameSpan.className = 'server-name';
      rightAlignDiv.className = 'right-aligned-items';
      onlineDot.className = 'player-count-dot';
      playerCountSpan.className = 'player-count';
      playerCountStaticSpan.className = 'player-count-static';
      joinButton.className = 'btn join-btn';
      joinButton.id = 'join';
      nameSpan.textContent = name;
      playerCountSpan.textContent = playerAmount;
      playerCountStaticSpan.textContent = 'Spieler online';
      joinButton.textContent = 'Beitreten';
      joinButton.addEventListener('click', () => {
        this.iface.callMethod('login', 'showLogin', name);
      });

      rightAlignDiv.appendChild(onlineDot);
      rightAlignDiv.appendChild(playerCountSpan);
      rightAlignDiv.appendChild(playerCountStaticSpan);
      rightAlignDiv.appendChild(joinButton);
      serverDiv.appendChild(nameSpan);
      serverDiv.appendChild(rightAlignDiv);
      this.serverListing.appendChild(serverDiv);
    }
  }
}