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