optimizer.php 11 KB

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