Files
sena_db_api_layer/public/game-config.html
silverpro89 41cfb533d5
All checks were successful
Deploy to Production / deploy (push) Successful in 20s
update
2026-01-28 20:34:15 +07:00

660 lines
23 KiB
HTML

<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: http:; connect-src 'self' http://senaai.tech:10000;">
<title>Game Config Manager - Sena AI</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
background: white;
border-radius: 10px;
padding: 30px;
margin-bottom: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.header h1 {
color: #667eea;
margin-bottom: 10px;
}
.header p {
color: #666;
}
.game-card {
background: white;
border-radius: 10px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
margin-bottom: 20px;
}
.game-info {
display: grid;
grid-template-columns: 200px 1fr;
gap: 30px;
margin-bottom: 30px;
}
.game-thumbnail {
width: 200px;
height: 200px;
object-fit: cover;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.game-details h2 {
color: #333;
margin-bottom: 10px;
}
.game-details p {
color: #666;
margin-bottom: 8px;
}
.badge {
display: inline-block;
padding: 4px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-right: 8px;
margin-bottom: 8px;
}
.badge-active {
background: #10b981;
color: white;
}
.badge-inactive {
background: #ef4444;
color: white;
}
.badge-premium {
background: #f59e0b;
color: white;
}
.badge-free {
background: #3b82f6;
color: white;
}
.config-section {
border-top: 2px solid #f0f0f0;
padding-top: 30px;
}
.config-section h3 {
color: #667eea;
margin-bottom: 15px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
color: #333;
font-weight: 600;
margin-bottom: 8px;
}
input[type="text"],
input[type="number"],
textarea,
select {
width: 100%;
padding: 12px;
border: 2px solid #e5e7eb;
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s;
}
input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
outline: none;
border-color: #667eea;
}
textarea {
min-height: 200px;
font-family: 'Courier New', monospace;
resize: vertical;
}
.checkbox-group {
display: flex;
align-items: center;
gap: 10px;
}
input[type="checkbox"] {
width: 20px;
height: 20px;
cursor: pointer;
}
.button-group {
display: flex;
gap: 10px;
margin-top: 30px;
}
.btn {
padding: 12px 30px;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5568d3;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn-secondary {
background: #6b7280;
color: white;
}
.btn-secondary:hover {
background: #4b5563;
}
.btn-danger {
background: #ef4444;
color: white;
}
.btn-danger:hover {
background: #dc2626;
}
.loading {
text-align: center;
padding: 50px;
color: #666;
}
.error {
background: #fee2e2;
border: 2px solid #ef4444;
color: #991b1b;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
}
.success {
background: #d1fae5;
border: 2px solid #10b981;
color: #065f46;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
}
.game-url {
color: #667eea;
text-decoration: none;
word-break: break-all;
}
.game-url:hover {
text-decoration: underline;
}
.input-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.game-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
gap: 20px;
}
.game-list-item {
display: flex;
gap: 15px;
padding: 15px;
border: 2px solid #e5e7eb;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s;
}
.game-list-item:hover {
border-color: #667eea;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.2);
}
.game-list-thumbnail {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 8px;
flex-shrink: 0;
}
.game-list-info {
flex: 1;
}
.game-list-info h3 {
color: #333;
margin-bottom: 5px;
font-size: 16px;
}
.game-type {
color: #667eea;
font-size: 12px;
font-weight: 600;
margin-bottom: 5px;
}
.game-desc {
color: #666;
font-size: 13px;
margin-bottom: 8px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.game-list-badges {
display: flex;
gap: 5px;
flex-wrap: wrap;
}
@media (max-width: 768px) {
.game-info {
grid-template-columns: 1fr;
}
.input-group {
grid-template-columns: 1fr;
}
.game-list {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🎮 Game Config Manager</h1>
<p>Quản lý cấu hình trò chơi - Sena AI Platform</p>
</div>
<div id="app">
<div class="loading">⏳ Đang tải dữ liệu...</div>
</div>
</div>
<script>
const API_BASE = 'http://localhost:10000/api';
let currentGame = null;
let allGames = [];
// Load all games
async function loadAllGames() {
try {
document.getElementById('app').innerHTML = '<div class="loading">⏳ Đang tải danh sách game...</div>';
const response = await fetch(`${API_BASE}/games/`);
const result = await response.json();
// Handle different response formats
let gamesData = [];
if (result.success && result.data) {
// If response has success and data wrapper
gamesData = Array.isArray(result.data.games) ? result.data.games : [];
} else if (Array.isArray(result)) {
// If response is directly an array
gamesData = result;
} else if (result.data && Array.isArray(result.data)) {
// If only has data property
gamesData = result.data;
}
allGames = gamesData;
renderGameList(allGames);
} catch (error) {
showError('Lỗi kết nối: ' + error.message);
}
}
// Render game list
function renderGameList(games) {
if (!games || games.length === 0) {
document.getElementById('app').innerHTML = `
<div class="game-card">
<p>Không có game nào.</p>
</div>
`;
return;
}
const gamesHtml = games.map(game => `
<div class="game-list-item" onclick="loadGame('${game.id}')">
<img src="${game.thumbnail}" alt="${game.title}" class="game-list-thumbnail" onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22100%22 height=%22100%22%3E%3Crect width=%22100%22 height=%22100%22 fill=%22%23ddd%22/%3E%3Ctext x=%2250%25%22 y=%2250%25%22 text-anchor=%22middle%22 dy=%22.3em%22 fill=%22%23999%22 font-size=%2212%22%3ENo Image%3C/text%3E%3C/svg%3E'">
<div class="game-list-info">
<h3>${game.title}</h3>
<p class="game-type">${game.type}</p>
<p class="game-desc">${game.description || 'Không có mô tả'}</p>
<div class="game-list-badges">
<span class="badge ${game.is_active ? 'badge-active' : 'badge-inactive'}">
${game.is_active ? '✓ Active' : '✗ Inactive'}
</span>
<span class="badge ${game.is_premium ? 'badge-premium' : 'badge-free'}">
${game.is_premium ? '⭐ Premium' : '🆓 Free'}
</span>
</div>
</div>
</div>
`).join('');
document.getElementById('app').innerHTML = `
<div class="game-card">
<h2 style="margin-bottom: 20px;">📋 Danh sách Game (${games.length})</h2>
<div class="game-list">
${gamesHtml}
</div>
</div>
`;
}
// Load game data
async function loadGame(gameId) {
try {
document.getElementById('app').innerHTML = '<div class="loading">⏳ Đang tải dữ liệu game...</div>';
const response = await fetch(`${API_BASE}/games/${gameId}`);
const result = await response.json();
if (result.success) {
currentGame = result.data;
renderGame(currentGame);
} else {
showError('Không thể tải dữ liệu game');
}
} catch (error) {
showError('Lỗi kết nối: ' + error.message);
}
}
// Render game information
function renderGame(game) {
const configJson = JSON.stringify(game.config, null, 2);
const html = `
<div class="game-card">
<div class="game-info">
<img src="${game.thumbnail}" alt="${game.title}" class="game-thumbnail" onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22200%22 height=%22200%22%3E%3Crect width=%22200%22 height=%22200%22 fill=%22%23ddd%22/%3E%3Ctext x=%2250%25%22 y=%2250%25%22 text-anchor=%22middle%22 dy=%22.3em%22 fill=%22%23999%22%3ENo Image%3C/text%3E%3C/svg%3E'">
<div class="game-details">
<h2>${game.title}</h2>
<p><strong>ID:</strong> ${game.id}</p>
<p><strong>Type:</strong> ${game.type}</p>
<p><strong>Description:</strong> ${game.description || 'N/A'}</p>
<p><strong>URL:</strong> <a href="${game.url}" target="_blank" class="game-url">${game.url}</a></p>
<div>
<span class="badge ${game.is_active ? 'badge-active' : 'badge-inactive'}">
${game.is_active ? '✓ Active' : '✗ Inactive'}
</span>
<span class="badge ${game.is_premium ? 'badge-premium' : 'badge-free'}">
${game.is_premium ? '⭐ Premium' : '🆓 Free'}
</span>
</div>
<p style="margin-top: 10px;"><strong>Play Count:</strong> ${game.play_count} | <strong>Rating:</strong> ${game.rating || 'N/A'}</p>
</div>
</div>
<div class="config-section">
<h3>📝 Game Configuration</h3>
<div id="message"></div>
<form id="gameForm" onsubmit="saveGame(event)">
<div class="input-group">
<div class="form-group">
<label for="title">Tiêu đề:</label>
<input type="text" id="title" value="${game.title}" required>
</div>
<div class="form-group">
<label for="type">Loại game:</label>
<input type="text" id="type" value="${game.type}" required>
</div>
</div>
<div class="form-group">
<label for="description">Mô tả:</label>
<input type="text" id="description" value="${game.description || ''}">
</div>
<div class="input-group">
<div class="form-group">
<label for="url">Game URL:</label>
<input type="text" id="url" value="${game.url}" required>
</div>
<div class="form-group">
<label for="thumbnail">Thumbnail URL:</label>
<input type="text" id="thumbnail" value="${game.thumbnail}">
</div>
</div>
<div class="input-group">
<div class="form-group">
<label for="min_grade">Lớp tối thiểu:</label>
<input type="number" id="min_grade" value="${game.min_grade || ''}" min="1" max="12">
</div>
<div class="form-group">
<label for="max_grade">Lớp tối đa:</label>
<input type="number" id="max_grade" value="${game.max_grade || ''}" min="1" max="12">
</div>
</div>
<div class="input-group">
<div class="form-group">
<label for="difficulty_level">Độ khó:</label>
<select id="difficulty_level">
<option value="">-- Chọn độ khó --</option>
<option value="easy" ${game.difficulty_level === 'easy' ? 'selected' : ''}>Dễ</option>
<option value="medium" ${game.difficulty_level === 'medium' ? 'selected' : ''}>Trung bình</option>
<option value="hard" ${game.difficulty_level === 'hard' ? 'selected' : ''}>Khó</option>
</select>
</div>
<div class="form-group">
<label for="display_order">Thứ tự hiển thị:</label>
<input type="number" id="display_order" value="${game.display_order}" min="0">
</div>
</div>
<div class="input-group">
<div class="form-group">
<div class="checkbox-group">
<input type="checkbox" id="is_active" ${game.is_active ? 'checked' : ''}>
<label for="is_active" style="margin: 0;">Kích hoạt game</label>
</div>
</div>
<div class="form-group">
<div class="checkbox-group">
<input type="checkbox" id="is_premium" ${game.is_premium ? 'checked' : ''}>
<label for="is_premium" style="margin: 0;">Premium game</label>
</div>
</div>
</div>
<div class="form-group">
<label for="config">Config JSON:</label>
<textarea id="config" placeholder='{"key": "value"}'>${configJson}</textarea>
<small style="color: #666;">Nhập cấu hình dưới dạng JSON. Ví dụ: {"maxTime": 60, "maxQuestions": 10}</small>
</div>
<div class="button-group">
<button type="submit" class="btn btn-primary">💾 Lưu cấu hình</button>
<button type="button" class="btn btn-secondary" onclick="resetForm()">🔄 Làm mới</button>
<button type="button" class="btn btn-danger" onclick="validateConfig()">✓ Kiểm tra JSON</button>
</div>
</form>
</div>
</div>
`;
document.getElementById('app').innerHTML = html;
}
// Validate JSON config
function validateConfig() {
const configText = document.getElementById('config').value;
const messageDiv = document.getElementById('message');
try {
if (configText.trim() === '') {
JSON.parse('{}');
} else {
JSON.parse(configText);
}
messageDiv.innerHTML = '<div class="success">✅ JSON hợp lệ!</div>';
setTimeout(() => {
messageDiv.innerHTML = '';
}, 3000);
} catch (error) {
messageDiv.innerHTML = `<div class="error">❌ JSON không hợp lệ: ${error.message}</div>`;
}
}
// Save game
async function saveGame(event) {
event.preventDefault();
const messageDiv = document.getElementById('message');
// Validate JSON first
const configText = document.getElementById('config').value;
let configObj = {};
console.log('Config Text:', configText); // Debug log
try {
configObj = configText.trim() === '' ? {} : JSON.parse(configText);
} catch (error) {
messageDiv.innerHTML = `<div class="error">❌ JSON config không hợp lệ: ${error.message}</div>`;
return;
}
const gameData = {
title: document.getElementById('title').value,
description: document.getElementById('description').value,
url: document.getElementById('url').value,
thumbnail: document.getElementById('thumbnail').value,
type: document.getElementById('type').value,
config: configObj,
is_active: document.getElementById('is_active').checked,
is_premium: document.getElementById('is_premium').checked,
min_grade: document.getElementById('min_grade').value ? parseInt(document.getElementById('min_grade').value) : null,
max_grade: document.getElementById('max_grade').value ? parseInt(document.getElementById('max_grade').value) : null,
difficulty_level: document.getElementById('difficulty_level').value || null,
display_order: parseInt(document.getElementById('display_order').value) || 0
};
try {
messageDiv.innerHTML = '<div class="loading">⏳ Đang lưu...</div>';
const response = await fetch(`${API_BASE}/games/${currentGame.id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(gameData)
});
const result = await response.json();
if (result.success) {
messageDiv.innerHTML = '<div class="success">✅ Lưu thành công!</div>';
currentGame = result.data;
setTimeout(() => {
loadGame(currentGame.id);
}, 1500);
} else {
messageDiv.innerHTML = `<div class="error">❌ Lỗi: ${result.message || 'Không thể lưu dữ liệu'}</div>`;
}
} catch (error) {
messageDiv.innerHTML = `<div class="error">❌ Lỗi kết nối: ${error.message}</div>`;
}
}
// Reset form
function resetForm() {
if (confirm('Bạn có chắc muốn làm mới form? Dữ liệu chưa lưu sẽ bị mất.')) {
loadGame(currentGame.id);
}
}
// Show error
function showError(message) {
document.getElementById('app').innerHTML = `
<div class="game-card">
<div class="error">${message}</div>
<button class="btn btn-primary" onclick="loadAllGames()">🔄 Tải lại</button>
</div>
`;
}
// Initialize
window.onload = () => {
loadAllGames();
};
</script>
</body>
</html>