Guest User

Untitled

a guest
Mar 1st, 2021
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 73.91 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('IN_PHPBB')) {
  4. exit;
  5. }
  6.  
  7. class score {
  8. var $user_id;
  9. var $games_sql;
  10. var $games_nbr;
  11.  
  12. function score($user_id = 0, $games = array()) {
  13. $this->games_sql = array();
  14. $this->games_sql[0] = array();
  15. $this->games_sql[1] = array();
  16.  
  17. if ($user_id < 0) {
  18. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  19. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  20.  
  21. meta_refresh(5, $redirect);
  22. trigger_error($user->lang['NO_USER'] . '<br /><br />' . sprintf($l_redirect, '<a href="' . $redirect . '">', '</a>', E_USER_ERROR));
  23. }
  24.  
  25. $this->user_id = (int)$user_id;
  26. $this->games_nbr = sizeof($games);
  27.  
  28. for ($i = 0; $i < $this->games_nbr; $i++) {
  29. if (!(int)$games[$i]['best']) {
  30. $this->games_sql[0][] = (int)$games[$i]['id'];
  31. } else {
  32. $this->games_sql[1][] = (int)$games[$i]['id'];
  33. }
  34. }
  35. }
  36.  
  37. //DoneArk Invaders
  38.  
  39. function is_valid_score($game_score, $variable_name, $game_ssid, $game_id) {
  40. global $db, $phpbb_root_path, $phpEx, $user;
  41.  
  42. if (!is_numeric($game_score)) {
  43. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  44. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  45.  
  46. meta_refresh(5, $redirect);
  47. trigger_error($user->lang['ERROR_SAVE_GAME'] . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  48. }
  49.  
  50. if ($game_score < 1) {
  51. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  52. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  53.  
  54. meta_refresh(5, $redirect);
  55. trigger_error('your score is zero, please replay<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  56. }
  57.  
  58. $game_score = (double)$game_score;
  59. $check_session = true;
  60.  
  61. if (($game_ssid != $user->data['session_id']) && ($check_session)) {
  62. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  63. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  64.  
  65. meta_refresh(5, $redirect);
  66. trigger_error(($user->lang['ERROR_SAVE_GAME']) . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  67. }
  68.  
  69. $sql_array = array(
  70. 'SELECT' => 's.session_id, s.session_forum_id, s.start_time',
  71.  
  72. 'FROM' => array(
  73. SESSIONS_ARCADE_TABLE => 's'
  74. ),
  75.  
  76. 'WHERE' => 'game_id = ' . $game_id . ' AND gamer_id = ' . (int)$user->data['user_id']
  77. );
  78.  
  79. $sql = $db->sql_build_query('SELECT', $sql_array);
  80. $result = $db->sql_query($sql);
  81. $sess = $db->sql_fetchrow($result);
  82. $db->sql_freeresult($result);
  83.  
  84. if (!$sess) {
  85. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  86. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  87.  
  88. meta_refresh(5, $redirect);
  89. trigger_error($user->lang['NO_SESSION'] . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  90. }
  91.  
  92. if ((md5($user->ip) != $sess['session_id']) || ($user->data['session_id'] != $sess['session_forum_id'])) {
  93. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  94. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  95.  
  96. meta_refresh(5, $redirect);
  97. trigger_error($user->lang['DIFFERENT_SESSION'] . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  98. }
  99.  
  100. $sql_array = array(
  101. 'SELECT' => 'g.variable_name, g.cat_id, g.name, g.best_score_first',
  102.  
  103. 'FROM' => array(
  104. GAMES_TABLE => 'g'
  105. ),
  106.  
  107. 'WHERE' => 'g.id = ' . (int)$game_id
  108. );
  109.  
  110. $sql = $db->sql_build_query('SELECT', $sql_array);
  111. $result = $db->sql_query($sql);
  112. $game_data = $db->sql_fetchrow($result);
  113. $db->sql_freeresult($result);
  114.  
  115. if (!$game_data) {
  116. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  117. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  118.  
  119. meta_refresh(5, $redirect);
  120. trigger_error($user->lang['NO_GAME'] . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  121. }
  122.  
  123. if ($game_data['variable_name'] != $variable_name) {
  124. $redirect = append_sid("{$phpbb_root_path}arcadeoa.$phpEx");
  125. $l_redirect = $user->lang['RETURN_ARCADEOA'];
  126.  
  127. meta_refresh(5, $redirect);
  128. trigger_error($user->lang['ERROR_VARIABLE_GAME'] . '<br /><br />' . sprintf($l_redirect, '<a href="arcadeoa.php?mode=game&id=' . $game_id . '">', '</a>', E_USER_ERROR));
  129. }
  130.  
  131. }
  132.  
  133. function send_challenge_pm($from, $to, $subject, $text) {
  134. global $db, $phpEx, $phpbb_root_path, $user;
  135. if (!class_exists('parse_message')) {
  136. include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
  137. }
  138. $time = time();
  139. $message_parser = new parse_message();
  140. $message_parser->parse(true, true, true, false, false, true, true);
  141. $uid = $message_parser->bbcode_uid;
  142. $db->sql_query('INSERT INTO phpbb_privmsgs (author_id,message_time,message_subject,`message_text`,to_address,bcc_address,bbcode_uid) VALUES (' . $from . ',' . $time . ',"' . $subject . '","' . addslashes($text) . '","u_' . $to . '",0,"' . $uid . '")');
  143.  
  144. $lastid = $db->sql_nextid();
  145. $db->sql_query('INSERT INTO phpbb_privmsgs_to (msg_id,user_id,author_id,folder_id) VALUES (' . $lastid . ',' . $to . ',' . $from . ',-3)');
  146.  
  147. $db->sql_query('UPDATE phpbb_users SET user_new_privmsg = user_new_privmsg+1,user_unread_privmsg = user_unread_privmsg+1,user_last_privmsg = ' . $time . ' WHERE user_id = ' . $to);
  148.  
  149. }
  150.  
  151. function challenges_check($gid, $gamename) {
  152. global $db, $user;
  153. $check = $db->sql_query('SELECT * FROM phpbb_oa_challenges WHERE gid = ' . $gid . ' and challenger = ' . $user->data['user_id'] . ' or `user` = ' . $user->data['user_id']);
  154. if (mysqli_num_rows($check) > 0) {
  155. $row = $db->sql_fetchrow($check);
  156. $losttext = 'Vous avez perdu le défi dans le jeu <a href="arcadeoa.php?mode=game&id=' . $gid . '" style="font-weight:bold;">' . $gamename . '</a>.';
  157. $wintext = 'Vous avez gagné le défi dans le jeu <a href="arcadeoa.php?mode=game&id=' . $gid . '" style="font-weight:bold;">' . $gamename . '</a>.<br>Félicitations!';
  158. if ($row['challenger'] == $user->data['user_id'] && $row['user_score'] > 0) {
  159.  
  160. if ($row['challenger_score'] > $row['user_score']) {
  161. $this->send_challenge_pm($user->data['user_id'], $row['user'], 'Défi perdu', $losttext);
  162. $this->send_challenge_pm($row['user'], $row['challenger'], 'Défi gagné', $wintext);
  163. $db->sql_query('UPDATE phpbb_users SET user_points = user_points+' . $row['bet'] . ' WHERE user_id = ' . $row['challenger']);
  164. } else {
  165. $this->send_challenge_pm($user->data['user_id'], $row['user'], 'Défi gagné', $wintext);
  166. $this->send_challenge_pm($row['user'], $row['challenger'], 'Défi perdu', $losttext);
  167. $db->sql_query('UPDATE phpbb_users SET user_points = user_points+' . $row['bet'] . ' WHERE user_id = ' . $row['user']);
  168. }
  169. $db->sql_query('DELETE FROM phpbb_oa_challenges WHERE id = ' . $row['id']);
  170. } elseif ($row['user'] == $user->data['user_id'] && $row['challenger_score'] > 0) {
  171. if ($row['user_score'] > $row['challenger_score']) {
  172. $this->send_challenge_pm($user->data['user_id'], $row['challenger'], 'Challenge lost', $losttext);
  173. $this->send_challenge_pm($row['challenger'], $row['user'], 'Challenge won', $wintext);
  174. $db->sql_query('UPDATE phpbb_users SET user_points = user_points+' . $row['bet'] . ' WHERE user_id = ' . $row['user']);
  175. } else {
  176. $this->send_challenge_pm($user->data['user_id'], $row['challenger'], 'Challenge won', $wintext);
  177. $this->send_challenge_pm($row['challenger'], $row['user'], 'Challenge lost', $losttext);
  178. $db->sql_query('UPDATE phpbb_users SET user_points = user_points+' . $row['bet'] . ' WHERE user_id = ' . $row['challenger']);
  179. }
  180. $db->sql_query('DELETE FROM phpbb_oa_challenges WHERE id = ' . $row['id']);
  181. }
  182. }
  183. }
  184.  
  185. function challenges_save($gid, $score) {
  186. global $db, $user, $phpEx, $arcadeoa;
  187. $check = $db->sql_query('SELECT * FROM phpbb_oa_challenges WHERE gid = ' . $gid . ' and challenger = ' . $user->data['user_id'] . ' or `user` = ' . $user->data['user_id']);
  188. if (mysqli_num_rows($check) > 0) {
  189. $row = $db->sql_fetchrow($check);
  190. if ($row['challenger'] == $user->data['user_id']) {
  191. $db->sql_query('UPDATE phpbb_oa_challenges SET challenger_score = ' . $score . ' WHERE id = ' . $row['id']);
  192. } elseif ($row['user'] == $user->data['user_id']) {
  193. $db->sql_query('UPDATE phpbb_oa_challenges SET user_score = ' . $score . ' WHERE id = ' . $row['id']);
  194. }
  195.  
  196. }
  197.  
  198. }
  199.  
  200. //Done
  201. function save_score($game_score, $game_id) {
  202. global $db, $user, $phpEx, $arcadeoa;
  203.  
  204. $sql_array = array(
  205. 'SELECT' => 'g.variable_name, g.cat_id, g.name, g.best_score_first',
  206.  
  207. 'FROM' => array(
  208. GAMES_TABLE => 'g'
  209. ),
  210.  
  211. 'WHERE' => 'g.id = ' . (int)$game_id
  212. );
  213. $playtime = time() - $_COOKIE['startplay'];
  214. unset($_COOKIE['startplay']);
  215. $sql = $db->sql_build_query('SELECT', $sql_array);
  216. $result = $db->sql_query($sql);
  217. $game_data = $db->sql_fetchrow($result);
  218. $db->sql_freeresult($result);
  219. $game_name = $game_data['name'];
  220. $this->challenges_save($game_id, $game_score);
  221. $this->challenges_check((int)$game_id, $game_name);
  222. //Si $bscore_first = true(1) -> Le plus gros score est le mieux pay�
  223. //Si $bscore_first = false(0) -> Le plus petit score est le mieux pay�
  224. $bscore_first = ((int)$game_data['best_score_first'] == 0) ? true : false;
  225.  
  226. //Select first score for the game
  227. $sql_array = array(
  228. 'SELECT' => 's.id, s.score_date, s.points, s.gamer_id',
  229.  
  230. 'FROM' => array(
  231. SCORES_TABLE => 's'
  232. ),
  233.  
  234. 'WHERE' => 's.is_first = 1 AND s.game_id = ' . (int)$game_id,
  235. );
  236.  
  237. $sql = $db->sql_build_query('SELECT', $sql_array);
  238. $result = $db->sql_query($sql);
  239. $row = $db->sql_fetchrow($result);
  240. $db->sql_freeresult($result);
  241.  
  242. $first_score = 0;
  243. $first_user = 0;
  244. $first_date = 0;//V4
  245. $sid = 0;
  246. $games = $db->sql_query('SELECT * FROM phpbb_games WHERE id = ' . $game_id);
  247. $game = $db->sql_fetchrow($games);
  248. if ($row) {
  249.  
  250. $first_score = $row['points']; //Meilleur score du jeu
  251. $first_user = (int)$row['gamer_id'];
  252. $first_date = $row['score_date'];//V4
  253. $sid = (int)$row['id'];
  254. } else {
  255.  
  256. //No score for this game, user will be the first, no need to check the score of other gamers
  257. $sql_ary = array(
  258. 'points' => $game_score,
  259. 'is_first' => 1,
  260. 'cat_id' => (int)$game_data['cat_id'],
  261. 'score_comment' => '',
  262. 'game_id' => (int)$game_id,
  263. 'gamer_id' => (int)$user->data['user_id'],
  264. 'score_date' => (int)time(),
  265. 'playtime' => $playtime,
  266. 'playcount' => 1
  267. );
  268.  
  269. $db->sql_query('INSERT INTO ' . SCORES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  270. $db->sql_query('UPDATE phpbb_games SET jackpot = jackpot + ' . $game['costs'] . ' WHERE id = ' . $game_id);
  271. return true;
  272. }
  273.  
  274. //Grab user score for this game
  275. $sql_array = array(
  276. 'SELECT' => 's.id, s.points',
  277.  
  278. 'FROM' => array(
  279. SCORES_TABLE => 's'
  280. ),
  281.  
  282. 'WHERE' => 's.game_id = ' . (int)$game_id . ' AND s.gamer_id = ' . (int)$user->data['user_id']
  283. );
  284.  
  285. $sql = $db->sql_build_query('SELECT', $sql_array);
  286. $result = $db->sql_query($sql);
  287. $row = $db->sql_fetchrow($result);
  288. $db->sql_freeresult($result);
  289.  
  290. $is_first = false;
  291.  
  292. if (!$row) //L'utilisateur n'a pas jou� � ce jeu
  293. {
  294. //No score for this game, insert one
  295. //Si jeu ou le plus gros score est le mieux pay� -> Si meilleur score SCORES_TABLE < nouveau score -> true
  296. //Si jeu ou le plus petit score est le mieux pay� -> Si meilleur score SCORES_TABLE > nouveau score -> true
  297. //$is_first = ($bscore_first ? ($first_score > $game_score ? false : true) : ($first_score < $game_score ? false : true));//V3
  298.  
  299. $is_first = ($bscore_first ? ($game_score > $first_score ? true : false) : ($game_score < $first_score ? true : false));//V4
  300.  
  301. if ($is_first) {
  302. $sql_ary = array(
  303. 'is_first' => 0,
  304. 'playcount' => 'playcount+1'
  305. );
  306.  
  307. $sql = 'UPDATE ' . SCORES_TABLE . ' SET is_first = 0,playcount = playcount+1 WHERE id = ' . $sid;
  308. //$sql = 'UPDATE ' . SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE id = ' . $sid;
  309. $db->sql_query($sql);
  310. $arcadeoa->send_pm_loser($first_user, $game_id, $game_name, 0, $user->data['user_id']);
  311. }
  312.  
  313. $sql_ary = array(
  314. 'points' => $game_score,
  315. 'is_first' => $is_first,
  316. 'cat_id' => (int)$game_data['cat_id'],
  317. 'score_comment' => '',
  318. 'game_id' => (int)$game_id,
  319. 'gamer_id' => (int)$user->data['user_id'],
  320. 'score_date' => (int)time(),
  321. 'playtime' => $playtime,
  322. 'playcount' => 1
  323. );
  324.  
  325. $db->sql_query('INSERT INTO ' . SCORES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  326. } else {
  327.  
  328. //User already played this game - L'utilisateur a d�j� jou� � ce jeu
  329. $old_score = $row['points'];
  330. $old_score_id = (int)$row['id'];
  331.  
  332. $did_he_beat_his_score = ($bscore_first ? ($old_score > $game_score ? false : true) : ($old_score < $game_score ? false : true));
  333.  
  334. //$is_first = ($bscore_first ? ($first_score > $game_score ? false : true) : ($first_score < $game_score ? false : true));//V3
  335.  
  336. $is_first = ($bscore_first ? ($game_score > $first_score ? true : false) : ($game_score < $first_score ? true : false));//V4
  337.  
  338. if ($is_first) {
  339. $sql_ary = array(
  340. 'is_first' => 0,
  341.  
  342. );
  343. $sql = 'UPDATE ' . SCORES_TABLE . ' SET is_first = 0,playcount = playcount+1 WHERE id = ' . $sid;
  344. // $sql = 'UPDATE ' . SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE id = ' . $sid;
  345. $db->sql_query($sql);
  346. $arcadeoa->send_pm_loser($first_user, $game_id, $game_name, 0, $user->data['user_id']);
  347. }
  348.  
  349. if ($did_he_beat_his_score) {
  350. $sql_ary = array(
  351. 'points' => $game_score,
  352. 'score_date' => time(),
  353. 'is_first' => $is_first,
  354. 'playtime' => $playtime
  355. );
  356. if (!$is_first) {
  357. $is_first = 0;
  358. }
  359. $sql = 'UPDATE ' . SCORES_TABLE . ' SET points = ' . $game_score . ',score_date = ' . time() . ',is_first = ' . $is_first . ',playtime=' . $playtime . ',playcount = playcount+1 WHERE id = ' . $old_score_id;
  360. //$sql = 'UPDATE ' . SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE id = ' . $old_score_id;
  361. $db->sql_query($sql);
  362. }
  363.  
  364. }
  365.  
  366. return $is_first;
  367. }
  368.  
  369. //Done
  370. function save_championship_score($game_score, $game_id, $round_id, $championship_id) {
  371. global $db, $user, $phpEx, $arcadeoa;
  372.  
  373. $sql_array = array(
  374. 'SELECT' => 'g.name, g.best_score_first',
  375.  
  376. 'FROM' => array(
  377. GAMES_TABLE => 'g'
  378. ),
  379.  
  380. 'WHERE' => 'g.id = ' . (int)$game_id
  381. );
  382.  
  383. $sql = $db->sql_build_query('SELECT', $sql_array);
  384. $result = $db->sql_query($sql);
  385.  
  386. $game_data = $db->sql_fetchrow($result);
  387. $db->sql_freeresult($result);
  388. $game_name = $game_data['name'];
  389. $bscore_first = ((int)$game_data['best_score_first'] == 0) ? true : false;
  390.  
  391. //Select first score for the game
  392. $sql_array = array(
  393. 'SELECT' => 's.id, s.gamer_id, s.points',
  394.  
  395. 'FROM' => array(
  396. CHAMPIONSHIPS_SCORES_TABLE => 's'
  397. ),
  398.  
  399. 'WHERE' => 's.is_first = 1 AND s.game_id = ' . (int)$game_id . '
  400. AND s.championship_id = ' . (int)$championship_id . '
  401. AND s.round_id = ' . (int)$round_id
  402. );
  403.  
  404. $sql = $db->sql_build_query('SELECT', $sql_array);
  405. $result = $db->sql_query($sql);
  406. $row = $db->sql_fetchrow($result);
  407. $db->sql_freeresult($result);
  408.  
  409. $first_score = 0;
  410. $first_user = 0;
  411. $sid = 0;
  412.  
  413. if ($row) {
  414. $first_user = $row['gamer_id'];
  415. $sid = $row['id'];
  416. $first_score = $row['points'];
  417. } else {
  418. //Nobody played this game, insert the score
  419. $sql_ary = array(
  420. 'points' => $game_score,
  421. 'round_id' => $round_id,
  422. 'is_first' => 1,
  423. 'score_comment' => '',
  424. 'championship_id' => $championship_id,
  425. 'game_id' => (int)$game_id,
  426. 'gamer_id' => (int)$user->data['user_id'],
  427. 'score_date' => (int)time()
  428. );
  429.  
  430. $db->sql_query('INSERT INTO ' . CHAMPIONSHIPS_SCORES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  431. return true;
  432. }
  433.  
  434. //Grab user score for this game
  435. $sql_array = array(
  436. 'SELECT' => 's.id, s.points',
  437.  
  438. 'FROM' => array(
  439. CHAMPIONSHIPS_SCORES_TABLE => 's'
  440. ),
  441.  
  442. 'WHERE' => 's.game_id = ' . (int)$game_id . '
  443. AND s.championship_id = ' . $championship_id . '
  444. AND s.gamer_id = ' . (int)$user->data['user_id'] . '
  445. AND s.round_id = ' . (int)$round_id
  446. );
  447.  
  448. $sql = $db->sql_build_query('SELECT', $sql_array);
  449. $result = $db->sql_query($sql);
  450. $row = $db->sql_fetchrow($result);
  451. $db->sql_freeresult($result);
  452.  
  453. if (!$row) {
  454. //No score for this game, insert one
  455. //$is_first = ($bscore_first ? ($first_score > $game_score ? 0 : 1) : ($first_score < $game_score ? 0 : 1));
  456. $is_first = ($bscore_first ? ($game_score > $first_score ? true : false) : ($game_score < $first_score ? true : false));//V4
  457. if ($is_first) {
  458. $sql_ary = array(
  459. 'is_first' => 0,
  460. );
  461. $sql = 'UPDATE ' . CHAMPIONSHIPS_SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  462. WHERE id = ' . (int)$sid;
  463. $db->sql_query($sql);
  464. $arcadeoa->send_pm_loser($first_user, $game_id, $game_name, $round_id, $user->data['user_id']);
  465. }
  466.  
  467. $sql_ary = array(
  468. 'points' => $game_score,
  469. 'round_id' => $round_id,
  470. 'is_first' => $is_first,
  471. 'championship_id' => $championship_id,
  472. 'score_comment' => '',
  473. 'game_id' => (int)$game_id,
  474. 'gamer_id' => (int)$user->data['user_id'],
  475. 'score_date' => (int)time()
  476. );
  477.  
  478. $db->sql_query('INSERT INTO ' . CHAMPIONSHIPS_SCORES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  479.  
  480. } else {
  481. //User already played this game
  482. $old_score = $row['points'];
  483. $old_score_id = (int)$row['id'];
  484. $did_he_beat_his_score = ($bscore_first ? ($old_score > $game_score ? 0 : 1) : ($old_score < $game_score ? 0 : 1));
  485.  
  486. //$is_first = ($bscore_first ? ($first_score > $game_score ? 0 : 1) : ($first_score < $game_score ? 0 : 1));
  487. $is_first = ($bscore_first ? ($game_score > $first_score ? true : false) : ($game_score < $first_score ? true : false));//V4
  488.  
  489. if ($is_first) {
  490. $sql_ary = array(
  491. 'is_first' => 0,
  492. );
  493. $sql = 'UPDATE ' . CHAMPIONSHIPS_SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  494. WHERE id = ' . (int)$sid;
  495. $db->sql_query($sql);
  496. $arcadeoa->send_pm_loser($first_user, $game_id, $game_name, $round_id, $user->data['user_id']);
  497. }
  498.  
  499. if ($did_he_beat_his_score) {
  500. $sql_ary = array(
  501. 'points' => $game_score,
  502. 'score_date' => time(),
  503. 'is_first' => $is_first,
  504. );
  505.  
  506. $sql = 'UPDATE ' . CHAMPIONSHIPS_SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  507. WHERE id = ' . (int)$old_score_id;
  508. $db->sql_query($sql);
  509. }
  510.  
  511. }
  512.  
  513. return $is_first;
  514. }
  515.  
  516. //Done
  517. function save_ultim_score($game_score, $game_id) {
  518. global $db, $user, $phpEx;
  519.  
  520. $sql_array = array(
  521. 'SELECT' => 'g.variable_name, g.cat_id, g.best_score_first',
  522.  
  523. 'FROM' => array(
  524. GAMES_TABLE => 'g'
  525. ),
  526.  
  527. 'WHERE' => 'g.id = ' . (int)$game_id
  528. );
  529.  
  530. $sql = $db->sql_build_query('SELECT', $sql_array);
  531. $result = $db->sql_query($sql);
  532. $game_data = $db->sql_fetchrow($result);
  533. $db->sql_freeresult($result);
  534. $bscore_first = ((int)$game_data['best_score_first'] == 0) ? true : false;
  535.  
  536. //Check for the ultim record
  537. $sql_array = array(
  538. 'SELECT' => 's.id, s.points',
  539.  
  540. 'FROM' => array(
  541. ULTIM_SCORES_TABLE => 's'
  542. ),
  543.  
  544. 'WHERE' => 's.game_id = ' . (int)$game_id
  545. );
  546.  
  547. $sql = $db->sql_build_query('SELECT', $sql_array);
  548. $result = $db->sql_query($sql);
  549. $row = $db->sql_fetchrow($result);
  550. $db->sql_freeresult($result);
  551. $games = $db->sql_query('SELECT * FROM phpbb_games WHERE id = ' . $game_id);
  552. $game = $db->sql_fetchrow($games);
  553.  
  554. if ($row) {
  555. $first_score = $row['points'];
  556. $sid = $row['id'];
  557. $beat_it = ($bscore_first ? ($first_score > $game_score ? 0 : 1) : ($first_score < $game_score ? 0 : 1));
  558. //There is an ultim score, check if we have beaten it
  559. if ($beat_it) {
  560. $sql_ary = array(
  561. 'points' => $game_score,
  562. 'score_date' => time(),
  563. 'gamer_id' => (int)$user->data['user_id']
  564. );
  565.  
  566.  
  567. $db->sql_query('UPDATE phpbb_users SET user_points = user_points + ' . $game['jackpot'] . ' WHERE user_id = ' . $user->data['user_id']);
  568. $db->sql_query('UPDATE phpbb_games SET jackpot = 40 WHERE id = ' . $game_id);
  569. $sql = 'UPDATE ' . ULTIM_SCORES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  570. WHERE id = ' . (int)$sid;
  571. $db->sql_query($sql);
  572. }
  573.  
  574. } else {
  575. //No ultim record, insert one
  576. $sql_ary = array(
  577. 'points' => $game_score,
  578. 'cat_id' => (int)$game_data['cat_id'],
  579. 'score_comment' => '',
  580. 'game_id' => (int)$game_id,
  581. 'gamer_id' => (int)$user->data['user_id'],
  582. 'score_date' => (int)time()
  583. );
  584.  
  585. $db->sql_query('INSERT INTO ' . ULTIM_SCORES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  586. $db->sql_query('UPDATE phpbb_users SET user_points = user_points + ' . $game['jackpot'] . ' WHERE user_id = ' . $user->data['user_id']);
  587. $db->sql_query('UPDATE phpbb_games SET jackpot = 40 WHERE id = ' . $game_id);
  588. }
  589.  
  590. $settings = $db->sql_query('SELECT * FROM phpbb_arcade WHERE config_name = "chatmsg"');
  591. $cfg = $db->sql_fetchrow($settings);
  592.  
  593. if ($cfg['config_value'] == 1) {
  594. $userrecord = getuser($user->data['user_id']);
  595. $text = $userrecord . ' a fait un score sur <a href="arcadeoa.php?mode=game&id=' . $game_id . '" style="color:red;">' . $game['name'] . '</a> avec ' . $game_score . ' points.';
  596. $db->sql_query('INSERT INTO phpbb_mchat (user_id,user_ip,message,message_time) VALUES (172,"' . $_SERVER['REMOTE_ADDR'] . '","' . addslashes($text) . '",' . time() . ')');
  597. }
  598.  
  599. }
  600.  
  601. //Get high scores for this games and all users in a championship - done
  602. function get_high_scores_games_championship($round_id, $champ_id) {
  603. global $db;
  604.  
  605. $scores = array();
  606. $max_points = array();
  607.  
  608. if ($this->games_nbr == 0) {
  609. return $scores;
  610. }
  611.  
  612. if (sizeof($this->games_sql[0])) {
  613. $sql_array = array(
  614. 'SELECT' => 's.id',
  615.  
  616. 'FROM' => array(
  617. CHAMPIONSHIPS_SCORES_TABLE => 's'
  618. ),
  619.  
  620. 'WHERE' => 's.round_id = ' . $round_id . ' AND s.championship_id = ' . $champ_id . ' AND s.is_first = 1 AND ' . $db->sql_in_set('s.game_id', $this->games_sql[0])
  621. );
  622.  
  623. $sql = $db->sql_build_query('SELECT', $sql_array);
  624. $result = $db->sql_query($sql);
  625.  
  626. while ($row = $db->sql_fetchrow($result)) {
  627. $max_points[] = $row['id'];
  628. }
  629. $db->sql_freeresult($result);
  630. }
  631.  
  632. if (sizeof($this->games_sql[1])) {
  633. $sql_array = array(
  634. 'SELECT' => 's.id',
  635.  
  636. 'FROM' => array(
  637. CHAMPIONSHIPS_SCORES_TABLE => 's'
  638. ),
  639.  
  640. 'WHERE' => 's.round_id = ' . $round_id . ' AND s.championship_id = ' . $champ_id . ' AND s.is_first = 1 AND ' . $db->sql_in_set('s.game_id', $this->games_sql[1])
  641. );
  642.  
  643. $sql = $db->sql_build_query('SELECT', $sql_array);
  644. $result = $db->sql_query($sql);
  645.  
  646. while ($row = $db->sql_fetchrow($result)) {
  647. $max_points[] = $row['id'];
  648. }
  649. $db->sql_freeresult($result);
  650. }
  651.  
  652. if (sizeof($max_points) == 0) {
  653. return $scores;
  654. }
  655.  
  656. $sql_array = array(
  657. 'SELECT' => 'u.username, u.user_colour, s.gamer_id, s.points, s.game_id',
  658.  
  659. 'FROM' => array(
  660. CHAMPIONSHIPS_SCORES_TABLE => 's',
  661. USERS_TABLE => 'u'
  662. ),
  663.  
  664. 'WHERE' => 'u.user_id = s.gamer_id AND ' . $db->sql_in_set('s.id', $max_points)
  665. );
  666.  
  667. $sql = $db->sql_build_query('SELECT', $sql_array);
  668.  
  669. $result = $db->sql_query($sql);
  670.  
  671. while ($row = $db->sql_fetchrow($result)) {
  672. $scores[$row['game_id']]['username'] = $row['username'];
  673. $scores[$row['game_id']]['user_colour'] = $row['user_colour'];
  674. $scores[$row['game_id']]['gamer_id'] = $row['gamer_id'];
  675. $scores[$row['game_id']]['points'] = $row['points'];
  676. }
  677. $db->sql_freeresult($result);
  678.  
  679. return $scores;
  680. }
  681.  
  682. //Get first scores for this games and all users - done
  683. function get_first_scores_games() {
  684. global $db;
  685.  
  686. $scores = array();
  687. $max_points = array();
  688.  
  689. if ($this->games_nbr == 0) {
  690. return $scores;
  691. }
  692.  
  693. if (sizeof($this->games_sql[0])) {
  694. $sql_array = array(
  695. 'SELECT' => 's.id, MAX(s.points) AS max_points',
  696.  
  697. 'FROM' => array(
  698. SCORES_TABLE => 's'
  699. ),
  700.  
  701. 'WHERE' => 's.is_first = 1 AND ' . $db->sql_in_set('s.game_id', $this->games_sql[0]),
  702.  
  703. 'GROUP_BY' => 's.game_id'
  704. );
  705.  
  706. $sql = $db->sql_build_query('SELECT', $sql_array);
  707. $result = $db->sql_query($sql);
  708.  
  709. while ($row = $db->sql_fetchrow($result)) {
  710. $max_points[] = $row['id'];
  711. }
  712. $db->sql_freeresult($result);
  713. }
  714.  
  715. if (sizeof($this->games_sql[1])) {
  716. $sql_array = array(
  717. 'SELECT' => 's.id, MIN(s.points) AS max_points',
  718.  
  719. 'FROM' => array(
  720. SCORES_TABLE => 's'
  721. ),
  722.  
  723. 'WHERE' => 's.is_first = 1 AND ' . $db->sql_in_set('s.game_id', $this->games_sql[1]),
  724.  
  725. 'GROUP_BY' => 's.game_id'
  726. );
  727.  
  728. $sql = $db->sql_build_query('SELECT', $sql_array);
  729. $result = $db->sql_query($sql);
  730.  
  731. while ($row = $db->sql_fetchrow($result)) {
  732. $max_points[] = $row['id'];
  733. }
  734. $db->sql_freeresult($result);
  735. }
  736. if (sizeof($max_points) == 0) {
  737. return $scores;
  738. }
  739.  
  740. $sql_array = array(
  741. 'SELECT' => 'u.username, u.user_colour, s.gamer_id, s.points, s.game_id,s.score_date',
  742.  
  743. 'FROM' => array(
  744. SCORES_TABLE => 's',
  745. USERS_TABLE => 'u'
  746. ),
  747.  
  748. 'WHERE' => 'u.user_id = s.gamer_id AND ' . $db->sql_in_set('s.id', $max_points)
  749. );
  750.  
  751. $sql = $db->sql_build_query('SELECT', $sql_array);
  752.  
  753. $result = $db->sql_query($sql);
  754.  
  755. while ($row = $db->sql_fetchrow($result)) {
  756. $scores[$row['game_id']]['username'] = $row['username'];
  757. $scores[$row['game_id']]['user_colour'] = $row['user_colour'];
  758. $scores[$row['game_id']]['gamer_id'] = $row['gamer_id'];
  759. $scores[$row['game_id']]['points'] = $row['points'];
  760. $scores[$row['game_id']]['date'] = $row['score_date'];
  761. }
  762. $db->sql_freeresult($result);
  763.  
  764. return $scores;
  765. }
  766.  
  767. //Get ultim scores for this games and all users - done
  768. function get_high_scores_games() {
  769. global $db;
  770.  
  771. $scores = array();
  772. $max_points = array();
  773.  
  774. if ($this->games_nbr == 0) {
  775. return $scores;
  776. }
  777.  
  778. if (sizeof($this->games_sql[0])) {
  779. $sql_array = array(
  780. 'SELECT' => 's.id',
  781.  
  782. 'FROM' => array(
  783. ULTIM_SCORES_TABLE => 's'
  784. ),
  785.  
  786. 'WHERE' => $db->sql_in_set('s.game_id', $this->games_sql[0]),
  787. );
  788.  
  789. $sql = $db->sql_build_query('SELECT', $sql_array);
  790. $result = $db->sql_query($sql);
  791.  
  792. while ($row = $db->sql_fetchrow($result)) {
  793. $max_points[] = $row['id'];
  794. }
  795. $db->sql_freeresult($result);
  796. }
  797.  
  798. if (sizeof($this->games_sql[1])) {
  799. $sql_array = array(
  800. 'SELECT' => 's.id',
  801.  
  802. 'FROM' => array(
  803. ULTIM_SCORES_TABLE => 's'
  804. ),
  805.  
  806. 'WHERE' => $db->sql_in_set('s.game_id', $this->games_sql[1]),
  807. );
  808.  
  809. $sql = $db->sql_build_query('SELECT', $sql_array);
  810. $result = $db->sql_query($sql);
  811.  
  812. while ($row = $db->sql_fetchrow($result)) {
  813. $max_points[] = $row['id'];
  814. }
  815. $db->sql_freeresult($result);
  816. }
  817.  
  818. if (sizeof($max_points) == 0) {
  819. return $scores;
  820. }
  821.  
  822. $sql_array = array(
  823. 'SELECT' => 'u.username, u.user_colour, s.gamer_id, s.points, s.game_id,s.score_date',
  824.  
  825. 'FROM' => array(
  826. ULTIM_SCORES_TABLE => 's',
  827. USERS_TABLE => 'u'
  828. ),
  829.  
  830. 'WHERE' => 'u.user_id = s.gamer_id AND ' . $db->sql_in_set('s.id', $max_points)
  831. );
  832.  
  833. $sql = $db->sql_build_query('SELECT', $sql_array);
  834.  
  835. $result = $db->sql_query($sql);
  836.  
  837. while ($row = $db->sql_fetchrow($result)) {
  838. $scores[$row['game_id']]['username'] = $row['username'];
  839. $scores[$row['game_id']]['user_colour'] = $row['user_colour'];
  840. $scores[$row['game_id']]['gamer_id'] = $row['gamer_id'];
  841. $scores[$row['game_id']]['points'] = $row['points'];
  842. $scores[$row['game_id']]['date'] = $row['score_date'];
  843. }
  844. $db->sql_freeresult($result);
  845.  
  846. return $scores;
  847. }
  848.  
  849. //Get high scores for this games from specified user in a championship - done
  850. function get_high_scores_user_games_championship($round_id, $champ_id) {
  851. global $db;
  852.  
  853. $high_scores = array();
  854.  
  855. if ($this->games_nbr == 0 || $this->user_id == 0) {
  856. return $high_scores;
  857. }
  858.  
  859. if (sizeof($this->games_sql[0])) {
  860. $sql_array = array(
  861. 'SELECT' => 's.points, s.game_id',
  862.  
  863. 'FROM' => array(
  864. CHAMPIONSHIPS_SCORES_TABLE => 's'
  865. ),
  866.  
  867. 'WHERE' => 's.championship_id = ' . (int)$champ_id . '
  868. AND s.round_id = ' . (int)$round_id . '
  869. AND ' . $db->sql_in_set('s.game_id', $this->games_sql[0]) . ' AND s.gamer_id = ' . (int)$this->user_id
  870. );
  871.  
  872. $sql = $db->sql_build_query('SELECT', $sql_array);
  873.  
  874. $result = $db->sql_query($sql);
  875.  
  876. while ($row = $db->sql_fetchrow($result)) {
  877. $high_scores[$row['game_id']] = $row['points'];
  878. }
  879. $db->sql_freeresult($result);
  880. }
  881.  
  882. if (sizeof($this->games_sql[1])) {
  883. $sql_array = array(
  884. 'SELECT' => 's.points, s.game_id',
  885.  
  886. 'FROM' => array(
  887. CHAMPIONSHIPS_SCORES_TABLE => 's'
  888. ),
  889.  
  890. 'WHERE' => 's.championship_id = ' . (int)$champ_id . '
  891. AND s.round_id = ' . (int)$round_id . '
  892. AND ' . $db->sql_in_set('s.game_id', $this->games_sql[1]) . ' AND s.gamer_id = ' . (int)$this->user_id
  893. );
  894.  
  895. $sql = $db->sql_build_query('SELECT', $sql_array);
  896.  
  897. $result = $db->sql_query($sql);
  898.  
  899. while ($row = $db->sql_fetchrow($result)) {
  900. $high_scores[$row['game_id']] = $row['points'];
  901. }
  902. $db->sql_freeresult($result);
  903. }
  904.  
  905. return $high_scores;
  906. }
  907.  
  908. //Get high scores for this games from specified user - done
  909. function get_high_scores_user_games() {
  910. global $db;
  911.  
  912. $high_scores = array();
  913.  
  914. if ($this->games_nbr == 0 || $this->user_id == 0) {
  915. return $high_scores;
  916. }
  917.  
  918. if (sizeof($this->games_sql[0])) {
  919. $sql_array = array(
  920. 'SELECT' => 's.points, s.game_id',
  921.  
  922. 'FROM' => array(
  923. SCORES_TABLE => 's'
  924. ),
  925.  
  926. 'WHERE' => $db->sql_in_set('s.game_id', $this->games_sql[0]) . ' AND gamer_id = ' . (int)$this->user_id
  927. );
  928.  
  929. $sql = $db->sql_build_query('SELECT', $sql_array);
  930.  
  931. $result = $db->sql_query($sql);
  932.  
  933. while ($row = $db->sql_fetchrow($result)) {
  934. $high_scores[$row['game_id']] = $row['points'];
  935. }
  936. $db->sql_freeresult($result);
  937. }
  938.  
  939. if (sizeof($this->games_sql[1])) {
  940. $sql_array = array(
  941. 'SELECT' => 's.points, s.game_id',
  942.  
  943. 'FROM' => array(
  944. SCORES_TABLE => 's'
  945. ),
  946.  
  947. 'WHERE' => $db->sql_in_set('s.game_id', $this->games_sql[1]) . ' AND gamer_id = ' . (int)$this->user_id
  948. );
  949.  
  950. $sql = $db->sql_build_query('SELECT', $sql_array);
  951.  
  952. $result = $db->sql_query($sql);
  953.  
  954. while ($row = $db->sql_fetchrow($result)) {
  955. $high_scores[$row['game_id']] = $row['points'];
  956. }
  957. $db->sql_freeresult($result);
  958. }
  959.  
  960. return $high_scores;
  961. }
  962.  
  963. //Get high scores for all games and all users - done
  964.  
  965. function display_stats_board($limit = 5) {
  966. global $template, $phpbb_root_path, $phpEx, $db, $arcadeoa;
  967.  
  968. $cups = $this->get_cups($limit);
  969. $most_played = $this->get_most_played($limit);
  970.  
  971. $last_games = array();
  972.  
  973. $sql_array = array(
  974. 'SELECT' => 'g.id, g.name, g.picture, g.description',
  975.  
  976. 'FROM' => array(
  977. GAMES_TABLE => 'g'
  978. ),
  979.  
  980. 'ORDER_BY' => 'g.id DESC'
  981. );
  982. $sql = $db->sql_build_query('SELECT', $sql_array);
  983. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  984.  
  985. while ($row = $db->sql_fetchrow($result)) {
  986. $last_games[] = $row;
  987. }
  988. $db->sql_freeresult($result);
  989.  
  990. $rowsNbr = sizeof($last_games);
  991.  
  992. $conf = $arcadeoa->get_config();
  993.  
  994. if ($rowsNbr > 0) {
  995. for ($i = 0; $i < $rowsNbr; $i++) {
  996.  
  997. $template->assign_block_vars('cups_top', array(
  998. 'LAST_GAME_NAME' => stripslashes($last_games[$i]['name']),
  999. 'LAST_GAME_PICTURE' => '<img src="' . $phpbb_root_path . 'pics/' . $last_games[$i]['picture'] . '" class="games_thumb" width="' . $conf['games_thumb_width'] . '" height="' . $conf['games_thumb_height'] . '" />',
  1000. 'LAST_DESCRIPTION' => $last_games[$i]['description'],
  1001. 'U_LAST_GAME' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . $last_games[$i]['id']),
  1002. 'S_CUPS' => isset($cups[$i]) ? true : false,
  1003. 'NBR_CUPS' => isset($cups[$i]) ? (int)$cups[$i]['cups_nbr'] : 0,
  1004. 'USER_NAME' => isset($cups[$i]) ? get_username_string('full', $cups[$i]['gamer_id'], $cups[$i]['username'], $cups[$i]['user_colour'], $cups[$i]['username']) : '',
  1005. 'TOP_PLAYED_NAME' => isset($most_played[$i]) ? stripslashes($most_played[$i]['name']) : '',
  1006. 'TOP_PLAYED_URL' => isset($most_played[$i]) ? append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . $most_played[$i]['id']) : '',
  1007. 'TOP_PLAYED_TIME' => isset($most_played[$i]) ? (int)$most_played[$i]['time_played'] : 0,
  1008. 'S_TOP_PLAYED' => isset($most_played[$i]) ? true : false,
  1009. 'TOP_PLAYED_PICTURE' => isset($most_played[$i]) ? '<img src="' . $phpbb_root_path . 'pics/' . $most_played[$i]['picture'] . '" class="games_thumb" width="' . $conf['games_thumb_width'] . '" height="' . $conf['games_thumb_height'] . '" />' : '',
  1010.  
  1011. ));
  1012. }
  1013. }
  1014.  
  1015. $top_5_user = $this->get_high_scores_user($limit);
  1016. $top_5_ultim = $this->get_highest_scores_arcadeoa($limit);
  1017. $top_5 = $this->get_high_scores_arcadeoa($limit);
  1018.  
  1019. $rowsNbr = sizeof($top_5);
  1020.  
  1021. if ($rowsNbr > 0) {
  1022. $template->assign_vars(array(
  1023. 'S_STATS' => true
  1024. ));
  1025.  
  1026. for ($i = 0; $i < $rowsNbr; $i++) {
  1027.  
  1028. $template->assign_block_vars('top', array(
  1029. 'TOP_5_PICTURE' => '<img src="' . $phpbb_root_path . 'pics/' . $top_5[$i]['picture'] . '" class="games_thumb" width="' . $conf['games_thumb_width'] . '" height="' . $conf['games_thumb_height'] . '" />',
  1030.  
  1031. 'S_TOP_5_GAME' => (isset($top_5[$i]['name']) ? true : false),
  1032. 'TOP_5_GAME_NAME' => isset($top_5[$i]['name']) ? stripslashes($top_5[$i]['name']) : '',
  1033. 'TOP_5_GAME_SCORE' => isset($top_5[$i]['points']) ? $top_5[$i]['points'] : 0,
  1034. 'TOP_5_GAME_USER' => get_username_string('full', $top_5[$i]['user_id'], $top_5[$i]['username'], $top_5[$i]['user_colour'], $top_5[$i]['username']),
  1035. 'U_TOP_5' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . $top_5[$i]['game_id']),
  1036.  
  1037. 'S_TOP_5_USER' => (isset($top_5_user[$i]['name']) ? true : false),
  1038. 'TOP_5_USER_GAME_NAME' => isset($top_5_user[$i]['name']) ? stripslashes($top_5_user[$i]['name']) : '',
  1039. 'TOP_5_USER_GAME_SCORE' => isset($top_5_user[$i]['points']) ? $top_5_user[$i]['points'] : 0,
  1040. 'U_TOP_5_USER' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . (isset($top_5_user[$i]['game_id']) ? (int)$top_5_user[$i]['game_id'] : 0)),
  1041.  
  1042. 'S_TOP_5_ULTIM' => (isset($top_5_ultim[$i]['name']) ? true : false),
  1043. 'TOP_5_ULTIM_GAME_NAME' => isset($top_5_ultim[$i]['name']) ? stripslashes($top_5_ultim[$i]['name']) : '',
  1044. 'TOP_5_ULTIM_GAME_SCORE' => isset($top_5_ultim[$i]['points']) ? $top_5_ultim[$i]['points'] : 0,
  1045. 'TOP_5_ULTIM_USER' => isset($top_5_ultim[$i]) ? get_username_string('full', $top_5_ultim[$i]['user_id'], $top_5_ultim[$i]['username'], $top_5_ultim[$i]['user_colour'], $top_5_ultim[$i]['username']) : '',
  1046. 'U_TOP_5_ULTIM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . (isset($top_5_ultim[$i]['game_id']) ? (int)$top_5_ultim[$i]['game_id'] : 0)),
  1047. 'TOP_5_ULTIM_PICTURE' => (isset($top_5_ultim[$i]['picture']) ? '<img src="' . $phpbb_root_path . 'pics/' . $top_5_ultim[$i]['picture'] . '" class="games_thumb" width="' . $conf['games_thumb_width'] . '" height="' . $conf['games_thumb_height'] . '" />' : ''),
  1048.  
  1049. ));
  1050. }
  1051. }
  1052. }
  1053.  
  1054. //Get ultim scores for all games and all users - done
  1055.  
  1056. function get_cups($limit = 5) {
  1057. global $db;
  1058.  
  1059. $cups = array();
  1060.  
  1061. $sql_array = array(
  1062. 'SELECT' => 's.gamer_id, COUNT(s.game_id) AS cups_nbr, u.username, u.user_colour',
  1063.  
  1064. 'FROM' => array(
  1065. SCORES_TABLE => 's',
  1066. ),
  1067.  
  1068. 'WHERE' => 's.is_first = 1',
  1069. 'LEFT_JOIN' => array(
  1070. array(
  1071. 'FROM' => array(USERS_TABLE => 'u'),
  1072. 'ON' => 'u.user_id = s.gamer_id'
  1073. )
  1074. ),
  1075. 'GROUP_BY' => 's.gamer_id',
  1076. 'ORDER_BY' => 'cups_nbr DESC'
  1077. );
  1078. $sql = $db->sql_build_query('SELECT', $sql_array);
  1079. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  1080.  
  1081. while ($row = $db->sql_fetchrow($result)) {
  1082. $cups[] = $row;
  1083. }
  1084. $db->sql_freeresult($result);
  1085.  
  1086. return $cups;
  1087. }
  1088.  
  1089. //Get high scores for all games from specified user - done
  1090.  
  1091. function get_most_played($limit = 5) {
  1092. global $db;
  1093.  
  1094. $games = array();
  1095.  
  1096. $sql_array = array(
  1097. 'SELECT' => 'g.id, g.name, g.time_played, g.picture',
  1098.  
  1099. 'FROM' => array(
  1100. GAMES_TABLE => 'g'
  1101. ),
  1102.  
  1103. 'ORDER_BY' => 'g.time_played DESC'
  1104. );
  1105. $sql = $db->sql_build_query('SELECT', $sql_array);
  1106. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  1107.  
  1108. while ($row = $db->sql_fetchrow($result)) {
  1109. $games[] = $row;
  1110. }
  1111. $db->sql_freeresult($result);
  1112.  
  1113. return $games;
  1114.  
  1115. }
  1116.  
  1117. //Display stats panel - done
  1118.  
  1119. function get_high_scores_user($limit = 5, $ultim = 0) {
  1120. global $db;
  1121.  
  1122. $scores = array();
  1123.  
  1124. if ($this->user_id == 0) {
  1125. return $scores;
  1126. }
  1127.  
  1128. $sql_array = array(
  1129. 'SELECT' => 'g.name, g.picture, s.game_id, s.points, s.score_date',
  1130.  
  1131. 'FROM' => array(
  1132. ($ultim == 1 ? ULTIM_SCORES_TABLE : SCORES_TABLE) => 's',
  1133. ),
  1134.  
  1135. 'WHERE' => 'g.best_score_first = 0 AND u.user_id = ' . (int)$this->user_id,
  1136. 'LEFT_JOIN' => array(
  1137. array(
  1138. 'FROM' => array(GAMES_TABLE => 'g'),
  1139. 'ON' => 'g.id = s.game_id'
  1140. ),
  1141. array(
  1142. 'FROM' => array(USERS_TABLE => 'u'),
  1143. 'ON' => 'u.user_id = s.gamer_id'
  1144. )
  1145. ),
  1146. 'ORDER_BY' => 's.points DESC',
  1147. );
  1148. $sql = $db->sql_build_query('SELECT', $sql_array);
  1149.  
  1150. if ($limit == -1) {
  1151. $result = $db->sql_query($sql);
  1152. } else {
  1153. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  1154. }
  1155. while ($row = $db->sql_fetchrow($result)) {
  1156. $scores[] = $row;
  1157. }
  1158. $db->sql_freeresult($result);
  1159.  
  1160. if (sizeof($scores) < $limit) {
  1161. $sql_array = array(
  1162. 'SELECT' => 'g.name, g.picture, s.game_id, s.points, s.score_date',
  1163.  
  1164. 'FROM' => array(
  1165. ($ultim == 1 ? ULTIM_SCORES_TABLE : SCORES_TABLE) => 's',
  1166. ),
  1167.  
  1168. 'WHERE' => 'g.best_score_first = 1 AND u.user_id = ' . (int)$this->user_id,
  1169. 'LEFT_JOIN' => array(
  1170. array(
  1171. 'FROM' => array(GAMES_TABLE => 'g'),
  1172. 'ON' => 'g.id = s.game_id'
  1173. ),
  1174. array(
  1175. 'FROM' => array(USERS_TABLE => 'u'),
  1176. 'ON' => 'u.user_id = s.gamer_id'
  1177. )
  1178. ),
  1179. 'ORDER_BY' => 's.points ASC',
  1180. );
  1181. $sql = $db->sql_build_query('SELECT', $sql_array);
  1182.  
  1183. if ($limit == -1) {
  1184. $result = $db->sql_query($sql);
  1185. } else {
  1186. $result = $db->sql_query_limit($sql, (int)$limit - sizeof($scores), 0);
  1187. }
  1188. while ($row = $db->sql_fetchrow($result)) {
  1189. $scores[] = $row;
  1190. }
  1191. $db->sql_freeresult($result);
  1192. }
  1193.  
  1194. return $scores;
  1195. }
  1196.  
  1197. //Get games where user has the high score (non ultim) - done
  1198.  
  1199. function get_highest_scores_arcadeoa($limit = 5) {
  1200. global $db;
  1201.  
  1202. $scores = array();
  1203.  
  1204. $sql_array = array(
  1205. 'SELECT' => 'u.username, u.user_id, u.user_colour, g.name, g.picture, s.game_id, s.points',
  1206.  
  1207. 'FROM' => array(
  1208. ULTIM_SCORES_TABLE => 's',
  1209. ),
  1210.  
  1211. 'WHERE' => 'g.best_score_first = 0',
  1212. 'LEFT_JOIN' => array(
  1213. array(
  1214. 'FROM' => array(GAMES_TABLE => 'g'),
  1215. 'ON' => 'g.id = s.game_id'
  1216. ),
  1217. array(
  1218. 'FROM' => array(USERS_TABLE => 'u'),
  1219. 'ON' => 'u.user_id = s.gamer_id'
  1220. )
  1221. ),
  1222. 'ORDER_BY' => 's.points DESC'
  1223. );
  1224. $sql = $db->sql_build_query('SELECT', $sql_array);
  1225. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  1226.  
  1227. while ($row = $db->sql_fetchrow($result)) {
  1228. $scores[] = $row;
  1229. }
  1230. $db->sql_freeresult($result);
  1231.  
  1232. if (sizeof($scores) < $limit) {
  1233. $sql_array = array(
  1234. 'SELECT' => 'u.username, u.user_id, u.user_colour, g.name, g.picture, s.game_id, s.points',
  1235.  
  1236. 'FROM' => array(
  1237. ULTIM_SCORES_TABLE => 's'
  1238. ),
  1239.  
  1240. 'WHERE' => 'g.best_score_first = 1',
  1241. 'LEFT_JOIN' => array(
  1242. array(
  1243. 'FROM' => array(GAMES_TABLE => 'g'),
  1244. 'ON' => 'g.id = s.game_id'
  1245. ),
  1246. array(
  1247. 'FROM' => array(USERS_TABLE => 'u'),
  1248. 'ON' => 'u.user_id = s.gamer_id'
  1249. )
  1250. ),
  1251. 'ORDER_BY' => 's.points ASC'
  1252. );
  1253. $sql = $db->sql_build_query('SELECT', $sql_array);
  1254. $result = $db->sql_query_limit($sql, (int)$limit - sizeof($scores), 0);
  1255.  
  1256. while ($row = $db->sql_fetchrow($result)) {
  1257. $scores[] = $row;
  1258. }
  1259. $db->sql_freeresult($result);
  1260. }
  1261.  
  1262. return $scores;
  1263. }
  1264.  
  1265. //Get infos on the user which has the most many cups, sorted by category - done
  1266.  
  1267. function get_high_scores_arcadeoa($limit = 5) {
  1268. global $db;
  1269.  
  1270. $scores = array();
  1271.  
  1272. if ($this->user_id == 0) {
  1273. return $scores;
  1274. }
  1275.  
  1276. $sql_array = array(
  1277. 'SELECT' => 'u.username, u.user_id, u.user_colour, g.name, g.picture, s.game_id, s.points',
  1278.  
  1279. 'FROM' => array(
  1280. SCORES_TABLE => 's',
  1281. ),
  1282.  
  1283. 'WHERE' => 'g.best_score_first = 0',
  1284. 'LEFT_JOIN' => array(
  1285. array(
  1286. 'FROM' => array(GAMES_TABLE => 'g'),
  1287. 'ON' => 'g.id = s.game_id'
  1288. ),
  1289. array(
  1290. 'FROM' => array(USERS_TABLE => 'u'),
  1291. 'ON' => 'u.user_id = s.gamer_id'
  1292. )
  1293. ),
  1294. 'ORDER_BY' => 's.points DESC'
  1295. );
  1296. $sql = $db->sql_build_query('SELECT', $sql_array);
  1297. $result = $db->sql_query_limit($sql, (int)$limit, 0);
  1298.  
  1299. while ($row = $db->sql_fetchrow($result)) {
  1300. $scores[] = $row;
  1301. }
  1302. $db->sql_freeresult($result);
  1303.  
  1304. if (sizeof($scores) < $limit) {
  1305. $sql_array = array(
  1306. 'SELECT' => 'u.username, u.user_id, u.user_colour, g.name, g.picture, s.game_id, s.points',
  1307.  
  1308. 'FROM' => array(
  1309. SCORES_TABLE => 's',
  1310. ),
  1311.  
  1312. 'WHERE' => 'g.best_score_first = 1',
  1313. 'LEFT_JOIN' => array(
  1314. array(
  1315. 'FROM' => array(GAMES_TABLE => 'g'),
  1316. 'ON' => 'g.id = s.game_id'
  1317. ),
  1318. array(
  1319. 'FROM' => array(USERS_TABLE => 'u'),
  1320. 'ON' => 'u.user_id = s.gamer_id'
  1321. )
  1322. ),
  1323. 'ORDER_BY' => 's.points ASC'
  1324. );
  1325. $sql = $db->sql_build_query('SELECT', $sql_array);
  1326. $result = $db->sql_query_limit($sql, (int)$limit - sizeof($scores), 0);
  1327.  
  1328. while ($row = $db->sql_fetchrow($result)) {
  1329. $scores[] = $row;
  1330. }
  1331. $db->sql_freeresult($result);
  1332. }
  1333.  
  1334. return $scores;
  1335. }
  1336.  
  1337. //Get cups from all users - done
  1338.  
  1339. function get_cups_user($user_id = 0) {
  1340. global $db;
  1341.  
  1342. $cups = array();
  1343.  
  1344. if ($user_id == 0) {
  1345. return $cups;
  1346. }
  1347.  
  1348. $sql_array = array(
  1349. 'SELECT' => 's.game_id',
  1350.  
  1351. 'FROM' => array(
  1352. SCORES_TABLE => 's'
  1353. ),
  1354.  
  1355. 'WHERE' => 's.is_first = 1 AND s.gamer_id = ' . (int)$user_id,
  1356. );
  1357. $sql = $db->sql_build_query('SELECT', $sql_array);
  1358. $result = $db->sql_query($sql);
  1359.  
  1360. while ($row = $db->sql_fetchrow($result)) {
  1361. $cups[] = $row['game_id'];
  1362. }
  1363. $db->sql_freeresult($result);
  1364.  
  1365. return $cups;
  1366. }
  1367.  
  1368. //Get most played games - done
  1369.  
  1370. function get_cups_by_category($c_sql = array()) {
  1371. global $db;
  1372.  
  1373. $cups = array();
  1374.  
  1375. if (!is_array($c_sql) || sizeof($c_sql) == 0) {
  1376. return $cups;
  1377. }
  1378.  
  1379. $sql_array = array(
  1380. 'SELECT' => 's.cat_id, s.gamer_id, COUNT(s.game_id) AS cups_nbr, u.username, u.user_colour, u.user_avatar, u.user_avatar_height, u.user_avatar_type, u.user_avatar_width',
  1381.  
  1382. 'FROM' => array(
  1383. SCORES_TABLE => 's',
  1384. ),
  1385.  
  1386. 'WHERE' => 's.is_first = 1 AND ' . $db->sql_in_set('s.cat_id', $c_sql),
  1387. 'LEFT_JOIN' => array(
  1388. array(
  1389. 'FROM' => array(USERS_TABLE => 'u'),
  1390. 'ON' => 'u.user_id = s.gamer_id'
  1391. )
  1392. ),
  1393. 'GROUP_BY' => 's.cat_id, s.gamer_id',
  1394. );
  1395.  
  1396. $sql = $db->sql_build_query('SELECT', $sql_array);
  1397.  
  1398. $result = $db->sql_query($sql);
  1399.  
  1400. while ($row = $db->sql_fetchrow($result)) {
  1401. if (isset($cups[$row['cat_id']])) {
  1402. if ($row['cups_nbr'] > $cups[$row['cat_id']]['cups_nbr']) {
  1403. $cups[$row['cat_id']] = $row;
  1404. }
  1405. } else {
  1406. $cups[$row['cat_id']] = $row;
  1407. }
  1408. }
  1409. $db->sql_freeresult($result);
  1410.  
  1411. return $cups;
  1412. }
  1413.  
  1414. //Display user stats panel - done
  1415.  
  1416. function display_user_stats($limit = 10) {
  1417. global $db, $template, $user, $phpbb_root_path, $phpEx;
  1418.  
  1419. $template->assign_block_vars('navlinks', array(
  1420. 'FORUM_NAME' => $user->lang['ARCADEOA'],
  1421. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx")
  1422. )
  1423. );
  1424.  
  1425. $template->assign_block_vars('navlinks', array(
  1426. 'FORUM_NAME' => $user->lang['STATS'],
  1427. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", 'mode=stats&amp;id=' . $this->user_id)
  1428. )
  1429. );
  1430.  
  1431. $top_scores_user = $this->get_high_scores_user($limit, 0);
  1432. $top_ultim_user = $this->get_high_scores_user($limit, 1);
  1433.  
  1434. for ($i = 0; $i < sizeof($top_scores_user); $i++) {
  1435. $template->assign_block_vars('top_record', array(
  1436. 'GAME_NAME' => stripslashes($top_scores_user[$i]['name']),
  1437. 'GAME_IMG' => $top_scores_user[$i]['picture'],
  1438. 'GAME_POINTS' => $top_scores_user[$i]['points'],
  1439. 'GAME_DATE' => $user->format_date($top_scores_user[$i]['score_date']),
  1440. 'U_GAME' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . $top_scores_user[$i]['game_id']),
  1441. ));
  1442. }
  1443.  
  1444. for ($i = 0; $i < sizeof($top_ultim_user); $i++) {
  1445. $template->assign_block_vars('ultim_record', array(
  1446. 'GAME_NAME' => stripslashes($top_ultim_user[$i]['name']),
  1447. 'GAME_IMG' => $top_ultim_user[$i]['picture'],
  1448. 'GAME_POINTS' => $top_ultim_user[$i]['points'],
  1449. 'GAME_DATE' => $user->format_date($top_ultim_user[$i]['score_date']),
  1450. 'U_GAME' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx", "mode=game&amp;id=" . $top_ultim_user[$i]['game_id']),
  1451. ));
  1452. }
  1453.  
  1454. page_header($user->lang['STATS']);
  1455.  
  1456. $template->set_filenames(array(
  1457. 'body' => 'arcadeoa_stats_user.html'
  1458. )
  1459. );
  1460.  
  1461. page_footer();
  1462. }
  1463.  
  1464. //
  1465. function view_championship_results($champ_id = 0) {
  1466. global $db, $template, $user, $phpbb_root_path, $phpEx;
  1467.  
  1468. $sql_array = array(
  1469. 'SELECT' => 'COUNT(ut.id) AS gamers_nbr',
  1470.  
  1471. 'FROM' => array(
  1472. USERS_TEAMS_TABLE => 'ut'
  1473. ),
  1474.  
  1475. 'WHERE' => 'ut.champ_id = ' . (int)$champ_id,
  1476.  
  1477. );
  1478.  
  1479. $sql = $db->sql_build_query('SELECT', $sql_array);
  1480. $result = $db->sql_query($sql);
  1481. $row = $db->sql_fetchrow($result);
  1482. $db->sql_freeresult($result);
  1483. $gamers_nbr = (int)$row['gamers_nbr'];
  1484.  
  1485. $sql_array = array(
  1486. 'SELECT' => 's.points, s.game_id, s.round_id, s.gamer_id, u.username, u.user_colour, g.name, g.best_score_first',
  1487.  
  1488. 'FROM' => array(
  1489. CHAMPIONSHIPS_SCORES_TABLE => 's'
  1490. ),
  1491.  
  1492. 'WHERE' => 's.championship_id = ' . (int)$champ_id,
  1493.  
  1494. 'LEFT_JOIN' => array(
  1495. array(
  1496. 'FROM' => array(GAMES_TABLE => 'g'),
  1497. 'ON' => 'g.id = s.game_id'
  1498. ),
  1499. array(
  1500. 'FROM' => array(USERS_TABLE => 'u'),
  1501. 'ON' => 'u.user_id = s.gamer_id'
  1502. )
  1503. )
  1504. );
  1505.  
  1506. $sql = $db->sql_build_query('SELECT', $sql_array);
  1507. $result = $db->sql_query($sql);
  1508.  
  1509. $games = array();
  1510. $users = array();
  1511. $rounds = array();
  1512. $users_ranks = array();
  1513. $users_team = array();
  1514. $teams = array();
  1515.  
  1516. while ($row = $db->sql_fetchrow($result)) {
  1517. if (!isset($games[$row['game_id']])) {
  1518. $games[$row['game_id']] = $row['best_score_first'];
  1519. }
  1520.  
  1521. $rounds[$row['round_id']][$row['game_id']][] = array('uid' => $row['gamer_id'],
  1522. 'pts' => $row['points']
  1523. );
  1524. if (!isset($users[$row['gamer_id']])) {
  1525. $users[$row['gamer_id']] = array('username' => $row['username'],
  1526. 'user_colour' => $row['user_colour']
  1527. );
  1528. }
  1529. }
  1530. $db->sql_freeresult($result);
  1531.  
  1532. $sql_array = array(
  1533. 'SELECT' => 't.id, t.name, ut.user_id',
  1534.  
  1535. 'FROM' => array(
  1536. USERS_TEAMS_TABLE => 'ut'
  1537. ),
  1538.  
  1539. 'WHERE' => 'ut.champ_id = ' . (int)$champ_id,
  1540. 'LEFT_JOIN' => array(
  1541. array(
  1542. 'FROM' => array(TEAMS_TABLE => 't'),
  1543. 'ON' => 't.id = ut.team_id'
  1544. )
  1545. )
  1546. );
  1547.  
  1548. $sql = $db->sql_build_query('SELECT', $sql_array);
  1549. $result = $db->sql_query($sql);
  1550.  
  1551. while ($row = $db->sql_fetchrow($result)) {
  1552. if (!isset($teams[$row['id']])) {
  1553. $teams[$row['id']] = $row['name'];
  1554. }
  1555. $users_team[$row['user_id']] = $row['id'];
  1556. }
  1557. $db->sql_freeresult($result);
  1558.  
  1559. $r = 1;
  1560. foreach ($rounds as $rid => $round) {
  1561.  
  1562. $template->assign_block_vars('round', array(
  1563. 'NUMBER' => $r,
  1564. ));
  1565.  
  1566. $user_scores = array();
  1567.  
  1568. foreach ($round as $gid => $score) {
  1569. for ($j = 0; $j < sizeof($score); $j++) {
  1570. $user_scores[$score[$j]['uid']] = isset($user_scores[$score[$j]['uid']]) ? $user_scores[$score[$j]['uid']] + $score[$j]['pts'] : $score[$j]['pts'];
  1571. }
  1572. }
  1573.  
  1574. if (!(int)$games[$gid]) {
  1575. arsort($user_scores);
  1576. } else {
  1577. asort($user_scores);
  1578. }
  1579.  
  1580. $m = 0;
  1581.  
  1582. foreach ($user_scores as $uid => $total) {
  1583. $users_ranks[$uid] = isset($users_ranks[$uid]) ? ($users_ranks[$uid] + $gamers_nbr - $m) : ($gamers_nbr - $m);
  1584. $m++;
  1585. if ($m < 11) {
  1586. $template->assign_block_vars('round.user', array(
  1587. 'RANK' => $m,
  1588. 'NAME' => get_username_string('full', $uid, $users[$uid]['username'], $users[$uid]['user_colour'], $users[$uid]['username']),
  1589. 'POINTS' => $gamers_nbr - $m + 1
  1590. ));
  1591. }
  1592. }
  1593.  
  1594. $r++;
  1595. }
  1596.  
  1597. $top_teams = array();
  1598.  
  1599. foreach ($teams as $id => $name) {
  1600. $top_teams[$id] = 0;
  1601. }
  1602.  
  1603. arsort($users_ranks);
  1604. $r = 1;
  1605. //Top
  1606. foreach ($users_ranks as $user_id => $points) {
  1607. $top_teams[$users_team[$user_id]] += $points;
  1608.  
  1609. $template->assign_block_vars('user_rank', array(
  1610. 'RANK' => $r,
  1611. 'U_GAMER' => append_sid("{$phpbb_root_path}memberlist." . $phpEx, "mode=viewprofile&amp;u=" . $user_id),
  1612. 'GAMER_NAME' => get_username_string('full', $user_id, $users[$user_id]['username'], $users[$user_id]['user_colour'], $users[$user_id]['username']),
  1613. 'POINTS' => $points
  1614. ));
  1615.  
  1616. $r++;
  1617.  
  1618. if ($r == 11) {
  1619. break;
  1620. }
  1621. }
  1622.  
  1623. $r = 1;
  1624. arsort($top_teams);
  1625.  
  1626. foreach ($top_teams as $id => $points) {
  1627. $template->assign_block_vars('team', array(
  1628. 'RANK' => $r,
  1629. 'NAME' => stripslashes($teams[$id]),
  1630. 'POINTS' => $points
  1631. ));
  1632.  
  1633. $r++;
  1634.  
  1635. if ($r == 11) {
  1636. break;
  1637. }
  1638. }
  1639.  
  1640. $template->assign_vars(array(
  1641. 'U_GAME_SEARCH' => append_sid("{$phpbb_root_path}arcadeoa." . $phpEx, "mode=search")
  1642. ));
  1643.  
  1644. $template->assign_block_vars('navlinks', array(
  1645. 'FORUM_NAME' => $user->lang['ARCADEOA'],
  1646. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx")
  1647. )
  1648. );
  1649.  
  1650. $template->assign_block_vars('navlinks', array(
  1651. 'FORUM_NAME' => $user->lang['CHAMPIONSHIP'],
  1652. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx")
  1653. )
  1654. );
  1655.  
  1656. page_header();
  1657.  
  1658. $template->set_filenames(array(
  1659. 'body' => 'championship_results.html'
  1660. )
  1661. );
  1662.  
  1663. page_footer();
  1664. }
  1665.  
  1666. //Done
  1667. function view_round_results($round_id = 0) {
  1668. global $db, $template, $user, $phpbb_root_path, $phpEx, $arcadeoa;
  1669.  
  1670. $champ_id = $arcadeoa->check_round($round_id, false);
  1671.  
  1672. $sql_array = array(
  1673. 'SELECT' => 'COUNT(ut.id) AS gamers_nbr',
  1674.  
  1675. 'FROM' => array(
  1676. USERS_TEAMS_TABLE => 'ut'
  1677. ),
  1678.  
  1679. 'WHERE' => 'ut.champ_id = ' . (int)$champ_id,
  1680.  
  1681. );
  1682.  
  1683. $sql = $db->sql_build_query('SELECT', $sql_array);
  1684. $result = $db->sql_query($sql);
  1685. $row = $db->sql_fetchrow($result);
  1686. $db->sql_freeresult($result);
  1687. $gamers_nbr = (int)$row['gamers_nbr'];
  1688.  
  1689. $games = array();
  1690. $users = array();
  1691. $users_points = array();
  1692. $users_ranks = array();
  1693. $scores = array();
  1694. $scores_table = array();
  1695. $users_team = array();
  1696. $teams = array();
  1697.  
  1698. $sql_array = array(
  1699. 'SELECT' => 's.points, s.game_id, s.gamer_id, u.username, u.user_colour, g.best_score_first',
  1700.  
  1701. 'FROM' => array(
  1702. CHAMPIONSHIPS_SCORES_TABLE => 's'
  1703. ),
  1704.  
  1705. 'WHERE' => 's.round_id = ' . (int)$round_id,
  1706.  
  1707. 'LEFT_JOIN' => array(
  1708. array(
  1709. 'FROM' => array(GAMES_TABLE => 'g'),
  1710. 'ON' => 'g.id = s.game_id'
  1711. ),
  1712. array(
  1713. 'FROM' => array(USERS_TABLE => 'u'),
  1714. 'ON' => 'u.user_id = s.gamer_id'
  1715. )
  1716. )
  1717. );
  1718.  
  1719. $sql = $db->sql_build_query('SELECT', $sql_array);
  1720. $result = $db->sql_query($sql);
  1721.  
  1722. while ($row = $db->sql_fetchrow($result)) {
  1723. if (!isset($games[$row['game_id']])) {
  1724. $games[$row['game_id']] = $row['best_score_first'];
  1725. }
  1726.  
  1727. $scores_table[$row['game_id']][] = array($row['gamer_id'] => $row['points']);
  1728.  
  1729. if (!isset($users[$row['gamer_id']])) {
  1730. $users[$row['gamer_id']] = $row;
  1731. }
  1732. }
  1733. $db->sql_freeresult($result);
  1734.  
  1735. $sql_array = array(
  1736. 'SELECT' => 't.id, t.name, ut.user_id',
  1737.  
  1738. 'FROM' => array(
  1739. USERS_TEAMS_TABLE => 'ut'
  1740. ),
  1741.  
  1742. 'WHERE' => 'ut.champ_id = ' . (int)$champ_id,
  1743. 'LEFT_JOIN' => array(
  1744. array(
  1745. 'FROM' => array(TEAMS_TABLE => 't'),
  1746. 'ON' => 't.id = ut.team_id'
  1747. )
  1748. )
  1749. );
  1750.  
  1751. $sql = $db->sql_build_query('SELECT', $sql_array);
  1752. $result = $db->sql_query($sql);
  1753.  
  1754. while ($row = $db->sql_fetchrow($result)) {
  1755. if (!isset($teams[$row['id']])) {
  1756. $teams[$row['id']] = $row['name'];
  1757. }
  1758. $users_team[$row['user_id']] = $row['id'];
  1759. }
  1760. $db->sql_freeresult($result);
  1761.  
  1762. foreach ($games as $id => $bscore) {
  1763. $scores_game = array();
  1764.  
  1765. for ($t = 0; $t < sizeof($scores_table[$id]); $t++) {
  1766. foreach ($scores_table[$id][$t] as $uid => $points) {
  1767. $scores_game[$uid] = $points;
  1768. }
  1769. }
  1770.  
  1771. if (!$bscore) {
  1772. arsort($scores_game);
  1773. } else {
  1774. asort($scores_game);
  1775. }
  1776.  
  1777. $m = 0;
  1778. foreach ($scores_game as $uid => $p) {
  1779. $users_ranks[$uid] = isset($users_ranks[$uid]) ? ($users_ranks[$uid] + $gamers_nbr - $m) : ($gamers_nbr - $m);
  1780. $m++;
  1781. }
  1782. }
  1783.  
  1784. arsort($users_ranks);
  1785.  
  1786. $r = 1;
  1787.  
  1788. //Top
  1789. $top_teams = array();
  1790.  
  1791. foreach ($teams as $id => $name) {
  1792. $top_teams[$id] = 0;
  1793. }
  1794.  
  1795. foreach ($users_ranks as $user_id => $points) {
  1796. $template->assign_block_vars('user_rank', array(
  1797. 'RANK' => $r,
  1798. 'U_GAMER' => append_sid("{$phpbb_root_path}memberlist." . $phpEx, "mode=viewprofile&amp;u=" . $user_id),
  1799. 'GAMER_NAME' => get_username_string('full', $user_id, $users[$user_id]['username'], $users[$user_id]['user_colour'], $users[$user_id]['username']),
  1800. 'POINTS' => $points
  1801. ));
  1802.  
  1803. $top_teams[$users_team[$user_id]] += $points;
  1804.  
  1805. $r++;
  1806.  
  1807. if ($r == 11) {
  1808. break;
  1809. }
  1810. }
  1811.  
  1812. $r = 1;
  1813. arsort($top_teams);
  1814.  
  1815. foreach ($top_teams as $id => $points) {
  1816. $template->assign_block_vars('team', array(
  1817. 'RANK' => $r,
  1818. 'NAME' => stripslashes($teams[$id]),
  1819. 'POINTS' => $points
  1820. ));
  1821.  
  1822. $r++;
  1823.  
  1824. if ($r == 11) {
  1825. break;
  1826. }
  1827. }
  1828.  
  1829. $template->assign_vars(array(
  1830. 'U_GAME_SEARCH' => append_sid("{$phpbb_root_path}arcadeoa." . $phpEx, "mode=search")
  1831. ));
  1832.  
  1833. $template->assign_block_vars('navlinks', array(
  1834. 'FORUM_NAME' => $user->lang['ARCADEOA'],
  1835. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx")
  1836. )
  1837. );
  1838.  
  1839. $template->assign_block_vars('navlinks', array(
  1840. 'FORUM_NAME' => $user->lang['ROUND'],
  1841. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}arcadeoa.$phpEx?mode=champ&amp;id=" . $round_id)
  1842. )
  1843. );
  1844.  
  1845. page_header();
  1846.  
  1847. $template->set_filenames(array(
  1848. 'body' => 'round_results.html'
  1849. )
  1850. );
  1851.  
  1852. page_footer();
  1853.  
  1854. }
  1855. }
  1856.  
Advertisement
Add Comment
Please, Sign In to add comment