Schafkopf-Server/Public/api.js

163 lines
5.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-12-25 16:53:58 +01:00
var useEnglishTexts = false
2021-12-23 11:16:03 +01:00
function webSocketPath() {
const prefix = (window.location.protocol === "https:") ? "wss://" : "ws://"
return prefix + window.location.host + apiPath + "/session/start"
}
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)
}
2021-12-23 11:16:03 +01:00
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")
2021-12-23 10:07:17 +01:00
case 404: // Not found
throw Error("Path not found")
2021-11-28 23:59:24 +01:00
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;
}
return JSON.parse(text);
2021-12-23 09:38:23 +01:00
}
2021-12-25 16:53:58 +01:00
// The state identifiers for all player states
const stateCanDouble = "canDouble"
const stateDidDouble = "doubled"
const stateIsDisconnected = "offline"
const stateMustBid = "bidder"
const stateDidFold = "fold"
const stateDidBid = "bid"
const stateIsGameSelector = "selects"
const stateIsWeddingOfferer = "wedding"
const stateIsCalled = "called"
const stateDidRaise = "raised"
const stateLeadsGame = "leads"
const stateIsWinner = "winner"
const stateIsLooser = "looser"
function convertStateToString(state) {
switch (state) {
case stateCanDouble:
return useEnglishTexts ? "Can double" : "Kann legen"
case stateDidDouble:
return useEnglishTexts ? "Doubled" : "Leger"
case stateIsDisconnected:
return useEnglishTexts ? "Disconnected" : "Abwesend"
case stateMustBid:
return useEnglishTexts ? "Must bid": "Muss ansagen"
case stateDidFold:
return useEnglishTexts ? "Fold" : "Weg"
case stateDidBid:
return useEnglishTexts ? "Wants to play" : "Spielt"
case stateIsGameSelector:
return useEnglishTexts ? "Selects game" : "Wählt Spiel"
case stateIsWeddingOfferer:
return useEnglishTexts ? "Offers wedding" : "Hochzeit"
case stateIsCalled:
return useEnglishTexts ? "Called" : "Gerufen"
case stateDidRaise:
return useEnglishTexts ? "Raised" : "Schuss"
case stateLeadsGame:
return useEnglishTexts ? "Player" : "Spieler"
case stateIsWinner:
return useEnglishTexts ? "Winner" : "Hat gewonnen"
case stateIsLooser:
return useEnglishTexts ? "Looser" : "Hat verloren"
default:
return state
}
}