Schafkopf-Server/Public/game.js

268 lines
6.3 KiB
JavaScript
Raw Normal View History

// The web socket to connect to the server
var socket = null;
2021-12-03 18:03:29 +01:00
var tableId = null;
var activePlayer = null;
2021-12-25 16:53:58 +01:00
const createTableRow = '<div class="table-row">'
+ '<button class="table-join-btn" onclick="createTable()">Create</button>'
+ '<input type="text" id="table-name-field" name="tablename" placeholder="Create new table..." required>'
+ '<input type="checkbox" id="table-public-checkbox" name="public-table" checked="checked">'
+ '<span id="table-public-label">Public</span>'
+ '</div>'
function closeSocketIfNeeded() {
if (socket) {
socket.close()
2021-11-28 23:59:24 +01:00
didCloseSocket()
}
}
2021-12-01 22:50:42 +01:00
function didLeaveTable() {
2021-12-03 18:03:29 +01:00
tableId = null
2021-12-01 22:50:42 +01:00
}
2021-11-28 23:59:24 +01:00
function didCloseSocket() {
socket = null
2021-11-27 11:59:13 +01:00
}
2021-11-28 23:59:24 +01:00
function setTableId(table) {
tableId = table
}
2021-12-03 18:03:29 +01:00
function showLoginWithError(error) {
showLoginWithText(error.message)
}
function showBlankLogin() {
showLoginWithText("")
}
function showLoginWithText(text) {
closeSocketIfNeeded()
2021-12-01 22:50:42 +01:00
didLeaveTable()
clearLoginPassword()
clearLoginName()
deleteSessionToken()
2021-12-01 22:50:42 +01:00
showLoginElements()
setLoginError(text)
}
2021-12-03 18:03:29 +01:00
function showTableOrList(table) {
if (table == null) {
didLeaveTable()
showTableListElements()
closeSocketIfNeeded()
refreshTables()
// TODO: Show table list, refresh
2021-11-28 23:59:24 +01:00
return
}
2021-12-03 18:03:29 +01:00
const token = getSessionToken()
if (token == null) {
showBlankLogin()
2021-11-28 23:59:24 +01:00
return
}
2021-12-03 18:03:29 +01:00
clearTableName()
setTableId(table.id)
showGameElements()
updateTableInfo(table)
openSocket(token)
}
2021-11-27 11:59:13 +01:00
2021-12-03 18:03:29 +01:00
function registerUser() {
createSession(performRegisterPlayerRequest)
}
2021-11-28 23:59:24 +01:00
function loginUser() {
2021-12-03 18:03:29 +01:00
createSession(performLoginPlayerRequest)
}
function createSession(inputFunction) {
2021-11-28 23:59:24 +01:00
const username = getLoginName()
const password = getLoginPassword()
if (username == "") {
2021-12-03 18:03:29 +01:00
showLoginWithText("Please enter your user name")
2021-11-28 23:59:24 +01:00
return
}
if (password == "") {
2021-12-03 18:03:29 +01:00
showLoginWithText("Please enter a password")
2021-11-28 23:59:24 +01:00
return
}
2021-12-03 18:03:29 +01:00
inputFunction(username, password)
2021-11-28 23:59:24 +01:00
.then(function(token) {
setSessionToken(token)
setPlayerName(username)
loadCurrentTable(token)
2021-12-03 18:03:29 +01:00
}).catch(showLoginWithError)
}
2021-11-28 23:59:24 +01:00
function logoutUser() {
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
}
2021-12-03 18:03:29 +01:00
performLogoutRequest(token)
.then(function() {
showBlankLogin()
}).catch(showLoginWithError)
}
function deletePlayerAccount() {
const name = getPlayerName()
const password = getLoginPassword()
performDeletePlayerRequest(name, password)
.then(showBlankLogin)
.catch(showLoginWithError)
}
2021-11-28 23:59:24 +01:00
function loadExistingSession() {
2021-12-07 09:10:28 +01:00
if (debugMode) {
showDebugLogins()
}
2021-11-28 23:59:24 +01:00
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
2021-11-28 23:59:24 +01:00
}
2021-12-03 18:03:29 +01:00
resumeSessionRequest(token)
.then(function(name) {
setPlayerName(name)
loadCurrentTable(token)
}).catch(showLoginWithError)
2021-11-28 23:59:24 +01:00
}
function loadCurrentTable(token) {
performGetCurrentTableRequest(token)
2021-12-03 18:03:29 +01:00
.then(showTableOrList)
.catch(showLoginWithError)
}
2021-11-28 23:59:24 +01:00
function createTable() {
const tableName = getTableName()
if (tableName == "") {
return
}
2021-11-28 23:59:24 +01:00
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
}
const isVisible = getTableVisibility()
performCreateTableRequest(token, tableName, isVisible)
.then(showTableOrList)
.catch(showLoginWithError)
}
2021-11-28 23:59:24 +01:00
function joinTable(tableId) {
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
2021-11-27 11:59:13 +01:00
}
2021-12-03 18:03:29 +01:00
performJoinTableRequest(tableId, token)
.then(showTableOrList)
.catch(showLoginWithError)
2021-11-27 11:59:13 +01:00
}
2021-11-30 20:55:25 +01:00
function leaveTable() {
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
2021-11-30 20:55:25 +01:00
}
2021-12-03 18:03:29 +01:00
performLeaveTableRequest(token)
.then(function() {
showTableOrList(null)
})
.catch(showLoginWithError)
2021-11-30 20:55:25 +01:00
}
2021-11-28 23:59:24 +01:00
function openSocket(token) {
2021-12-23 11:16:03 +01:00
const socketPath = webSocketPath()
socket = new WebSocket(socketPath)
2021-12-03 18:03:29 +01:00
socket.onopen = function(e) {
socket.send(token);
2021-12-01 22:50:42 +01:00
showConnectedState()
};
socket.onmessage = function(event) {
2021-12-03 18:03:29 +01:00
const table = convertJsonResponse(event.data)
updateTableInfo(table)
};
socket.onclose = function(event) {
if (event.wasClean) {
2021-12-03 18:03:29 +01:00
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
2021-11-28 23:59:24 +01:00
// TODO: Retry several times to open socket,
// stop after too many failed attempts
}
2021-12-01 22:50:42 +01:00
didCloseSocket()
showDisconnectedState()
};
socket.onerror = function(error) {
// error.message
};
2021-11-27 11:59:13 +01:00
}
function refreshTables() {
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
}
2021-12-03 18:03:29 +01:00
performGetPublicTablesRequest(token)
.then(processTableList)
.catch(showLoginWithError)
2021-11-27 11:59:13 +01:00
}
function processTableList(tables) {
2021-12-25 16:53:58 +01:00
var html = createTableRow
2021-11-28 23:59:24 +01:00
for (let i = 0, len = tables.length; i < len; i++) {
tableInfo = tables[i]
2021-11-28 23:59:24 +01:00
html += "<div class=\"table-row\">"
if (tableInfo.players.length < 4) {
html += "<button class=\"table-join-btn\" onclick=\"joinTable('" + tableInfo.id + "')\">Join</button>"
} else {
html += "<button class=\"table-join-btn\" disabled>Full</button>"
}
html += "<div class=\"table-title\">" + tableInfo.name + "</div>"
2021-12-01 22:50:42 +01:00
if (tableInfo.players.length == 0) {
html += "<div class=\"table-subtitle\">No players</div>"
} else {
2021-12-03 18:03:29 +01:00
const names = tableInfo.players.join(", ")
html += "<div class=\"table-subtitle\">" + names + "</div>"
2021-12-01 22:50:42 +01:00
}
2021-11-28 23:59:24 +01:00
html += "</div>" // table-row
}
2021-12-03 18:03:29 +01:00
setTableListContent(html)
2021-12-01 22:50:42 +01:00
}
function dealCards() {
const token = getSessionToken()
2021-12-03 18:03:29 +01:00
if (token == null) {
showBlankLogin()
return
2021-12-01 22:50:42 +01:00
}
2021-12-03 18:03:29 +01:00
performDealCardsRequest(token)
.catch(showLoginWithError)
2021-12-06 18:27:52 +01:00
}
function playCard(card) {
const token = getSessionToken()
if (token == null) {
showBlankLogin()
return
}
performPlayCardRequest(token, card)
.catch(function(error) {
console.log(error)
})
}