Commit 93bd1cc9 authored by Sylvain Hatier's avatar Sylvain Hatier Committed by Arnaud Bey
Browse files

Fix Combo, interface Admin

parent 5428857a
......@@ -106,6 +106,8 @@ class DeleteLexiconCommand extends ContainerAwareCommand
$fileSystem->remove($bigramPath);
$output->writeln('<comment>Suppression fichier bigrammes</comment>');
//TODO supp acquisition, et rules (ComboPoints et WordLength)
$output->writeln('<info>########### SUPPRESSION LANGUE OK ############</info>');
} else {
echo 'Argument missing';
......
......@@ -2,6 +2,7 @@
namespace LexiconBundle\Entity;
use MagicWordBundle\Entity\Rules\ComboPoints;
use MagicWordBundle\Entity\Rules\WordLength;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -25,6 +26,8 @@ class Language
*/
private $value;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
......@@ -55,6 +58,11 @@ class Language
*/
private $wordLengths;
/**
* @ORM\OneToMany(targetEntity="MagicWordBundle\Entity\Rules\ComboPoints", mappedBy="language", cascade={"remove"})
*/
private $comboPoints;
/**
* @ORM\Column(type="string", length=255)
*/
......@@ -66,6 +74,7 @@ class Language
$this->features = new ArrayCollection();
$this->letters = new ArrayCollection();
$this->wordLengths = new ArrayCollection();
$this->comboPoints = new ArrayCollection();
}
public function getId(): ?int
......@@ -214,6 +223,38 @@ class Language
return $this;
}
/**
* @return Collection|ComboPoints[]
*/
public function getComboPoints(): Collection
{
return $this->comboPoints;
}
public function addComboPoints(ComboPoints $comboPoints): self
{
if (!$this->comboPoints->contains($comboPoints)) {
$this->comboPoints[] = $comboPoints;
$comboPoints->setLanguage($this);
}
return $this;
}
public function removeComboPoints(ComboPoints $comboPoints): self
{
if ($this->comboPoints->contains($comboPoints)) {
$this->comboPoints->removeElement($comboPoints);
// set the owning side to null (unless already changed)
if ($comboPoints->getLanguage() === $this) {
$comboPoints->setLanguage(null);
}
}
return $this;
}
public function getDirectory(): ?string
{
return $this->directory;
......@@ -225,4 +266,31 @@ class Language
return $this;
}
/**
* Add comboPoint.
*
* @param \MagicWordBundle\Entity\Rules\ComboPoints $comboPoint
*
* @return Language
*/
public function addComboPoint(\MagicWordBundle\Entity\Rules\ComboPoints $comboPoint)
{
$this->comboPoints[] = $comboPoint;
return $this;
}
/**
* Remove comboPoint.
*
* @param \MagicWordBundle\Entity\Rules\ComboPoints $comboPoint
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeComboPoint(\MagicWordBundle\Entity\Rules\ComboPoints $comboPoint)
{
return $this->comboPoints->removeElement($comboPoint);
}
}
......@@ -46,10 +46,7 @@ class Word implements JsonSerializable
*/
private $root;
/**
* @ORM\Column(type="integer")
*/
private $points;
public function __construct()
{
......@@ -135,17 +132,6 @@ class Word implements JsonSerializable
return $this;
}
public function getPoints(): ?int
{
return $this->points;
}
public function setPoints(int $points): self
{
$this->points = $points;
return $this;
}
public function jsonSerialize()
{
......
......@@ -16,7 +16,7 @@ class ImportManager
private $fm;
private $lm;
private $rm;
private $wlm;
private $rulesManager;
private $em;
private $letterm;
private $bgm;
......@@ -31,10 +31,10 @@ class ImportManager
* "rm" = @DI\Inject("lexicon_manager.root"),
* "bgm" = @DI\Inject("lexicon_manager.bigram"),
* "letterm" = @DI\Inject("lexicon_manager.letter"),
* "wlm" = @DI\Inject("mw_manager.wordLength"),
* "rulesManager" = @DI\Inject("mw_manager.rules"),
* })
*/
public function __construct($dir, $em, $wm, $fm, $lm, $rm, $wlm, $bgm, $letterm)
public function __construct($dir, $em, $wm, $fm, $lm, $rm, $rulesManager, $bgm, $letterm)
{
$this->dir = $dir;
$this->em = $em;
......@@ -44,7 +44,7 @@ class ImportManager
$this->rm = $rm;
$this->bgm = $bgm;
$this->letterm = $letterm;
$this->wlm = $wlm;
$this->rulesManager = $rulesManager;
}
public function importLexicon($pathLexiconDir)
......@@ -71,6 +71,7 @@ class ImportManager
$rewriteTo=[];
$weights=[];
$lengths=[];
$combos=[];
$languageProperty=[];
$languageProperty["directory"]=$pathLexiconDir;
$specs["directory"]=$pathLexiconDir;
......@@ -106,7 +107,14 @@ class ImportManager
foreach ($weightsNLengths as $weightsNLength) {
$tab = explode("=", $weightsNLength);
$lengths[$tab[0]] = $tab[1];
$this->wlm->findOrCreate($language, $tab[0], $tab[1]);
$this->rulesManager->findOrCreateWordLength($language, $tab[0], $tab[1]);
}
} elseif (preg_match_all("/^COMBOPOINTS:(.+)\s*$/", $line, $matches)) {
$comboNpoints = explode(",", $matches[1][0]);
foreach ($comboNpoints as $comboNpoint) {
$tab = explode("=", $comboNpoint);
$combos[$tab[0]] = $tab[1];
$this->rulesManager->findOrCreateCombo($language, $tab[0], $tab[1]);
}
}
}
......@@ -114,6 +122,7 @@ class ImportManager
$specs["rewriteTo"] = $rewriteTo;
$specs["weights"] = $weights;
$specs["lengths"] = $lengths;
$specs["combos"] = $combos;
return $specs;
}
......@@ -154,8 +163,6 @@ class ImportManager
$string2print.=" / EREGI = ".$cleanWordValue."\n";
// Gestion de la root
$root = $this->rm->findOrCreate($language, $rootValue, $roots);
// Gestion des points
$points = $this->wm->calculatePoints($cleanWordValue, $weights, $lengths);
// Gestion des features
$labelsNValues = explode(",", $matches[3][0]);
......@@ -164,7 +171,7 @@ class ImportManager
$featureStringTab = explode("=", $labelNValue);
$features[] = $this->fm->findOrCreate($language, $featureStringTab[0], $featureStringTab[1]);
}
$this->wm->create($language, $root, $features, $wordValue, $cleanWordValue, $points);
$this->wm->create($language, $root, $features, $wordValue, $cleanWordValue);
// Gestion des lettres et débuts de mots
$wordsLetters = preg_split('//u', $cleanWordValue, null, PREG_SPLIT_NO_EMPTY);
......
......@@ -3,8 +3,10 @@
namespace LexiconBundle\Manager;
use LexiconBundle\Entity\Language;
use LexiconBundle\Entity\Letter;
use LexiconBundle\Entity\Word;
use LexiconBundle\Entity\WordStart;
use MagicWordBundle\Entity\Rules\WordLength;
use JMS\DiExtraBundle\Annotation as DI;
/**
......@@ -24,7 +26,7 @@ class WordManager
$this->em = $em;
}
public function create(Language $language, $root, $features, $value, $cleanValue, $points)
public function create(Language $language, $root, $features, $value, $cleanValue)
{
$word = new Word;
$word->setLanguage($language);
......@@ -34,7 +36,6 @@ class WordManager
}
$word->setValue($value);
$word->setCleanValue($cleanValue);
$word->setPoints($points);
$this->em->persist($word);
......@@ -65,21 +66,25 @@ class WordManager
return;
}
public function calculatePoints($str, $weights, $lengths)
public function recalculate(Word $word)
{
$points = 0;
$language = $word->getLanguage();
$wordPoints=0;
$str=$word->getCleanValue();
$chars = preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
$points += (isset($weights[$char]))
? $weights[$char]
: 1;
if ($pointChar = $this->em->getRepository(Letter::class)->findOneBy(["language" => $language, "value" => $char])) {
$wordPoints += $pointChar->getPoints();
} else {
$wordPoints += 1;
}
}
$length = mb_strlen($str);
$points += (isset($lengths[$length]))
? $lengths[$length]
: $length;
if ($pointLength = $this->em->getRepository(WordLength::class)->findOneBy(["language" => $language, "length" => $length])) {
$wordPoints += $pointLength->getPoints();
}
return $points;
return $wordPoints;
}
}
......@@ -69,11 +69,12 @@ class ActivityController extends Controller
/**
* @Route("/add-combo-points/round/{roundId}/length/{length}", name="add_combopoints", options={"expose"=true})
* @ParamConverter("round", class="MagicWordBundle:Round", options={"id" = "roundId"})
* @ParamConverter("comboPoints", class="MagicWordBundle:Rules\ComboPoints", options={"length" = "length"})
* @Method("POST")
*/
public function addComboPointsAction(Round $round, ComboPoints $comboPoints)
public function addComboPointsAction(Round $round, $length)
{
$language = $round->getLanguage();
$comboPoints = $this->getDoctrine()->getRepository('MagicWordBundle:Rules\ComboPoints')->findOneBy(["length"=>$length, "language"=>$language]);
$this->get('mw_manager.activity')->addComboPoints($round, $comboPoints);
return new JsonResponse(['points' => $comboPoints->getPoints()]);
......@@ -82,11 +83,12 @@ class ActivityController extends Controller
/**
* @Route("/save-combo-finished/round/{roundId}/length/{length}", name="save_combo_finished", options={"expose"=true})
* @ParamConverter("round", class="MagicWordBundle:Round", options={"id" = "roundId"})
* @ParamConverter("comboPoints", class="MagicWordBundle:Rules\ComboPoints", options={"length" = "length"})
* @Method("POST")
*/
public function saveComboFinishedAction(Round $round, ComboPoints $comboPoints)
public function saveComboFinishedAction(Round $round, $length)
{
$language = $round->getLanguage();
$comboPoints = $this->getDoctrine()->getRepository('MagicWordBundle:Rules\ComboPoints')->findOneBy(["length"=>$length, "language"=>$language]);
$this->get('mw_manager.activity')->saveComboFinished($round, $comboPoints);
return new JsonResponse(['points' => $comboPoints->getPoints()]);
......
......@@ -2,9 +2,10 @@
namespace MagicWordBundle\Controller;
use MagicWordBundle\Entity\Letter\LetterLanguage;
use LexiconBundle\Entity\Language;
use LexiconBundle\Entity\Letter;
use MagicWordBundle\Entity\Rules\ComboPoints;
use MagicWordBundle\Entity\Rules\WordLengthPoints;
use MagicWordBundle\Entity\Rules\WordLength;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
......@@ -13,7 +14,7 @@ use Symfony\Component\HttpFoundation\Request;
class AdministrationController extends Controller
{
/**
* @Route("/administration/index", name="admin_index")
* @Route("/administration", name="admin_index")
*/
public function indexAction()
{
......@@ -21,101 +22,98 @@ class AdministrationController extends Controller
}
/**
* @Route("/administration/wordlengthpoints", name="wordlengthpoints")
* @Route("/administration/{id}/wordlengths", name="wordlengths")
*/
public function wordLengthPointsAction()
public function wordLengthAction(Language $language)
{
$wordlengthpoints = $this->getDoctrine()->getRepository('MagicWordBundle:Rules\WordLengthPoints')->findAll();
$wordlengths = $this->getDoctrine()->getRepository(WordLength::class)->findByLanguage($language);
return $this->render('MagicWordBundle:Administration:wordlengthpoints.html.twig', ['wordlengthpoints' => $wordlengthpoints]);
return $this->render('MagicWordBundle:Administration:wordlength.html.twig', ['wordlengths' => $wordlengths]);
}
/**
* @Route("/administration/combopoints", name="combopoints")
* @Route("/administration/{id}/combopoints", name="combopoints")
*/
public function comboPointsAction()
public function comboPointsAction(Language $language)
{
$combopoints = $this->getDoctrine()->getRepository('MagicWordBundle:Rules\ComboPoints')->findAll();
$combopoints = $this->getDoctrine()->getRepository(ComboPoints::class)->findByLanguage($language);
return $this->render('MagicWordBundle:Administration:combopoints.html.twig', ['combopoints' => $combopoints]);
}
/**
* @Route("/administration/wordlengthpoints/{id}", name="wordlengthpoint_edit")
* @Route("/administration/wordlength/{id}", name="wordlength_edit")
* @Method("GET")
*/
public function wordLengthPointEditAction(WordLengthPoints $wordlengthpoint)
public function wordLengthEditAction(WordLength $wordlength)
{
$form = $this->get('mw_manager.administration')->getWordLengthPointForm($wordlengthpoint);
$form = $this->get('mw_manager.rules')->getWordLengthForm($wordlength);
return $this->render('MagicWordBundle:Administration:wordlengthpoint-edit.html.twig', ['form' => $form, 'wordlengthpoint' => $wordlengthpoint]);
return $this->render('MagicWordBundle:Administration:wordlength-edit.html.twig', ['form' => $form, 'wordlength' => $wordlength]);
}
/**
* @Route("/administration/combopoints/{id}", name="combopoint_edit")
* @Route("/administration/combopoint/{id}", name="combopoint_edit")
* @Method("GET")
*/
public function comboPointEditAction(ComboPoints $combopoint)
{
$form = $this->get('mw_manager.administration')->getComboPointForm($combopoint);
$form = $this->get('mw_manager.rules')->getComboPointForm($combopoint);
return $this->render('MagicWordBundle:Administration:wordlengthpoint-edit.html.twig', ['form' => $form, 'wordlengthpoint' => $combopoint]);
return $this->render('MagicWordBundle:Administration:combopoint-edit.html.twig', ['form' => $form, 'combopoints' => $combopoint]);
}
/**
* @Route("/administration/wordlengthpoints/{id}", name="wordlengthpoint_submit")
* @Route("/administration/wordlength/{id}", name="wordlength_submit")
* @Method("POST")
*/
public function wordLengthPointSubmitAction(WordLengthPoints $wordlengthpoint, Request $request)
public function wordLengthSubmitAction(WordLength $wordlength, Request $request)
{
$this->get('mw_manager.administration')->handleWordLengthPointForm($wordlengthpoint, $request);
$this->get('mw_manager.rules')->handleWordLengthForm($wordlength, $request);
return $this->redirectToRoute('wordlengthpoints');
return $this->redirectToRoute('wordlengths', ['id' => $wordlength->getLanguage()->getId()]);
}
/**
* @Route("/administration/combopoints/{id}", name="combopoint_submit")
* @Route("/administration/combopoint/{id}", name="combopoint_submit")
* @Method("POST")
*/
public function comboPointSubmitAction(ComboPoints $combopoint, Request $request)
{
$this->get('mw_manager.administration')->handleComboPointForm($combopoint, $request);
$this->get('mw_manager.rules')->handleComboPointForm($combopoint, $request);
return $this->redirectToRoute('combopoints');
return $this->redirectToRoute('combopoints', ['id' => $combopoint->getLanguage()->getId()]);
}
/**
* @Route("/administration/letterslanguage", name="letterslanguage")
* @Route("/administration/letters/{id}", name="letters")
*/
public function languageLetterPointsAction()
public function letterPointsAction(Language $language)
{
$letterslanguage = $this->getDoctrine()->getRepository('MagicWordBundle:Letter\LetterLanguage')->findAll();
$letters = $this->getDoctrine()->getRepository('LexiconBundle:Letter')->findBy(["language" => $language], ["value" => "ASC"]);
return $this->render('MagicWordBundle:Administration:letterslanguage.html.twig', ['letterslanguage' => $letterslanguage]);
return $this->render('MagicWordBundle:Administration:letterslanguage.html.twig', ['letters' => $letters]);
}
/**
* @Route("/administration/letterlanguage/{id}", name="letterlanguage_edit")
* @Route("/administration/letter/{id}", name="letter_edit")
* @Method("GET")
*/
public function languageLetterPointsEditAction(LetterLanguage $letterlanguage)
public function letterPointsEditAction(Letter $letter)
{
$form = $this->get('mw_manager.administration')->getLetterLanguageForm($letterlanguage);
$form = $this->get('mw_manager.rules')->getLetterForm($letter);
return $this->render('MagicWordBundle:Administration:letterslanguage-edit.html.twig', ['letterlanguage' => $letterlanguage, 'form' => $form]);
return $this->render('MagicWordBundle:Administration:letterslanguage-edit.html.twig', ['letter' => $letter, 'form' => $form]);
}
/**
* @Route("/administration/letterlanguage/{id}", name="letterlanguage_submit")
* @Route("/administration/letter/{id}", name="letter_submit")
* @Method("POST")
*/
public function languageLetterPointsSubmitAction(LetterLanguage $letterlanguage, Request $request)
public function letterPointsSubmitAction(Letter $letter, Request $request)
{
$this->get('mw_manager.administration')->handleLetterLanguageForm($letterlanguage, $request);
$this->get('mw_manager.rules')->handleLetterLanguageForm($letter, $request);
return $this->redirectToRoute('letterslanguage');
return $this->redirectToRoute('letters', ['id' => $letter->getLanguage()->getId()]);
}
......
......@@ -2,6 +2,7 @@
namespace MagicWordBundle\Entity\Rules;
use LexiconBundle\Entity\Language;
use Doctrine\ORM\Mapping as ORM;
/**
......@@ -35,6 +36,12 @@ class ComboPoints
*/
private $points;
/**
* @ORM\ManyToOne(targetEntity="LexiconBundle\Entity\Language", inversedBy="wordLengths")
* @ORM\JoinColumn(nullable=false)
*/
private $language;
/**
* Get id.
*
......@@ -92,4 +99,16 @@ class ComboPoints
{
return $this->points;
}
public function getLanguage(): ?Language
{
return $this->language;
}
public function setLanguage(?Language $language): self
{
$this->language = $language;
return $this;
}
}
<?php
namespace MagicWordBundle\Entity\Rules;
use Doctrine\ORM\Mapping as ORM;
/**
* Grid.
*
* @ORM\Table(name="word_length_points")
* @ORM\Entity()
*/
class WordLengthPoints
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="length", type="integer")
*/
private $length;
/**
* @var int
*
* @ORM\Column(name="points", type="integer")
*/
private $points;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set length
*
* @param integer $length
*
* @return WordLengthPoints
*/
public function setLength($length)
{
$this->length = $length;
return $this;
}
/**
* Get length
*
* @return integer
*/
public function getLength()
{
return $this->length;
}
/**
* Set points
*
* @param integer $points
*
* @return WordLengthPoints
*/
public function setPoints($points)
{
$this->points = $points;