2021-11-28 15:53:47 +01:00
|
|
|
|
|
|
|
// 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-11-28 15:53:47 +01:00
|
|
|
|
|
|
|
function closeSocketIfNeeded() {
|
|
|
|
if (socket) {
|
|
|
|
socket.close()
|
2021-11-28 23:59:24 +01:00
|
|
|
didCloseSocket()
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-28 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2021-12-03 18:03:29 +01:00
|
|
|
function showLoginWithError(error) {
|
|
|
|
showLoginWithText(error.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
function showBlankLogin() {
|
|
|
|
showLoginWithText("")
|
|
|
|
}
|
|
|
|
|
|
|
|
function showLoginWithText(text) {
|
2021-11-28 15:53:47 +01:00
|
|
|
closeSocketIfNeeded()
|
2021-12-01 22:50:42 +01:00
|
|
|
didLeaveTable()
|
2021-11-28 15:53:47 +01:00
|
|
|
clearLoginPassword()
|
|
|
|
clearLoginName()
|
|
|
|
deleteSessionToken()
|
2021-12-01 22:50:42 +01:00
|
|
|
showLoginElements()
|
2021-11-28 15:53:47 +01:00
|
|
|
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-28 15:53:47 +01:00
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-12-03 18:03:29 +01:00
|
|
|
function registerUser() {
|
|
|
|
createSession(performRegisterPlayerRequest)
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
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 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function logoutUser() {
|
2021-11-28 15:53:47 +01:00
|
|
|
const token = getSessionToken()
|
2021-12-03 18:03:29 +01:00
|
|
|
if (token == null) {
|
|
|
|
showBlankLogin()
|
|
|
|
return
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
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 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function loadExistingSession() {
|
|
|
|
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 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function createTable() {
|
|
|
|
const tableName = getTableName()
|
|
|
|
if (tableName == "") {
|
|
|
|
return
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
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 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function joinTable(tableId) {
|
2021-11-28 15:53:47 +01:00
|
|
|
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-11-28 15:53:47 +01:00
|
|
|
socket = new WebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/session/start")
|
2021-12-03 18:03:29 +01:00
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
socket.onopen = function(e) {
|
|
|
|
socket.send(token);
|
2021-12-01 22:50:42 +01:00
|
|
|
showConnectedState()
|
2021-11-28 15:53:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function(event) {
|
2021-12-03 18:03:29 +01:00
|
|
|
const table = convertJsonResponse(event.data)
|
|
|
|
updateTableInfo(table)
|
2021-11-28 15:53:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onclose = function(event) {
|
|
|
|
if (event.wasClean) {
|
2021-12-03 18:03:29 +01:00
|
|
|
|
2021-11-28 15:53:47 +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-11-28 15:53:47 +01:00
|
|
|
}
|
2021-12-01 22:50:42 +01:00
|
|
|
didCloseSocket()
|
|
|
|
showDisconnectedState()
|
2021-11-28 15:53:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onerror = function(error) {
|
|
|
|
// error.message
|
|
|
|
};
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
function refreshTables() {
|
|
|
|
const token = getSessionToken()
|
2021-12-03 18:03:29 +01:00
|
|
|
if (token == null) {
|
|
|
|
showBlankLogin()
|
|
|
|
return
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
2021-12-03 18:03:29 +01:00
|
|
|
performGetPublicTablesRequest(token)
|
|
|
|
.then(processTableList)
|
|
|
|
.catch(showLoginWithError)
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
2021-11-28 15:53:47 +01:00
|
|
|
|
|
|
|
function processTableList(tables) {
|
|
|
|
var html = ""
|
2021-11-28 23:59:24 +01:00
|
|
|
for (let i = 0, len = tables.length; i < len; i++) {
|
2021-11-28 15:53:47 +01:00
|
|
|
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-11-28 15:53:47 +01:00
|
|
|
}
|
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-11-28 15:53:47 +01:00
|
|
|
}
|