optimizer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. * @var Teams_Page $page The page model.
  9. * @var int $UID User 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. <!-- Meta tags -->
  23. <link rel='canonical' href='<?=$page->canonical?>'/>
  24. <link rel='author' href='<?=$page->author?>'/>
  25. <link rel='publisher' href='<?=$page->author?>'/>
  26. <meta name='description' content='<?=$page->description?>'/>
  27. <meta property='og:title' content='<?=$page->title?>'/>
  28. <meta property='og:url' content='<?=$page->canonical?>'/>
  29. <meta property='og:description' content='<?=$page->description?>'/>
  30. <meta property='og:image' content='<?=$page->icon?>'/>
  31. <meta property='og:site_name' content='<?=$page->name?>'/>
  32. <meta property='og:type' content='website'/>
  33. <meta property='og:locale' content='en'/>
  34. <meta name='twitter:card' content='summary'/>
  35. <meta name='twitter:title' content='<?=$page->title?>'/>
  36. <meta name='twitter:description' content='<?=$page->description?>'/>
  37. <meta name='twitter:image' content='<?=$page->icon?>'/>
  38. <meta name='twitter:url' content='<?=$page->canonical?>'/>
  39. <meta name='robots' content='index follow'/>
  40. <script>
  41. units = [];
  42. <?php
  43. $units_added = [];
  44. foreach ($page->teams as $team){
  45. foreach ($team->unit as $unit){
  46. if (!in_array($unit->id, $units_added)){
  47. array_push($units_added, $unit->id);
  48. $panel = json_encode("<div class='monster_panel'>" . HTML::unit_panel($unit) . "</div>");
  49. ?>
  50. units.push(
  51. {
  52. id: "<?=$unit->id?>",
  53. score: <?=$team->score?>,
  54. panel: <?=$panel?>
  55. }
  56. );
  57. <?php
  58. }
  59. else{
  60. // Find in array and increase score
  61. ?>
  62. for (let i = 0; i < units.length; i++) {
  63. if (units[i].id == "<?=$unit->id?>"){
  64. units[i].score += <?=$team->score?>;
  65. break;
  66. }
  67. }
  68. <?php
  69. }
  70. }
  71. }
  72. ?>
  73. /**
  74. * Changes the score of a team.
  75. *
  76. * @param team ID of the team to change.
  77. * @param score New score.
  78. * @return true on success, false on error.
  79. */
  80. function rateTeam(team, score){
  81. params = '?uid=<?=$UID?>&team=' + team + '&score=' + score;
  82. console.log(params);
  83. var xhttp = new XMLHttpRequest();
  84. xhttp.onreadystatechange = function() {
  85. if (this.readyState == 4){
  86. // TODO
  87. // Loop units array and set the new score
  88. calculateScores();
  89. }
  90. };
  91. xhttp.open('GET', '/action/rate_team/' + params, true);
  92. xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  93. xhttp.send();
  94. return true;
  95. }
  96. /**
  97. * Redirect to a unit optimization page.
  98. *
  99. * @param unit ID of the unit to optimize.
  100. */
  101. function optimize(unit){
  102. window.location.href = "/<?=$UID?>/optimizer/" + unit;
  103. }
  104. /**
  105. * Builds the unitpriority table.
  106. *
  107. * Calculates the score of each unit acording to teams score, and
  108. * builds a sorted table enabling a link to optimization.
  109. */
  110. function calculateScores(){
  111. // Sort array, descending score
  112. units = units.sort(function(a, b){return b.score - a.score;});
  113. // Draw table
  114. const container = document.getElementById('unit_list');
  115. while (container.firstChild) {
  116. container.removeChild(container.lastChild);
  117. }
  118. let table = document.createElement('table');
  119. let tr = document.createElement('tr');
  120. let th = document.createElement('th');
  121. th.innerHTML = 'Unit';
  122. th.setAttribute('class', 'unit');
  123. tr.appendChild(th);
  124. th = document.createElement('th');
  125. th.setAttribute('class', 'score');
  126. th.innerHTML = 'Score';
  127. tr.appendChild(th);
  128. th = document.createElement('th');
  129. th.innerHTML = 'Action';
  130. th.setAttribute('class', 'action');
  131. tr.appendChild(th);
  132. table.appendChild(tr);
  133. var odd = 'odd';
  134. for (let i = 0; i < units.length; i++) {
  135. let tr = document.createElement('tr');
  136. let td = document.createElement('td');
  137. td.setAttribute('class', 'unit ' + odd);
  138. td.innerHTML = units[i].panel;
  139. tr.appendChild(td);
  140. td = document.createElement('td');
  141. td.setAttribute('class', 'score ' + odd);
  142. td.innerHTML = units[i].score;
  143. tr.appendChild(td);
  144. td = document.createElement('td');
  145. td.setAttribute('class', 'action ' + odd);
  146. btn = document.createElement('input');
  147. btn.setAttribute('type', 'button');
  148. btn.setAttribute('value', 'Optimize');
  149. btn.setAttribute('onclick', 'optimize("' + units[i].id + '")');
  150. td.appendChild(btn);
  151. tr.appendChild(td);
  152. table.appendChild(tr);
  153. if (odd == ''){
  154. odd = 'odd';
  155. }
  156. else{
  157. odd = '';
  158. }
  159. }
  160. container.appendChild(table);
  161. }
  162. </script>
  163. </head>
  164. <body onLoad='calculateScores();'>
  165. <?php
  166. include __DIR__ . "/inc/header.php";
  167. ?>
  168. <aside>
  169. <h2>
  170. Optimizer
  171. </h2>
  172. <p>
  173. Rate your teams and optimize the units that you use the most.
  174. <br/>
  175. <br/>
  176. <br/>
  177. To optimize units that are not on teams, do so from the <a href='/<?=$UID?>/monsters/'>monsters page</a>.
  178. </p>
  179. </aside>
  180. <main>
  181. <section class='content'>
  182. <h2>
  183. Teams
  184. </h2>
  185. <article>
  186. <table id='teams'>
  187. <tr>
  188. <th>
  189. Team
  190. </th>
  191. <th>
  192. Units
  193. </th>
  194. <th>
  195. Score
  196. </th>
  197. </tr>
  198. <?php
  199. $odd = "odd";
  200. foreach ($page->teams as $team){
  201. ?>
  202. <tr>
  203. <td class='team <?=$odd?>'>
  204. <?=$team->name?>
  205. </td>
  206. <td class='units <?=$odd?>'>
  207. <?php
  208. $prev_front = false;
  209. $front = "undefined";
  210. foreach ($team->unit as $unit){
  211. $front = in_array($unit->id, $team->frontline);
  212. if ($front != "undefined" && $prev_front != $front){
  213. ?>
  214. <hr class='frontline'/>
  215. <?php
  216. }
  217. ?>
  218. <a href='/<?=$UID?>/monsters/<?=$unit->id?>' target='_blank'>
  219. <div class='monster_panel'>
  220. <?=HTML::unit_panel($unit)?>
  221. <?php
  222. if ($unit->id == $team->leader){
  223. ?>
  224. <img alt='Leader' class='leader' src='<?=URL::IMG["ICON"]?>leader.png'/>
  225. <?php
  226. }
  227. ?>
  228. </div>
  229. </a>
  230. <?php
  231. $prev_front = $front;
  232. }
  233. ?>
  234. </td>
  235. <td class='score <?=$odd?>'>
  236. <input type='number' min='0' max='20' value='<?=$team->score?>' onChange='rateTeam(<?=$team->id?>, this.value);'/>
  237. </td>
  238. </tr>
  239. <?php
  240. if ($odd == ""){
  241. $odd = "odd";
  242. }
  243. else{
  244. $odd = "";
  245. }
  246. }
  247. ?>
  248. </table>
  249. <div id='unit_list'>
  250. </div>
  251. </article>
  252. </section> <!-- .list -->
  253. </main>
  254. <?php
  255. include __DIR__ . "/inc/footer.php";
  256. ?>
  257. </body>
  258. </html>