optimizer.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Optimizer teams view.
  4. *
  5. * Contains the view layout and ome code to present the data.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. * @category View
  11. * @property Optimizer_Page $page The page model.
  12. * @var Optimizer_Page $page The page model.
  13. */
  14. ?>
  15. <!DOCTYPE html>
  16. <html lang='en'>
  17. <head>
  18. <?=$page->generate_head()?>
  19. <script>
  20. units = [];
  21. <?php
  22. $units_added = [];
  23. foreach ($page->get_teams() as $team){
  24. foreach ($team->get_units() as $unit){
  25. if (!in_array($unit->get_id(), $units_added)){
  26. array_push($units_added, $unit->get_id());
  27. $panel = json_encode("<div class='monster_panel'>" . HTML::unit_panel($unit) . "</div>");
  28. ?>
  29. units.push(
  30. {
  31. id: "<?=$unit->get_id()?>",
  32. score: <?=$team->get_score()?>,
  33. panel: <?=$panel?>
  34. }
  35. );
  36. <?php
  37. }
  38. else{
  39. // Find in array and increase score
  40. ?>
  41. for (let i = 0; i < units.length; i++) {
  42. if (units[i].id == "<?=$unit->get_id()?>"){
  43. units[i].score += <?=$team->get_score()?>;
  44. break;
  45. }
  46. }
  47. <?php
  48. }
  49. }
  50. }
  51. ?>
  52. /**
  53. * Changes the score of a team.
  54. *
  55. * @param team ID of the team to change.
  56. * @param score New score.
  57. * @return true on success, false on error.
  58. */
  59. function rateTeam(team, score){
  60. params = '?player=<?=get_context()->get_player()->get_id()?>&team=' + team + '&score=' + score;
  61. console.log(params);
  62. var xhttp = new XMLHttpRequest();
  63. xhttp.onreadystatechange = function() {
  64. if (this.readyState == 4){
  65. // TODO
  66. // Loop units array and set the new score
  67. calculateScores();
  68. }
  69. };
  70. xhttp.open('GET', '/action/rate_team/' + params, true);
  71. xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  72. xhttp.send();
  73. return true;
  74. }
  75. /**
  76. * Redirect to a unit optimization page.
  77. *
  78. * @param unit ID of the unit to optimize.
  79. */
  80. function optimize(unit){
  81. window.location.href = "/player/<?=get_context()->get_player()->get_id()?>/optimizer/" + unit;
  82. }
  83. /**
  84. * Builds the unitpriority table.
  85. *
  86. * Calculates the score of each unit acording to teams score, and
  87. * builds a sorted table enabling a link to optimization.
  88. */
  89. function calculateScores(){
  90. // Sort array, descending score
  91. units = units.sort(function(a, b){return b.score - a.score;});
  92. // Draw table
  93. const container = document.getElementById('unit_list');
  94. while (container.firstChild) {
  95. container.removeChild(container.lastChild);
  96. }
  97. let table = document.createElement('table');
  98. let tr = document.createElement('tr');
  99. let th = document.createElement('th');
  100. th.innerHTML = 'Unit';
  101. th.setAttribute('class', 'unit');
  102. tr.appendChild(th);
  103. th = document.createElement('th');
  104. th.setAttribute('class', 'score');
  105. th.innerHTML = 'Score';
  106. tr.appendChild(th);
  107. th = document.createElement('th');
  108. th.innerHTML = 'Action';
  109. th.setAttribute('class', 'action');
  110. tr.appendChild(th);
  111. table.appendChild(tr);
  112. var odd = 'odd';
  113. for (let i = 0; i < units.length; i++) {
  114. let tr = document.createElement('tr');
  115. let td = document.createElement('td');
  116. td.setAttribute('class', 'unit ' + odd);
  117. td.innerHTML = units[i].panel;
  118. tr.appendChild(td);
  119. td = document.createElement('td');
  120. td.setAttribute('class', 'score ' + odd);
  121. td.innerHTML = units[i].score;
  122. tr.appendChild(td);
  123. td = document.createElement('td');
  124. td.setAttribute('class', 'action ' + odd);
  125. btn = document.createElement('input');
  126. btn.setAttribute('type', 'button');
  127. btn.setAttribute('value', 'Optimize');
  128. btn.setAttribute('onclick', 'optimize("' + units[i].id + '")');
  129. td.appendChild(btn);
  130. tr.appendChild(td);
  131. table.appendChild(tr);
  132. if (odd == ''){
  133. odd = 'odd';
  134. }
  135. else{
  136. odd = '';
  137. }
  138. }
  139. container.appendChild(table);
  140. }
  141. </script>
  142. </head>
  143. <body onLoad='calculateScores();'>
  144. <header>
  145. <?php
  146. include __DIR__ . "/inc/header_app.php";
  147. include __DIR__ . "/inc/header_player.php";
  148. ?>
  149. </header>
  150. <aside>
  151. <h2>
  152. Optimizer
  153. </h2>
  154. <p>
  155. Rate your teams and optimize the units that you use the most.
  156. <br/>
  157. <br/>
  158. <br/>
  159. To optimize units that are not on teams, do so from the <a href='/<?=get_context()->get_player()->get_id()?>/units/'>units page</a>.
  160. </p>
  161. </aside>
  162. <main>
  163. <section class='content'>
  164. <h2>
  165. Teams
  166. </h2>
  167. <article>
  168. <table id='teams'>
  169. <tr>
  170. <th>
  171. Team
  172. </th>
  173. <th>
  174. Units
  175. </th>
  176. <th>
  177. Score
  178. </th>
  179. </tr>
  180. <?php
  181. $odd = "odd";
  182. foreach ($page->get_teams() as $team){
  183. ?>
  184. <tr>
  185. <td class='team <?=$odd?>'>
  186. <?=$team->get_name()?>
  187. </td>
  188. <td class='units <?=$odd?>'>
  189. <?php
  190. $prev_front = false;
  191. $front = "undefined";
  192. foreach ($team->get_units() as $unit){
  193. $front = in_array($unit->get_id(), $team->get_frontline());
  194. if ($front != "undefined" && $prev_front != $front){
  195. ?>
  196. <hr class='frontline'/>
  197. <?php
  198. }
  199. ?>
  200. <a href='/<?=get_context()->get_player()->get_id()?>/units/<?=$unit->get_id()?>' target='_blank'>
  201. <div class='monster_panel'>
  202. <?=HTML::unit_panel($unit)?>
  203. <?php
  204. if ($unit->get_id() == $team->get_leader()){
  205. ?>
  206. <img alt='Leader' class='leader' src='<?=URL::IMG["ICON"]?>leader.png'/>
  207. <?php
  208. }
  209. ?>
  210. </div>
  211. </a>
  212. <?php
  213. $prev_front = $front;
  214. }
  215. ?>
  216. </td>
  217. <td class='score <?=$odd?>'>
  218. <input type='number' min='0' max='20' value='<?=$team->get_score()?>' onChange='rateTeam(<?=$team->get_id()?>, this.value);'/>
  219. </td>
  220. </tr>
  221. <?php
  222. if ($odd == ""){
  223. $odd = "odd";
  224. }
  225. else{
  226. $odd = "";
  227. }
  228. }
  229. ?>
  230. </table>
  231. <div id='unit_list'>
  232. </div>
  233. </article>
  234. </section> <!-- .list -->
  235. </main>
  236. <?php
  237. include __DIR__ . "/inc/footer.php";
  238. ?>
  239. </body>
  240. </html>