PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/applicationlib.php

https://bitbucket.org/mikel3377/webgame
PHP | 516 lines | 433 code | 75 blank | 8 comment | 24 complexity | 6835f05efa49950590368ef70cc25972 MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. include('mysqllib.php');
  7. class Application
  8. {
  9. public static $GameDataService = NULL;
  10. public static $GameData = NULL;
  11. public static function GetCurrentUser()
  12. {
  13. if( isset( $_SESSION['id'] ))
  14. {
  15. return self::$GameDataService->GetUserByID( (int)$_SESSION['id'] );
  16. }
  17. if( PageHelper::$RequireLogin )
  18. PageHelper::Redirect("login.php");
  19. return null;
  20. }
  21. public static function GetUserByName( $name )
  22. {
  23. return self::$GameDataService->GetUserByName( $name );
  24. }
  25. public static function CreateUser( $name, $pass )
  26. {
  27. MySql::CreateUser( $name, $pass );
  28. }
  29. public static function CheckPassword( $name, $pass )
  30. {
  31. $name = mysql_real_escape_string( $name );
  32. $query="SELECT pass, id FROM userpass WHERE user='$name'";
  33. $result = mysql_query($query) or die(mysql_error());
  34. $realpass = mysql_fetch_array($result);
  35. // TODO : uncommment password
  36. if (true){//$realpass[0] == pass_hash($pass)){
  37. $_SESSION['id'] = $realpass['id'];
  38. return TRUE;
  39. }
  40. else{
  41. return FALSE;
  42. }
  43. }
  44. }
  45. Application::$GameDataService = GameDataService::GetInstance();
  46. if( isset( $_GET['id']))
  47. {
  48. $game_id = mysql_real_escape_string($_GET['id']);
  49. Application::$GameData = Application::$GameDataService->GetGameByID( $game_id );
  50. }
  51. include('widgets/widgetlib.php');
  52. session_start();
  53. $rootDir = '/PhpProject1/';
  54. $uri = $_SERVER['REQUEST_URI'];
  55. $uri = substr( $uri, strlen($rootDir));
  56. $slashes = count( explode('/',$uri)) - 1;
  57. $prefix = "";
  58. for( $i = 0; $i < $slashes; $i++)
  59. {
  60. $prefix = $prefix."../";
  61. }
  62. class PageHelper
  63. {
  64. public static $RequireLogin = true;
  65. private static $console = true;
  66. private static $javascripts = array(
  67. "scripts/include/jquery-1.7.2.js",
  68. "scripts/include/jquery-ui-1.8.20.js",
  69. "scripts/include/angular.js",
  70. "scripts/lib.js");
  71. private static $stylesheets = array(
  72. "styles/jqueryui/jquery-ui.css",
  73. "styles/global.css",
  74. );
  75. private static $links = array(
  76. 'Home' => "index.php",
  77. 'Generate Data' => 'gamedatagen.php',
  78. 'Actions' => 'actiontest.php',
  79. 'Challenge' => 'creategame.php');
  80. public static $PageTitle = "No page title set";
  81. public static $GETGameData = FALSE;
  82. // Data functions
  83. // Utility functions
  84. public static function AddScript( $script )
  85. {
  86. if( !in_array( $script, PageHelper::$javascripts ))
  87. PageHelper::$javascripts[] = $script;
  88. }
  89. public static function AddScripts( $scripts )
  90. {
  91. foreach( $scripts as $script )
  92. {
  93. PageHelper::AddScript( $script );
  94. }
  95. }
  96. public static function AddStyle( $style )
  97. {
  98. if( !in_array( $style, PageHelper::$stylesheets ))
  99. PageHelper::$stylesheets[] = $style;
  100. }
  101. public static function AddStyles( $styles )
  102. {
  103. foreach( $styles as $style )
  104. {
  105. PageHelper::AddScript( $style );
  106. }
  107. }
  108. public static function DisableConsole()
  109. {
  110. PageHelper::$console = false;
  111. }
  112. // Page rendering
  113. public static function Redirect( $url )
  114. {
  115. ?>
  116. <!DOCTYPE html>
  117. <html><head><script type="text/javascript"> window.location = "<?=$url?>";</script></head></html>
  118. <?
  119. die();
  120. }
  121. public static function RenderError( $m )
  122. {
  123. global $message;
  124. $message = $m;
  125. PageHelper::Render( function()
  126. {
  127. global $message;
  128. echo $message;
  129. });
  130. return;
  131. }
  132. private static function RenderHead()
  133. {
  134. global $prefix;
  135. ?>
  136. <head>
  137. <title>PhpProject1 | <?= PageHelper::$PageTitle?></title>
  138. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  139. <?
  140. foreach( PageHelper::$stylesheets as $style )
  141. {
  142. ?>
  143. <link rel="stylesheet" type="text/css" href="<?=$prefix.$style?>" />
  144. <?
  145. }
  146. foreach( PageHelper::$javascripts as $script )
  147. {
  148. ?>
  149. <script type="text/javascript" src="<?=$prefix.$script?>" ></script>
  150. <?
  151. }
  152. if( PageHelper::$GETGameData == TRUE )
  153. {
  154. $gameData = Application::$GameData;
  155. $user_id = Application::GetCurrentUser();
  156. if( ! $user_id )
  157. $user_id = 'null';
  158. $map = Application::$GameDataService->GetGameMap( $gameData );
  159. ?>
  160. <script type="text/javascript">
  161. var UserData = <? echo json_encode( $user_id ) ?>;
  162. var GameData = <?php echo json_encode( $gameData ) ?>;
  163. GameData.Map = <?php echo json_encode( $map ) ?>;
  164. var Data = GameData;
  165. var TileConfig = <?php echo json_encode( Configuration::GetTileConfiguration() ) ?>;
  166. var UnitConfig = <?php echo json_encode( Configuration::GetUnitConfiguration() ) ?>;
  167. </script>
  168. <?
  169. }
  170. ?>
  171. </head>
  172. <?
  173. }
  174. public static function Render( $contentGen = null )
  175. {
  176. $user = Application::GetCurrentUser();
  177. global $c;
  178. if( PageHelper::$RequireLogin && !$user )
  179. {
  180. PageHelper::Redirect( "login.php" );
  181. return;
  182. }
  183. global $prefix;
  184. if( PageHelper::$GETGameData && !isset($_GET['id']))
  185. {
  186. PageHelper::Redirect( $_SERVER['REQUEST_URI']."?id=1");
  187. }
  188. $contentGen = ($contentGen != null ) ? $contentGen : function()
  189. {
  190. echo "NO CONTENT SET";
  191. }
  192. ?>
  193. <!DOCTYPE html>
  194. <html>
  195. <? PageHelper::RenderHead(); ?>
  196. <body ng:app ng-click="rootClick($event)">
  197. <div id="page-title" class="ui-corner-all single-column-content">
  198. <h1><?= PageHelper::$PageTitle?></h1>
  199. </div>
  200. <? if( $user )
  201. {
  202. ?>
  203. <div class="ui-corner-all single-column-content">
  204. <?
  205. foreach( PageHelper::$links as $label => $link )
  206. {
  207. ?>
  208. <div class="link-div"><a href="<?=$prefix.$link?>"><?=$label?></a></div>
  209. <?
  210. }
  211. ?>
  212. <div id="user-info" class="link-div">
  213. <div style="height:1em;"><form method="post" name="logout" action="login.php">
  214. <input type="submit" name="submit" value="Logout" />
  215. </form></div>
  216. <div style="margin-top:.2em;"><b>User:</b><?=$user->UserName?></div>
  217. </div>
  218. <div style="clear:both;"></div>
  219. <div></div>
  220. </div>
  221. <?}?>
  222. <div class="ui-corner-all single-column-content page-content">
  223. <? $contentGen(); ?>
  224. </div>
  225. <? if( PageHelper::$console ) {
  226. } ?>
  227. </body>
  228. </html>
  229. <?php
  230. }
  231. }
  232. $trigrams = array("all",
  233. "and",
  234. "are",
  235. "but",
  236. "ent",
  237. "era" ,
  238. "ere" ,
  239. "eve",
  240. "for",
  241. "had",
  242. "hat",
  243. "hen",
  244. "her",
  245. "hin",
  246. "his",
  247. "ing",
  248. "ion",
  249. "ith",
  250. "not",
  251. "ome",
  252. "oul",
  253. "our",
  254. "sho",
  255. "ted",
  256. "ter",
  257. "tha",
  258. "the",
  259. "thi",
  260. "tio",
  261. "uld",
  262. "fu",
  263. "ver","was",
  264. "wit",
  265. "you");
  266. class Rule
  267. {
  268. public $names;
  269. public $next;
  270. public function Rule( $n, $next )
  271. {
  272. $this->names = $n;
  273. $this->next = $next;
  274. }
  275. static function FirstNames( $next )
  276. {
  277. return new Rule( array ( "Toast", "Patches", "Buddy", "Spider", "Peter","Le", "Eric","Ivan", "Evan","Lee","Nacho","Frank","Abe","Remi","Abraham","Ganny","Tim","Rames","Steve", "Jim", "Spencer", "Beef", "Pedro", "Shen", "Jimmy", "James", "Guy", "Captain Jack", "David", "Edward", "Eddy", "Mister", "Sir", "Professor", "Doctor", "Kevin", "Johnny", "Plank"), $next );
  278. }
  279. static function Trigrams( $next )
  280. {
  281. global $trigrams;
  282. return new Rule( $trigrams, $next );
  283. }
  284. static function LastStart1( $next )
  285. {
  286. return new Rule( array( "Hu", "Sprink", "Heim", "Dan", "Fru","Spa","Wel", "Smel", "Rustle", "Tag", "Dar", "Ramm","Rem","Mug", "Doo", "Pot", "Fus", "Loo", "Lan", "Rog", "Smert", "Derp", "Widdly", "Mar", "Edd", "Dik", "Ali", "Mor", "Qua", "Quar", "Plar", "Jen"), $next );
  287. }
  288. static function LastMid1( $next )
  289. {
  290. return new Rule( array ('y', "stol", "ing", "ton", "ty", "ot", "ro", "sly", "ran", "mer", "ter", "oly", "o", "s", "ulu", "li", "dy", "er", "ling", "spider" ), $next );
  291. }
  292. static function LastEnd1( $next )
  293. {
  294. return new Rule( array ( "hoff", "dinger", "ton", "man", "son", "ston", "worth", "smith", "dale", "dah", "derp", "scuds", "cus", "cal", "zer", "vark", "inator", "smek", "go", "iels", "stein", "stolt", "han", "tits", "nuts" ), $next );
  295. }
  296. static function Space( $next )
  297. {
  298. return new Rule( array ( " " ), $next );
  299. }
  300. static function EmptyRule( $next )
  301. {
  302. return new Rule( array( "" ), $next );
  303. }
  304. static function Hyphen( $next )
  305. {
  306. return new Rule( array ( "-", "-", "-" ), $next );
  307. }
  308. static function BaseFirst ( $next )
  309. {
  310. return new Rule( array( "Super", "Jimmy", "Power", "Weapon", "Derp", "Summer", "Winter", "Village", "Town", "City", "Population", "Windy", "Blooming","Sunny","Ghost","King", "Giga", "Poop", "Sword", "Punching", "Battle", "Warrior", "Party"), $next);
  311. }
  312. static function BaseSecond( $next )
  313. {
  314. return new Rule( array( " House", " Fortress", " Manor", " Tower", " City", " Hut", "mart", "geshayla", " Center", "topolis", "topia", "town", "ville", "dale", "fieldsdale", "fields", " Hills". ""), $next);
  315. }
  316. static function CityBattleStart( $next )
  317. {
  318. return new Rule( array("Battle of ", "Seige of ", "Battle of ", "Massacre of " ), $next);
  319. }
  320. static function CityBattleEnd( $next )
  321. {
  322. return new Rule( array(" War", " Battle", " Revolution", " Massacre" ), $next);
  323. }
  324. static function GameOf( $next )
  325. {
  326. return new Rule( array("Battle of ", "Seige of ", "War of ", "Battle of " ), $next);
  327. }
  328. static function GameOfEnd( $next )
  329. {
  330. return new Rule( array("" ), $next);
  331. }
  332. static function Year( $next )
  333. {
  334. return new Rule( array( "".rand(100,4000)), $next );
  335. }
  336. public static function GeneratorRule( $gen, $next )
  337. {
  338. return new Rule( array( $gen->Generate() ), $next );
  339. }
  340. }
  341. class NameGenerator
  342. {
  343. public $rule;
  344. public function NameGenerator()
  345. {
  346. }
  347. public static function StandardNameGenerator()
  348. {
  349. $gen = new NameGenerator();
  350. $gen->rule = Rule::FirstNames( array(
  351. Rule::Space( array (
  352. Rule::LastStart1( array (
  353. Rule::LastEnd1( null ),
  354. Rule::Trigrams( array(
  355. Rule::LastEnd1( null )
  356. ) ),
  357. Rule::Trigrams( null ),
  358. Rule::LastMid1( array(
  359. Rule::LastEnd1( null ),
  360. Rule::LastMid1( array(
  361. Rule::LastEnd1( null )
  362. ))
  363. ))
  364. )),
  365. Rule::Trigrams( array(
  366. Rule::Trigrams( null ),
  367. Rule::Trigrams( array(
  368. Rule::Hyphen(array(
  369. Rule::Trigrams(null),
  370. Rule::LastEnd1(null)
  371. )),
  372. Rule::Trigrams( null ),
  373. Rule::LastEnd1( null ),
  374. Rule::LastMid1( array(
  375. Rule::LastEnd1( null ),
  376. Rule::Trigrams( null )
  377. ))
  378. ))
  379. ))
  380. ))
  381. ));
  382. return $gen;
  383. }
  384. public static function BaseNameGenerator()
  385. {
  386. $gen = new NameGenerator();
  387. $gen->rule = Rule::BaseFirst( array(
  388. Rule::BaseSecond( null )
  389. ) );
  390. return $gen;
  391. }
  392. public static function BattleNameGenerator()
  393. {
  394. $baseName = NameGenerator::BaseNameGenerator();
  395. $gen = new NameGenerator();
  396. $gen->rule = Rule::EmptyRule( array(
  397. Rule::CityBattleStart( array(
  398. Rule::GeneratorRule( $baseName, null ),
  399. Rule::GeneratorRule( $baseName, null ),
  400. Rule::Year( null )
  401. ) ),
  402. Rule::GeneratorRule( $baseName, array(
  403. Rule::CityBattleEnd( null )
  404. ))
  405. ) );
  406. return $gen;
  407. }
  408. public function Generate()
  409. {
  410. $name = "";
  411. $r = $this->rule;
  412. while($r != null )
  413. {
  414. $v = rand(0, count( $r->names) - 1);
  415. $name = $name.$r->names[$v];
  416. $v = rand( 0, count( $r->next ) - 1);
  417. if( $r->next == null )
  418. break;
  419. $r = $r->next[$v];
  420. }
  421. $space = false;
  422. for($i = 0, $s = strlen($name); $i < $s; $i++)
  423. {
  424. if( $space && substr_compare( $name, "of ", $i, 3) != 0)
  425. $name[$i] = strtoupper( $name[$i]);
  426. $space = ( $name[$i] === ' ');
  427. }
  428. return $name;
  429. }
  430. }
  431. function GenerateName()
  432. {
  433. $v = NameGenerator::StandardNameGenerator();
  434. return $v->Generate();
  435. }
  436. function GenerateBaseName()
  437. {
  438. $v = NameGenerator::BaseNameGenerator();
  439. return $v->Generate();
  440. }
  441. ?>