summaryrefslogtreecommitdiff
path: root/src/renderer/serverView.ts
blob: d502cc50bd7b29588c0ebcfc76c1e6374b60d34b (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
import { ipcRenderer, shell } from "electron";
import GameServer from "./gameserver/server";
import { switchPage } from "./customEvents";

import CheckNAcceptTOS from "./gameserver/TOSCheck";
import { acceptLastTOS } from "./gameserver/TOSCheck";

let GameServerList: GameServer[] = null;
let SelectedGameserver: GameServer = null;
const playBtn: HTMLButtonElement = document.getElementById(
  "play"
) as HTMLButtonElement;

const TOSDialog: HTMLDialogElement = document.getElementById(
  "TOSDialog"
) as HTMLDialogElement;

playBtn.addEventListener("click", async () => {
  if (await CheckNAcceptTOS(SelectedGameserver)) {
    SelectedGameserver.play();
  } else {
    //Open Please accept TOS window
    TOSDialog.showModal();
  }
});
const TOSCancel: HTMLButtonElement = document.getElementById(
  "TOSCancel"
) as HTMLButtonElement;
TOSCancel.addEventListener("click", function () {
  TOSDialog.close();
});
const TOSOpen: HTMLButtonElement = document.getElementById(
  "TOSOpen"
) as HTMLButtonElement;
TOSOpen.addEventListener("click", function (e) {
  e.preventDefault();
  shell.openExternal(SelectedGameserver.TOSLink);
});
const TOSAccept: HTMLButtonElement = document.getElementById(
  "TOSAccept"
) as HTMLButtonElement;
TOSAccept.addEventListener("click", function () {
  acceptLastTOS(SelectedGameserver);
  SelectedGameserver.play();
  TOSDialog.close();
});

const sidebarReference = document.getElementById("sidebar");
let clickableMenueEntries: HTMLElement[] = [];
function updateView() {
  // This is for updating the ui to new data

  if (GameServerList) {
    // Clear every thing out
    while (sidebarReference.firstChild) {
      sidebarReference.removeChild(sidebarReference.firstChild);
    }

    GameServerList.forEach((server) => {
      sidebarReference.appendChild(server.getMenuEntry());
    });

    //Switch page according localstorage:
    if (GameServerList.length != 0) {
      const selectedServer = localStorage.getItem("selected_server");
      if (selectedServer && selectedServer !== null) {
        if (
          GameServerList.filter((server) => server.name == selectedServer)
            .length == 0
        ) {
          localStorage.removeItem("selected_server");
          switchPage("SERVER", GameServerList[0].name, "INFO");
        } else {
          switchPage("SERVER", selectedServer, "INFO");
        }
      } else {
        //TODO ask if is on special page like global settings first
        switchPage("SERVER", GameServerList[0].name, "INFO");
      }
    }
  }
}

const serverPage = document.getElementById("serverPage");
const switchPageEvent = document.createElement("span");
switchPageEvent.classList.add("switch-page-event");
switchPageEvent.addEventListener("site-changed", (event: CustomEvent) => {
  if (event.detail.sitetype === "SERVER") {
    SelectedGameserver = GameServerList.filter(
      (server) => server.name == event.detail.page
    )[0];
    localStorage.setItem("selected_server", SelectedGameserver.name);

    while (serverPage.firstChild) {
      serverPage.removeChild(serverPage.firstChild);
    }
    let page = SelectedGameserver.pageController.getPage(event.detail.subPage);
    serverPage.appendChild(page);
    // page.classList.add('animated');
    // page.classList.add('fadeIn');
  } else {
    SelectedGameserver == null;
  }

  if (typeof SelectedGameserver === "undefined" || SelectedGameserver == null) {
    // No gameserver selected
    playBtn.hidden = true;
  } else {
    // A gameserver is selected
    playBtn.innerText = `Play ${SelectedGameserver.menuName}`;
    playBtn.hidden = false;
  }
  setBackground();
});
document.getElementById("topbar").appendChild(switchPageEvent);

let peviousSelectedGameserver: any = null;
const contentBackground = document.getElementById("contentBackground");
function setBackground() {
  if (peviousSelectedGameserver !== SelectedGameserver) {
    while (contentBackground.firstChild) {
      contentBackground.removeChild(contentBackground.firstChild);
    }
    if (
      typeof SelectedGameserver === "undefined" ||
      SelectedGameserver == null
    ) {
      contentBackground.hidden = true;
      contentBackground.classList.add("hidden");
    } else {
      if (
        SelectedGameserver.backgrounds[0] &&
        !SelectedGameserver.backgrounds[0].isVideo
      ) {
        const background1 = document.createElement("img");
        background1.src = `./media/server/${SelectedGameserver.backgrounds[0].file}`;
        contentBackground.appendChild(background1);
        background1.classList.add("animated");
        background1.classList.add("fadeIn");
      }
      contentBackground.appendChild(document.createElement("div"));
      contentBackground.hidden = false;
      contentBackground.classList.remove("hidden");
    }
  }
  peviousSelectedGameserver = SelectedGameserver;
}

ipcRenderer.on("status-update", (event: any, status: any) => {
  // Everything that isnt server view related is in index.ts
  playBtn.disabled = status.playing;
});

import { GameServers } from "./gameserver/data";
GameServerList = GameServers;
updateView();