118 lines
4.3 KiB
PHP
118 lines
4.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Point UNIQUE de détermination du statut d'un objet céleste.
|
|
* Basé sur les règles configurées dans l'administration.
|
|
*/
|
|
function calculateCelestialStatus($planet, $db, $statuses_map) {
|
|
$profile_id = $planet['status_profile_id'] ?? null;
|
|
|
|
// Si pas de profil sur la planète, on prend celui du type d'objet
|
|
if (!$profile_id && isset($planet['type'])) {
|
|
$stmt = $db->prepare("SELECT status_profile_id FROM celestial_object_types WHERE slug = ?");
|
|
$stmt->execute([$planet['type']]);
|
|
$profile_id = $stmt->fetchColumn();
|
|
}
|
|
|
|
// Si toujours pas de profil, on retourne le statut actuel (fallback alpha)
|
|
if (!$profile_id) {
|
|
return $planet['status'];
|
|
}
|
|
|
|
// Récupération des règles du profil par priorité
|
|
$stmt = $db->prepare("SELECT r.*, s.slug as status_slug
|
|
FROM celestial_object_status_rules r
|
|
JOIN celestial_object_statuses s ON r.status_id = s.id
|
|
WHERE r.profile_id = ? AND r.is_active = 1
|
|
ORDER BY r.priority DESC, r.id ASC");
|
|
$stmt->execute([$profile_id]);
|
|
$rules = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Préparation des données de l'objet pour l'évaluation
|
|
$orbital_controls = $planet['orbital_controls'] ?? [];
|
|
$terrestrial_controls = $planet['terrestrial_controls'] ?? [];
|
|
|
|
$orbital_factions = array_filter($orbital_controls, fn($v) => $v > 0);
|
|
$terrestrial_factions = array_filter($terrestrial_controls, fn($v) => $v > 0);
|
|
|
|
$orb_count = count($orbital_factions);
|
|
$terr_count = count($terrestrial_factions);
|
|
|
|
$orb_dom = null;
|
|
if ($orb_count > 0) {
|
|
arsort($orbital_factions);
|
|
$orb_dom = array_key_first($orbital_factions);
|
|
}
|
|
|
|
$terr_dom = null;
|
|
if ($terr_count > 0) {
|
|
arsort($terrestrial_factions);
|
|
$terr_dom = array_key_first($terrestrial_factions);
|
|
}
|
|
|
|
$is_empty = ($orb_count === 0 && $terr_count === 0);
|
|
|
|
// Évaluation des règles
|
|
foreach ($rules as $rule) {
|
|
$match = true;
|
|
|
|
// Condition Case Vide
|
|
if ($rule['is_empty_case'] && !$is_empty) $match = false;
|
|
if ($match && !$rule['is_empty_case'] && $is_empty && ($rule['orbital_count_op'] || $rule['terrestrial_count_op'] || $rule['orbital_dominance'] || $rule['terrestrial_dominance'])) {
|
|
// Si la règle n'est pas "case vide" mais qu'on a d'autres conditions et que c'est vide, ça ne matchera probablement pas
|
|
// Sauf si les conditions acceptent 0. On laisse la suite gérer.
|
|
}
|
|
|
|
// Condition Nombre Factions Orbite
|
|
if ($match && $rule['orbital_count_op']) {
|
|
$match = evaluateOperator($orb_count, $rule['orbital_count_op'], $rule['orbital_count_val']);
|
|
}
|
|
|
|
// Condition Nombre Factions Sol
|
|
if ($match && $rule['terrestrial_count_op']) {
|
|
$match = evaluateOperator($terr_count, $rule['terrestrial_count_op'], $rule['terrestrial_count_val']);
|
|
}
|
|
|
|
// Condition Dominance Orbite
|
|
if ($match && $rule['orbital_dominance']) {
|
|
if ($rule['orbital_dominance'] === 'ANY') {
|
|
if (!$orb_dom) $match = false;
|
|
} elseif ($rule['orbital_dominance'] === 'NONE') {
|
|
if ($orb_dom) $match = false;
|
|
} else {
|
|
if ($orb_dom != $rule['orbital_dominance']) $match = false;
|
|
}
|
|
}
|
|
|
|
// Condition Dominance Sol
|
|
if ($match && $rule['terrestrial_dominance']) {
|
|
if ($rule['terrestrial_dominance'] === 'ANY') {
|
|
if (!$terr_dom) $match = false;
|
|
} elseif ($rule['terrestrial_dominance'] === 'NONE') {
|
|
if ($terr_dom) $match = false;
|
|
} else {
|
|
if ($terr_dom != $rule['terrestrial_dominance']) $match = false;
|
|
}
|
|
}
|
|
|
|
if ($match) {
|
|
return $rule['status_slug'];
|
|
}
|
|
}
|
|
|
|
// Fallback final
|
|
return $planet['status'];
|
|
}
|
|
|
|
function evaluateOperator($val, $op, $target) {
|
|
switch ($op) {
|
|
case '=': return $val == $target;
|
|
case '>': return $val > $target;
|
|
case '<': return $val < $target;
|
|
case '>=': return $val >= $target;
|
|
case '<=': return $val <= $target;
|
|
case '!=': return $val != $target;
|
|
}
|
|
return true;
|
|
}
|