Refactor frontend to use HTMX, and do most rendering serverside
This commit is contained in:
parent
d1dbebcc39
commit
8eabbe3211
12 changed files with 739 additions and 775 deletions
127
index.html
127
index.html
|
|
@ -4,10 +4,19 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Gomoku Game</title>
|
||||
<meta name="gameId" content="" />
|
||||
<meta name="playerId" content="" />
|
||||
<script
|
||||
src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.6/dist/htmx.min.js"
|
||||
crossorigin="anonymous"
|
||||
></script>
|
||||
<script
|
||||
src="https://cdn.jsdelivr.net/npm/htmx-ext-ws@2.0.2"
|
||||
crossorigin="anonymous"
|
||||
></script>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
|
@ -22,10 +31,13 @@
|
|||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||
text-align: center;
|
||||
}
|
||||
#game-board {
|
||||
margin-top: 20px;
|
||||
.game-board-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(15, 1fr);
|
||||
width: 450px; /* 15 * 30px */
|
||||
height: 450px; /* 15 * 30px */
|
||||
border: 1px solid black;
|
||||
margin-top: 20px;
|
||||
}
|
||||
.board-cell {
|
||||
width: 30px;
|
||||
|
|
@ -61,12 +73,111 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="game-container">
|
||||
<h1>Gomoku</h1>
|
||||
<div id="game-container" hx-ext="ws">
|
||||
<div id="player-info"></div>
|
||||
<div id="game-board"></div>
|
||||
<div id="messages"></div>
|
||||
<div id="game-board" class="game-board-grid"></div>
|
||||
<div id="messages" hx-swap-oob="beforeend"></div>
|
||||
</div>
|
||||
<script type="module" src="./dist/bundle.js"></script>
|
||||
<div id="game-link-container">
|
||||
Share link to this game:
|
||||
<input type="text" id="game-link" size="50" readonly />
|
||||
<button onclick="copyGameLink()">Copy</button>
|
||||
</div>
|
||||
<div id="ws-status" style="margin-top: 10px; color: grey">
|
||||
Connecting...
|
||||
</div>
|
||||
<script>
|
||||
// Get gameId and playerId from meta tags generated by the server
|
||||
const gameIdMeta = document.querySelector('meta[name="gameId"]');
|
||||
const playerIdMeta = document.querySelector('meta[name="playerId"]');
|
||||
|
||||
if (gameIdMeta && playerIdMeta) {
|
||||
const gameId = gameIdMeta.content;
|
||||
const playerId = playerIdMeta.content;
|
||||
|
||||
// Dynamically construct WebSocket URL
|
||||
const wsUrl = `ws://${window.location.host}/ws?gameId=${gameId}&playerId=${playerId}`;
|
||||
|
||||
// Get the game container element
|
||||
const gameContainer = document.getElementById('game-container');
|
||||
|
||||
// Set the ws-connect attribute
|
||||
gameContainer.setAttribute('ws-connect', wsUrl);
|
||||
|
||||
// Tell HTMX to connect the WebSocket
|
||||
htmx.trigger(gameContainer, ' and connect');
|
||||
|
||||
// Update the game link input
|
||||
const gameLinkInput = document.getElementById('game-link');
|
||||
if (gameLinkInput) {
|
||||
gameLinkInput.value =
|
||||
window.location.origin +
|
||||
window.location.pathname +
|
||||
`?gameId=${gameId}`;
|
||||
}
|
||||
|
||||
const wsStatusDiv = document.getElementById('ws-status');
|
||||
|
||||
// Listen for new messages over the WebSocket
|
||||
gameContainer.addEventListener('htmx:wsOpen', function () {
|
||||
if (wsStatusDiv) wsStatusDiv.textContent = 'Connected';
|
||||
// Re-render the board for new game states
|
||||
gameContainer.addEventListener(
|
||||
'htmx:wsAfterMessage',
|
||||
function (event) {
|
||||
const data = JSON.parse(event.detail.message);
|
||||
if (data.type === 'game_state') {
|
||||
document.getElementById('game-board').innerHTML =
|
||||
data.boardHtml;
|
||||
document.getElementById('player-info').innerHTML =
|
||||
data.playerInfoHtml;
|
||||
} else if (data.type === 'message') {
|
||||
document.getElementById('messages').innerHTML =
|
||||
data.messagesHtml;
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// Add event listener for board cells to send move actions
|
||||
document.addEventListener('htmx:wsConfigSend', function(e) {
|
||||
if (e.target.classList.contains('board-cell')) {
|
||||
const row = parseInt(e.target.dataset.row);
|
||||
const col = parseInt(e.target.dataset.col);
|
||||
|
||||
// Set the custom JSON data
|
||||
e.detail.parameters = {
|
||||
type: "make_move",
|
||||
row: row,
|
||||
col: col
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
gameContainer.addEventListener('htmx:wsClose', function () {
|
||||
if (wsStatusDiv) wsStatusDiv.textContent = 'Disconnected';
|
||||
});
|
||||
|
||||
gameContainer.addEventListener('htmx:wsError', function () {
|
||||
if (wsStatusDiv) wsStatusDiv.textContent = 'Connection Error';
|
||||
});
|
||||
} else {
|
||||
console.error('Game ID or Player ID meta tags not found.');
|
||||
}
|
||||
|
||||
function copyGameLink() {
|
||||
const gameLinkInput = document.getElementById('game-link');
|
||||
if (gameLinkInput) {
|
||||
navigator.clipboard
|
||||
.writeText(gameLinkInput.value)
|
||||
.then(() => {
|
||||
alert('Game link copied to clipboard!');
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Failed to copy link: ', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue