/apps/learn/zones/ZoneDefault.php

http://zoop.googlecode.com/ · PHP · 247 lines · 169 code · 38 blank · 40 comment · 13 complexity · 41261caa5d5b22e58a959b5753229a60 MD5 · raw file

  1. <?
  2. class ZoneDefault extends GuiZone
  3. {
  4. function pageDefault()
  5. {
  6. die('hello pehppy!');
  7. }
  8. function pageImportWords()
  9. {
  10. $handle = fopen("/path/to/scrabble_dictionary.txt", "r");
  11. $words = array();
  12. if($handle)
  13. {
  14. while(!feof($handle))
  15. {
  16. $buffer = trim(fgets($handle, 4096));
  17. // echo $buffer;
  18. $sql = "insert into word (word, len) values ('$buffer', length('$buffer'))";
  19. echo $sql . '<br>';
  20. SqlInsertRow($sql, array());
  21. $words[] = $buffer;
  22. }
  23. fclose($handle);
  24. }
  25. }
  26. function exportWordlist()
  27. {
  28. }
  29. function pageGenerateWordLetters()
  30. {
  31. Learn::regenerateAllWordLetters();
  32. }
  33. function pageGetSuggestions($p, $z)
  34. {
  35. $start = microtime(1);
  36. $tray = $p[1];
  37. if(isset($p[2]) && $p[2])
  38. $testList = array($p[2]);
  39. else
  40. {
  41. $testList = array('W', 'A', 'R', 'T', 'E', 'D');
  42. }
  43. foreach($testList as $test)
  44. {
  45. echo "<br><br><strong>test = $test</strong><br>";
  46. $sql = "select
  47. word.word
  48. from
  49. word
  50. inner join word_letter on word.id = word_letter.word_id
  51. where";
  52. $all = $tray . $test;
  53. $len = strlen($all);
  54. $counts = array();
  55. for($i = 0; $i < $len; $i++)
  56. {
  57. if(isset($counts[$all[$i]]))
  58. $counts[$all[$i]]++;
  59. else
  60. $counts[$all[$i]] = 2;
  61. }
  62. $parts = array();
  63. for($i = 0; $i < $len; $i++)
  64. {
  65. $parts[] = "(word_letter.letter = '" . $all[$i] . "' and word_letter.count < " . $counts[$all[$i]] . ")\n";
  66. }
  67. $sql .= implode(' or ', $parts);
  68. $len = strlen($test);
  69. $parts = array();
  70. for($i = 0; $i < $len; $i++)
  71. {
  72. $parts[] = "(sum(case when word_letter.letter = '" . $test[$i] . "' then word_letter.count else 0 end) > 0)\n";
  73. }
  74. $testList = implode(' and ', $parts);
  75. $sql .= "group by
  76. word.id, word.word, word.len
  77. having
  78. (sum(word_letter.count) = word.len)
  79. and
  80. $testList
  81. ";
  82. // echo_r($sql);
  83. $rows = SqlFetchRows($sql, array());
  84. foreach($rows as $thisRow)
  85. {
  86. echo $thisRow['word'] . '<br>';
  87. }
  88. }
  89. $end = microtime(1);
  90. echo '<br> time = ' . ($end - $start);
  91. }
  92. function pageBegin($p, $z)
  93. {
  94. $this->display('begin');
  95. }
  96. function postBegin($p, $z)
  97. {
  98. $parsed = parse_url($_POST['url']);
  99. $parts = explode('&', $parsed['query']);
  100. $vars = array();
  101. foreach($parts as $thisPart)
  102. {
  103. $subs = explode('=', $thisPart);
  104. $vars[$subs[0]] = $subs[1];
  105. }
  106. // echo_r($parsed);
  107. // echo_r($vars);
  108. $this->redirect('board/' . $vars['gid'] . '/' . $vars['pid'] . '/' . $vars['password']);
  109. }
  110. function pageBoard($p, $z)
  111. {
  112. // $gid = $p[1];
  113. // $pid = $p[2];
  114. // $password = $p[3];
  115. // $numerator = rand(1, 50);
  116. // $denominator = rand(51, 100);
  117. // $rnd = $numerator/$denominator;
  118. // $url = "this no longer works";
  119. // $gameInfo = simplexml_load_file($url);
  120. // $url = "this no longer works";
  121. // $boardInfo = simplexml_load_file($url);
  122. // echo_r($boardInfo);
  123. // $rack = (string)$gameInfo->info->myrack[0];
  124. $boards = DbObject::_find('Board', array('name' => 'default'));
  125. $board = $boards[1];
  126. $board->loadCells();
  127. // foreach($boardInfo->t as $thisTile)
  128. // {
  129. // $board->setCellLetter($thisTile->r + 1, $thisTile->c + 1, $thisTile->t);
  130. // }
  131. // $sets = $board->getSets();
  132. // echo_r($sets);
  133. //
  134. // foreach($sets as $direction => $rowcols)
  135. // {
  136. // foreach($rowcols as $thisRowcol)
  137. // {
  138. // foreach($thisRowcol as $thisWord)
  139. // {
  140. // echo "<br><br><strong>test = $thisWord</strong><br>";
  141. // $matches = $this->getMatches($rack, $thisWord);
  142. //
  143. // foreach($matches as $thisMatch)
  144. // {
  145. // echo $thisMatch . '<br>';
  146. // }
  147. // }
  148. // }
  149. // }
  150. $board->draw();
  151. $board->getWords();
  152. }
  153. public function postBoard()
  154. {
  155. $boards = DbObject::_find('Board', array('name' => 'default'));
  156. $board = $boards[1];
  157. $board->saveCells($_POST['cell']);
  158. Redirect(virtual_url);
  159. }
  160. public function pageClear()
  161. {
  162. SqlDeleteRows("delete from board_cell", array());
  163. }
  164. function getMatches($tray, $test)
  165. {
  166. $test = strtoupper($test);
  167. $sql = "select
  168. word.word
  169. from
  170. word
  171. inner join word_letter on word.id = word_letter.word_id
  172. where";
  173. $all = $tray . $test;
  174. $len = strlen($all);
  175. $counts = array();
  176. for($i = 0; $i < $len; $i++)
  177. {
  178. if(isset($counts[$all[$i]]))
  179. $counts[$all[$i]]++;
  180. else
  181. $counts[$all[$i]] = 2;
  182. }
  183. $parts = array();
  184. for($i = 0; $i < $len; $i++)
  185. {
  186. $parts[] = "(word_letter.letter = '" . $all[$i] . "' and word_letter.count < " . $counts[$all[$i]] . ")\n";
  187. }
  188. $sql .= implode(' or ', $parts);
  189. $len = strlen($test);
  190. $parts = array();
  191. for($i = 0; $i < $len; $i++)
  192. {
  193. $parts[] = "(sum(case when word_letter.letter = '" . $test[$i] . "' then word_letter.count else 0 end) > 0)\n";
  194. }
  195. $testList = implode(' and ', $parts);
  196. $sql .= "group by
  197. word.id,
  198. word.word,
  199. word.len
  200. having
  201. (sum(word_letter.count) = word.len)
  202. and
  203. $testList
  204. ";
  205. // echo_r($sql);
  206. $words = SqlFetchColumn($sql, array());
  207. return $words;
  208. }
  209. }