action_game.php (action: 'get_radar'). Reçoit un tableau JSON. Si le tableau contient des joueurs, met à jour la liste HTML #ready-players-list. Sinon, affiche un message "Aucun signal".
function updateRadar() {
$.ajax({
url: 'api/action_game.php',
type: 'POST',
data: { action: 'get_radar' },
dataType: 'json',
success: function(res) {
if(res.success && res.players) {
const list = $('#ready-players-list');
const countSpan = $('#player-count');
const msg = $('#no-players-msg');
list.empty();
countSpan.text(res.players.length);
if (res.players.length === 0) msg.show().text("Aucun signal détecté...");
else {
msg.hide();
res.players.forEach(p => {
let nameStyle = p.is_me ? 'color: #63b3ed; font-weight: bold;' : 'color: #e2e8f0;';
let suffix = p.is_me ? ' (Vous)' : '';
list.append(`${p.login_user}${suffix} `);
});
}
}
}
});
}
opponent_id est renvoyé, redirige immédiatement vers univers.php. Sinon, met à jour l'état local (lobbyState) et rafraîchit l'apparence des boutons.
function checkLobbyStatus() {
$.ajax({
url: 'api/action_game.php',
type: 'POST',
data: { action: 'check_lobby_status' },
dataType: 'json',
success: function(data) {
if (data.opponent_id) window.location.href = 'univers.php';
lobbyState = data.am_i_ready;
updateActionBar();
}
});
}
myFleet) en un tableau simple d'IDs pour le PHP. Envoie la requête AJAX et gère les erreurs.
$('#mainActionBtn').click(function() {
let currentAction = (lobbyState === 1) ? 'cancel_lobby' : 'join_lobby';
let requestData = { action: currentAction };
if (currentAction === 'join_lobby') {
// On formate la flotte en un tableau d'objets simple pour PHP
requestData.fleet = myFleet.map(s => ({ ship_id: s.shipId }));
}
$.ajax({
url: 'api/action_game.php',
type: 'POST',
data: requestData,
dataType: 'json',
success: function(resp) {
if(!resp.success && resp.msg) {
alert("Erreur: " + resp.msg);
}
checkLobbyStatus();
},
error: function() {
alert("Erreur de connexion au QG.");
}
});
});
DB_SHIPS pour ne garder que les vaisseaux débloqués.sort(0.5 - Math.random()) pour mélanger.myFleet.
function fillFleetRandomly() {
myFleet = [];
let unlockedShips = DB_SHIPS.filter(s => USER_VICTORIES >= s.victoires_necessaire);
if (unlockedShips.length < 5) {
alert("Pas assez de vaisseaux débloqués (min 5).");
return;
}
let shuffled = [...unlockedShips].sort(() => 0.5 - Math.random());
shuffled.slice(0, 5).forEach(s => {
myFleet.push({ shipId: s.id, shipName: s.nom, type: s.type, hp: s.points_vie });
});
renderFleet();
}
myFleet et injecte le HTML de chaque carte vaisseau. Si la flotte n'est pas pleine, ajoute une carte "Slot Vide" cliquable.
function renderFleet() {
const container = $('#fleet-container');
container.empty();
myFleet.forEach((slot, index) => {
container.append(`
${slot.shipName}
${slot.type.toUpperCase()}
PV: ${slot.hp}
`);
});
if (myFleet.length < 5) {
container.append(`
+
AJOUTER
${myFleet.length + 1} / 5
`);
}
updateActionBar();
}
myFleet.
add : Vérifie les conditions de victoire. Push dans le tableau et ferme la modale.remove : Utilise splice pour supprimer l'élément à l'index donné.
window.addShipToFleet = function(id) {
let s = DB_SHIPS.find(x => x.id == id);
if (USER_VICTORIES < s.victoires_necessaire) return;
myFleet.push({ shipId: s.id, shipName: s.nom, type: s.type, hp: s.points_vie });
closeModals();
renderFleet();
};
window.removeShip = function(index) {
myFleet.splice(index, 1);
renderFleet();
};
DB_SHIPS. Applique les filtres de recherche. Vérifie si le vaisseau est verrouillé (pas assez de victoires). Génère le tableau HTML avec boutons "Choisir" ou "Verrouillé".
function renderShipTable() {
const tbody = $('#shipTableBody');
tbody.empty();
let filter = $('#searchShip').val().toLowerCase();
let takenIds = myFleet.map(s => s.shipId);
DB_SHIPS.forEach(ship => {
if (filter && !ship.nom.toLowerCase().includes(filter) && !ship.type.toLowerCase().includes(filter)) return;
if (takenIds.includes(ship.id)) return;
let isLocked = USER_VICTORIES < ship.victoires_necessaire;
let btn = isLocked
? ``
: ``;
tbody.append(`
${ship.nom}
${ship.type}
${ship.points_vie}
${btn}
`);
});
}
function updateActionBar() {
let btn = $('#mainActionBtn');
let txt = $('#status-text');
btn.removeClass('btn-play btn-cancel btn-disabled').prop('disabled', false);
if (lobbyState === 1) {
btn.text("ANNULER RECHERCHE").addClass('btn-cancel');
txt.text("RECHERCHE D'ADVERSAIRE...");
$('.btn-remove, .slot-empty').css('pointer-events', 'none').css('opacity', '0.5');
return;
}
$('.btn-remove, .slot-empty').css('pointer-events', 'auto').css('opacity', '1');
if (myFleet.length === 5) {
btn.text("LANCER LE COMBAT").addClass('btn-play');
txt.text("FLOTTE OPÉRATIONNELLE");
} else {
btn.text(`EN ATTENTE (${myFleet.length}/5)`).addClass('btn-disabled').prop('disabled', true);
txt.text(`Sélectionnez 5 vaisseaux.`);
}
}
window.sortTable = function(tableId, n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById(tableId);
switching = true; dir = "asc";
while (switching) {
switching = false; rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
let xVal = isNaN(x.innerHTML) ? x.innerHTML.toLowerCase() : parseFloat(x.innerHTML);
let yVal = isNaN(y.innerHTML) ? y.innerHTML.toLowerCase() : parseFloat(y.innerHTML);
if (dir == "asc") { if (xVal > yVal) { shouldSwitch = true; break; } }
else if (dir == "desc") { if (xVal < yVal) { shouldSwitch = true; break; } }
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true; switchcount++;
} else {
if (switchcount == 0 && dir == "asc") { dir = "desc"; switching = true; }
}
}
};