lawsCTRL.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* c'est la table qui contient toutes les lois choisi par le joueur */
  2. var tableLaws = [];
  3. var tableEventsLaws = [];
  4. /* Lister les lois votee */
  5. function genListeTableLaws() {
  6. let res = "";
  7. for (var i = 0; i < tableLaws.length; i++) {
  8. res = res + genTableLaw(tableLaws[i][0].id, tableLaws[i][1]);
  9. }
  10. for (var i = 0; i < tableEventsLaws.length; i++) {
  11. res = res + genTableEventsLaw(tableEventsLaws[i][0], tableEventsLaws[i][1]);
  12. }
  13. return res;
  14. }
  15. function genTableLaw(lawID, lawLevel) {
  16. return '<div><p>' + laws[lawID].nom + '</p><p>' + laws[lawID].descriptions[lawLevel] + "</p></div>";
  17. }
  18. function genTableEventsLaw(eventID, id) {
  19. return '<div><p>' + events[eventID].lois[id].nom + '</p><p>' + events[eventID].lois[id].descriptions[0] + "</p></div>";
  20. }
  21. /* id d'une loi non evenementiel au hazard */
  22. function randLaw(selectedLaws){
  23. let found = false;
  24. let id;
  25. while (!found) {
  26. if ((tableLaws.length + selectedLaws.length) >= laws.length) {
  27. id = -1;
  28. break;
  29. }
  30. id = randInt(0, laws.length - 1);
  31. found = true;
  32. for (var i = 0; i < selectedLaws.length; i++) {
  33. if (selectedLaws[i].id == id) {
  34. found = false;
  35. }
  36. }
  37. for (var i = 0; i < tableLaws.length; i++) {
  38. if (tableLaws[i][0].id == id) {
  39. found = false;
  40. }
  41. }
  42. }
  43. return id;
  44. }
  45. /* Ajouter une loi non evenementiel */
  46. function displayAddLaws(newEventId){
  47. let res = "";
  48. console.log(newEventId);
  49. console.log("bon");
  50. if (newEventId == -1) {
  51. res = genListeAddLaws();
  52. }
  53. else {
  54. res = genAddEventLaw(newEventId, 0) + genAddEventLaw(newEventId, 1);
  55. }
  56. return res;
  57. }
  58. const numberAddLaws = 3;
  59. function genListeAddLaws() {
  60. let selectedLaws = [];
  61. let res = "";
  62. for (var i = 0; i < numberAddLaws; i++) {
  63. let newLaw = randLaw(selectedLaws);
  64. selectedLaws.push(laws[newLaw]);
  65. if (newLaw > -1) {
  66. res = res + genAddLaw(newLaw);
  67. }
  68. }
  69. return res;
  70. }
  71. function genAddLaw(id){
  72. return "<div onClick=\"addLaw(" + id + ")\"><p>" + laws[id].nom + '</p><p>' + laws[id].descriptions[0] + "</p></div>";
  73. }
  74. function addLaw(id){
  75. tableLaws.unshift([laws[id], 0]);
  76. updateIndicatorsAddLaws(laws[id]);
  77. turnForward();
  78. }
  79. /* Ajouter une loi evenementiel */
  80. function genAddEventLaw(eventID, id){
  81. return "<div onClick=\"addEventLaw(" + eventID + "," + id + ")\"><p>" + events[eventID].lois[id].nom + '</p><p>' + events[eventID].lois[id].descriptions[0] + "</p></div>";
  82. }
  83. function addEventLaw(eventID, id){
  84. tableEventsLaws.unshift([eventID, id]);
  85. updateIndicatorsAddLaws(events[eventID].lois[id]);
  86. turnForward();
  87. }
  88. /* Supprimer une loi */
  89. function genListeRemoveLaws() {
  90. let res = "";
  91. for (var i = 0; i < tableLaws.length; i++) {
  92. res = res + genRemoveLaw(tableLaws[i][0].id, tableLaws[i][1], i);
  93. }
  94. return res;
  95. }
  96. function genRemoveLaw(lawID, lawLevel, num) {
  97. return '<div onClick=\"removeLaw('+ num + "," + lawID + ')\"><p>' + laws[lawID].nom + '</p><p>' + laws[lawID].descriptions[lawLevel] + "</p></div>";
  98. }
  99. function removeLaw(pos, lawID){
  100. tableLaws.splice(pos, 1);
  101. updateIndicatorsRemoveLaws(laws[lawID]);
  102. turnForward()
  103. }
  104. /* Ameliorer une loi */
  105. function genListeUpgradeLaws() {
  106. let res = "";
  107. let num = 0;
  108. for (var i = 0; i < tableLaws.length; i++) {
  109. if (tableLaws[i][1] < 2) {
  110. res = res + genUpgradeLaw(tableLaws[i][0].id, tableLaws[i][1], num);
  111. num++;
  112. }
  113. }
  114. return res;
  115. }
  116. function genUpgradeLaw(lawID, lawLevel, num) {
  117. return "<div onClick=\"upgradeLaw(" + num + "," + lawID + ")\"><p>" + laws[lawID].nom + '</p><p>' + laws[lawID].descriptions[lawLevel] + "</p></div>";
  118. }
  119. function upgradeLaw(pos, lawID){
  120. tableLaws[pos][1] = tableLaws[pos][1] + 1;
  121. updateIndicatorsUpgradeLaws(laws[lawID]);
  122. turnForward()
  123. }