feat(ui): added js routing for create game
added js routing for create game, removed the form and button
This commit is contained in:
@@ -77,4 +77,47 @@
|
||||
})
|
||||
})
|
||||
})
|
||||
})()
|
||||
})()
|
||||
|
||||
function createGameJS() {
|
||||
let lobbyName = document.getElementById("lobbyname").value;
|
||||
if (lobbyName === "") {
|
||||
lobbyName = "DefaultLobby"
|
||||
}
|
||||
const playerAmount = document.getElementById("playeramount").value;
|
||||
const jsonObj = {
|
||||
lobbyname: lobbyName,
|
||||
playeramount: playerAmount
|
||||
}
|
||||
sendGameCreationRequest(jsonObj);
|
||||
}
|
||||
|
||||
function sendGameCreationRequest(dataObject) {
|
||||
const route = jsRoutes.controllers.MainMenuController.createGame();
|
||||
|
||||
fetch(route.url, {
|
||||
method: route.type,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(dataObject)
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.status === 'success') {
|
||||
// Redirect the user
|
||||
window.location.href = data.redirectUrl;
|
||||
} else {
|
||||
console.error("Game creation failed:", data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fetch error:', error);
|
||||
alert('Could not create game. Please try again.');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user