PageRenderTime 73ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/braldahim/library/Bral/Competences/Brasser.php

https://github.com/Canop/braldahim
PHP | 420 lines | 333 code | 43 blank | 44 comment | 55 complexity | 8f0a0c6146eb093ad69fd8d93efb5765 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_Brasser extends Bral_Competences_Competence {
  8. const POIDS_INGREDIENT = 5.025; // 5KG + 25g
  9. function prepareCommun() {
  10. $this->view->sourceOk = false;
  11. $this->view->nbBieres = $this->view->user->force_base_braldun;
  12. $this->idDestination = null;
  13. $this->idSource = null;
  14. if ($this->view->nbBieres < 1) {
  15. $this->view->nbBieres = 1;
  16. }
  17. $this->calculEchoppe("cuisinier");
  18. $this->calculCharrette();
  19. $this->prepareDestinations(); // Soit dans l'échoppe, soit le laban, soit la charrette
  20. $this->prepareIngredients();
  21. $this->controleSource();
  22. }
  23. function prepareFormulaire() {
  24. if ($this->view->assezDePa == false) {
  25. return;
  26. }
  27. }
  28. private function prepareDestinations() {
  29. $tabDestinations = null;
  30. if ($this->view->estSurEchoppe === true) {
  31. $tabDestinations["echoppe"]["possible"] = true;
  32. $tabDestinations["echoppe"]["nom"] = "Votre échoppe";
  33. $tabDestinations["echoppe"]["poids_apres_ingredient"] = 10000;
  34. $tabDestinations["echoppe"]["poids_restant"] = 10000;
  35. $tabDestinations["echoppe"]["selected"] = "selected";
  36. } else {
  37. /* if ($this->view->possedeCharrette === true) {
  38. $tabDestinations["charrette"]["possible"] = true;
  39. $tabDestinations["charrette"]["nom"] = "Votre charrette";
  40. $tabDestinations["charrette"]["poids_apres_ingredient"] = $this->view->poidsRestantCharrette + self::POIDS_INGREDIENT;
  41. $tabDestinations["charrette"]["poids_restant"] = $this->view->poidsRestantCharrette;
  42. }
  43. $tabDestinations["laban"]["possible"] = true;
  44. $tabDestinations["laban"]["nom"] = "Votre laban";
  45. $tabDestinations["laban"]["poids_apres_ingredient"] = $this->view->user->poids_transportable_braldun - $this->view->user->poids_transporte_braldun + self::POIDS_INGREDIENT;
  46. $tabDestinations["laban"]["poids_restant"] = $this->view->user->poids_transportable_braldun - $this->view->user->poids_transporte_braldun;
  47. $tabDestinations["sol"]["possible"] = true;
  48. $tabDestinations["sol"]["nom"] = "Au Sol";
  49. $tabDestinations["sol"]["poids_apres_ingredient"] = 10000;
  50. $tabDestinations["sol"]["poids_restant"] = 10000;
  51. */
  52. }
  53. $this->view->destinations = $tabDestinations;
  54. }
  55. private function prepareIngredients() {
  56. Zend_Loader::loadClass("RecetteAliments");
  57. Zend_Loader::loadClass("TypeAliment");
  58. $recetteAlimentsTable = new RecetteAliments();
  59. $ingredientsRecetteRowset = $recetteAlimentsTable->findByIdTypeAliment(TypeAliment::ID_TYPE_LAGER);
  60. if ($ingredientsRecetteRowset == null || count($ingredientsRecetteRowset) < 0) {
  61. throw new Zend_Exception("Erreur recette aliment" . TypeAliment::ID_TYPE_LAGER);
  62. }
  63. $tabSources = null;
  64. if ($this->view->estSurEchoppe === true) {
  65. $tabSources["echoppe"]["nom"] = "Votre échoppe";
  66. $tabSources["echoppe"]["possible"] = true;
  67. $tabSources["echoppe"]["selected"] = "selected";
  68. Zend_Loader::loadClass("EchoppeIngredient");
  69. $echoppeIngredientTable = new EchoppeIngredient();
  70. $ingredients = $echoppeIngredientTable->findByIdEchoppe($this->view->idEchoppe);
  71. $tabSources["echoppe"]["ingredients"] = $ingredients;
  72. } else {
  73. /*
  74. if ($this->view->possedeCharrette === true) {
  75. $tabSources["charrette"]["nom"] = "Votre charrette";
  76. $tabSources["charrette"]["possible"] = true;
  77. Zend_Loader::loadClass("CharretteIngredient");
  78. $charretteIngredientTable = new CharretteIngredient();
  79. $ingredients = $charretteIngredientTable->findByIdCharrette($this->view->idCharrette);
  80. $tabSources["charrette"]["ingredients"] = $ingredients;
  81. }
  82. $tabSources["laban"]["nom"] = "Votre laban";
  83. $tabSources["laban"]["possible"] = true;
  84. Zend_Loader::loadClass("LabanIngredient");
  85. $labanIngredientTable = new LabanIngredient();
  86. $ingredients = $labanIngredientTable->findByIdBraldun($this->view->user->id_braldun);
  87. $tabSources["laban"]["ingredients"] = $ingredients;
  88. */
  89. }
  90. $tabIngredients = null;
  91. $poidsIngredients = 0;
  92. foreach ($ingredientsRecetteRowset as $i) {
  93. $tabIngredients[] = array(
  94. 'nom_type_ingredient' => $i["nom_type_ingredient"],
  95. 'id_type_ingredient' => $i["id_type_ingredient"],
  96. 'quantite_recette_aliments' => $i["quantite_recette_aliments"],
  97. );
  98. $poidsIngredients = $poidsIngredients + ($i["quantite_recette_aliments"] * $i["poids_unitaire_type_ingredient"]);
  99. $this->controleIngredientsDispo($tabSources, $i["id_type_ingredient"], $i["quantite_recette_aliments"]);
  100. }
  101. $this->view->ingredients = $tabIngredients;
  102. $this->view->sources = $tabSources;
  103. }
  104. private function controleIngredientsDispo(&$tabSources, $idTypeIngredient, $quantite) {
  105. if ($tabSources != null && count($tabSources) > 0) {
  106. foreach ($tabSources as $k => $v) {
  107. if ($tabSources[$k]["possible"] === true) {
  108. if ($tabSources[$k]["ingredients"] != null && count($tabSources[$k]["ingredients"]) > 0) {
  109. $ingredientOk = false;
  110. foreach ($tabSources[$k]["ingredients"] as $i) {
  111. $prefix = $k;
  112. if ($k == "echoppe") {
  113. $prefix = "arriere_echoppe";
  114. }
  115. if ($i["id_type_ingredient"] == $idTypeIngredient && $i["quantite_" . $prefix . "_ingredient"] >= $quantite) {
  116. $ingredientOk = true;
  117. }
  118. }
  119. $tabSources[$k]["possible"] = $ingredientOk;
  120. } else {
  121. $tabSources[$k]["possible"] = false;
  122. $tabSources[$k]["selected"] = "";
  123. }
  124. }
  125. }
  126. }
  127. }
  128. private function controleSource() {
  129. $uneSourceOk = false;
  130. if ($this->view->sources != null && count($this->view->sources) > 0) {
  131. foreach ($this->view->sources as $s) {
  132. if ($s["possible"] == true) {
  133. $uneSourceOk = true;
  134. }
  135. }
  136. }
  137. if ($uneSourceOk === true) {
  138. $this->view->sourceOk = true;
  139. } else {
  140. $this->view->sourceOk = false;
  141. }
  142. }
  143. function prepareResultat() {
  144. // Verification des Pa
  145. if ($this->view->assezDePa == false) {
  146. throw new Zend_Exception(get_class($this) . " Pas assez de PA : " . $this->view->user->pa_braldun);
  147. }
  148. // Verification cuisiner
  149. if ($this->view->sourceOk == false) {
  150. throw new Zend_Exception(get_class($this) . " Cuisiner interdit source KO ");
  151. }
  152. $idSource = $this->request->get("valeur_1");
  153. $idDestination = $this->request->get("valeur_2");
  154. $sourceOk = false;
  155. foreach ($this->view->sources as $k => $v) {
  156. if ($k == $idSource && $v["possible"] === true) {
  157. $sourceOk = true;
  158. }
  159. }
  160. if ($sourceOk == false) {
  161. throw new Zend_Exception(get_class($this) . " Brasser interdit source KO B idSource:" . $idSource);
  162. }
  163. $this->idSource = $idSource;
  164. $destinationOk = false;
  165. foreach ($this->view->destinations as $k => $v) {
  166. if ($k == $idDestination && $v["possible"] === true) {
  167. $destinationOk = true;
  168. }
  169. }
  170. if ($destinationOk == false) {
  171. throw new Zend_Exception(get_class($this) . " Brasser interdit destination KO idDestination:" . $idDestination);
  172. }
  173. $this->idDestination = $idDestination;
  174. // calcul des jets
  175. $this->calculJets();
  176. if ($this->view->okJet1 === true) {
  177. $this->calculBrasser($idSource, $idDestination);
  178. } else {
  179. $this->retireIngredients($idSource, true);
  180. }
  181. $this->calculPx();
  182. $this->calculPoids();
  183. $this->calculBalanceFaim();
  184. $this->majBraldun();
  185. }
  186. private function calculBrasser($idSource, $idDestination) {
  187. $idTypeAliment = $this->calculQualite();
  188. $this->retireIngredients($idSource);
  189. $poidsRestant = $this->view->destinations[$idDestination]["poids_restant"];
  190. if ($idSource == $idDestination) {
  191. $poidsRestant = $this->view->destinations[$idDestination]["poids_apres_ingredient"];
  192. }
  193. $nbBieresPossible = floor($poidsRestant / Bral_Util_Poids::POIDS_BIERE);
  194. if ($nbBieresPossible < 0) {
  195. $nbBieresPossible = 0;
  196. }
  197. $this->view->nbBieresATerre = 0;
  198. if ($this->view->nbBieres > $nbBieresPossible) {
  199. $this->view->nbBieresDestination = intval($nbBieresPossible);
  200. $this->view->nbBieresATerre = floor($this->view->nbAliment - $this->view->nbBieresDestination);
  201. } else {
  202. $this->view->nbBieresDestination = $this->view->nbBieres;
  203. }
  204. $this->creationBiere($idTypeAliment, $idDestination, $idSource);
  205. $this->idTypeAliment = $idTypeAliment;
  206. }
  207. private function calculQualite() {
  208. $maitrise = $this->braldun_competence["pourcentage_hcomp"] / 100;
  209. $chance_a = -0.375 * $maitrise + 40 + $this->view->user->force_base_braldun;
  210. $chance_b = 0.25 * $maitrise + 50 - ($this->view->user->force_base_braldun / 2);
  211. $chance_c = 0.125 * $maitrise + 10 - ($this->view->user->force_base_braldun / 2);
  212. Zend_Loader::loadClass("Aliment");
  213. $tirage = Bral_Util_De::get_1d100();
  214. if ($tirage > 0 && $tirage <= $chance_a) {
  215. $this->view->qualite = "de Lager";
  216. $idTypeAliment = TypeAliment::ID_TYPE_LAGER;
  217. } elseif ($tirage > $chance_a && $tirage <= $chance_a + $chance_b) {
  218. $this->view->qualite = "d'Ale";
  219. $idTypeAliment = TypeAliment::ID_TYPE_ALE;
  220. } else {
  221. $this->view->qualite = "de Stout";
  222. $idTypeAliment = TypeAliment::ID_TYPE_STOUT;
  223. }
  224. return $idTypeAliment;
  225. }
  226. private function creationBiere($idTypeAliment, $idDestination, $idSource) {
  227. if ($idDestination == "echoppe") {
  228. $prefix = "echoppe";
  229. Zend_Loader::loadClass("EchoppeAliment");
  230. $table = new EchoppeAliment();
  231. $tabBase["id_fk_echoppe_echoppe_aliment"] = $this->view->idEchoppe;
  232. } else if ($idDestination == "charrette") {
  233. $prefix = "charrette";
  234. Zend_Loader::loadClass("CharretteAliment");
  235. $table = new CharretteAliment();
  236. $tabBase["id_fk_charrette_aliment"] = $this->view->idCharrette;
  237. } else if ($idDestination == "laban") {
  238. $prefix = "laban";
  239. Zend_Loader::loadClass("LabanAliment");
  240. $table = new LabanAliment();
  241. $tabBase["id_fk_braldun_laban_aliment"] = $this->view->user->id_braldun;
  242. } else if ($idDestination == "sol") {
  243. $prefix = "element";
  244. Zend_Loader::loadClass("ElementAliment");
  245. $table = new ElementAliment();
  246. $tabBase["x_element_aliment"] = $this->view->user->x_braldun;
  247. $tabBase["y_element_aliment"] = $this->view->user->y_braldun;
  248. $tabBase["z_element_aliment"] = $this->view->user->z_braldun;
  249. } else {
  250. throw new Zend_Exception("creationAliment::Source invalide:" . $idDestination);
  251. }
  252. Zend_Loader::loadClass("ElementAliment");
  253. $elementAlimentTable = new ElementAliment();
  254. Zend_Loader::loadClass("IdsAliment");
  255. $idsAliment = new IdsAliment();
  256. Zend_Loader::loadClass('Aliment');
  257. $alimentTable = new Aliment();
  258. Zend_Loader::loadClass("Bral_Util_Effets");
  259. for ($i = 1; $i <= $this->view->nbBieres; $i++) {
  260. $idAliment = $idsAliment->prepareNext();
  261. $idEffetBraldun = null;
  262. if ($idTypeAliment == TypeAliment::ID_TYPE_STOUT) {
  263. // la valeur est calculée sur l'application de l'effet
  264. $idEffetBraldun = Bral_Util_Effets::ajouteEtAppliqueEffetBraldun(null, Bral_Util_Effets::CARACT_STOUT, Bral_Util_Effets::TYPE_BONUS, Bral_Util_De::get_1d3(), 0, 'Lovely day for a stout !');
  265. }
  266. $data = array(
  267. "id_aliment" => $idAliment,
  268. "id_fk_type_aliment" => $idTypeAliment,
  269. "id_fk_type_qualite_aliment" => 2,
  270. "bbdf_aliment" => 0,
  271. "id_fk_effet_braldun_aliment" => $idEffetBraldun,
  272. );
  273. $alimentTable->insert($data);
  274. $data = array(
  275. "id_element_aliment" => $idAliment,
  276. "x_element_aliment" => $this->view->user->x_braldun,
  277. "y_element_aliment" => $this->view->user->y_braldun,
  278. "z_element_aliment" => $this->view->user->z_braldun,
  279. );
  280. $elementAlimentTable->insert($data);
  281. if ($i <= $this->view->nbBieresDestination) {
  282. $where = "id_element_aliment = " . (int)$idAliment;
  283. $elementAlimentTable->delete($where);
  284. $data = $tabBase;
  285. $data['id_' . $prefix . '_aliment'] = $idAliment;
  286. $table->insert($data);
  287. }
  288. }
  289. }
  290. private function retireIngredients($idSource, $estRate = false) {
  291. $prefix2 = "";
  292. if ($idSource == "echoppe") {
  293. $prefix = "echoppe";
  294. $prefix2 = "arriere_";
  295. Zend_Loader::loadClass("EchoppeIngredient");
  296. $table = new EchoppeIngredient();
  297. $data["id_fk_echoppe_echoppe_ingredient"] = $this->view->idEchoppe;
  298. } else if ($idSource == "charrette") {
  299. $prefix = "charrette";
  300. Zend_Loader::loadClass("CharretteIngredient");
  301. $table = new CharretteIngredient();
  302. $data["id_fk_charrette_ingredient"] = $this->view->idCharrette;
  303. } else if ($idSource == "laban") {
  304. $prefix = "laban";
  305. Zend_Loader::loadClass("LabanIngredient");
  306. $table = new LabanIngredient();
  307. $data["id_fk_braldun_laban_ingredient"] = $this->view->user->id_braldun;
  308. } else {
  309. throw new Zend_Exception("retireIngredients::Source invalide:" . $idSource);
  310. }
  311. foreach ($this->view->ingredients as $i) {
  312. $quantite = -$i["quantite_recette_aliments"];
  313. if ($estRate) {
  314. $quantite = floor($quantite / 2);
  315. }
  316. $data["id_fk_type_" . $prefix . "_ingredient"] = $i["id_type_ingredient"];
  317. $data["quantite_" . $prefix2 . $prefix . "_ingredient"] = $quantite;
  318. $table->insertOrUpdate($data);
  319. }
  320. }
  321. function getListBoxRefresh() {
  322. $tab[] = 'box_competences';
  323. if ($this->idDestination == 'echoppe' || $this->idSource == 'echoppe') {
  324. $tab[] = 'box_echoppes';
  325. }
  326. if ($this->idDestination == 'laban' || $this->idSource == 'laban') {
  327. $tab[] = 'box_laban';
  328. }
  329. if ($this->idDestination == 'sol' || $this->idSource == 'sol' || $this->view->nbBieresATerre > 0) {
  330. $tab[] = 'box_vue';
  331. }
  332. if ($this->idDestination == 'charrette' || $this->idSource == 'charrette') {
  333. $tab[] = 'box_charrette';
  334. }
  335. return $this->constructListBoxRefresh($tab);
  336. }
  337. public function calculPx() {
  338. $this->view->nb_px_commun = 0;
  339. $this->view->calcul_px_generique = true;
  340. if ($this->view->okJet1 === true) {
  341. if ($this->idTypeAliment == TypeAliment::ID_TYPE_LAGER) {
  342. $gain = 1;
  343. } elseif ($this->idTypeAliment == TypeAliment::ID_TYPE_ALE) {
  344. $gain = 2;
  345. } elseif ($this->idTypeAliment == TypeAliment::ID_TYPE_STOUT) {
  346. $gain = 4;
  347. } else {
  348. throw new Zend_Exception("Calcul gain invalide:idTypeAliment:" . $idTypeAliment);
  349. }
  350. $this->view->nb_px_perso = $this->competence["px_gain"] + $gain;
  351. } else {
  352. $this->view->nb_px_perso = 0;
  353. }
  354. $this->view->nb_px = floor($this->view->nb_px_perso + $this->view->nb_px_commun);
  355. }
  356. }