optimizer.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Optimizer teams view.
  4. *
  5. * Contains the view layout and ome code to present the data.
  6. *
  7. * @category View
  8. * @property_read Teams_Page $page The page model.
  9. * @property_read int $page Player ID.
  10. */
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang='en'>
  14. <head>
  15. <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
  16. <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
  17. <title><?=$page->title?></title>
  18. <link rel='shortcut icon' href='<?=$page->favicon?>'/>
  19. <!-- CSS files -->
  20. <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
  21. <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>optimizer.css'/>
  22. <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>filters.css'/>
  23. <!-- Meta tags -->
  24. <link rel='canonical' href='<?=$page->canonical?>'/>
  25. <link rel='author' href='<?=$page->author?>'/>
  26. <link rel='publisher' href='<?=$page->author?>'/>
  27. <meta name='description' content='<?=$page->description?>'/>
  28. <meta property='og:title' content='<?=$page->title?>'/>
  29. <meta property='og:url' content='<?=$page->canonical?>'/>
  30. <meta property='og:description' content='<?=$page->description?>'/>
  31. <meta property='og:image' content='<?=$page->icon?>'/>
  32. <meta property='og:site_name' content='<?=$page->name?>'/>
  33. <meta property='og:type' content='website'/>
  34. <meta property='og:locale' content='en'/>
  35. <meta name='twitter:card' content='summary'/>
  36. <meta name='twitter:title' content='<?=$page->title?>'/>
  37. <meta name='twitter:description' content='<?=$page->description?>'/>
  38. <meta name='twitter:image' content='<?=$page->icon?>'/>
  39. <meta name='twitter:url' content='<?=$page->canonical?>'/>
  40. <meta name='robots' content='index follow'/>
  41. <script>
  42. units = [];
  43. <?php
  44. $units_added = [];
  45. foreach ($page->teams as $team){
  46. foreach ($team->unit as $unit){
  47. if (!in_array($unit->id, $units_added)){
  48. array_push($units_added, $unit->id);
  49. $panel = json_encode("<div class='monster_panel'>" . HTML::unit_panel($unit) . "</div>");
  50. ?>
  51. units.push(
  52. {
  53. id: "<?=$unit->id?>",
  54. score: <?=$team->score?>,
  55. panel: <?=$panel?>
  56. }
  57. );
  58. <?php
  59. }
  60. else{
  61. // Find in array and increase score
  62. ?>
  63. for (let i = 0; i < units.length; i++) {
  64. if (units[i].id == "<?=$unit->id?>"){
  65. units[i].score += <?=$team->score?>;
  66. break;
  67. }
  68. }
  69. <?php
  70. }
  71. }
  72. }
  73. ?>
  74. /**
  75. * Changes the score of a team.
  76. *
  77. * @param team ID of the team to change.
  78. * @param score New score.
  79. * @return true on success, false on error.
  80. */
  81. function changeScore(team, score){
  82. // TODO
  83. return true;
  84. }
  85. /**
  86. * Redirect to a unit optimization page.
  87. *
  88. * @param unit ID of the unit to optimize.
  89. */
  90. function optimize(unit){
  91. window.location.href = "/<?=$UID?>/optimizer/" + unit;
  92. }
  93. /**
  94. * Builds the unitpriority table.
  95. *
  96. * Calculates the score of each unit acording to teams score, and
  97. * builds a sorted table enabling a link to optimization.
  98. */
  99. function calculateScores(){
  100. // Sort array, descending score
  101. units = units.sort(function(a, b){return b.score - a.score;});
  102. // Draw table
  103. const container = document.getElementById('unit_list');
  104. while (container.firstChild) {
  105. container.removeChild(container.lastChild);
  106. }
  107. let table = document.createElement('table');
  108. let tr = document.createElement('tr');
  109. let th = document.createElement('th');
  110. th.innerHTML = 'Unit';
  111. th.setAttribute('class', 'unit');
  112. tr.appendChild(th);
  113. th = document.createElement('th');
  114. th.setAttribute('class', 'score');
  115. th.innerHTML = 'Score';
  116. tr.appendChild(th);
  117. th = document.createElement('th');
  118. th.innerHTML = 'Action';
  119. th.setAttribute('class', 'action');
  120. tr.appendChild(th);
  121. table.appendChild(tr);
  122. var odd = 'odd';
  123. for (let i = 0; i < units.length; i++) {
  124. let tr = document.createElement('tr');
  125. let td = document.createElement('td');
  126. td.setAttribute('class', 'unit ' + odd);
  127. td.innerHTML = units[i].panel;
  128. tr.appendChild(td);
  129. td = document.createElement('td');
  130. td.setAttribute('class', 'score ' + odd);
  131. td.innerHTML = units[i].score;
  132. tr.appendChild(td);
  133. td = document.createElement('td');
  134. td.setAttribute('class', 'action ' + odd);
  135. btn = document.createElement('input');
  136. btn.setAttribute('type', 'button');
  137. btn.setAttribute('value', 'Optimize');
  138. btn.setAttribute('onclick', 'optimize("' + units[i].id + '")');
  139. td.appendChild(btn);
  140. tr.appendChild(td);
  141. table.appendChild(tr);
  142. if (odd == ''){
  143. odd = 'odd';
  144. }
  145. else{
  146. odd = '';
  147. }
  148. }
  149. container.appendChild(table);
  150. }
  151. </script>
  152. </head>
  153. <body onLoad='calculateScores();'>
  154. <?php
  155. include __DIR__ . "/inc/header.php";
  156. ?>
  157. <section class='list'>
  158. <h2>
  159. Teams
  160. </h2>
  161. <article>
  162. <table id='teams'>
  163. <tr>
  164. <th>
  165. Team
  166. </th>
  167. <th>
  168. Description
  169. </th>
  170. <th>
  171. Units
  172. </th>
  173. <th>
  174. Score
  175. </th>
  176. </tr>
  177. <?php
  178. $odd = "odd";
  179. foreach ($page->teams as $team){
  180. ?>
  181. <tr>
  182. <td class='team <?=$odd?>'>
  183. <?=$team->name?>
  184. </td>
  185. <td class='description <?=$odd?>'>
  186. <?=$team->description?>
  187. </td>
  188. <td class='units <?=$odd?>'>
  189. <?php
  190. $prev_front = false;
  191. $front = "undefined";
  192. foreach ($team->unit as $unit){
  193. $front = in_array($unit->id, $team->frontline);
  194. if ($front != "undefined" && $prev_front != $front){
  195. ?>
  196. <hr class='frontline'/>
  197. <?php
  198. }
  199. ?>
  200. <a href='/<?=$UID?>/monsters/<?=$unit->id?>' target='_blank'>
  201. <div class='monster_panel'>
  202. <?=HTML::unit_panel($unit)?>
  203. <?php
  204. if ($unit->id == $team->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->score?>' onChange='changeScore(<?=$team->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. <?php
  236. include __DIR__ . "/inc/footer.php";
  237. ?>
  238. </body>
  239. </html>