81 lines
2.7 KiB
JavaScript
81 lines
2.7 KiB
JavaScript
/**
|
|
* This file acts as an abstraction layer to the server.
|
|
*/
|
|
|
|
async function performRegisterPlayerRequest(name, password){
|
|
return fetch("/player/register/" + name, { method: 'POST', body: password })
|
|
.then(convertServerResponse)
|
|
}
|
|
|
|
async function performDeletePlayerRequest(name, password){
|
|
return fetch("/player/delete/" + name, { method: 'POST', body: password })
|
|
.then(convertServerResponse)
|
|
.then(function(value) {})
|
|
}
|
|
|
|
async function performLoginPlayerRequest(name, password) {
|
|
return fetch("/player/login/" + name, { method: 'POST', body: password })
|
|
.then(convertServerResponse)
|
|
}
|
|
|
|
async function performLogoutRequest(token) {
|
|
return fetch("/player/logout", { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
.then(function(value) {})
|
|
}
|
|
|
|
async function resumeSessionRequest(token) {
|
|
return fetch("/player/resume", { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
}
|
|
|
|
async function performGetCurrentTableRequest(token) {
|
|
return fetch("player/table", { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
}
|
|
|
|
async function performCreateTableRequest(token, name, visibility) {
|
|
const vis = visibility ? "public" : "private";
|
|
return fetch("/table/create/" + vis + "/" + name, { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
}
|
|
|
|
async function performJoinTableRequest(tableId, token) {
|
|
return fetch("/table/join/" + tableId, { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
.then(function(value) {})
|
|
}
|
|
|
|
async function performGetPublicTablesRequest(token) {
|
|
return fetch("/tables/public", { method: 'POST', body: token })
|
|
.then(convertServerResponse)
|
|
.then(function(text) {
|
|
const decoded = atob(text)
|
|
return JSON.parse(decoded);
|
|
})
|
|
}
|
|
|
|
function convertServerResponse(response) {
|
|
switch (response.status) {
|
|
case 200: // Success
|
|
return response.text()
|
|
case 400: // Bad request
|
|
throw Error("The request was malformed")
|
|
case 401: // Unauthorized (Invalid session token)
|
|
throw Error("Please log in again")
|
|
case 403: // Forbidden
|
|
throw Error("Invalid username or password")
|
|
case 410: // Gone
|
|
throw Error("The table could not be found")
|
|
case 406: // notAcceptable
|
|
throw Error("The password or name is too long")
|
|
case 409: // Conflict
|
|
throw Error("A user with the same name is already registered")
|
|
case 417: // Expectation failed
|
|
throw Error("The table is already full and can't be joined")
|
|
case 424: // Failed dependency
|
|
throw Error("The request couldn't be completed")
|
|
default:
|
|
throw Error("Unexpected response: " + response.statusText)
|
|
}
|
|
} |