PageRenderTime 101ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 1ms

/sudoku.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 679 lines | 524 code | 78 blank | 77 comment | 119 complexity | 0ee92799b5bd8fd18ff8038476c6f618 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * Majorflam - (majorflam@majormod.com) - (http://majormod.com)
  14. *
  15. */
  16. define('IN_SUDOKU', true);
  17. define('IN_ICYPHOENIX', true);
  18. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  19. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  20. include(IP_ROOT_PATH . 'common.' . PHP_EXT);
  21. // Start session management
  22. $user->session_begin();
  23. $auth->acl($user->data);
  24. $user->setup();
  25. // End session management
  26. // Get language Variables and page specific functions
  27. setup_extra_lang(array('lang_sudoku'));
  28. include_once(IP_ROOT_PATH . 'includes/functions_sudoku.' . PHP_EXT);
  29. // Make sure the player is registered
  30. $user_id = $user->data['user_id'];
  31. if (!$user->data['session_logged_in'])
  32. {
  33. $header_location = (@preg_match("/Microsoft|WebSTAR|Xitami/", getenv("SERVER_SOFTWARE"))) ? "Refresh: 0; URL=" : "Location: ";
  34. header($header_location . append_sid(CMS_PAGE_LOGIN . "?redirect=sudoku." . PHP_EXT, true));
  35. exit;
  36. }
  37. $sql = " SELECT game_pack FROM " . SUDOKU_STARTS . "
  38. ORDER BY game_pack DESC LIMIT 1";
  39. $result = $db->sql_query($sql);
  40. $row = $db->sql_fetchrow($result);
  41. $latest_pack = $row['game_pack'];
  42. // set standard vars
  43. $mode = request_var('mode', '');
  44. $type = request_var('type', '');
  45. $pack = request_var('p', 0);
  46. $num = request_var('n', 0);
  47. $level = request_var('l', 0);
  48. $num_insert = request_post_var('num_input', 0);
  49. $co_ord = explode('_', htmlspecialchars($_GET['tile']));
  50. $redirect = '<meta http-equiv = "refresh" content = "3;url = ' . append_sid('sudoku.' . PHP_EXT) . '">';
  51. $games = array();
  52. $points = array();
  53. $admin_tools = ($user->data['user_level'] == ADMIN) ? '|| <a href="' . append_sid('sudoku.' . PHP_EXT . '?&amp;mode=resynch') . '" class="nav">' . $lang['sudoku_resynch'] . '</a> || <a href="' . append_sid('sudoku.' . PHP_EXT . '?&amp;mode=reset_game') . '" class="nav">' . $lang['sudoku_reset_game'] . '</a>' : '';
  54. // Set template Vars
  55. $template->assign_vars(array(
  56. 'SUDOKU_VERSION' => sprintf($lang['Sudoku_Version'], $config['sudoku_version'], $latest_pack),
  57. 'L_SUBMIT' => $lang['Submit'],
  58. 'INSTRUCTIONS' => $lang['sudoku_instructions'],
  59. 'HOW_TO' => $lang['sudoku_howto'],
  60. 'STATS' => $lang['sudoku_stats'],
  61. 'STATISTICS' => $lang['sudoku_statistics'],
  62. 'PLAYER_STATS' => $lang['sudoku_player_stats'],
  63. 'LEADERBOARD' => $lang['sudoku_leaderboard'],
  64. 'USERNAME' => $lang['Username'],
  65. 'PLAYED' => $lang['sudoku_played'],
  66. 'POINTS' => $lang['sudoku_points'],
  67. 'LEAD_PLAYED' => $lang['sudoku_lead_played'],
  68. 'LEAD_POINTS' => $lang['sudoku_lead_points'],
  69. 'THESE_POINTS' => $lang['sudoku_these_points'],
  70. 'LEAD_CURRENT_GAME' => $lang['sudoku_lead_current_game'],
  71. 'PLACE' => $lang['sudoku_place'],
  72. // navigation
  73. 'RESET' => '<a href="' . append_sid('sudoku.' . PHP_EXT . '?mode=reset') . '" class="nav">' . $lang['sudoku_reset_grid'] . '</a>',
  74. 'ADMIN_TOOLS' => $admin_tools
  75. )
  76. );
  77. //
  78. // end vars
  79. //
  80. if ($mode == 'resynch')
  81. {
  82. if ($user->data['user_level'] != ADMIN)
  83. {
  84. message_die(GENERAL_MESSAGE, $lang['Not_Authorized'] . $redirect);
  85. }
  86. sudoku_resynch();
  87. message_die(GENERAL_MESSAGE, $lang['sudoku_resynch_success'] . $redirect);
  88. }
  89. if ($mode == 'reset_game')
  90. {
  91. if ($user->data['user_level'] != ADMIN)
  92. {
  93. message_die(GENERAL_MESSAGE, $lang['Not_Authorized'] . $redirect);
  94. }
  95. if (!isset($_POST['confirm']) && !isset($_POST['cancel']))
  96. {
  97. $template->assign_vars(array(
  98. 'MESSAGE_TITLE' => $lang['sudoku_reset_game'],
  99. 'S_CONFIRM_ACTION' => append_sid('sudoku.' . PHP_EXT . '?mode=reset_game'),
  100. 'MESSAGE_TEXT' => $lang['sudoku_reset_game_text'],
  101. 'L_YES' => $lang['Yes'],
  102. 'L_NO' => $lang['No'],
  103. )
  104. );
  105. full_page_generation('confirm_body.tpl', $lang['Confirm'], '', '');
  106. }
  107. if (isset($_POST['cancel']))
  108. {
  109. $message = $lang['sudoku_reset_game_cancelled'] . $redirect;
  110. message_die(GENERAL_MESSAGE, $message);
  111. }
  112. // OK, reset the game
  113. // first truncate sudoku tables
  114. $sql = " DELETE FROM " . SUDOKU_SESSIONS;
  115. $db->sql_query($sql);
  116. $sql = " DELETE FROM " . SUDOKU_USERS;
  117. $db->sql_query($sql);
  118. $sql = " DELETE FROM " . SUDOKU_STATS;
  119. $db->sql_query($sql);
  120. $sql = " UPDATE " . USERS_TABLE . "
  121. SET user_sudoku_playing=0
  122. WHERE user_sudoku_playing>0
  123. ";
  124. $db->sql_query($sql);
  125. // let them know the good news
  126. $redirect='<meta http-equiv="refresh" content="3;url=' . append_sid('sudoku.' . PHP_EXT . '?#grid') . '">';
  127. message_die(GENERAL_MESSAGE,$lang['sudoku_rest_game_success'] . $redirect);
  128. }
  129. if ($mode == 'buy')
  130. {
  131. if (!isset($_POST['confirm']) && !isset($_POST['cancel']))
  132. {
  133. $template->assign_vars(array(
  134. 'MESSAGE_TITLE' => $lang['sudoku_buy_number'],
  135. 'S_CONFIRM_ACTION' => append_sid('sudoku.' . PHP_EXT . '?mode=buy&amp;p=' . $pack . '&amp;n=' . $num),
  136. 'MESSAGE_TEXT' => $lang['sudoku_confirm_buy_text'],
  137. 'L_YES' => $lang['Yes'],
  138. 'L_NO' => $lang['No'],
  139. )
  140. );
  141. full_page_generation('confirm_body.tpl', $lang['Confirm'], '', '');
  142. }
  143. if (isset($_POST['cancel']))
  144. {
  145. $message = $lang['sudoku_buy_cancelled'] . $redirect;
  146. message_die(GENERAL_MESSAGE, $message);
  147. }
  148. // buy the number
  149. $and_clause = '';
  150. sudoku_starting_data($pack, $num, SUDOKU_SOLUTIONS, $and_clause);
  151. $solutions_ary = $line;
  152. unset($line);
  153. unset($lrow);
  154. $and_clause = 'AND user_id = ' . $user->data['user_id'];
  155. sudoku_starting_data($pack, $num, SUDOKU_USERS, $and_clause);
  156. $users_ary = $line;
  157. unset($line);
  158. // find the unknowns
  159. $unknown_ary = array();
  160. for ($i = 0; $i < 9; $i++)
  161. {
  162. for ($x = 0; $x < 9; $x++)
  163. {
  164. if ($users_ary[$i][$x] == 'x')
  165. {
  166. $unknown_ary[] = $i . '_' . $x;
  167. }
  168. }
  169. }
  170. // create a 3 dimensional array
  171. $unknowns = array();
  172. for ($i = 0; $i < sizeof($unknown_ary); $i++)
  173. {
  174. $unknowns[] = explode('_', $unknown_ary[$i]);
  175. }
  176. // grab the random number to insert
  177. $random_insertion = rand(0, (sizeof($unknowns)-1));
  178. // grab the solution for that co-ord
  179. $x_co = $unknowns[$random_insertion][0];
  180. $y_co = $unknowns[$random_insertion][1];
  181. $ran_sol = $solutions_ary[$x_co][$y_co];
  182. // insert the new number
  183. $key = 'line_' . ($x_co+1);
  184. $user_line_upd = explode('a',$lrow[$key]);
  185. $val = $user_line_upd[$y_co];
  186. if ($val != 'x' || sizeof($user_line_upd) != 9)
  187. {
  188. message_die(GENERAL_MESSAGE, $lang['sudoku_ran_error']);
  189. }
  190. $user_line_upd[$y_co] = $ran_sol + 20;
  191. $new_line = implode('a', $user_line_upd);
  192. $sql = " UPDATE " . SUDOKU_USERS . "
  193. SET $key = '$new_line', points = points - 30
  194. WHERE game_pack = $pack
  195. AND game_num = $num
  196. AND user_id = " . $user->data['user_id'];
  197. $db->sql_query($sql);
  198. $redirect='<meta http-equiv="refresh" content="3;url=' . append_sid('sudoku.' . PHP_EXT . '?#grid') . '">';
  199. message_die(GENERAL_MESSAGE,$lang['sudoku_ran_success'] . $redirect);
  200. }
  201. if (isset($_POST['input_num']))
  202. {
  203. // insert the number to the users data
  204. if (!$num_insert)
  205. {
  206. $message = $lang['sudoku_no_number'] . $redirect;
  207. message_die(GENERAL_MESSAGE, $message);
  208. }
  209. if ($num_insert > 19)
  210. {
  211. $message = $lang['sudoku_invalid_number'] . $redirect;
  212. message_die(GENERAL_MESSAGE, $message);
  213. }
  214. $this_line = 'line_' . $co_ord[0];
  215. $sql = " SELECT $this_line FROM " . SUDOKU_USERS . "
  216. WHERE game_pack = $pack
  217. AND game_num = $num
  218. AND user_id = " . $user->data['user_id'];
  219. $result = $db->sql_query($sql);
  220. $row = $db->sql_fetchrow($result);
  221. $insert_line = explode('a', $row[$this_line]);
  222. $pos = ($co_ord['1'] - 1);
  223. // add for blank number
  224. if (($type == 'insert') && ($num_insert == -1))
  225. {
  226. $message = $lang['sudoku_no_blank_starter'] . $redirect;
  227. message_die(GENERAL_MESSAGE, $message);
  228. }
  229. // end blank number
  230. // test for url tricks
  231. if (($type == 'insert') && ($insert_line[$pos] == 'x'))
  232. {
  233. $insert_line[$pos] = $num_insert;
  234. }
  235. elseif (($type == 'edit') && ($insert_line[$pos] > 9))
  236. {
  237. $insert_line[$pos] = ($num_insert > 0) ? $num_insert : 'x';
  238. }
  239. elseif ($insert_line[$pos] < 10)
  240. {
  241. $message = $lang['sudoku_no_change_starter'] . $redirect;
  242. message_die(GENERAL_MESSAGE, $message);
  243. }
  244. else
  245. {
  246. $message = $lang['sudoku_no_url_tricks'] . $redirect;
  247. message_die(GENERAL_MESSAGE, $message);
  248. }
  249. // end url tricks test
  250. $inserted_line = implode('a',$insert_line);
  251. // changed for blank number
  252. //$points_addition= ($type == 'insert') ? 10 : -15;
  253. if ($num_insert > -1)
  254. {
  255. $points_addition = ($type == 'insert') ? 10 : -15;
  256. }
  257. else
  258. {
  259. $points_addition = -25;
  260. }
  261. // end blank number
  262. $sql = " UPDATE " . SUDOKU_USERS . "
  263. SET $this_line = '" . $inserted_line . "',points=points+$points_addition
  264. WHERE game_pack = $pack
  265. AND game_num = $num
  266. AND user_id = " . $user->data['user_id'];
  267. $db->sql_query($sql);
  268. }
  269. // check to see if this user has played the game, if not we'll set them up for it
  270. $sql = " SELECT user_id, game_pack, game_num, game_level, points FROM " . SUDOKU_USERS . "
  271. WHERE user_id=" . $user->data['user_id'] . "
  272. ORDER BY game_pack DESC, game_num DESC
  273. LIMIT 1
  274. ";
  275. $result = $db->sql_query($sql);
  276. $row = $db->sql_fetchrow($result);
  277. if (!$row['user_id'])
  278. {
  279. //
  280. // insert user to database for first run
  281. //
  282. sudoku_starting_data(1,1, SUDOKU_STARTS,'');
  283. $sql = " INSERT INTO " . SUDOKU_USERS . "
  284. (user_id,game_pack,game_num,game_level,line_1,line_2,line_3,line_4,line_5,line_6,line_7,line_8,line_9)
  285. VALUES
  286. (" . $user->data['user_id'] . ",1,1,1,'" . $lrow['line_1'] . "','" . $lrow['line_2'] . "','" . $lrow['line_3'] . "','" . $lrow['line_4'] . "','" . $lrow['line_5'] . "','" . $lrow['line_6'] . "','" . $lrow['line_7'] . "','" . $lrow['line_8'] . "','" . $lrow['line_9'] . "')
  287. ";
  288. $db->sql_query($sql);
  289. $row['game_pack'] = 1;
  290. $row['game_num'] = 1;
  291. $row['game_level'] = 1;
  292. $sql = " UPDATE " . USERS_TABLE . "
  293. SET user_sudoku_playing = 1
  294. WHERE user_id=" . $user->data['user_id'];
  295. $db->sql_query($sql);
  296. }
  297. //
  298. // set sudokudata for this user
  299. //
  300. $pack = $row['game_pack'];
  301. $num = $row['game_num'];
  302. $level = $row['game_level'];
  303. $curr_points = $row['points'];
  304. $and_clause='AND user_id=' . $user->data['user_id'];
  305. switch ($level)
  306. {
  307. case EASY:
  308. $v_level = $lang['sudoku_level_easy'];
  309. break;
  310. case MEDIUM:
  311. $v_level = $lang['sudoku_level_medium'];
  312. break;
  313. case HARD:
  314. $v_level = $lang['sudoku_level_hard'];
  315. break;
  316. case VERY_HARD:
  317. $v_level = $lang['sudoku_level_very_hard'];
  318. break;
  319. default:
  320. $v_level = $lang['sudoku_level_easy'];
  321. }
  322. //
  323. // are we resetting?
  324. //
  325. if ($mode == 'reset')
  326. {
  327. if (!isset($_POST['confirm']) && !isset($_POST['cancel']))
  328. {
  329. $template->assign_vars(array(
  330. 'MESSAGE_TITLE' => $lang['sudoku_confirm_reset'],
  331. 'S_CONFIRM_ACTION' => append_sid('sudoku.' . PHP_EXT . '?mode=reset'),
  332. 'MESSAGE_TEXT' => $lang['sudoku_confirm_reset_text'],
  333. 'L_YES' => $lang['Yes'],
  334. 'L_NO' => $lang['No'],
  335. ));
  336. full_page_generation('confirm_body.tpl', $lang['Confirm'], '', '');
  337. }
  338. if (isset($_POST['cancel']))
  339. {
  340. $message = $lang['sudoku_reset_cancelled'] . $redirect;
  341. message_die(GENERAL_MESSAGE, $message);
  342. }
  343. sudoku_starting_data($pack,$num, SUDOKU_STARTS,'');
  344. $sql = " UPDATE " . SUDOKU_USERS . "
  345. SET points=-200,line_1='" . $lrow['line_1'] . "',line_2='" . $lrow['line_2'] . "',line_3='" . $lrow['line_3'] . "',line_4='" . $lrow['line_4'] . "',line_5='" . $lrow['line_5'] . "',line_6='" . $lrow['line_6'] . "',line_7='" . $lrow['line_7'] . "',line_8='" . $lrow['line_8'] . "',line_9='" . $lrow['line_9'] . "'
  346. WHERE user_id=" . $user->data['user_id'] . "
  347. AND game_pack = $pack
  348. AND game_num = $num
  349. ";
  350. $db->sql_query($sql);
  351. $mode = 0;
  352. $message = $lang['sudoku_reset_confirmed'] . $redirect;
  353. message_die(GENERAL_MESSAGE, $message);
  354. }
  355. // perform tasks
  356. sudoku_tasks($user->data['user_id'], $pack, $num, $level);
  357. sudoku_starting_data($pack, $num, SUDOKU_USERS, $and_clause);
  358. // check for completed grid
  359. if (!in_array('x', $line[0]) && !in_array('x', $line[1]) && !in_array('x', $line[2]) && !in_array('x', $line[3]) && !in_array('x', $line[4]) &&
  360. !in_array('x', $line[5]) && !in_array('x', $line[6]) && !in_array('x', $line[7]) && !in_array('x', $line[8]))
  361. {
  362. // OK, so grid is complete, but is it right according to default grid solution?
  363. $u_line = array();
  364. // let's lower the user and random numbers to do the check
  365. for ($i = 0; $i < 9; $i++)
  366. {
  367. for ($x = 0; $x < 9; $x++)
  368. {
  369. // first lower the random numbers
  370. if ($line[$i][$x] > 19)
  371. {
  372. $line[$i][$x] = $line[$i][$x]-20;
  373. }
  374. // then the user numbers
  375. if ($line[$i][$x] > 9)
  376. {
  377. $line[$i][$x] = $line[$i][$x]-10;
  378. }
  379. }
  380. }
  381. $u_line[0] = $line[0];
  382. $u_line[1] = $line[1];
  383. $u_line[2] = $line[2];
  384. $u_line[3] = $line[3];
  385. $u_line[4] = $line[4];
  386. $u_line[5] = $line[5];
  387. $u_line[6] = $line[6];
  388. $u_line[7] = $line[7];
  389. $u_line[8] = $line[8];
  390. $test_line = $line;
  391. unset($line);
  392. sudoku_starting_data($pack, $num, SUDOKU_SOLUTIONS, '');
  393. // OK, so lets compare!
  394. if ($u_line[0] == $line[0] && $u_line[1] == $line[1] && $u_line[2] == $line[2] && $u_line[3] == $line[3] && $u_line[4] == $line[4] &&
  395. $u_line[5] == $line[5] && $u_line[6] == $line[6] && $u_line[7] == $line[7] && $u_line[8] == $line[8])
  396. {
  397. // success!
  398. sudoku_grid_success($pack, $num, $curr_points, $redirect);
  399. }
  400. else
  401. {
  402. $wrong_required=false;
  403. $bad=array();
  404. // OK, so we need to check for an alternative solution that the user may have entered
  405. // if successful, then $wrong_required=false
  406. // do the basic check on the 9 grids
  407. $bad[] = (count(array_unique($test_line[0])) == 9) ? 0 : 1;
  408. $bad[] = (count(array_unique($test_line[1])) == 9) ? 0 : 1;
  409. $bad[] = (count(array_unique($test_line[2])) == 9) ? 0 : 1;
  410. $bad[] = (count(array_unique($test_line[3])) == 9) ? 0 : 1;
  411. $bad[] = (count(array_unique($test_line[4])) == 9) ? 0 : 1;
  412. $bad[] = (count(array_unique($test_line[5])) == 9) ? 0 : 1;
  413. $bad[] = (count(array_unique($test_line[6])) == 9) ? 0 : 1;
  414. $bad[] = (count(array_unique($test_line[7])) == 9) ? 0 : 1;
  415. $bad[] = (count(array_unique($test_line[8])) == 9) ? 0 : 1;
  416. // now we must create horizontal arrays
  417. $hor_test = array();
  418. $hor_test[0] = array($test_line[0][0],$test_line[0][1],$test_line[0][2],$test_line[1][0],$test_line[1][1],$test_line[1][2],$test_line[2][0],$test_line[2][1],$test_line[2][2]);
  419. $hor_test[1] = array($test_line[0][3],$test_line[0][4],$test_line[0][5],$test_line[1][3],$test_line[1][4],$test_line[1][5],$test_line[2][3],$test_line[2][4],$test_line[2][5]);
  420. $hor_test[2] = array($test_line[0][6],$test_line[0][7],$test_line[0][8],$test_line[1][6],$test_line[1][7],$test_line[1][8],$test_line[2][6],$test_line[2][7],$test_line[2][8]);
  421. $hor_test[3] = array($test_line[3][0],$test_line[3][1],$test_line[3][2],$test_line[4][0],$test_line[4][1],$test_line[4][2],$test_line[5][0],$test_line[5][1],$test_line[5][2]);
  422. $hor_test[4] = array($test_line[3][3],$test_line[3][4],$test_line[3][5],$test_line[4][3],$test_line[4][4],$test_line[4][5],$test_line[5][3],$test_line[5][4],$test_line[5][5]);
  423. $hor_test[5] = array($test_line[3][6],$test_line[3][7],$test_line[3][8],$test_line[4][6],$test_line[4][7],$test_line[4][8],$test_line[5][6],$test_line[5][7],$test_line[5][8]);
  424. $hor_test[6] = array($test_line[6][0],$test_line[6][1],$test_line[6][2],$test_line[7][0],$test_line[7][1],$test_line[7][2],$test_line[8][0],$test_line[8][1],$test_line[8][2]);
  425. $hor_test[7] = array($test_line[6][3],$test_line[6][4],$test_line[6][5],$test_line[7][3],$test_line[7][4],$test_line[7][5],$test_line[8][3],$test_line[8][4],$test_line[8][5]);
  426. $hor_test[8] = array($test_line[6][6],$test_line[6][7],$test_line[6][8],$test_line[7][6],$test_line[7][7],$test_line[7][8],$test_line[8][6],$test_line[8][7],$test_line[8][8]);
  427. $bad[] = (count(array_unique($hor_test[0])) == 9) ? 0 : 1;
  428. $bad[] = (count(array_unique($hor_test[1])) == 9) ? 0 : 1;
  429. $bad[] = (count(array_unique($hor_test[2])) == 9) ? 0 : 1;
  430. $bad[] = (count(array_unique($hor_test[3])) == 9) ? 0 : 1;
  431. $bad[] = (count(array_unique($hor_test[4])) == 9) ? 0 : 1;
  432. $bad[] = (count(array_unique($hor_test[5])) == 9) ? 0 : 1;
  433. $bad[] = (count(array_unique($hor_test[6])) == 9) ? 0 : 1;
  434. $bad[] = (count(array_unique($hor_test[7])) == 9) ? 0 : 1;
  435. $bad[] = (count(array_unique($hor_test[8])) == 9) ? 0 : 1;
  436. // finally, vertical arrays
  437. $ver_test = array();
  438. $ver_test[0] = array($test_line[0][0],$test_line[0][3],$test_line[0][6],$test_line[3][0],$test_line[3][3],$test_line[3][6],$test_line[6][0],$test_line[6][3],$test_line[6][6]);
  439. $ver_test[1] = array($test_line[0][1],$test_line[0][4],$test_line[0][7],$test_line[3][1],$test_line[3][4],$test_line[3][7],$test_line[6][1],$test_line[6][4],$test_line[6][7]);
  440. $ver_test[2] = array($test_line[0][2],$test_line[0][5],$test_line[0][8],$test_line[3][2],$test_line[3][5],$test_line[3][8],$test_line[6][2],$test_line[6][5],$test_line[6][8]);
  441. $ver_test[3] = array($test_line[1][0],$test_line[1][3],$test_line[1][6],$test_line[4][0],$test_line[4][3],$test_line[4][6],$test_line[7][0],$test_line[7][3],$test_line[7][6]);
  442. $ver_test[4] = array($test_line[1][1],$test_line[1][4],$test_line[1][7],$test_line[4][1],$test_line[4][4],$test_line[4][7],$test_line[7][1],$test_line[7][4],$test_line[7][7]);
  443. $ver_test[5] = array($test_line[1][2],$test_line[1][5],$test_line[1][8],$test_line[4][2],$test_line[4][5],$test_line[4][8],$test_line[7][2],$test_line[7][5],$test_line[7][8]);
  444. $ver_test[6] = array($test_line[2][0],$test_line[2][3],$test_line[2][6],$test_line[5][0],$test_line[5][3],$test_line[5][6],$test_line[8][0],$test_line[8][3],$test_line[8][6]);
  445. $ver_test[7] = array($test_line[2][1],$test_line[2][4],$test_line[2][7],$test_line[5][1],$test_line[5][4],$test_line[5][7],$test_line[8][1],$test_line[8][4],$test_line[8][7]);
  446. $ver_test[8] = array($test_line[2][2],$test_line[2][5],$test_line[2][8],$test_line[5][2],$test_line[5][5],$test_line[5][8],$test_line[8][2],$test_line[8][5],$test_line[8][8]);
  447. $bad[] = (count(array_unique($ver_test[0])) == 9) ? 0 : 1;
  448. $bad[] = (count(array_unique($ver_test[1])) == 9) ? 0 : 1;
  449. $bad[] = (count(array_unique($ver_test[2])) == 9) ? 0 : 1;
  450. $bad[] = (count(array_unique($ver_test[3])) == 9) ? 0 : 1;
  451. $bad[] = (count(array_unique($ver_test[4])) == 9) ? 0 : 1;
  452. $bad[] = (count(array_unique($ver_test[5])) == 9) ? 0 : 1;
  453. $bad[] = (count(array_unique($ver_test[6])) == 9) ? 0 : 1;
  454. $bad[] = (count(array_unique($ver_test[7])) == 9) ? 0 : 1;
  455. $bad[] = (count(array_unique($ver_test[8])) == 9) ? 0 : 1;
  456. if (in_array(1, $bad))
  457. {
  458. $wrong_required=true;
  459. }
  460. else
  461. {
  462. // success!
  463. sudoku_grid_success($pack, $num, $curr_points, $redirect);
  464. }
  465. }
  466. if ($wrong_required == true)
  467. {
  468. // OK, so we need to suss the wrong numbers according to our default solution
  469. $u_line_insert=array();
  470. for ($i=0; $i<9; $i++)
  471. {
  472. for ($x=0; $x<9; $x++)
  473. {
  474. if ($line[$i][$x] != $u_line[$i][$x])
  475. {
  476. $u_line[$i][$x]='x';
  477. $points_minus = $points_minus+20;
  478. $bad_numbers++;
  479. }
  480. }
  481. $u_line_insert[]=implode('a', $u_line[$i]);
  482. }
  483. // now we update the users grid
  484. $sql = " UPDATE " . SUDOKU_USERS . "
  485. SET line_1 = '" . $u_line_insert[0] . "',line_2 = '" . $u_line_insert[1] . "',line_3 = '" . $u_line_insert[2] . "',line_4 = '" . $u_line_insert[3] . "',line_5 = '" . $u_line_insert[4] . "',
  486. line_6 = '" . $u_line_insert[5] . "',line_7 = '" . $u_line_insert[6] . "',line_8 = '" . $u_line_insert[7] . "',line_9 = '" . $u_line_insert[8] . "',points = points-$points_minus
  487. WHERE user_id = " . $user->data['user_id'] . "
  488. AND game_pack = $pack
  489. AND game_num = $num
  490. ";
  491. $db->sql_query($sql);
  492. $new_redirect='<meta http-equiv="refresh" content="6;url=' . append_sid('sudoku.' . PHP_EXT) . '">';
  493. $message=sprintf($lang['sudoku_wrong_numbers'], $bad_numbers, $points_minus) . $new_redirect;
  494. message_die(GENERAL_MESSAGE, $message);
  495. }
  496. }
  497. // end completion check
  498. // build lines for template
  499. sudoku_grid_build();
  500. // end lines for template
  501. // get statistics data
  502. // grab the top ten
  503. $sql = " SELECT * FROM " . SUDOKU_STATS . " ORDER BY points DESC LIMIT 10";
  504. $result = $db->sql_query($sql);
  505. $x = 1;
  506. while ($row = $db->sql_fetchrow($result))
  507. {
  508. $stat_points = $row['points'];
  509. $stat_played = $row['played'];
  510. $stat_userid = $row['user_id'];
  511. $sql_a="SELECT username
  512. FROM " . USERS_TABLE . "
  513. WHERE user_id = " . $row['user_id'];
  514. $result_a = $db->sql_query($sql_a);
  515. $row_a = $db->sql_fetchrow($result_a);
  516. $stat_username = $row_a['username'];
  517. $stat_user_id = $row_a['user_id'];
  518. // grab current game
  519. $sql_a = "SELECT game_pack, game_num FROM " . SUDOKU_USERS . "
  520. WHERE user_id = " . $row['user_id'] . "
  521. ORDER BY game_pack DESC, game_num DESC
  522. LIMIT 1";
  523. $result_a = $db->sql_query($sql_a);
  524. $row_a = $db->sql_fetchrow($result_a);
  525. $stat_gamepack = $row_a['game_pack'];
  526. $stat_gamenum = $row_a['game_num'];
  527. // send to template
  528. $row_class = (!($x % 2)) ? $theme['td_class1'] : $theme['td_class2'];
  529. $template->assign_block_vars('leaderboard', array(
  530. 'ROW_CLASS' => $row_class,
  531. 'USERNAME' => colorize_username($row['user_id']),
  532. 'POINTS' => $stat_points,
  533. 'PLAYED' => $stat_played,
  534. 'POS' => $x,
  535. 'CURRENT_GAME' => sprintf($lang['sudoku_current_game_text'], $stat_gamepack, $stat_gamenum),
  536. )
  537. );
  538. $x++;
  539. }
  540. // grab online info
  541. $s_users_online_today = sizeof($s_users_today);
  542. $s_users_active_now = sizeof($s_users_active);
  543. // apply usernames to users
  544. $s_users_today_names = array();
  545. $name = array();
  546. $user_active = array();
  547. $user_color = array();
  548. $s_users_active_names = array();
  549. for ($i = 0; $i < sizeof($s_users_today); $i++)
  550. {
  551. $sql = "SELECT username, user_active, user_color FROM " . USERS_TABLE . "
  552. WHERE user_id = " . $s_users_today[$i] . "
  553. ORDER BY username ASC";
  554. $result = $db->sql_query($sql);
  555. $row = $db->sql_fetchrow($result);
  556. $disp_userid = $s_users_today[$i];
  557. $name[$disp_userid] = $row['username'];
  558. $user_active[$disp_userid] = $row['user_active'];
  559. $user_color[$disp_userid] = $row['user_color'];
  560. }
  561. asort($name);
  562. $name_keys = array_keys($name);
  563. for ($i = 0; $i < sizeof($name); $i++)
  564. {
  565. $disp_userid = $name_keys[$i];
  566. $s_users_today_names[] = colorize_username($disp_userid, $name[$disp_userid], $user_color[$disp_userid], $user_active[$disp_userid]);
  567. }
  568. $active_name=array();
  569. for ($i = 0; $i < sizeof($s_users_active); $i++)
  570. {
  571. $disp_userid = $s_users_active[$i];
  572. $active_name[$disp_userid] = $name[$disp_userid];
  573. }
  574. asort($active_name);
  575. $active_name_keys = array_keys($active_name);
  576. for ($i = 0; $i < sizeof($active_name); $i++)
  577. {
  578. $disp_userid = $active_name_keys[$i];
  579. $s_users_active_names[] = colorize_username($disp_userid, $name[$disp_userid], $user_color[$disp_userid], $user_active[$disp_userid]);
  580. }
  581. $s_users_today_disp=implode(', ', $s_users_today_names);
  582. $s_users_active_disp=implode(', ', $s_users_active_names);
  583. $this_userid = $user->data['user_id'];
  584. // parse to template
  585. $template->assign_vars(array(
  586. 'POINTS_VALUE' => number_format($points[$this_userid]),
  587. 'PLAYED_VALUE' => number_format($games[$this_userid]),
  588. 'THESE_POINTS_VALUE' => number_format($curr_points),
  589. 'GAME_INFO' => sprintf($lang['sudoku_game_info'], $pack,$num,$v_level),
  590. 'WHO_IS_ONLINE' => $lang['sudoku_who_is_online'],
  591. 'ONLINE_EXPLAIN' => $lang['sudoku_online_explain'],
  592. 'SUDOKU_GAME_STATS' => sprintf($lang['sudoku_game_stats'], number_format($alltime_players), number_format($alltime_played)),
  593. 'BUY_NUMBER' => '<a href="' . append_sid('sudoku.' . PHP_EXT . '?&amp;mode=buy&amp;p=' . $pack . '&amp;n=' . $num) . '" class="nav">' . $lang['sudoku_buy_number'] . '</a>',
  594. 'L_TOTAL_USERS_ONLINE' => sprintf($lang['sudoku_total_online'], number_format($s_users_online_today)),
  595. 'L_LOGGED_IN_USER_LIST' => $lang['sudoku_logged_in_list'],
  596. 'L_TODAY_USER_LIST' => $s_users_today_disp,
  597. 'L_ACTIVE_USER_LIST' => $s_users_active_disp,
  598. )
  599. );
  600. full_page_generation('sudoku.tpl', $lang['Sudoku'], '', '');
  601. ?>