sauvegarder que les définitions

This commit is contained in:
Adrien MALINGREY 2025-05-11 01:57:43 +02:00
parent c4e98016b4
commit ee76bbfc61
2 changed files with 46 additions and 91 deletions

View File

@ -31,7 +31,7 @@ class Grille implements ArrayAccess
public $lignes = []; public $lignes = [];
public $colonnes = []; public $colonnes = [];
public $valide = false; public $valide = false;
private $id; public $definitions = [];
public function __construct($hauteur, $largeur) public function __construct($hauteur, $largeur)
{ {
@ -73,6 +73,28 @@ class Grille implements ArrayAccess
$grilles->current(); $grilles->current();
if ($grilles->valid()) { if ($grilles->valid()) {
$this->definitions = [
"horizontales" => [],
"verticales" => []
];
foreach($this->lignes as $y => $mots) {
$this->definitions["horizontales"][$y] = [];
foreach($mots as $mot) {
$definitions = $this->dico[strlen($mot)][$mot];
if (count($definitions)) {
$this->definitions["horizontales"][$y][] = $definitions[mt_rand(0, count($definitions) - 1)];
}
}
}
foreach($this->colonnes as $x => $mots) {
$this->definitions["verticales"][$x] = [];
foreach($mots as $mot) {
$definitions = $this->dico[strlen($mot)][$mot];
if (count($definitions)) {
$this->definitions["verticales"][$x][] = $definitions[mt_rand(0, count($definitions) - 1)];
}
}
}
$this->save($id); $this->save($id);
return true; return true;
} else { } else {
@ -181,22 +203,8 @@ class Grille implements ArrayAccess
session_id("$this->largeur,$this->hauteur,$id"); session_id("$this->largeur,$this->hauteur,$id");
session_start(["use_cookies" => false]); session_start(["use_cookies" => false]);
$_SESSION["grille"] = (string)$this; $_SESSION["grille"] = $this->grille;
$_SESSION["dico"] = []; $_SESSION["definitions"] = $this->definitions;
foreach ($this->lignes as $y => $mots) {
foreach($mots as $mot) {
$longueur = strlen($mot);
if (!isset($_SESSION["dico"][$longueur])) $_SESSION["dico"][$longueur] = [];
$_SESSION["dico"][$longueur][$mot] = $this->dico[$longueur][$mot];
}
}
foreach ($this->colonnes as $y => $mots) {
foreach($mots as $mot) {
$longueur = strlen($mot);
if (!isset($_SESSION["dico"][$longueur])) $_SESSION["dico"][$longueur] = [];
$_SESSION["dico"][$longueur][$mot] = $this->dico[$longueur][$mot];
}
}
} }
public function load($id) public function load($id)
@ -208,27 +216,8 @@ class Grille implements ArrayAccess
return false; return false;
} }
foreach (explode(PHP_EOL, $_SESSION["grille"]) as $y => $ligne) { $this->grille = $_SESSION["grille"];
foreach (str_split($ligne) as $x => $lettre) { $this->definitions = $_SESSION["definitions"];
$this->grille[$y][$x] = $lettre;
}
}
for ($y = 0; $y < $this->hauteur; $y++) {
$mots = explode(CASE_NOIRE, $this->get_ligne($y, $this->largeur));
$this->lignes[$y] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2;
});
}
for ($x = 0; $x < $this->largeur; $x++) {
$mots = explode(CASE_NOIRE, $this->get_colonne($x, $this->hauteur));
$this->colonnes[$x] = array_filter($mots, function ($mot) {
return strlen($mot) >= 2;
});
}
$this->dico = $_SESSION["dico"];
return true; return true;
} }

View File

