Client: Allow the API URL to be changed

This commit is contained in:
Andrio Celos
2022-10-01 16:27:10 +10:00
parent 37e13c0001
commit e5ad902e09
7 changed files with 17 additions and 10 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
assets/
build/
config/
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

View File

@@ -4,6 +4,7 @@
<meta charset="UTF-8"/>
<title>Tableturf Battle</title>
<link rel="stylesheet" href="tableturf.css"/>
<script src="config/config.js"></script>
</head>
<body>
<div id="noJSSection">This application requires JavaScript.</div>
@@ -105,6 +106,6 @@
<div id="errorModal" hidden>
<div id="errorModalBox">A communication error has occurred.</div>
</div>
<script src="build/tsbuild.js"></script>
<script src="build/tsbuild.js"></script>
</body>
</html>

View File

@@ -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) {

View File

@@ -0,0 +1,5 @@
interface Config {
apiBaseUrl: string
}
var config: Config;

View File

@@ -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);

View File

@@ -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');

View File

@@ -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);