Add Game and Config UI
This commit is contained in:
+131
-4
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
// On commence par session_start AVANT tout envoi de texte
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
@@ -13,7 +12,6 @@ $options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
];
|
||||
|
||||
try {
|
||||
@@ -23,7 +21,7 @@ try {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Lecture de l'input
|
||||
// Lecture de l'input JSON
|
||||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json, true);
|
||||
|
||||
@@ -36,4 +34,133 @@ $action = $data['action'];
|
||||
$params = $data['params'] ?? [];
|
||||
|
||||
switch ($action) {
|
||||
case "":
|
||||
|
||||
// --- INITIALISATION ---
|
||||
|
||||
case 'createGame':
|
||||
// On insère la game avec un admin_id temporaire à 0
|
||||
$stmt = $pdo->prepare("INSERT INTO games (game_name, admin_id) VALUES (?, 0)");
|
||||
$stmt->execute([$params['game_name']]);
|
||||
$game_id = $pdo->lastInsertId();
|
||||
|
||||
echo json_encode(['success' => true, 'game_id' => $game_id]);
|
||||
exit;
|
||||
|
||||
case 'addPlayer':
|
||||
// Ajout d'un joueur avec sa couleur et définition du tour
|
||||
$stmt = $pdo->prepare("INSERT INTO players (game_id, player_name, player_color, is_turn) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([
|
||||
(int)$params['game_id'],
|
||||
$params['name'],
|
||||
$params['color'],
|
||||
(int)$params['is_turn']
|
||||
]);
|
||||
$player_id = $pdo->lastInsertId();
|
||||
|
||||
// Si c'est le premier joueur ajouté, on le définit comme admin de la game
|
||||
if (isset($params['is_admin']) && $params['is_admin'] === true) {
|
||||
$update = $pdo->prepare("UPDATE games SET admin_id = ? WHERE id = ?");
|
||||
$update->execute([$player_id, (int)$params['game_id']]);
|
||||
$_SESSION['current_player_id'] = $player_id; // On stocke en session qui est l'admin
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'player_id' => $player_id]);
|
||||
exit;
|
||||
|
||||
// --- RÉCUPÉRATION ---
|
||||
|
||||
case 'getGameData':
|
||||
$game_id = (int)$params['game_id'];
|
||||
|
||||
// 1. Infos Game
|
||||
$stmt = $pdo->prepare("SELECT * FROM games WHERE id = ?");
|
||||
$stmt->execute([$game_id]);
|
||||
$game = $stmt->fetch();
|
||||
|
||||
// 2. Infos Players
|
||||
$stmt = $pdo->prepare("SELECT * FROM players WHERE game_id = ? ORDER BY id ASC");
|
||||
$stmt->execute([$game_id]);
|
||||
$players = $stmt->fetchAll();
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'game' => $game,
|
||||
'players' => $players,
|
||||
'session_player_id' => $_SESSION['current_player_id'] ?? null
|
||||
]);
|
||||
exit;
|
||||
|
||||
case 'get_all_games':
|
||||
// Liste pour l'index
|
||||
$stmt = $pdo->query("SELECT g.*, (SELECT COUNT(*) FROM players WHERE game_id = g.id) as nb_players FROM games g");
|
||||
echo json_encode(['success' => true, 'games' => $stmt->fetchAll()]);
|
||||
exit;
|
||||
|
||||
// --- GAMEPLAY & SCORES ---
|
||||
|
||||
case 'saveScore':
|
||||
// $params contient 'player_id', 'column' (ex: score_brelan), et 'value'
|
||||
$column = $params['column'];
|
||||
$allowed_columns = [
|
||||
'score_1','score_2','score_3','score_4','score_5','score_6',
|
||||
'score_brelan','score_carre','score_full','score_petite_suite',
|
||||
'score_grande_suite','score_yahtzee','score_chance'
|
||||
];
|
||||
|
||||
if (!in_array($column, $allowed_columns)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Colonne invalide']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("UPDATE players SET $column = ? WHERE id = ?");
|
||||
$stmt->execute([(int)$params['value'], (int)$params['player_id']]);
|
||||
|
||||
// On recalcule le total automatiquement ici ou en JS plus tard
|
||||
echo json_encode(['success' => true]);
|
||||
exit;
|
||||
|
||||
case 'nextTurn':
|
||||
$game_id = (int)$params['game_id'];
|
||||
$current_player_id = (int)$params['player_id'];
|
||||
|
||||
// 1. Retirer le tour au joueur actuel
|
||||
$pdo->prepare("UPDATE players SET is_turn = 0 WHERE id = ?")->execute([$current_player_id]);
|
||||
|
||||
// 2. Trouver le suivant
|
||||
$stmt = $pdo->prepare("SELECT id FROM players WHERE game_id = ? AND id > ? ORDER BY id ASC LIMIT 1");
|
||||
$stmt->execute([$game_id, $current_player_id]);
|
||||
$next = $stmt->fetchColumn();
|
||||
|
||||
if (!$next) {
|
||||
$stmt = $pdo->prepare("SELECT id FROM players WHERE game_id = ? ORDER BY id ASC LIMIT 1");
|
||||
$stmt->execute([$game_id]);
|
||||
$next = $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
$pdo->prepare("UPDATE players SET is_turn = 1 WHERE id = ?")->execute([$next]);
|
||||
echo json_encode(['success' => true, 'next_player_id' => $next]);
|
||||
exit;
|
||||
|
||||
// --- ADMIN ---
|
||||
|
||||
case 'delete_game':
|
||||
$game_id = (int)$params['game_id'];
|
||||
$player_id = $_SESSION['current_player_id'] ?? 0;
|
||||
|
||||
// Vérification admin
|
||||
$stmt = $pdo->prepare("SELECT admin_id FROM games WHERE id = ?");
|
||||
$stmt->execute([$game_id]);
|
||||
$admin_id = $stmt->fetchColumn();
|
||||
|
||||
if ($admin_id == $player_id) {
|
||||
$pdo->prepare("DELETE FROM games WHERE id = ?")->execute([$game_id]);
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Action réservée à l\'admin']);
|
||||
}
|
||||
exit;
|
||||
|
||||
default:
|
||||
echo json_encode(['success' => false, 'error' => 'Action inconnue']);
|
||||
exit;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
:root {
|
||||
--night-blue: radial-gradient(circle, #1a2a6c 0%, #0a1128 100%);
|
||||
--gold: #d4af37;
|
||||
--neon-red: #ff3131;
|
||||
}
|
||||
|
||||
body {
|
||||
background: #050505;
|
||||
color: white;
|
||||
font-family: sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.yahtzee-table {
|
||||
background: var(--night-blue);
|
||||
border: 10px solid #2d0000;
|
||||
border-radius: 50px;
|
||||
padding: 30px;
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
text-align: center;
|
||||
box-shadow: 0 20px 40px rgba(0,0,0,0.8);
|
||||
}
|
||||
|
||||
.game-header h1 {
|
||||
color: var(--gold);
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.players-grid {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 20px;
|
||||
flex-wrap: wrap;
|
||||
margin: 30px 0;
|
||||
}
|
||||
|
||||
.player-card {
|
||||
padding: 15px;
|
||||
border-radius: 15px;
|
||||
background: rgba(0,0,0,0.3);
|
||||
border-bottom: 4px solid transparent;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.player-card.active {
|
||||
border-color: #fff;
|
||||
box-shadow: 0 0 15px rgba(255,255,255,0.2);
|
||||
}
|
||||
|
||||
.btn-admin-close {
|
||||
background: transparent;
|
||||
color: var(--neon-red);
|
||||
border: 1px solid var(--neon-red);
|
||||
padding: 5px 15px;
|
||||
border-radius: 20px;
|
||||
cursor: pointer;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.debug-zone { margin-top: 40px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 20px; }
|
||||
+26
-5
@@ -1,14 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||
<title>Partie - Yahtzee PAF</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel='stylesheet' type='text/css' media='screen' href='css/game.css'>
|
||||
<script src='main.js'></script>
|
||||
<link rel='stylesheet' type='text/css' media='screen' href='../css/game.css'>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<div class="main-wrapper">
|
||||
<div class="yahtzee-table table-main">
|
||||
<div class="table-inner">
|
||||
<header class="game-header">
|
||||
<h1 id="display-game-name">Chargement...</h1>
|
||||
<div id="admin-controls"></div>
|
||||
</header>
|
||||
|
||||
<div class="game-status">
|
||||
<h3>Joueurs à table :</h3>
|
||||
<div id="players-list" class="players-grid">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="debug-zone">
|
||||
<button class="btn-main-create" onclick="window.location.href='../index.html'">Retour Accueil</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src='../js/game.js'></script>
|
||||
</body>
|
||||
</html></html>
|
||||
</html>
|
||||
+71
-1
@@ -28,4 +28,74 @@ function updatePlayerInputs() {
|
||||
row.appendChild(nameInput);
|
||||
container.appendChild(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function SqlRequest(action, params = {}) {
|
||||
try {
|
||||
const response = await fetch('RequestsHandler.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
action: action,
|
||||
params: params
|
||||
})
|
||||
});
|
||||
|
||||
const resultat = await response.json();
|
||||
|
||||
if (resultat.success) {
|
||||
return resultat;
|
||||
} else {
|
||||
console.error("Erreur :", resultat.error);
|
||||
}
|
||||
} catch (erreur) {
|
||||
console.error("Erreur de communication :", erreur);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const loginForm = document.getElementById('config-form'); // Vérifie bien que l'ID match ton HTML
|
||||
|
||||
loginForm.addEventListener('submit', async function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
// 1. Récupération des infos de base
|
||||
const gameName = document.getElementById('game-name').value;
|
||||
const playerRows = document.querySelectorAll('.player-row');
|
||||
// 2. Création de la partie dans la BDD
|
||||
let response = await SqlRequest('createGame', {
|
||||
game_name: gameName
|
||||
});
|
||||
|
||||
if (response && response.success) {
|
||||
const gameId = parseInt(response.game_id);
|
||||
|
||||
// 3. Boucle sur les lignes de joueurs pour les ajouter un par un
|
||||
for (let i = 0; i < playerRows.length; i++) {
|
||||
const name = playerRows[i].querySelector('.player-input').value || `Joueur ${i + 1}`;
|
||||
const color = playerRows[i].querySelector('.color-picker').value;
|
||||
|
||||
// Le premier joueur (index 0) sera l'admin et commencera le tour (is_turn: 1)
|
||||
const isAdmin = (i === 0);
|
||||
const isTurn = (i === 0) ? 1 : 0;
|
||||
|
||||
await SqlRequest('addPlayer', {
|
||||
game_id: gameId,
|
||||
name: name,
|
||||
color: color,
|
||||
is_turn: isTurn,
|
||||
is_admin: isAdmin // Paramètre utilisé par le PHP pour l'admin_id
|
||||
});
|
||||
}
|
||||
|
||||
// 4. Redirection vers la table de jeu
|
||||
console.log("Partie créée avec succès, ID:", gameId);
|
||||
window.location.href = '../game.html?game_id=' + gameId;
|
||||
|
||||
} else {
|
||||
console.error("Erreur lors de la création de la partie :", response?.error);
|
||||
alert("Erreur : " + (response?.error || "Impossible de joindre le serveur"));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
async function SqlRequest(action, params = {}) {
|
||||
try {
|
||||
const response = await fetch('RequestsHandler.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
action: action,
|
||||
params: params
|
||||
})
|
||||
});
|
||||
|
||||
const resultat = await response.json();
|
||||
|
||||
if (resultat.success) {
|
||||
return resultat;
|
||||
} else {
|
||||
console.error("Erreur :", resultat.error);
|
||||
}
|
||||
} catch (erreur) {
|
||||
console.error("Erreur de communication :", erreur);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const gameId = urlParams.get('game_id');
|
||||
|
||||
if (!gameId) {
|
||||
alert("ID de partie manquant");
|
||||
window.location.href = '../index.html';
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupération des données via le RequestsHandler
|
||||
const response = await SqlRequest('getGameData', { game_id: gameId });
|
||||
|
||||
if (response && response.success) {
|
||||
const game = response.game;
|
||||
const players = response.players;
|
||||
const sessionPlayerId = response.session_player_id;
|
||||
|
||||
// 1. Nom de la partie
|
||||
document.getElementById('display-game-name').innerText = game.game_name;
|
||||
|
||||
// 2. Bouton Admin (si le joueur actuel est l'admin)
|
||||
if (game.admin_id == sessionPlayerId) {
|
||||
const adminDiv = document.getElementById('admin-controls');
|
||||
adminDiv.innerHTML = `<button class="btn-admin-close" onclick="closeGame(${gameId})">Fermer la table</button>`;
|
||||
}
|
||||
|
||||
// 3. Liste des joueurs
|
||||
const listContainer = document.getElementById('players-list');
|
||||
players.forEach(p => {
|
||||
const card = document.createElement('div');
|
||||
card.className = `player-card ${p.is_turn == 1 ? 'active' : ''}`;
|
||||
card.style.borderBottomColor = p.player_color;
|
||||
card.innerHTML = `
|
||||
<div style="color: ${p.player_color}; font-weight: bold;">${p.player_name}</div>
|
||||
<small>${p.is_turn == 1 ? '🎲 En train de jouer' : 'En attente'}</small>
|
||||
`;
|
||||
listContainer.appendChild(card);
|
||||
});
|
||||
|
||||
} else {
|
||||
console.error("Erreur chargement:", response?.error);
|
||||
}
|
||||
});
|
||||
|
||||
async function closeGame(id) {
|
||||
if(confirm("Voulez-vous fermer cette table ?")){
|
||||
const res = await SqlRequest('delete_game', { game_id: id });
|
||||
if(res.success) window.location.href = '../index.html';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user