/app/modules/tourney_status/create.php

https://github.com/eb/phptourney · PHP · 259 lines · 237 code · 14 blank · 8 comment · 28 complexity · 101d58033955eb3165c4996e1ac1860f MD5 · raw file

  1. <?php
  2. $content_tpl->set_block("F_CONTENT", "B_MESSAGE_BRACKET_CREATED", "H_MESSAGE_BRACKET_CREATED");
  3. $content_tpl->set_block("F_CONTENT", "B_MESSAGE", "H_MESSAGE");
  4. $content_tpl->set_block("F_CONTENT", "B_WARNING_TOURNEY_SYSTEM", "H_WARNING_TOURNEY_SYSTEM");
  5. $content_tpl->set_block("F_CONTENT", "B_WARNING_BRACKET_CREATED", "H_WARNING_BRACKET_CREATED");
  6. $content_tpl->set_block("F_CONTENT", "B_WARNING_TOURNEY_RUNNING", "H_WARNING_TOURNEY_RUNNING");
  7. $content_tpl->set_block("F_CONTENT", "B_WARNING_TOURNEY_FINISHED", "H_WARNING_TOURNEY_FINISHED");
  8. $content_tpl->set_block("F_CONTENT", "B_WARNING_NO_ACCESS", "H_WARNING_NO_ACCESS");
  9. $content_tpl->set_block("F_CONTENT", "B_WARNING", "H_WARNING");
  10. $content_tpl->set_block("F_CONTENT", "B_BACK_OVERVIEW", "H_BACK_OVERVIEW");
  11. // Access for headadmins only
  12. if ($user['usertype_headadmin'])
  13. {
  14. if ($season['single_elimination'] == "")
  15. {
  16. $content_tpl->parse("H_WARNING_TOURNEY_SYSTEM", "B_WARNING_TOURNEY_SYSTEM");
  17. $content_tpl->parse("H_WARNING", "B_WARNING");
  18. $content_tpl->set_var("I_ID_SEASON", $season['id']);
  19. $content_tpl->parse("H_BACK_OVERVIEW", "B_BACK_OVERVIEW");
  20. }
  21. elseif ($season['status'] == "bracket")
  22. {
  23. $content_tpl->parse("H_WARNING_BRACKET_CREATED", "B_WARNING_BRACKET_CREATED");
  24. $content_tpl->parse("H_WARNING", "B_WARNING");
  25. $content_tpl->set_var("I_ID_SEASON", $season['id']);
  26. $content_tpl->parse("H_BACK_OVERVIEW", "B_BACK_OVERVIEW");
  27. }
  28. elseif ($season['status'] == "running")
  29. {
  30. $content_tpl->parse("H_WARNING_TOURNEY_RUNNING", "B_WARNING_TOURNEY_RUNNING");
  31. $content_tpl->parse("H_WARNING", "B_WARNING");
  32. $content_tpl->set_var("I_ID_SEASON", $season['id']);
  33. $content_tpl->parse("H_BACK_OVERVIEW", "B_BACK_OVERVIEW");
  34. }
  35. elseif ($season['status'] == "finished")
  36. {
  37. $content_tpl->parse("H_WARNING_TOURNEY_FINISHED", "B_WARNING_TOURNEY_FINISHED");
  38. $content_tpl->parse("H_WARNING", "B_WARNING");
  39. $content_tpl->set_var("I_ID_SEASON", $season['id']);
  40. $content_tpl->parse("H_BACK_OVERVIEW", "B_BACK_OVERVIEW");
  41. }
  42. else
  43. {
  44. // Process seeding-groups
  45. $season_users_ref = dbQuery("SELECT * FROM `season_users` " .
  46. "WHERE `id_season` = {$season['id']} " .
  47. "AND `rejected` = 0 " .
  48. "AND `seedgroup` > 0 " .
  49. "AND `usertype_player` = 1 " .
  50. "ORDER BY `seedgroup` ASC, `submitted` ASC");
  51. $seeding_groups = array();
  52. while ($season_users_row = dbFetch($season_users_ref))
  53. {
  54. if (!isset($seeding_groups[$season_users_row['seedgroup']]))
  55. {
  56. $seeding_groups[$season_users_row['seedgroup']] = array();
  57. }
  58. array_push($seeding_groups[$season_users_row['seedgroup']], $season_users_row['id_user']);
  59. }
  60. $seedlevel = 1;
  61. ksort($seeding_groups);
  62. foreach($seeding_groups as $seeding_group) {
  63. $sg_size = count($seeding_group);
  64. for ($i = 0; $i < $sg_size; $i++)
  65. {
  66. srand((float)microtime() * 10000000);
  67. $rand_key = array_rand($seeding_group);
  68. $uid = $seeding_group[$rand_key];
  69. unset($seeding_group[$rand_key]);
  70. dbQuery("UPDATE `season_users` SET `seedlevel` = $seedlevel " .
  71. "WHERE `id_user` = $uid AND `id_season` = {$season['id']}");
  72. $seedlevel++;
  73. }
  74. }
  75. // Seed unseeded players
  76. $season_users_ref = dbQuery("SELECT * FROM `season_users` " .
  77. "WHERE `id_season` = {$season['id']} " .
  78. "AND `rejected` = 0 " .
  79. "AND `seedgroup` = 0 " .
  80. "AND `usertype_player` = 1 " .
  81. "ORDER BY `submitted` ASC");
  82. while ($season_users_row = dbFetch($season_users_ref))
  83. {
  84. dbQuery("UPDATE `season_users` SET `seedlevel` = $seedlevel " .
  85. "WHERE `id` = {$season_users_row['id']} AND `id_season` = {$season['id']}");
  86. $seedlevel++;
  87. }
  88. // Set all players who signed up too late to rejected
  89. if ($season['qualification'] == 1)
  90. {
  91. dbQuery("UPDATE `season_users` SET `rejected` = 1 " .
  92. "WHERE `id_season` = {$season['id']} " .
  93. "AND `rejected` = 0 " .
  94. "AND `seedlevel` > {$season['single_elimination']} + {$season['single_elimination']} / 2 " .
  95. "AND `usertype_player` = 1");
  96. // Unset qualification if no qualification games were created
  97. $season_users_ref = dbQuery("SELECT * FROM `season_users` " .
  98. "WHERE `id_season` = {$season['id']} " .
  99. "AND `seedlevel` > {$season['single_elimination']} " .
  100. "AND `usertype_player` = 1");
  101. if (dbNumRows($season_users_ref) == 0)
  102. {
  103. dbQuery("UPDATE `seasons` SET `qualification` = 0 " .
  104. "WHERE `id` = {$season['id']}");
  105. }
  106. }
  107. else
  108. {
  109. dbQuery("UPDATE `season_users` SET `rejected` = 1 " .
  110. "WHERE `id_season` = {$season['id']} " .
  111. "AND `rejected` = 0 " .
  112. "AND `seedlevel` > {$season['single_elimination']} " .
  113. "AND `usertype_player` = 1");
  114. }
  115. // Set up the first round of matches
  116. $num_winmaps = $season['winmaps'];
  117. $num_matches = $season['single_elimination'] / 2;
  118. $matches = getSeeding($num_matches);
  119. $counter = 1;
  120. foreach($matches as $match) {
  121. $seedlevel = $counter;
  122. $users_ref = dbQuery("SELECT U.`id` " .
  123. "FROM `users` U, `season_users` SU " .
  124. "WHERE SU.`id_season` = {$season['id']} " .
  125. "AND SU.`usertype_player` = 1 " .
  126. "AND SU.`seedlevel` = $seedlevel " .
  127. "AND U.`id` = SU.`id_user`");
  128. if ($users_row = dbFetch($users_ref))
  129. {
  130. $id_player1 = $users_row['id'];
  131. }
  132. $seedlevel = $season['single_elimination'] - $counter + 1;
  133. $users_ref = dbQuery("SELECT U.`id` " .
  134. "FROM `users` U, `season_users` SU " .
  135. "WHERE SU.`id_season` = {$season['id']} " .
  136. "AND SU.`usertype_player` = 1 " .
  137. "AND SU.`seedlevel` = $seedlevel " .
  138. "AND U.`id` = SU.`id_user`");
  139. if ($users_row = dbFetch($users_ref))
  140. {
  141. $id_player2 = $users_row['id'];
  142. }
  143. $qualification = 0;
  144. if ($season['qualification'] == 1 and isset($id_player2))
  145. {
  146. $seedlevel = $season['single_elimination'] + $counter;
  147. $users_ref = dbQuery("SELECT U.`id` " .
  148. "FROM `users` U, `season_users` SU " .
  149. "WHERE SU.`id_season` = {$season['id']} " .
  150. "AND SU.`usertype_player` = 1 " .
  151. "AND SU.`seedlevel` = $seedlevel " .
  152. "AND U.`id` = SU.`id_user`");
  153. if ($users_row = dbFetch($users_ref))
  154. {
  155. $id_player3 = $users_row['id'];
  156. }
  157. if (isset($id_player1) and isset($id_player2) and isset($id_player3))
  158. {
  159. $qualification = 1;
  160. dbQuery("INSERT INTO `matches` (`id_season`, `bracket`, `round`, `match`, `id_player1`, `num_winmaps`) " .
  161. "VALUES ({$season['id']}, 'wb', 1, $match, $id_player1, $num_winmaps)");
  162. dbQuery("INSERT INTO `matches` (`id_season`, `bracket`, `round`, `match`, `id_player1`, `id_player2`, `num_winmaps`) " .
  163. "VALUES ({$season['id']}, 'q', 1, $match, $id_player2, $id_player3, $num_winmaps)");
  164. }
  165. }
  166. if (!$qualification)
  167. {
  168. if (isset($id_player1) and isset($id_player2))
  169. {
  170. dbQuery("INSERT INTO `matches` (`id_season`, `bracket`, `round`, `match`, `id_player1`, `id_player2`, `num_winmaps`) " .
  171. "VALUES ({$season['id']}, 'wb', 1, $match, $id_player1, $id_player2, $num_winmaps)");
  172. }
  173. elseif (isset($id_player1) and !isset($id_player2))
  174. {
  175. dbQuery("INSERT INTO `matches` " .
  176. "(`id_season`, `bracket`, `round`, `match`, `id_player1`, `num_winmaps`) " .
  177. "VALUES ({$season['id']}, 'wb', 1, $match, $id_player1, $num_winmaps)");
  178. }
  179. elseif (!isset($id_player1) and isset($id_player2))
  180. {
  181. dbQuery("INSERT INTO `matches` " .
  182. "(`id_season`, `bracket`, `round`, `match`, `id_player2`, `num_winmaps`) " .
  183. "VALUES ({$season['id']}, 'wb', 1, $match, $id_player2, $num_winmaps)");
  184. }
  185. else
  186. {
  187. dbQuery("INSERT INTO `matches` " .
  188. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  189. "VALUES ({$season['id']}, 'wb', 1, $match, $num_winmaps)");
  190. }
  191. }
  192. unset($id_player1);
  193. unset($id_player2);
  194. unset($id_player3);
  195. $counter++;
  196. }
  197. // Set up the remaining rounds of matches
  198. for ($i = 2; $i <= getNumWBRounds($season); $i++)
  199. {
  200. $num_matches = $season['single_elimination'] / pow(2, $i);
  201. for ($j = 1; $j <= $num_matches; $j++)
  202. {
  203. dbQuery("INSERT INTO `matches` " .
  204. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  205. "VALUES ({$season['id']}, 'wb', $i, $j, $num_winmaps)");
  206. }
  207. }
  208. if ($season['double_elimination'] != "")
  209. {
  210. for ($i = 1; $i <= getNumLBRounds($season) / 2; $i++)
  211. {
  212. $num_matches = $season['double_elimination'] / pow(2, $i + 1);
  213. for ($j = 1; $j <= $num_matches; $j++)
  214. {
  215. $num_round = $i * 2 - 1;
  216. dbQuery("INSERT INTO `matches` " .
  217. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  218. "VALUES ({$season['id']}, 'lb', $num_round, $j, $num_winmaps)");
  219. $num_round = $i * 2;
  220. dbQuery("INSERT INTO `matches` " .
  221. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  222. "VALUES ({$season['id']}, 'lb', $num_round, $j, $num_winmaps)");
  223. }
  224. }
  225. dbQuery("INSERT INTO `matches` " .
  226. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  227. "VALUES ({$season['id']}, 'gf', 1, 1, $num_winmaps)");
  228. dbQuery("INSERT INTO `matches` " .
  229. "(`id_season`, `bracket`, `round`, `match`, `num_winmaps`) " .
  230. "VALUES ({$season['id']}, 'gf', 1, 2, $num_winmaps)");
  231. }
  232. // Set season-status to bracket
  233. dbQuery("UPDATE `seasons` SET `status` = 'bracket' WHERE `id` = {$season['id']}");
  234. $content_tpl->parse("H_MESSAGE_BRACKET_CREATED", "B_MESSAGE_BRACKET_CREATED");
  235. $content_tpl->parse("H_MESSAGE", "B_MESSAGE");
  236. $content_tpl->set_var("I_ID_SEASON", $season['id']);
  237. $content_tpl->parse("H_BACK_OVERVIEW", "B_BACK_OVERVIEW");
  238. }
  239. }
  240. else
  241. {
  242. $content_tpl->parse("H_WARNING_NO_ACCESS", "B_WARNING_NO_ACCESS");
  243. $content_tpl->parse("H_WARNING", "B_WARNING");
  244. }
  245. ?>