2021-11-28 15:53:47 +01:00
|
|
|
|
|
|
|
// The web socket to connect to the server
|
|
|
|
var socket = null;
|
2021-11-28 23:59:24 +01:00
|
|
|
var tableId = "";
|
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-11-28 23:59:24 +01:00
|
|
|
function didCloseSocket() {
|
|
|
|
socket = null
|
|
|
|
tableId = ""
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function showBlankLoginScreen(text) {
|
|
|
|
closeSocketIfNeeded()
|
|
|
|
clearLoginPassword()
|
|
|
|
clearLoginName()
|
|
|
|
deleteSessionToken()
|
|
|
|
showLoginWindow()
|
|
|
|
setLoginError(text)
|
|
|
|
}
|
|
|
|
|
2021-11-29 11:06:20 +01:00
|
|
|
function showTableList() {
|
|
|
|
showTableListElements()
|
|
|
|
hideLoginWindow()
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function showGame(tableId) {
|
|
|
|
setTableId(tableId)
|
|
|
|
const token = getSessionToken()
|
|
|
|
if (token) {
|
|
|
|
openSocket(token)
|
2021-11-29 11:06:20 +01:00
|
|
|
hideTableListElements()
|
2021-11-28 23:59:24 +01:00
|
|
|
// TODO: Show interface
|
|
|
|
console.log("Show table " + tableId)
|
|
|
|
} else {
|
|
|
|
showBlankLoginScreen("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerUser() {
|
|
|
|
const username = getLoginName()
|
|
|
|
const password = getLoginPassword()
|
|
|
|
if (username == "") {
|
|
|
|
setLoginError("Please enter your desired user name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (password == "") {
|
|
|
|
setLoginError("Please enter a password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
performRegisterPlayerRequest(username, password)
|
|
|
|
.then(function(token) {
|
|
|
|
setSessionToken(token)
|
|
|
|
setPlayerName(username)
|
2021-11-29 11:06:20 +01:00
|
|
|
showTableList()
|
2021-11-28 23:59:24 +01:00
|
|
|
loadCurrentTable(token)
|
|
|
|
}).catch(function(error) {
|
|
|
|
setLoginError(error.message)
|
|
|
|
})
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function deletePlayerAccount() {
|
2021-11-28 15:53:47 +01:00
|
|
|
const name = getPlayerName()
|
|
|
|
const password = getLoginPassword()
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
performDeletePlayerRequest(name, password, function(error) {
|
|
|
|
if (error == "") {
|
|
|
|
showBlankLoginScreen("")
|
|
|
|
console.log("Player deleted")
|
|
|
|
} else {
|
|
|
|
closeSocketIfNeeded()
|
|
|
|
deleteSessionToken()
|
|
|
|
alert(error)
|
|
|
|
console.log(error)
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function loginUser() {
|
|
|
|
const username = getLoginName()
|
|
|
|
const password = getLoginPassword()
|
|
|
|
if (username == "") {
|
|
|
|
setLoginError("Please enter your user name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (password == "") {
|
|
|
|
setLoginError("Please enter your password")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
performLoginPlayerRequest(username, password)
|
|
|
|
.then(function(token) {
|
|
|
|
setSessionToken(token)
|
|
|
|
setPlayerName(username)
|
2021-11-29 11:06:20 +01:00
|
|
|
showTableList()
|
2021-11-28 23:59:24 +01:00
|
|
|
loadCurrentTable(token)
|
|
|
|
}).catch(function(error) {
|
|
|
|
setLoginError(error.message)
|
|
|
|
})
|
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()
|
|
|
|
if (token) {
|
|
|
|
performLogoutRequest(token)
|
2021-11-28 23:59:24 +01:00
|
|
|
.then(function() {
|
|
|
|
showBlankLoginScreen("")
|
|
|
|
}).catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
console.log(error)
|
|
|
|
})
|
2021-11-28 15:53:47 +01:00
|
|
|
} else {
|
|
|
|
showBlankLoginScreen("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:59:24 +01:00
|
|
|
function loadExistingSession() {
|
|
|
|
const token = getSessionToken()
|
|
|
|
if (token) {
|
|
|
|
resumeSessionRequest(token)
|
|
|
|
.then(function(name) {
|
|
|
|
setPlayerName(name)
|
2021-11-29 11:06:20 +01:00
|
|
|
showTableList()
|
2021-11-28 23:59:24 +01:00
|
|
|
loadCurrentTable(token)
|
|
|
|
}).catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
showBlankLoginScreen("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadCurrentTable(token) {
|
|
|
|
performGetCurrentTableRequest(token)
|
|
|
|
.then(function(tableId) {
|
|
|
|
if (tableId == "") {
|
|
|
|
refreshTables()
|
2021-11-28 15:53:47 +01:00
|
|
|
return
|
|
|
|
}
|
2021-11-28 23:59:24 +01:00
|
|
|
showGame(tableId)
|
2021-11-28 15:53:47 +01:00
|
|
|
}).catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
if (token) {
|
|
|
|
const isVisible = getTableVisibility()
|
|
|
|
performCreateTableRequest(token, tableName, isVisible)
|
|
|
|
.then(function(tableId) {
|
|
|
|
clearTableName()
|
|
|
|
showGame(tableId)
|
|
|
|
}).catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
showBlankLoginScreen("")
|
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-11-27 11:59:13 +01:00
|
|
|
if (token) {
|
2021-11-28 23:59:24 +01:00
|
|
|
performJoinTableRequest(tableId, token)
|
|
|
|
.then(function() {
|
|
|
|
showGame(tableId)
|
|
|
|
})
|
|
|
|
.catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
})
|
2021-11-28 15:53:47 +01:00
|
|
|
} else {
|
2021-11-28 23:59:24 +01:00
|
|
|
showBlankLoginScreen("")
|
2021-11-27 11:59:13 +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-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
socket.onopen = function(e) {
|
|
|
|
socket.send(token);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function(event) {
|
|
|
|
// TODO: Handle server data
|
2021-11-28 23:59:24 +01:00
|
|
|
//event.data
|
2021-11-28 15:53:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onclose = function(event) {
|
|
|
|
if (event.wasClean) {
|
2021-11-28 23:59:24 +01:00
|
|
|
didCloseSocket()
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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()
|
|
|
|
if (token) {
|
2021-11-28 23:59:24 +01:00
|
|
|
performGetPublicTablesRequest(token)
|
|
|
|
.then(function(json) {
|
|
|
|
const html = processTableList(json)
|
|
|
|
setTableListContent(html)
|
|
|
|
}).catch(function(error) {
|
|
|
|
showBlankLoginScreen(error.message)
|
|
|
|
return
|
|
|
|
})
|
2021-11-28 15:53:47 +01:00
|
|
|
} else {
|
|
|
|
showBlankLoginScreen()
|
|
|
|
}
|
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>"
|
|
|
|
html += "<div class=\"table-subtitle\">Players: " + tableInfo.players.join(", ") + "</div>"
|
|
|
|
html += "</div>" // table-row
|
2021-11-28 15:53:47 +01:00
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|