PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/braldahim/library/Bral/Competences/Entretenir.php

https://github.com/Canop/braldahim
PHP | 204 lines | 158 code | 38 blank | 8 comment | 47 complexity | 7a2ef5a2f2daa54959ffd6adc32f2cbc MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of Braldahim, under Gnu Public Licence v3.
  4. * See licence.txt or http://www.gnu.org/licenses/gpl-3.0.html
  5. * Copyright: see http://www.braldahim.com/sources
  6. */
  7. class Bral_Competences_Entretenir extends Bral_Competences_Competence {
  8. function prepareCommun() {
  9. Zend_Loader::loadClass("Champ");
  10. Zend_Loader::loadClass("Bral_Util_Communaute");
  11. Zend_Loader::loadClass("TypeLieu");
  12. Zend_Loader::loadClass("Bral_Util_Messagerie");
  13. if ($this->verificationChamp() == false) {
  14. return null;
  15. }
  16. $this->verificationChamp();
  17. $this->preparePositions();
  18. }
  19. private function verificationChamp() {
  20. $this->view->entretenirChampOk = false;
  21. $champTable = new Champ();
  22. $niveauGrenier = Bral_Util_Communaute::getNiveauDuLieu($this->view->user->id_fk_communaute_braldun, TypeLieu::ID_TYPE_GRENIER);
  23. if ($niveauGrenier != null && $niveauGrenier >= Bral_Util_Communaute::NIVEAU_GRENIER_RECOLTER) {
  24. $champs = $champTable->findByCase($this->view->user->x_braldun, $this->view->user->y_braldun, $this->view->user->z_braldun, null, null, $this->view->user->id_fk_communaute_braldun);
  25. } else {
  26. $champs = $champTable->findByCase($this->view->user->x_braldun, $this->view->user->y_braldun, $this->view->user->z_braldun, $this->view->user->id_braldun);
  27. }
  28. $retour = false;
  29. if (count($champs) == 1) {
  30. $this->view->champ = $champs[0];
  31. if ($this->view->champ["phase_champ"] == "seme") {
  32. $this->view->entretenirChampOk = true;
  33. $this->idProprietaire = $this->view->champ["id_braldun"];
  34. $retour = true;
  35. }
  36. }
  37. return $retour;
  38. }
  39. private function preparePositions() {
  40. $selection = $this->request->get("position"); // si l'on vient de la vue (clic sur l'icone marcher)
  41. $tabPositions = null;
  42. for ($i = 1; $i <= 10; $i++) {
  43. for ($j = 1; $j <= 10; $j++) {
  44. $selected = "";
  45. if ($selection == $i . "-" . $j) {
  46. $selected = "selected";
  47. }
  48. $tabPositions[$i . 't' . $j] = array(
  49. 'possible' => true,
  50. 'selected' => $selected,
  51. 'x' => $i,
  52. 'y' => $j,
  53. );
  54. }
  55. }
  56. Zend_Loader::loadClass("ChampTaupe");
  57. $champTaupeTable = new ChampTaupe();
  58. $taupes = $champTaupeTable->findByIdChamp($this->view->champ["id_champ"]);
  59. $tabTaupes = array();
  60. if ($this->view->champ["phase_champ"] == 'seme') {
  61. if ($taupes != null) {
  62. foreach ($taupes as $t) {
  63. if ($t["etat_champ_taupe"] != 'vivant') {
  64. $tabPositions[$t["x_champ_taupe"] . 't' . $t["y_champ_taupe"]]["possible"] = false;
  65. $tabPositions[$t["x_champ_taupe"] . 't' . $t["y_champ_taupe"]]["selected"] = "";
  66. }
  67. }
  68. }
  69. }
  70. $this->view->positions = $tabPositions;
  71. }
  72. function prepareFormulaire() {
  73. if ($this->view->assezDePa == false) {
  74. return;
  75. }
  76. }
  77. function prepareResultat() {
  78. // Verification des Pa
  79. if ($this->view->assezDePa == false) {
  80. throw new Zend_Exception(get_class($this) . " Pas assez de PA : " . $this->view->user->pa_braldun);
  81. }
  82. // Verification semer
  83. if ($this->view->entretenirChampOk == false) {
  84. throw new Zend_Exception(get_class($this) . " Entretenir Champ interdit");
  85. }
  86. $x_y = $this->request->get("valeur_1");
  87. list ($x, $y) = preg_split("/t/", $x_y);
  88. if ($x < 0 || $x > 10 || $y < 0 || $y > 10) {
  89. throw new Zend_Exception(get_class($this) . " XY invalides : " . $x_y);
  90. }
  91. if ($this->view->positions[$x . "t" . $y]["possible"] !== true) {
  92. throw new Zend_Exception(get_class($this) . " XY impossibles : " . $x_y);
  93. }
  94. $this->entretenir($x, $y);
  95. $idType = $this->view->config->game->evenements->type->competence;
  96. $details = "[b" . $this->view->user->id_braldun . "] a entretenu son champ";
  97. $this->setDetailsEvenement($details, $idType);
  98. $this->setEvenementQueSurOkJet1(false);
  99. //message pour le braldûn propriétaire
  100. if ($this->idProprietaire != $this->view->user->id_braldun) {
  101. Zend_Loader::loadClass("Bral_Util_Messagerie");
  102. $message = "[Ceci est un message automatique d'agriculture]" . PHP_EOL;
  103. $message .= $this->view->user->prenom_braldun . " " . $this->view->user->nom_braldun . " a entretenu votre champ en x:" . $this->view->champ["x_champ"] . ", y:" . $this->view->champ["y_champ"] . PHP_EOL;
  104. $message .= "Zone entretenue : x: " . $this->view->etatZone["x"] . " y:" . $this->view->etatZone["y"] . "." . PHP_EOL;
  105. if ($this->view->etatZone["etat"] == ChampTaupe::ETAT_ENTRETENU) {
  106. $message .= "Aucun taupe n'a été trouvée à cet endroit." . PHP_EOL;
  107. } else {
  108. $message .= "Une partie de taupe de taille " . $this->view->etatZone["taille"] . " a été trouvée !" . PHP_EOL;
  109. if ($this->view->taupeDetruite) {
  110. $message .= "La taupe est occise." . PHP_EOL;
  111. } else {
  112. $message .= "Consultez le champ pour voir ce qu'il reste à trouver de cette taupe pour l'éliminer complètement." . PHP_EOL;
  113. }
  114. }
  115. Bral_Util_Messagerie::envoiMessageAutomatique($this->view->user->id_braldun, $this->idProprietaire, $message, $this->view);
  116. }
  117. $this->calculPx();
  118. $this->calculBalanceFaim();
  119. $this->majBraldun();
  120. }
  121. private function entretenir($x, $y) {
  122. Zend_Loader::loadClass("ChampTaupe");
  123. $champTaupeTable = new ChampTaupe();
  124. $data = array(
  125. 'x_champ_taupe' => $x,
  126. 'y_champ_taupe' => $y,
  127. 'id_fk_champ_taupe' => $this->view->champ["id_champ"],
  128. 'date_entretien_champ_taupe' => date('Y-m-d H:i:s'),
  129. );
  130. $etatZone = $champTaupeTable->entretenir($data);
  131. $etatZone["x"] = $x;
  132. $etatZone["y"] = $y;
  133. $this->view->taupeDetruite = false;
  134. if ($etatZone["etat"] == ChampTaupe::ETAT_DETRUIT) {
  135. $taupe = $champTaupeTable->findByIdChampNumeroTaupeVivant($this->view->champ["id_champ"], $etatZone["numero"]);
  136. if ($taupe == null || count($taupe) < 1) {
  137. $this->view->taupeDetruite = true;
  138. }
  139. }
  140. $champTable = new Champ();
  141. $data = array(
  142. 'date_utilisation_champ' => date("Y-m-d 00:00:00"),
  143. );
  144. $where = 'id_champ=' . $this->view->champ["id_champ"];
  145. $champTable->update($data, $where);
  146. $this->view->etatZone = $etatZone;
  147. }
  148. function getListBoxRefresh() {
  149. return $this->constructListBoxRefresh(array("box_competences", "box_champs"));
  150. }
  151. public function calculPx() {
  152. $this->view->nb_px_commun = 0;
  153. $this->view->nb_px_perso = 0;
  154. $this->view->calcul_px_generique = true;
  155. if ($this->view->taupeDetruite == true) {
  156. if ($this->view->etatZone["taille"] == 4) {
  157. $this->view->nb_px_perso = 4;
  158. } elseif ($this->view->etatZone["taille"] == 3) {
  159. $this->view->nb_px_perso = 5;
  160. } elseif ($this->view->etatZone["taille"] == 2) {
  161. $this->view->nb_px_perso = 8;
  162. }
  163. }
  164. $this->view->nb_px = floor($this->view->nb_px_perso + $this->view->nb_px_commun);
  165. }
  166. }