optimizer.php 11 KB

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