PageRenderTime 55ms CodeModel.GetById 26ms app.highlight 18ms RepoModel.GetById 7ms app.codeStats 0ms

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