gomoku/index.html

184 lines
5.8 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gomoku!</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;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#game-container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
.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;
height: 30px;
border: 1px solid #ccc;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.board-cell:hover {
background-color: #e0e0e0;
}
.board-cell > div {
width: 24px;
height: 24px;
border-radius: 50%;
border: 1px solid #333;
}
.last-move {
box-shadow: 0 0 5px 3px rgba(255, 255, 0, 0.7); /* Yellow glow */
}
#messages {
margin-top: 10px;
font-size: 0.9em;
color: #555;
}
#player-info {
margin-top: 10px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="game-container" hx-ext="ws">
<div id="player-info"></div>
<div id="game-board" class="game-board-grid"></div>
<div id="messages" hx-swap-oob="beforeend"></div>
</div>
<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>