Schafkopf-Server/Public/api.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-11-28 23:59:24 +01:00
/**
* This file acts as an abstraction layer to the server.
*/
2021-12-23 09:38:23 +01:00
const apiPath = "/schafkopf"
2021-11-28 23:59:24 +01:00
async function performRegisterPlayerRequest(name, password){
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/register/" + name, { method: 'POST', body: password })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
}
async function performDeletePlayerRequest(name, password){
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/delete/" + name, { method: 'POST', body: password })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
.then(function(value) {})
}
async function performLoginPlayerRequest(name, password) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/login/" + name, { method: 'POST', body: password })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
}
async function performLogoutRequest(token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/logout", { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
.then(function(value) {})
}
async function resumeSessionRequest(token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/resume", { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
}
async function performGetCurrentTableRequest(token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/table", { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
2021-12-03 18:03:29 +01:00
.then(convertJsonResponse)
2021-11-28 23:59:24 +01:00
}
async function performCreateTableRequest(token, name, visibility) {
const vis = visibility ? "public" : "private";
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/table/create/" + vis + "/" + name, { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
2021-12-03 18:03:29 +01:00
.then(convertJsonResponse)
2021-11-28 23:59:24 +01:00
}
async function performJoinTableRequest(tableId, token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/table/join/" + tableId, { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
2021-12-03 18:03:29 +01:00
.then(convertJsonResponse)
2021-11-28 23:59:24 +01:00
}
async function performGetPublicTablesRequest(token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/tables/public", { method: 'POST', body: token })
2021-11-28 23:59:24 +01:00
.then(convertServerResponse)
2021-12-03 18:03:29 +01:00
.then(convertJsonResponse)
2021-11-28 23:59:24 +01:00
}
2021-11-30 20:55:25 +01:00
async function performLeaveTableRequest(token) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/table/leave", { method: 'POST', body: token })
2021-11-30 20:55:25 +01:00
.then(convertServerResponse)
2021-12-01 22:50:42 +01:00
.then(function(value) {})
}
2021-12-03 18:03:29 +01:00
async function performPlayerActionRequest(token, action) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/action/" + action, { method: 'POST', body: token })
2021-12-01 22:50:42 +01:00
.then(convertServerResponse)
2021-11-30 20:55:25 +01:00
}
2021-12-06 18:27:52 +01:00
async function performPlayCardRequest(token, card) {
2021-12-23 09:38:23 +01:00
return fetch(apiPath + "/player/card/" + card, { method: 'POST', body: token })
2021-12-06 18:27:52 +01:00
.then(convertServerResponse)
.then(function(value) {})
}
2021-11-28 23:59:24 +01:00
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)
}
2021-11-29 11:54:50 +01:00
}
2021-12-03 18:03:29 +01:00
function convertJsonResponse(text) {
if (text == "") {
return null;
}
2021-12-06 18:27:52 +01:00
console.log(text)
2021-12-03 18:03:29 +01:00
return JSON.parse(text);
2021-12-23 09:38:23 +01:00
}