From e5ad902e091cf13886bf915a9e565c43aef41dfb Mon Sep 17 00:00:00 2001 From: Andrio Celos Date: Sat, 1 Oct 2022 16:27:10 +1000 Subject: [PATCH] Client: Allow the API URL to be changed --- .gitignore | 1 + TableturfBattleClient/index.html | 3 ++- TableturfBattleClient/src/CardDatabase.ts | 2 +- TableturfBattleClient/src/Config.ts | 5 +++++ TableturfBattleClient/src/Pages/GamePage.ts | 6 +++--- TableturfBattleClient/src/Pages/LobbyPage.ts | 2 +- TableturfBattleClient/src/Pages/PreGamePage.ts | 8 ++++---- 7 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 TableturfBattleClient/src/Config.ts diff --git a/.gitignore b/.gitignore index 971b55e..13dcc59 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ assets/ build/ +config/ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/TableturfBattleClient/index.html b/TableturfBattleClient/index.html index e263e22..5527326 100644 --- a/TableturfBattleClient/index.html +++ b/TableturfBattleClient/index.html @@ -4,6 +4,7 @@ Tableturf Battle +
This application requires JavaScript.
@@ -105,6 +106,6 @@ - + diff --git a/TableturfBattleClient/src/CardDatabase.ts b/TableturfBattleClient/src/CardDatabase.ts index 38fae53..af2affd 100644 --- a/TableturfBattleClient/src/CardDatabase.ts +++ b/TableturfBattleClient/src/CardDatabase.ts @@ -7,7 +7,7 @@ const cardDatabase = { return; } const cardListRequest = new XMLHttpRequest(); - cardListRequest.open('GET', 'http://localhost:3333/api/cards'); + cardListRequest.open('GET', `${config.apiBaseUrl}/cards`); cardListRequest.addEventListener('load', e => { const cards = [ ]; if (cardListRequest.status == 200) { diff --git a/TableturfBattleClient/src/Config.ts b/TableturfBattleClient/src/Config.ts new file mode 100644 index 0000000..ae6dde8 --- /dev/null +++ b/TableturfBattleClient/src/Config.ts @@ -0,0 +1,5 @@ +interface Config { + apiBaseUrl: string +} + +var config: Config; diff --git a/TableturfBattleClient/src/Pages/GamePage.ts b/TableturfBattleClient/src/Pages/GamePage.ts index ba2d02c..6dd0cc6 100644 --- a/TableturfBattleClient/src/Pages/GamePage.ts +++ b/TableturfBattleClient/src/Pages/GamePage.ts @@ -200,7 +200,7 @@ function updateHand(cards: any[]) { canPlay = false; // Send the play to the server. let req = new XMLHttpRequest(); - req.open('POST', `http://localhost:3333/api/games/${currentGame!.id}/play`); + req.open('POST', `${config.apiBaseUrl}/games/${currentGame!.id}/play`); req.addEventListener('load', e => { if (req.status == 204) { } @@ -227,7 +227,7 @@ function updateHand(cards: any[]) { (document.getElementById('redrawYesButton') as HTMLButtonElement).addEventListener('click', redrawButton_click); function redrawButton_click(e: MouseEvent) { let req = new XMLHttpRequest(); - req.open('POST', `http://localhost:3333/api/games/${currentGame!.id}/redraw`); + req.open('POST', `${config.apiBaseUrl}/games/${currentGame!.id}/redraw`); let data = new URLSearchParams(); data.append('clientToken', clientToken); data.append('redraw', (e.target as HTMLButtonElement).dataset.redraw!); @@ -292,7 +292,7 @@ board.onclick = (x, y) => { canPlay = false; // Send the play to the server. let req = new XMLHttpRequest(); - req.open('POST', `http://localhost:3333/api/games/${currentGame.id}/play`); + req.open('POST', `${config.apiBaseUrl}/games/${currentGame.id}/play`); req.addEventListener('load', e => { if (req.status != 204) { alert(req.responseText); diff --git a/TableturfBattleClient/src/Pages/LobbyPage.ts b/TableturfBattleClient/src/Pages/LobbyPage.ts index ce5f0f8..d4af68d 100644 --- a/TableturfBattleClient/src/Pages/LobbyPage.ts +++ b/TableturfBattleClient/src/Pages/LobbyPage.ts @@ -25,7 +25,7 @@ function updatePlayerListItem(playerIndex: number) { submitDeckButton.addEventListener('click', e => { let req = new XMLHttpRequest(); - req.open('POST', `http://localhost:3333/api/games/${currentGame!.id}/chooseDeck`); + req.open('POST', `${config.apiBaseUrl}/games/${currentGame!.id}/chooseDeck`); req.addEventListener('load', e => { if (req.status == 204) { showSection('lobby'); diff --git a/TableturfBattleClient/src/Pages/PreGamePage.ts b/TableturfBattleClient/src/Pages/PreGamePage.ts index 15f6e37..5cb5364 100644 --- a/TableturfBattleClient/src/Pages/PreGamePage.ts +++ b/TableturfBattleClient/src/Pages/PreGamePage.ts @@ -6,7 +6,7 @@ newGameButton.addEventListener('click', e => { window.localStorage.setItem('name', name); let request = new XMLHttpRequest(); - request.open('POST', 'http://localhost:3333/api/games/new'); + request.open('POST', `${config.apiBaseUrl}/games/new`); request.addEventListener('load', e => { if (request.status == 200) { let response = JSON.parse(request.responseText); @@ -36,7 +36,7 @@ function tryJoinGame(name: string, idOrUrl: string) { } let request = new XMLHttpRequest(); - request.open('POST', `http://localhost:3333/api/games/${gameID}/join`); + request.open('POST', `${config.apiBaseUrl}/games/${gameID}/join`); request.addEventListener('load', e => { if (request.status == 200) { let response = JSON.parse(request.responseText); @@ -61,10 +61,10 @@ function getGameInfo(gameID: string, myPlayerIndex: number | null) { board.playerIndex = myPlayerIndex; window.location.hash = `#${gameID}`; - const webSocket = new WebSocket(`ws://localhost:3333/api/websocket?gameID=${gameID}&clientToken=${clientToken}`); + const webSocket = new WebSocket(`${config.apiBaseUrl.replace(/(http)(s)?\:\/\//, 'ws$2://')}/websocket?gameID=${gameID}&clientToken=${clientToken}`); webSocket.addEventListener('open', e => { const request2 = new XMLHttpRequest(); - request2.open('GET', `http://localhost:3333/api/games/${gameID}/playerData/${clientToken}`); + request2.open('GET', `${config.apiBaseUrl}/games/${gameID}/playerData/${clientToken}`); request2.addEventListener('load', e => { if (request2.status == 200) { const response = JSON.parse(request2.responseText);