Files
jackboxpartypack-gamepicker/scripts/get-player-count.html

469 lines
16 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jackbox Player Count Fetcher</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.container {
background: white;
border-radius: 12px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.input-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
input {
flex: 1;
padding: 12px 16px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 6px;
text-transform: uppercase;
font-weight: bold;
letter-spacing: 2px;
}
input:focus {
outline: none;
border-color: #667eea;
}
button {
padding: 12px 24px;
font-size: 16px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
button:active {
transform: translateY(0);
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.status {
padding: 12px;
border-radius: 6px;
margin-bottom: 20px;
display: none;
}
.status.loading {
background: #e3f2fd;
color: #1976d2;
display: block;
}
.status.error {
background: #ffebee;
color: #c62828;
display: block;
}
.status.success {
background: #e8f5e9;
color: #2e7d32;
display: block;
}
.results {
display: none;
}
.results.visible {
display: block;
}
.result-card {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.stat-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.stat {
text-align: center;
padding: 15px;
background: white;
border-radius: 6px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.stat-label {
font-size: 12px;
color: #666;
text-transform: uppercase;
letter-spacing: 1px;
margin-bottom: 5px;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #333;
}
.stat-value.highlight {
color: #667eea;
}
.players-list {
background: white;
border-radius: 6px;
padding: 15px;
}
.players-list h3 {
margin-top: 0;
color: #333;
}
.player-item {
padding: 10px;
margin: 5px 0;
background: #f8f9fa;
border-radius: 4px;
display: flex;
justify-content: space-between;
align-items: center;
}
.player-name {
font-weight: 500;
}
.player-role {
font-size: 12px;
padding: 4px 8px;
border-radius: 4px;
text-transform: uppercase;
letter-spacing: 1px;
}
.player-role.host {
background: #fff3e0;
color: #e65100;
}
.player-role.player {
background: #e8f5e9;
color: #2e7d32;
}
.info-row {
display: flex;
justify-content: space-between;
padding: 8px 0;
border-bottom: 1px solid #eee;
}
.info-row:last-child {
border-bottom: none;
}
.info-label {
color: #666;
font-weight: 500;
}
.info-value {
color: #333;
}
.badge {
display: inline-block;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
}
.badge.success {
background: #e8f5e9;
color: #2e7d32;
}
.badge.error {
background: #ffebee;
color: #c62828;
}
</style>
</head>
<body>
<div class="container">
<h1>🎮 Jackbox Player Count Fetcher</h1>
<p class="subtitle">Enter a room code to get real-time player information</p>
<div class="input-group">
<input type="text" id="roomCode" placeholder="Enter room code (e.g., JYET)" maxlength="4">
<button id="fetchBtn" onclick="fetchPlayerCount()">Get Player Count</button>
</div>
<div id="status" class="status"></div>
<div id="results" class="results">
<div class="result-card">
<div class="stat-grid">
<div class="stat">
<div class="stat-label">Players</div>
<div class="stat-value highlight" id="playerCount">-</div>
</div>
<div class="stat">
<div class="stat-label">Max Players</div>
<div class="stat-value" id="maxPlayers">-</div>
</div>
<div class="stat">
<div class="stat-label">Audience</div>
<div class="stat-value" id="audienceCount">-</div>
</div>
</div>
<div class="info-row">
<span class="info-label">Room Code:</span>
<span class="info-value" id="displayRoomCode">-</span>
</div>
<div class="info-row">
<span class="info-label">Game:</span>
<span class="info-value" id="gameTag">-</span>
</div>
<div class="info-row">
<span class="info-label">Game State:</span>
<span class="info-value" id="gameState">-</span>
</div>
<div class="info-row">
<span class="info-label">Lobby State:</span>
<span class="info-value" id="lobbyState">-</span>
</div>
<div class="info-row">
<span class="info-label">Locked:</span>
<span class="info-value" id="locked">-</span>
</div>
<div class="info-row">
<span class="info-label">Full:</span>
<span class="info-value" id="full">-</span>
</div>
</div>
<div id="playersContainer" class="players-list" style="display: none;">
<h3>Current Players</h3>
<div id="playersList"></div>
</div>
</div>
</div>
<script>
// Allow Enter key to trigger fetch
document.getElementById('roomCode').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
fetchPlayerCount();
}
});
async function fetchPlayerCount() {
const roomCode = document.getElementById('roomCode').value.toUpperCase().trim();
if (!roomCode || roomCode.length !== 4) {
showStatus('Please enter a valid 4-letter room code', 'error');
return;
}
showStatus('Fetching room information...', 'loading');
document.getElementById('fetchBtn').disabled = true;
document.getElementById('results').classList.remove('visible');
try {
// Step 1: Get room info
const roomInfo = await getRoomInfo(roomCode);
showStatus('Connecting to WebSocket...', 'loading');
// Step 2: Connect to WebSocket and get player count
const result = await getPlayerCount(roomCode, roomInfo);
showStatus('✓ Successfully retrieved player information', 'success');
displayResults(result);
setTimeout(() => {
document.getElementById('status').style.display = 'none';
}, 3000);
} catch (error) {
showStatus(`Error: ${error.message}`, 'error');
} finally {
document.getElementById('fetchBtn').disabled = false;
}
}
async function getRoomInfo(roomCode) {
const response = await fetch(`https://ecast.jackboxgames.com/api/v2/rooms/${roomCode}`);
const data = await response.json();
if (!data.ok) {
throw new Error('Room not found or invalid');
}
return data.body;
}
async function getPlayerCount(roomCode, roomInfo) {
return new Promise((resolve, reject) => {
const wsUrl = `wss://${roomInfo.host}/api/v2/rooms/${roomCode}`;
const ws = new WebSocket(wsUrl);
const timeout = setTimeout(() => {
ws.close();
reject(new Error('Connection timeout'));
}, 10000);
ws.onopen = () => {
// Join as audience to get lobby state without affecting the game
ws.send(JSON.stringify({
opcode: 'client/connect',
params: {
name: 'WebObserver',
role: 'audience',
format: 'json'
}
}));
};
ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
if (message.opcode === 'client/welcome' && message.result) {
clearTimeout(timeout);
const here = message.result.here || {};
const playerCount = Object.keys(here).length;
const audienceCount = message.result.entities?.audience?.[1]?.count || 0;
const lobbyState = message.result.entities?.['bc:room']?.[1]?.val?.lobbyState || 'Unknown';
const gameState = message.result.entities?.['bc:room']?.[1]?.val?.state || 'Unknown';
const players = [];
for (const [id, playerData] of Object.entries(here)) {
const roles = playerData.roles || {};
if (roles.host) {
players.push({ id, role: 'host', name: 'Host' });
} else if (roles.player) {
players.push({ id, role: 'player', name: roles.player.name || 'Unknown' });
}
}
ws.close();
resolve({
roomCode,
appTag: roomInfo.appTag,
playerCount,
audienceCount,
maxPlayers: roomInfo.maxPlayers,
gameState,
lobbyState,
locked: roomInfo.locked,
full: roomInfo.full,
players
});
}
} catch (e) {
// Ignore parse errors
}
};
ws.onerror = (error) => {
clearTimeout(timeout);
reject(new Error('WebSocket connection failed'));
};
});
}
function showStatus(message, type) {
const status = document.getElementById('status');
status.textContent = message;
status.className = `status ${type}`;
}
function displayResults(result) {
document.getElementById('results').classList.add('visible');
document.getElementById('playerCount').textContent = result.playerCount;
document.getElementById('maxPlayers').textContent = result.maxPlayers;
document.getElementById('audienceCount').textContent = result.audienceCount;
document.getElementById('displayRoomCode').textContent = result.roomCode;
document.getElementById('gameTag').textContent = result.appTag;
document.getElementById('gameState').textContent = result.gameState;
document.getElementById('lobbyState').textContent = result.lobbyState;
document.getElementById('locked').innerHTML = result.locked
? '<span class="badge error">Yes</span>'
: '<span class="badge success">No</span>';
document.getElementById('full').innerHTML = result.full
? '<span class="badge error">Yes</span>'
: '<span class="badge success">No</span>';
if (result.players && result.players.length > 0) {
const playersList = document.getElementById('playersList');
playersList.innerHTML = result.players.map(player => `
<div class="player-item">
<span class="player-name">${player.name}</span>
<span class="player-role ${player.role}">${player.role}</span>
</div>
`).join('');
document.getElementById('playersContainer').style.display = 'block';
} else {
document.getElementById('playersContainer').style.display = 'none';
}
}
</script>
</body>
</html>