PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mysqllib.php

https://bitbucket.org/mikel3377/webgame
PHP | 218 lines | 173 code | 38 blank | 7 comment | 14 complexity | bda967584f2318b6cf6643a7e7a6f704 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('dbconns.php');
  7. include_once('primitives.php');
  8. include('updatelib.php');
  9. class GameDataService
  10. {
  11. public static function GetInstance()
  12. {
  13. if( !self::$instance)
  14. self::$instance = new GameDataService();
  15. return self::$instance;
  16. }
  17. public static function GetGame( $id )
  18. {
  19. $i = self::GetInstance();
  20. return $i->GetGameByID( $id );
  21. }
  22. public function GetUserById( $userid )
  23. {
  24. return $this->getUserData( "id = $userid");
  25. }
  26. public function GetUserByName( $username )
  27. {
  28. return $this->getUserData( "user = '$username'");
  29. }
  30. public function GetGameByID( $gameID )
  31. {
  32. $query = "SELECT * FROM games WHERE id=$gameID";
  33. $result = mysql_query( $query ) or die(mysql_error());
  34. $row = mysql_fetch_array($result);
  35. $g = new GameData();
  36. $g->ID = (int)$gameID;
  37. $g->Label = $row['label'];
  38. $g->ActiveUser = (int)$row['activeuser'];
  39. $g->Width = (int)$row['width'];
  40. $g->Height = (int)$row['height'];
  41. $g->Seed = (int)$row['seed'];
  42. $g->UpdateID = (int)$row['updateid'];
  43. $g->Turn = (int)$row['turn'];
  44. $this->initGamePlayers( $g );
  45. $this->initGameUnits( $g );
  46. return $g;
  47. }
  48. public function GetGameMap( $gamedata )
  49. {
  50. return $this->createMapFromGameData( $gamedata );
  51. }
  52. //private
  53. private static $instance;
  54. private function __construct()
  55. {
  56. MySql::Init();
  57. }
  58. private function getUserData( $where )
  59. {
  60. $u = new UserData();
  61. $query = "SELECT * FROM userpass WHERE $where";
  62. $result = mysql_query( $query ) or die(mysql_error());
  63. $row = mysql_fetch_array($result);
  64. if(!$row ) return null;
  65. $u->ID = (int)$row['id'];
  66. $u->UserName = $row['user'];
  67. $u->Wins = (int)$row['wins'];
  68. $u->Color = $row['color'];
  69. return $u;
  70. }
  71. private function initGamePlayers( &$gamedata )
  72. {
  73. $query = "SELECT * FROM userpass, usergames WHERE usergames.gameid = $gamedata->ID AND userpass.id = usergames.userid";
  74. $result = mysql_query( $query ) or die(mysql_error());
  75. $players = array();
  76. while( $row = mysql_fetch_array($result))
  77. {
  78. $p = $this->createPlayerDataFromRow( $row );
  79. if( $gamedata->ActiveUser == $p->UserID)
  80. $gamedata->ActivePlayer = $p;
  81. $players[] = $p;
  82. }
  83. $gamedata->Players = $players;
  84. }
  85. private function createPlayerDataFromRow( $row )
  86. {
  87. $p = new PlayerData();
  88. $p->GameID = (int)$row['gameid'];
  89. $p->UserID = (int)$row['id'];
  90. $p->UserName = (string)$row['user'];
  91. $p->Color = (string)$row['color'];
  92. $p->Resources = (int)$row['resources'];
  93. return $p;
  94. }
  95. public static function generateUnitQueryWhere( $whereClause )
  96. {
  97. $unitquery = "SELECT units.*,
  98. userpass.color, userpass.user
  99. FROM units, userpass WHERE userpass.id = units.ownerid";
  100. return $unitquery." AND ( ".$whereClause." )";
  101. }
  102. private function initGameUnits( &$gamedata )
  103. {
  104. $query = $this->generateUnitQueryWhere("units.gameid = $gamedata->ID");
  105. $result = mysql_query( $query ) or die(mysql_error());
  106. $units = array();
  107. while( $row = mysql_fetch_array($result))
  108. {
  109. $units[] = $this->createUnitDataFromRow( $row );
  110. }
  111. return $gamedata->Units = $units;
  112. }
  113. private function createUnitDataFromRow( $row )
  114. {
  115. $u = new UnitData();
  116. ///die((string))."<br/>");
  117. $u->ID = (int)$row['id'];
  118. $u->OwnerID = (int)$row['ownerid'];
  119. $u->OwnerName = (string)$row['user'];
  120. $u->Name = (string)$row['name'];
  121. $u->Type = (int)$row['type'];
  122. $u->X = (int)$row['x'];
  123. $u->Y = (int)$row['y'];
  124. $u->Color = $row['color'];
  125. $u->HP = (int)$row['hp'];
  126. $u->Moves = (int)$row['moves'];
  127. $u->ActionUsed = (bool)$row['actionused'];
  128. $t = UnitConfiguration::GetByID( $u->Type );
  129. //$this->Stats = new UnitStats($row);
  130. $u->MaxHP = $t->MaxHP;
  131. $u->AttackPower = $t->AttackPower;
  132. $u->Defense = $t->Defense;
  133. $u->AttackRange = $t->AttackRange;
  134. $u->MoveRange = $t->MoveRange;
  135. $u->TypeName = $t->Name;
  136. $u->GatherSpeed = $t->GatherSpeed;
  137. $u->GameID = (int)$row['gameid'];
  138. return $u;
  139. }
  140. private function createMapFromGameData( $gamedata )
  141. {
  142. $m = new Map();
  143. $m->Width = $gamedata->Width;
  144. $m->Height = $gamedata->Height;
  145. srand( $gamedata->Seed );
  146. for( $i = 0; $i < $gamedata->Width; $i++)
  147. {
  148. $m->Cells[$i] = array();
  149. for( $j = 0; $j < $gamedata->Height; $j++)
  150. {
  151. $v = rand(0,99);
  152. $t = 3;
  153. $t = ( $v < 100 ) ? 2 : $t;
  154. $t = ( $v < 99 ) ? 1 : $t;
  155. $t = ( $v < 79 ) ? 0 : $t;
  156. $m->Cells[$i][$j] = new Cell( $i, $j, $t );
  157. }
  158. }
  159. $this->genMapWater($m);
  160. srand();
  161. return $m;
  162. }
  163. private function genMapWater( &$map )
  164. {
  165. $x = 0;
  166. $y = rand(1,$map->Height-2);
  167. while( $x < $map->Width-1 && $y > 0 && $y < $map->Height-2)
  168. {
  169. if( $x % 2 == 1 )
  170. $y += 1;
  171. $y -= rand(0,1);
  172. if( rand(0,2) != 0)
  173. $map->Cells[$x][$y] = new Cell( $x, $y, 3 );
  174. if( rand(0,2) != 0)
  175. $map->Cells[$x][$y+1] = new Cell( $x, $y, 3 );
  176. $x ++;
  177. }
  178. }
  179. }
  180. ?>