@ -42,15 +42,7 @@ if (!isset($_GET["grille"]) || $_GET["grille"] == "") {
$grille_valide = $grille->load($id) || $grille->genere($id); $grille_valide = $grille->load($id) || $grille->genere($id);
} }
mt_srand(crc32($id)); function formatter_definition($definition) {
if ($grille_valide) {
$definitions_horizontales = [];
for ($y = 0; $y < $hauteur; $y++) {
$definitions_horizontales[$y] = [];
foreach ($grille->lignes[$y] as $mot) {
$definitions = $grille->dico[strlen($mot)][$mot];
if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)];
if (strpos($definition, "#") !== false) { if (strpos($definition, "#") !== false) {
[$definition, $nb_mots] = explode("#", $definition); [$definition, $nb_mots] = explode("#", $definition);
$nb_mots = " <small>($nb_mots mots)</small>"; $nb_mots = " <small>($nb_mots mots)</small>";
@ -63,33 +55,7 @@ if ($grille_valide) {
} else { } else {
$auteur = ""; $auteur = "";
} }
$definitions_horizontales[$y][] = $definition; return $definition;
}
}
}
$definitions_verticales = [];
for ($x = 0 ; $x < $largeur; $x++) {
$definitions_verticales[$x] = [];
foreach ($grille->colonnes[$x] as $mot) {
$definitions = $grille->dico[strlen($mot)][$mot];
if (count($definitions)) {
$definition = $definitions[mt_rand(0, count($definitions) - 1)];
if (strpos($definition, "#") !== false) {
[$definition, $nb_mots] = explode("#", $definition);
$nb_mots = " <small>($nb_mots mots)</small>";
} else {
$nb_mots = "";
}
if (strpos($definition, "@") !== false) {
[$definition, $auteur] = explode("@", $definition);
$auteur = " <small><em>$auteur</em></small>";
} else {
$auteur = "";
}
$definitions_verticales[$x][] = $definition . $nb_mots . $auteur;
}
}
}
} }
?> ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
@ -168,7 +134,7 @@ if ($grille_valide) {
<?php else: ?> <?php else: ?>
<td class="case blanche"> <td class="case blanche">
<input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>" <input id="<?= chr($x + 65) . ($y + 1) ?>" type="text" maxlength="1" size="1" pattern="[A-Z]" placeholder="<?= $grille[$y][$x] ?>"
title="<?= strip_tags(" " . implode("\n→ ", $definitions_horizontales[$y]) . "\n↓ " . implode("\n↓ ", $definitions_verticales[$x])) ?>" /> title="<?= strip_tags(" " . implode("\n→ ", array_map("formatter_definition", $grille->definitions["horizontales"][$y])) . "\n↓ " . implode("\n↓ ", array_map("formatter_definition", $grille->definitions["verticales"][$x]))) ?>" />
</td> </td>
<?php endif; ?> <?php endif; ?>
<?php endfor; ?> <?php endfor; ?>
@ -179,15 +145,15 @@ if ($grille_valide) {
<div class="definitions horizontales"> <div class="definitions horizontales">
<h2>Horizontalement</h2> <h2>Horizontalement</h2>
<ol type="1"> <ol type="1">
<?php foreach ($definitions_horizontales as $y => $definitions): ?> <?php foreach ($grille->definitions["horizontales"] as $y => $definitions): ?>
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?> <?= formatter_definition($definitions[0]) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li> <li><?= formatter_definition($definition) ?></li>
<?php endforeach ?> <?php endforeach ?>
</ol> </ol>
<?php endif ?> <?php endif ?>
@ -199,15 +165,15 @@ if ($grille_valide) {
<div class="definitions verticales"> <div class="definitions verticales">
<h2>Verticalement</h2> <h2>Verticalement</h2>
<ol type="A"> <ol type="A">
<?php foreach ($definitions_verticales as $x => $definitions): ?> <?php foreach ($grille->definitions["verticales"] as $x => $definitions): ?>
<li> <li>
<?php if (count($definitions)): ?> <?php if (count($definitions)): ?>
<?php if (count($definitions) == 1): ?> <?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?> <?= formatter_definition($definitions[0]) ?>
<?php else: ?> <?php else: ?>
<ol> <ol>
<?php foreach ($definitions as $definition) : ?> <?php foreach ($definitions as $definition) : ?>
<li><?= $definition ?></li> <li><?= formatter_definition($definition) ?></li>
<?php endforeach ?> <?php endforeach ?>
</ol> </ol>
<?php endif ?> <?php endif ?>