diff options
-rw-r--r-- | src/renderer/gameserver/data.ts | 10 | ||||
-rw-r--r-- | src/renderer/gameserver/onlineCount.ts | 86 | ||||
-rw-r--r-- | src/renderer/gameserver/server.ts | 12 |
3 files changed, 88 insertions, 20 deletions
diff --git a/src/renderer/gameserver/data.ts b/src/renderer/gameserver/data.ts index b994215..a55b745 100644 --- a/src/renderer/gameserver/data.ts +++ b/src/renderer/gameserver/data.ts @@ -3,6 +3,7 @@ import {socialLink} from './server'; import GameServer from './server'; import GameServerProfile from './profile'; import { NewsType } from './news'; +import { OnlineListParser } from './onlineCount'; const TMW2 = new GameServer( @@ -29,7 +30,8 @@ const TMW2 = new GameServer( new socialLink("fab fa-twitter","Twitter","https://twitter.com/TheManaWorld2"), new socialLink("fab fa-gitlab","Gitlab","https://gitlab.com/TMW2"), ], - "https://tmw2.org/legal" + "https://tmw2.org/legal", + {parser:OnlineListParser.EXAMPLE_DATA,url:"example"} ); const Evol = new GameServer( @@ -48,7 +50,8 @@ const Evol = new GameServer( [ new socialLink("fas fa-home","Website","https://evolonline.org/"), ], - "http://wiki.evolonline.org/rules/gamerules" + "http://wiki.evolonline.org/rules/gamerules", + null ); const TMW = new GameServer( @@ -68,7 +71,8 @@ const TMW = new GameServer( new socialLink("fas fa-home","Website","https://www.themanaworld.org/"), new socialLink("fas fa-users","Forum","https://forums.themanaworld.org/"), ], - "https://wiki.themanaworld.org/index.php/The_Mana_World:Terms_and_Conditions" + "https://wiki.themanaworld.org/index.php/The_Mana_World:Terms_and_Conditions", + {parser:OnlineListParser.TMW, url:"https://server.themanaworld.org/"} ); export const GameServers = [ diff --git a/src/renderer/gameserver/onlineCount.ts b/src/renderer/gameserver/onlineCount.ts index af8a7b6..d78fa96 100644 --- a/src/renderer/gameserver/onlineCount.ts +++ b/src/renderer/gameserver/onlineCount.ts @@ -1,18 +1,19 @@ import GameServer from './server'; -const MAX_SHOWN_NAMES = 3; +const MAX_SHOWN_NAMES = 5; -export function makeOnlineCounterList(server:GameServer):HTMLElement{ +export async function makeOnlineCounterList(server:GameServer):Promise<HTMLElement>{ - const playerList = [ - 'LawnCable', - 'Saulc GM', - 'Crazyfefe', - 'Jesus Saves', - 'DUSTMAN' - ]; - - return generateHTML(playerList); + try { + const playerList = await fetchOnlineList(server); + return generateHTML(playerList); + } catch (err) { + console.log(err); + const OnlineCounterContainer = document.createElement('span'); + OnlineCounterContainer.classList.add("onlineCounter"); + OnlineCounterContainer.innerHTML = `<i class="fas fa-times fa-xs"></i>Error:<br>${err}`; + return OnlineCounterContainer; + } } @@ -49,3 +50,66 @@ function generateHTML(onlinePlayers:string[]):HTMLElement{ OnlineCounterContainer.appendChild(PlayerList); return OnlineCounterContainer; } + +export enum OnlineListParser { + Invalid, + TMW, + TMW2API, + EXAMPLE_DATA, +}; + +async function fetchOnlineList(server:GameServer):Promise<string[]>{ + + if(!server.OnlineList)throw new Error("No Online list was\n specified for this Server"); + + if(server.OnlineList.parser && server.OnlineList.url){ + switch(server.OnlineList.parser){ + case OnlineListParser.EXAMPLE_DATA: + return [ + 'LawnCable', + 'Saulc GM', + 'Crazyfefe', + 'Jesus Saves', + 'DUSTMAN' + ]; + case OnlineListParser.TMW: + return tmwParser(await request(server.OnlineList.url)); + default: + throw new Error("Online List Parser is unknown"); + } + } else { + throw new Error("Online List wasn't configured right"); + } +} + + +function tmwParser(rawData:string):string[]{ + let stringArray:string[]=[]; + rawData.replace(/<td>(.+?)<\/td>/g, (x:string, content:string) =>{ + stringArray.push(content.replace(/<\/{0,1}.+?>/g, "")); + return ""; + }); + return stringArray; +} + + +function request(url:string):Promise<string>{ + return new Promise((res, rej)=>{ + var xhr = new XMLHttpRequest(); + xhr.open('GET', url); + xhr.addEventListener("error", (ev)=>{ + console.log("Probably a network error:", ev); + rej(new Error("Probably a network error")); + }); + xhr.onload = function() { + if (xhr.status === 200) { + res(xhr.responseText); + } + else { + console.log(`xhr.status: ${xhr.status} != 200`); + rej(new Error("Probably a network error")); + } + }; + xhr.send(); + }); +} diff --git a/src/renderer/gameserver/server.ts b/src/renderer/gameserver/server.ts index ed462c4..4c54235 100644 --- a/src/renderer/gameserver/server.ts +++ b/src/renderer/gameserver/server.ts @@ -2,8 +2,7 @@ import GameServerProfile from './profile'; import { shell, ipcRenderer } from 'electron'; import { switchPage } from '../customEvents'; import { News, NewsType } from './news'; -import { makeOnlineCounterList } from './onlineCount'; - +import { makeOnlineCounterList, OnlineListParser } from './onlineCount'; export default class GameServer { constructor( @@ -16,7 +15,8 @@ export default class GameServer { public backgrounds:{isVideo:boolean,file:string}[], public icon:string, public socialLinks:socialLink[], - public TOSLink:string + public TOSLink:string, + public OnlineList:{parser:OnlineListParser,url:string}, ){} getMenuEntry():HTMLElement{ @@ -48,15 +48,15 @@ export default class GameServer { let onlineBoxActive:boolean = false; let onlineBoxSchouldBeActive:boolean = false; - let updateOnlineContainer = () => { + let updateOnlineContainer = async () => { if(onlineBoxSchouldBeActive == onlineBoxActive)return; + onlineBoxActive = onlineBoxSchouldBeActive; while (OnlineCounterContainer.firstChild) { OnlineCounterContainer.removeChild(OnlineCounterContainer.firstChild); } if(onlineBoxSchouldBeActive){ - OnlineCounterContainer.appendChild(makeOnlineCounterList(this)); + OnlineCounterContainer.appendChild(await makeOnlineCounterList(this)); } - onlineBoxActive = onlineBoxSchouldBeActive; console.log(onlineBoxSchouldBeActive); } |