cards/test_client.html

97 lines
3 KiB
HTML
Raw Normal View History

2024-04-28 04:53:00 -04:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cards For Humanity Test Client</title>
<style>
html, body, input, textarea, button {
background-color: #111;
color: #DDD;
}
2024-04-29 03:32:44 -04:00
div {
margin-top: 1rem;
}
2024-04-30 02:28:43 -04:00
p {
margin: 0;
padding: 0;
}
2024-04-28 04:53:00 -04:00
</style>
</head>
<body>
<h1>Cards For Humanity Test Client</h1>
2024-04-29 02:18:20 -04:00
<div id="status">
<p><em>Disconnected...</em></p>
</div>
2024-04-30 02:28:43 -04:00
<div style="display: flex;">
<span>
<p>Username</p>
<p>Game Name</p>
</span>
<span>
<form id="new-game" onsubmit="createGame();return false">
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
<input id="gamename" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="game name">
<br />
<button id="create-game" type="submit">Create Game</button>
</form>
</span>
2024-04-29 03:32:44 -04:00
</div>
<div>
<form id="chat" onsubmit="chatSubmit();return false">
<textarea id="chat-history" readonly="true" wrap="soft" style="display:block; width:30rem; height:10rem; box-sizing: border-box" cols="30" rows="10"></textarea>
<input id="chat-input" style="display:block; width:30rem; box-sizing: border-box" type="text" placeholder="chat">
</form>
</div>
2024-04-28 04:53:00 -04:00
<script type="text/javascript">
2024-04-29 02:18:20 -04:00
websocket = new WebSocket("ws://localhost:3030/websocket");
2024-04-28 04:53:00 -04:00
2024-04-30 02:28:43 -04:00
function createGame() {
document.getElementById("create-game").disabled = true;
let CAHPlayer = {
name: username.value,
role: 'h',
white: [],
black: [],
};
let NewGameRequest = {
name: gamename.value,
host: CAHPlayer,
packs: [0],
};
websocket.send(JSON.stringify(NewGameRequest));
2024-04-29 02:18:20 -04:00
};
2024-04-28 04:53:00 -04:00
2024-04-29 02:18:20 -04:00
websocket.onopen = function() {
2024-04-29 03:32:44 -04:00
console.log("connection opened",websocket.extensions, websocket.protocol, websocket.readyState);
2024-04-29 02:18:20 -04:00
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
}
2024-04-28 04:53:00 -04:00
2024-04-29 02:18:20 -04:00
websocket.onclose = function() {
console.log("connection closed");
// document.getElementById("join-chat").disabled = false;
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
}
2024-04-28 04:53:00 -04:00
2024-04-29 02:18:20 -04:00
websocket.onmessage = function(e) {
2024-04-29 03:32:44 -04:00
const history = document.getElementById("chat-history")
2024-04-29 02:18:20 -04:00
console.log("received message: "+e.data);
2024-04-29 03:32:44 -04:00
history.value += e.data+"\r";
history.scrollTop = history.scrollHeight;
2024-04-29 02:18:20 -04:00
}
2024-04-28 04:53:00 -04:00
2024-04-29 02:18:20 -04:00
function chatSubmit() {
let input = document.getElementById("chat-input");
websocket.send(input.value);
input.value = "";
};
2024-04-29 03:32:44 -04:00
websocket.addEventListener("error", (event) => {
console.log("WebSocket error: ", event);
});
2024-04-28 04:53:00 -04:00
</script>
</body>
</html>