feat: initialize interactive live-stream game backend MVP
- Add FastAPI backend with WebSocket support - Implement ConnectionManager for client connections - Create GameEngine with async game loop (2s tick) - Add RuleBasedAgent for keyword-based responses - Define Pydantic schemas for GameEvent protocol - Create debug frontend dashboard for testing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
157
frontend/app.js
Normal file
157
frontend/app.js
Normal file
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* The Island - Debug Client JavaScript
|
||||
* Handles WebSocket connection and UI interactions
|
||||
*/
|
||||
|
||||
let ws = null;
|
||||
const WS_URL = 'ws://localhost:8000/ws';
|
||||
|
||||
// DOM Elements
|
||||
const statusDot = document.getElementById('statusDot');
|
||||
const statusText = document.getElementById('statusText');
|
||||
const connectBtn = document.getElementById('connectBtn');
|
||||
const eventLog = document.getElementById('eventLog');
|
||||
const usernameInput = document.getElementById('username');
|
||||
const messageInput = document.getElementById('message');
|
||||
const autoScrollCheckbox = document.getElementById('autoScroll');
|
||||
|
||||
/**
|
||||
* Toggle WebSocket connection
|
||||
*/
|
||||
function toggleConnection() {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.close();
|
||||
} else {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish WebSocket connection
|
||||
*/
|
||||
function connect() {
|
||||
statusText.textContent = 'Connecting...';
|
||||
connectBtn.disabled = true;
|
||||
|
||||
ws = new WebSocket(WS_URL);
|
||||
|
||||
ws.onopen = () => {
|
||||
statusDot.classList.add('connected');
|
||||
statusText.textContent = 'Connected';
|
||||
connectBtn.textContent = 'Disconnect';
|
||||
connectBtn.disabled = false;
|
||||
logEvent({ event_type: 'system', data: { message: 'WebSocket connected' } });
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
statusDot.classList.remove('connected');
|
||||
statusText.textContent = 'Disconnected';
|
||||
connectBtn.textContent = 'Connect';
|
||||
connectBtn.disabled = false;
|
||||
logEvent({ event_type: 'system', data: { message: 'WebSocket disconnected' } });
|
||||
};
|
||||
|
||||
ws.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
logEvent({ event_type: 'error', data: { message: 'Connection error' } });
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
logEvent(data);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse message:', e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a mock comment to the server
|
||||
*/
|
||||
function sendComment() {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||
alert('Not connected to server');
|
||||
return;
|
||||
}
|
||||
|
||||
const user = usernameInput.value.trim() || 'Anonymous';
|
||||
const message = messageInput.value.trim();
|
||||
|
||||
if (!message) {
|
||||
alert('Please enter a message');
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
action: 'send_comment',
|
||||
payload: { user, message }
|
||||
};
|
||||
|
||||
ws.send(JSON.stringify(payload));
|
||||
messageInput.value = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format timestamp for display
|
||||
*/
|
||||
function formatTime(timestamp) {
|
||||
const date = new Date(timestamp * 1000);
|
||||
return date.toLocaleTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format event data for display
|
||||
*/
|
||||
function formatEventData(eventType, data) {
|
||||
switch (eventType) {
|
||||
case 'comment':
|
||||
return `${data.user}: ${data.message}`;
|
||||
case 'agent_response':
|
||||
return data.response;
|
||||
case 'tick':
|
||||
return `Tick #${data.tick}`;
|
||||
case 'system':
|
||||
case 'error':
|
||||
return data.message;
|
||||
default:
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an event to the display
|
||||
*/
|
||||
function logEvent(event) {
|
||||
const eventType = event.event_type || 'unknown';
|
||||
const timestamp = event.timestamp || Date.now() / 1000;
|
||||
const data = event.data || {};
|
||||
|
||||
const div = document.createElement('div');
|
||||
div.className = `event ${eventType}`;
|
||||
div.innerHTML = `
|
||||
<span class="event-time">${formatTime(timestamp)}</span>
|
||||
<span class="event-type">${eventType}</span>
|
||||
<div class="event-data">${formatEventData(eventType, data)}</div>
|
||||
`;
|
||||
|
||||
eventLog.appendChild(div);
|
||||
|
||||
if (autoScrollCheckbox.checked) {
|
||||
eventLog.scrollTop = eventLog.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the event log
|
||||
*/
|
||||
function clearLog() {
|
||||
eventLog.innerHTML = '';
|
||||
}
|
||||
|
||||
// Allow Enter key to send comment
|
||||
messageInput.addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
sendComment();
|
||||
}
|
||||
});
|
||||
179
frontend/debug_client.html
Normal file
179
frontend/debug_client.html
Normal file
@@ -0,0 +1,179 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>The Island - Debug Client</title>
|
||||
<style>
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
||||
color: #eee;
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
margin-bottom: 20px;
|
||||
color: #00d4ff;
|
||||
text-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
|
||||
}
|
||||
.status-bar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
padding: 10px 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.status-indicator {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.status-dot {
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border-radius: 50%;
|
||||
background: #ff4444;
|
||||
}
|
||||
.status-dot.connected {
|
||||
background: #44ff44;
|
||||
box-shadow: 0 0 10px rgba(68, 255, 68, 0.5);
|
||||
}
|
||||
.panels {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
}
|
||||
.panel {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
}
|
||||
.panel h2 {
|
||||
color: #00d4ff;
|
||||
margin-bottom: 15px;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
.input-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
input[type="text"] {
|
||||
flex: 1;
|
||||
min-width: 150px;
|
||||
padding: 10px 15px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 6px;
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
color: #fff;
|
||||
font-size: 14px;
|
||||
}
|
||||
input[type="text"]:focus {
|
||||
outline: none;
|
||||
border-color: #00d4ff;
|
||||
}
|
||||
button {
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: #00d4ff;
|
||||
color: #1a1a2e;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
button:hover {
|
||||
background: #00b8e6;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
button:disabled {
|
||||
background: #666;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.controls {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.controls label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.event-log {
|
||||
height: 400px;
|
||||
overflow-y: auto;
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
font-family: 'Consolas', monospace;
|
||||
font-size: 13px;
|
||||
}
|
||||
.event {
|
||||
padding: 8px 12px;
|
||||
margin-bottom: 8px;
|
||||
border-radius: 6px;
|
||||
border-left: 3px solid;
|
||||
}
|
||||
.event.comment { border-color: #ffaa00; background: rgba(255, 170, 0, 0.1); }
|
||||
.event.agent_response { border-color: #00ff88; background: rgba(0, 255, 136, 0.1); }
|
||||
.event.tick { border-color: #888; background: rgba(136, 136, 136, 0.1); }
|
||||
.event.system { border-color: #00d4ff; background: rgba(0, 212, 255, 0.1); }
|
||||
.event.error { border-color: #ff4444; background: rgba(255, 68, 68, 0.1); }
|
||||
.event-time { color: #888; font-size: 11px; }
|
||||
.event-type { font-weight: bold; text-transform: uppercase; font-size: 11px; }
|
||||
.event-data { margin-top: 5px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>The Island - Debug Client</h1>
|
||||
|
||||
<div class="status-bar">
|
||||
<div class="status-indicator">
|
||||
<div class="status-dot" id="statusDot"></div>
|
||||
<span id="statusText">Disconnected</span>
|
||||
</div>
|
||||
<button id="connectBtn" onclick="toggleConnection()">Connect</button>
|
||||
</div>
|
||||
|
||||
<div class="panels">
|
||||
<div class="panel">
|
||||
<h2>Send Mock Comment</h2>
|
||||
<div class="input-group">
|
||||
<input type="text" id="username" placeholder="Username" value="TestUser">
|
||||
<input type="text" id="message" placeholder="Message (try: Attack!, Heal, Help...)">
|
||||
<button onclick="sendComment()">Send</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<h2>Event Log</h2>
|
||||
<div class="controls">
|
||||
<button onclick="clearLog()">Clear Log</button>
|
||||
<label><input type="checkbox" id="autoScroll" checked> Auto-scroll</label>
|
||||
</div>
|
||||
<div class="event-log" id="eventLog"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user