optimize.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * File for the optimization action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Executes the optimization action.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * mail
  14. * pass + currentPass
  15. * api
  16. * Then it updates the selected info with the prameter vlue. Multiple
  17. * itemas can be updated at the same time.
  18. *
  19. * @return int|string 0 on success, negative values on error. If the API
  20. * key has been updated, the new key.
  21. * @category Action
  22. * @global resource Database connection.
  23. */
  24. function action(){
  25. global $db;
  26. $response = null;
  27. $uid = filter_input(INPUT_POST, 'uid');
  28. if ($uid == null){
  29. error_log("-1");
  30. return -1;
  31. }
  32. $unit = filter_input(INPUT_POST, 'unit');
  33. if ($uid == null){
  34. error_log("-2");
  35. return -2;
  36. }
  37. // Sets
  38. // TODO: Check if valid.
  39. // TODO: Check if compatible sets, not just size.
  40. $sets = [];
  41. foreach ($_POST["set"] as $x){
  42. array_push($sets, intval($x));
  43. }
  44. if (sizeof($sets) < 2 || sizeof($sets) > 3){
  45. error_log("-3");
  46. return -3;
  47. }
  48. // Main stats in even slots.
  49. // TODO: Check if valid.
  50. // TODO: Check if valid per slot
  51. $stats = [];
  52. foreach ($_POST["stat"] as $x){
  53. array_push($stats, intval($x));
  54. }
  55. $source = filter_input(INPUT_POST, 'source');
  56. if ($source == null){
  57. error_log("-4");
  58. return -4;
  59. }
  60. $tuning = filter_input(INPUT_POST, 'tuning');
  61. if ($tuning == null){
  62. error_log("-5");
  63. return -5;
  64. }
  65. // Get optimization id and create the view:
  66. $optmization_id = $uid . "-" . $unit . "-" . rand(0,1000);
  67. $db->query($s);
  68. $s = "
  69. SELECT *
  70. FROM rune
  71. WHERE
  72. uid = $uid AND
  73. set IN (
  74. ";
  75. foreach ($sets as $set){
  76. $s .= ($set . ",");
  77. }
  78. $s .= "-1) AND ";
  79. switch ($source){
  80. case 0: // Storage only
  81. $s .= "assigned_to IS NULL";
  82. break;
  83. case 1: // Units in no teams
  84. $s .= "
  85. (
  86. assigned_to IS NULL OR
  87. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit)
  88. )
  89. ";
  90. break;
  91. case 2: // Units in teams with 0 score
  92. $s .= "
  93. (
  94. assigned_to IS NULL OR
  95. assigned_to NOT IN (SELECT DISTINCT unit FROM team_unit) OR
  96. assigned_to NOT IN (
  97. SELECT DISTINCT unit
  98. FROM
  99. team,
  100. team_unit
  101. WHERE
  102. team.id = team_unit.team AND
  103. team.score > 0
  104. )
  105. )
  106. ";
  107. break;
  108. case 3: // Units with lower overall score
  109. // TODO
  110. break;
  111. case 4: // All runes - dont filter
  112. $s .= " 1 = 1 ";
  113. break;
  114. default: // Invalid options - return nothing
  115. $s .= " 1 = 0 ";
  116. }
  117. error_log($s);
  118. $response = $s;
  119. if ($response == null){
  120. return 0;
  121. }
  122. else{
  123. return $response;
  124. }
  125. }
  126. ?>