2021-11-28 23:59:24 +01:00
|
|
|
/**
|
|
|
|
* This file acts as an abstraction layer to the server.
|
|
|
|
*/
|
|
|
|
|
2022-11-24 21:31:19 +01:00
|
|
|
/*
|
|
|
|
The path where the server is hosted, to provide the correct links.
|
|
|
|
|
|
|
|
If the server runs under https://example.com/schafkopf
|
|
|
|
then apiPath = "/schafkopf"
|
|
|
|
*/
|
2021-12-23 09:38:23 +01:00
|
|
|
const apiPath = "/schafkopf"
|
|
|
|
|
2021-12-25 16:53:58 +01:00
|
|
|
var useEnglishTexts = false
|
|
|
|
|
2022-10-12 19:55:22 +02:00
|
|
|
const headerKeyPassword = "password";
|
|
|
|
const headerKeyToken = "token";
|
|
|
|
const headerKeyName = "name";
|
|
|
|
const headerKeyMail = "email";
|
2022-10-12 21:53:16 +02:00
|
|
|
const headerKeyVisibility = "visibility";
|
|
|
|
const headerKeyAction = "action";
|
2022-10-12 19:55:22 +02:00
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:55:22 +02:00
|
|
|
async function performRegisterPlayerRequest(name, password, email) {
|
|
|
|
return fetch(apiPath + "/player/register", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyName]: name,
|
|
|
|
[headerKeyPassword]: password,
|
|
|
|
[headerKeyMail]: email
|
|
|
|
}
|
|
|
|
})
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(convertServerResponse)
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:16:03 +01:00
|
|
|
async function performDeletePlayerRequest(name, password) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/delete", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyName]: name,
|
|
|
|
[headerKeyPassword]: password,
|
|
|
|
}
|
|
|
|
})
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(convertServerResponse)
|
|
|
|
.then(function(value) {})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function performLoginPlayerRequest(name, password) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/login", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyName]: name,
|
|
|
|
[headerKeyPassword]: password,
|
|
|
|
}
|
|
|
|
})
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(convertServerResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function performLogoutRequest(token) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/logout", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyToken]: token },
|
|
|
|
})
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(convertServerResponse)
|
|
|
|
.then(function(value) {})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resumeSessionRequest(token) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/resume", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyToken]: token },
|
|
|
|
})
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(convertServerResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function performGetCurrentTableRequest(token) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/table", {
|
|
|
|
method: 'POST',
|
2022-10-12 22:02:13 +02:00
|
|
|
headers: { [headerKeyToken]: token },
|
2022-10-12 21:53:16 +02:00
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2022-10-12 19:55:22 +02:00
|
|
|
async function performRecoveryEmailRequest(name) {
|
|
|
|
return fetch(apiPath + "/player/password/reset", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyName] : name },
|
|
|
|
})
|
|
|
|
.then(convertServerResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function performResetPasswordRequest(token, password) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/password/new", {
|
2022-10-12 19:55:22 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyPassword] : password, [headerKeyToken] : token }
|
|
|
|
})
|
|
|
|
.then(convertServerResponse)
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
async function performCreateTableRequest(token, name, visibility) {
|
|
|
|
const vis = visibility ? "public" : "private";
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/table/create", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyVisibility]: vis,
|
|
|
|
[headerKeyToken]: token,
|
|
|
|
[headerKeyName]: name,
|
|
|
|
}
|
|
|
|
})
|
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) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/table/join", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyName]: tableId,
|
|
|
|
[headerKeyToken]: 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) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/tables/public", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyToken] : 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) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/table/leave", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [headerKeyToken] : 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) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/action", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyToken] : token,
|
|
|
|
[headerKeyAction] : action,
|
|
|
|
},
|
|
|
|
})
|
2021-12-01 22:50:42 +01:00
|
|
|
.then(convertServerResponse)
|
2022-10-12 21:53:16 +02:00
|
|
|
.then(function(value) {})
|
2021-11-30 20:55:25 +01:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:27:52 +01:00
|
|
|
async function performPlayCardRequest(token, card) {
|
2022-10-12 21:53:16 +02:00
|
|
|
return fetch(apiPath + "/player/card", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[headerKeyToken] : token,
|
|
|
|
[headerKeyAction] : card,
|
|
|
|
},
|
|
|
|
})
|
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
|
|
|
|
}
|
2022-10-12 19:55:22 +02:00
|
|
|
}
|