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

/app/model/build.php

https://github.com/minea94/DboorZ
PHP | 386 lines | 346 code | 29 blank | 11 comment | 22 complexity | ef268e31db9e3bd8d20dc9d6b0c21db5 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @ This file is created by Decodeby.US
  5. * @ deZender Public (PHP5 Decompiler)
  6. *
  7. * @ Version : 1.0.0.0
  8. * @ Author : Ps2Gamer & Cyko
  9. * @ Release on : 30.05.2011
  10. * @ Official site : http://decodeby.us
  11. *
  12. */
  13. class BuildModel extends ModelBase
  14. {
  15. public function getVillageOases( $villageOasesid )
  16. {
  17. if ( $villageOasesid == "" )
  18. {
  19. return NULL;
  20. }
  21. return $this->provider->fetchResultSet( "SELECT\r\n\t\t\t\tv.id,\r\n\t\t\t\tv.rel_x, v.rel_y, \r\n\t\t\t\tv.image_num, \r\n\t\t\t\tv.allegiance_percent\r\n\t\t\tFROM\r\n\t\t\t\tp_villages v \r\n\t\t\tWHERE\r\n\t\t\t\tv.id IN (%s)", array(
  22. $villageOasesid
  23. ) );
  24. }
  25. public function getChildVillagesFor( $villageIds )
  26. {
  27. if ( $villageIds == "" )
  28. {
  29. return NULL;
  30. }
  31. return $this->provider->fetchResultSet( "SELECT\r\n\t\t\t\tv.id,\r\n\t\t\t\tv.village_name,\r\n\t\t\t\tv.people_count,\r\n\t\t\t\tv.rel_x, v.rel_y,\r\n\t\t\t\tDATE_FORMAT(v.creation_date, '%%Y/%%m/%%d') creation_date\r\n\t\t\tFROM\r\n\t\t\t\tp_villages v \r\n\t\t\tWHERE\r\n\t\t\t\tv.id IN (%s)", array(
  32. $villageIds
  33. ) );
  34. }
  35. public function getVillagesCp( $villages_id )
  36. {
  37. return $this->provider->fetchResultSet( "SELECT\r\n\t\t\t\tv.cp,\r\n\t\t\t\tTIME_TO_SEC(TIMEDIFF(NOW(), v.last_update_date)) elapsedTimeInSeconds\r\n\t\t\tFROM p_villages v\r\n\t\t\tWHERE v.id IN (%s)", array(
  38. $villages_id
  39. ) );
  40. }
  41. public function getVillageDataByName( $villagesName )
  42. {
  43. return $this->provider->fetchRow( "SELECT v.id, v.rel_x, v.rel_y, v.village_name, v.player_id, v.player_name FROM p_villages v WHERE v.is_oasis=0 AND v.village_name='%s'", array(
  44. $villagesName
  45. ) );
  46. }
  47. public function getVillageDataById( $villagesId )
  48. {
  49. return $this->provider->fetchRow( "SELECT v.id, v.rel_x, v.rel_y, v.village_name, v.player_id, v.player_name FROM p_villages v WHERE v.id='%s' AND NOT ISNULL(v.player_id) AND v.is_oasis=0", array(
  50. $villagesId
  51. ) );
  52. }
  53. public function getVillageName( $villageId )
  54. {
  55. return $this->provider->fetchScalar( "SELECT v.village_name FROM p_villages v WHERE v.id=%s", array(
  56. $villageId
  57. ) );
  58. }
  59. public function getPlayerName( $playerId )
  60. {
  61. return $this->provider->fetchScalar( "SELECT p.name FROM p_players p WHERE p.id=%s", array(
  62. $playerId
  63. ) );
  64. }
  65. public function getPlayType( $player_id )
  66. {
  67. return $this->provider->fetchScalar( "SELECT p.player_type FROM p_players p WHERE p.id=%s", array(
  68. $player_id
  69. ) );
  70. }
  71. public function getPlayerAllianceId( $playerId )
  72. {
  73. return $this->provider->fetchScalar( "SELECT p.alliance_id FROM p_players p WHERE p.id=%s", array(
  74. $playerId
  75. ) );
  76. }
  77. public function getOffers( $villageId )
  78. {
  79. return $this->provider->fetchResultSet( "SELECT m.* FROM p_merchants m WHERE m.village_id=%s ORDER BY m.id ASC", array(
  80. $villageId
  81. ) );
  82. }
  83. public function getAllOffersCount( $villageId, $x, $y, $radius, $speed )
  84. {
  85. $angle = $radius / 180;
  86. $x /= $angle;
  87. $y /= $angle;
  88. return $this->provider->fetchScalar( "SELECT \r\n\t\t\t\tCOUNT(*)\r\n\t\t\tFROM p_merchants m \r\n\t\t\tWHERE\r\n\t\t\t\tm.village_id!=%s\r\n\t\t\t\tAND IF(m.max_time>0, \r\n\t\t\t\t\t((ACOS(SIN(%s * PI() / 180) * SIN(m.village_x/%s * PI() / 180) + COS(%s * PI() / 180) * COS(m.village_x/%s * PI() / 180) * COS((%s - m.village_y/%s) * PI() / 180)) * 180 / PI()) * %s)/%s*3600\r\n\t\t\t\t\t<=m.max_time*3600,\r\n\t\t\t\t1)", array(
  89. $villageId,
  90. $x,
  91. $angle,
  92. $x,
  93. $angle,
  94. $y,
  95. $angle,
  96. $angle,
  97. $speed
  98. ) );
  99. }
  100. public function getAllOffers( $villageId, $x, $y, $radius, $speed, $pageIndex, $pageSize )
  101. {
  102. $angle = $radius / 180;
  103. $x /= $angle;
  104. $y /= $angle;
  105. return $this->provider->fetchResultSet( "SELECT \r\n\t\t\t\tm.*,\r\n\t\t\t\t((ACOS(SIN(%s * PI() / 180) * SIN(m.village_x/%s * PI() / 180) + COS(%s * PI() / 180) * COS(m.village_x/%s * PI() / 180) * COS((%s - m.village_y/%s) * PI() / 180)) * 180 / PI()) * %s)/m.merchants_speed*3600 timeInSeconds\r\n\t\t\tFROM p_merchants m \r\n\t\t\tHAVING\r\n\t\t\t\tm.village_id!=%s\r\n\t\t\t\tAND IF(m.max_time>0, timeInSeconds*m.merchants_speed/%s<=m.max_time*3600,1)\r\n\t\t\tORDER BY timeInSeconds ASC\r\n\t\t\tLIMIT %s,%s", array(
  106. $x,
  107. $angle,
  108. $x,
  109. $angle,
  110. $y,
  111. $angle,
  112. $angle,
  113. $villageId,
  114. $speed,
  115. $pageIndex * $pageSize,
  116. $pageSize
  117. ) );
  118. }
  119. public function getOffer( $offerId, $playerId, $villageId )
  120. {
  121. return $this->provider->fetchRow( "SELECT m.* FROM p_merchants m WHERE id=%s AND player_id=%s AND village_id=%s", array(
  122. $offerId,
  123. $playerId,
  124. $villageId
  125. ) );
  126. }
  127. public function getOffer2( $offerId, $x, $y, $radius )
  128. {
  129. $angle = $radius / 180;
  130. $x /= $angle;
  131. $y /= $angle;
  132. return $this->provider->fetchRow( "SELECT \r\n\t\t\t\tm.*,\r\n\t\t\t\t((ACOS(SIN(%s * PI() / 180) * SIN(m.village_x/%s * PI() / 180) + COS(%s * PI() / 180) * COS(m.village_x/%s * PI() / 180) * COS((%s - m.village_y/%s) * PI() / 180)) * 180 / PI()) * %s)/m.merchants_speed*3600 timeInSeconds\r\n\t\t\tFROM p_merchants m \r\n\t\t\tWHERE \r\n\t\t\t\tid=%s", array(
  133. $x,
  134. $angle,
  135. $x,
  136. $angle,
  137. $y,
  138. $angle,
  139. $angle,
  140. $offerId
  141. ) );
  142. }
  143. public function removeMerchantOffer( $offerId, $playerId, $villageId )
  144. {
  145. $merchants_num = intval( $this->provider->fetchScalar( "SELECT merchants_num FROM p_merchants WHERE id=%s", array(
  146. intval( $offerId )
  147. ) ) );
  148. if ( $merchants_num <= 0 )
  149. {
  150. return;
  151. }
  152. $this->provider->executeQuery( "UPDATE p_villages v SET \r\n\t\t\tv.offer_merchants_count=IF(v.offer_merchants_count-%s<0, 0, v.offer_merchants_count-%s)\r\n\t\t\tWHERE\r\n\t\t\t\tv.id=%s", array(
  153. $merchants_num,
  154. $merchants_num,
  155. $villageId
  156. ) );
  157. $this->provider->executeQuery( "DELETE FROM p_merchants WHERE id=%s AND player_id=%s AND village_id=%s", array(
  158. intval( $offerId ),
  159. $playerId,
  160. $villageId
  161. ) );
  162. }
  163. public function addMerchantOffer( $playerId, $playerName, $villageId, $x, $y, $merchantNum, $offer, $allianceOnly, $maxTime, $merchantsSpeed )
  164. {
  165. $this->provider->executeQuery( "INSERT INTO p_merchants SET \r\n\t\t\tplayer_id=%s,\r\n\t\t\tplayer_name='%s',\r\n\t\t\tvillage_id=%s,\r\n\t\t\tvillage_x=%s,\r\n\t\t\tvillage_y=%s,\r\n\t\t\toffer='%s',\r\n\t\t\talliance_only=%s,\r\n\t\t\tmax_time=%s,\r\n\t\t\tmerchants_num=%s,\r\n\t\t\tmerchants_speed=%s", array(
  166. $playerId,
  167. $playerName,
  168. $villageId,
  169. $x,
  170. $y,
  171. $offer,
  172. $allianceOnly ? 1 : 0,
  173. $maxTime,
  174. $merchantNum,
  175. $merchantsSpeed
  176. ) );
  177. $this->provider->executeQuery( "UPDATE p_villages v SET \r\n\t\t\tv.offer_merchants_count=v.offer_merchants_count+%s\r\n\t\t\tWHERE\r\n\t\t\t\tv.id=%s", array(
  178. $merchantNum,
  179. $villageId
  180. ) );
  181. }
  182. public function makeVillageAsCapital( $playerId, $villageId )
  183. {
  184. $mq = new QueueJobModel( );
  185. $capitalRow = $this->provider->fetchRow( "SELECT v.id, v.buildings FROM p_villages v WHERE v.player_id=%s AND v.is_capital=1", array(
  186. $playerId
  187. ) );
  188. $buildingArr = explode( ",", $capitalRow['buildings'] );
  189. $c = 0;
  190. foreach ( $buildingArr as $buildingItem )
  191. {
  192. ++$c;
  193. $update_state = explode( " ", $buildingItem[2] );
  194. $level = explode( " ", $buildingItem[1] );
  195. $item_id = explode( " ", $buildingItem[0] );
  196. list( $item_id, $level, $update_state ) = $item_id;
  197. if ( $item_id == 0 )
  198. {
  199. continue;
  200. }
  201. $max_lvl_in_non_capital = $GLOBALS['GameMetadata']['items'][$item_id]['max_lvl_in_non_capital'];
  202. if ( $max_lvl_in_non_capital == NULL || $level + $update_state <= $max_lvl_in_non_capital )
  203. {
  204. continue;
  205. }
  206. $dropLevels = $level + $update_state - $max_lvl_in_non_capital;
  207. while ( 0 < $dropLevels-- )
  208. {
  209. $mq->upgradeBuilding( $capitalRow['id'], $c, $item_id, TRUE );
  210. }
  211. }
  212. $this->provider->executeQuery( "UPDATE p_villages v SET v.is_capital=0 WHERE v.player_id=%s", array(
  213. $playerId
  214. ) );
  215. $this->provider->executeQuery( "UPDATE p_villages v SET v.is_capital=1 WHERE v.id=%s AND v.player_id=%s", array(
  216. $villageId,
  217. $playerId
  218. ) );
  219. }
  220. public function changeHeroName( $playerId, $heroName )
  221. {
  222. $this->provider->executeQuery( "UPDATE p_players p SET p.hero_name='%s' WHERE p.id=%s", array(
  223. $heroName,
  224. $playerId
  225. ) );
  226. }
  227. public function decreaseGoldNum( $playerId, $goldCost )
  228. {
  229. $this->provider->executeQuery( "UPDATE p_players p SET p.gold_num=p.gold_num-%s WHERE p.id=%s", array(
  230. $goldCost,
  231. $playerId
  232. ) );
  233. }
  234. public function allianceExists( $allianceName )
  235. {
  236. return 0 < intval( $this->provider->fetchScalar( "SELECT a.id FROM p_alliances a WHERE a.name='%s'", array(
  237. $allianceName
  238. ) ) );
  239. }
  240. public function createAlliance( $playerId, $allianceName, $allianceName2, $maxPlayer )
  241. {
  242. $allianceRoles = ( ALLIANCE_ROLE_SETROLES | ALLIANCE_ROLE_REMOVEPLAYER | ALLIANCE_ROLE_EDITNAMES | ALLIANCE_ROLE_EDITCONTRACTS | ALLIANCE_ROLE_SENDMESSAGE | ALLIANCE_ROLE_INVITEPLAYERS )." ".alliance_creator;
  243. $this->provider->executeQuery( "INSERT INTO p_alliances \r\n\t\t\tSET \r\n\t\t\t\tname='%s', \r\n\t\t\t\tname2='%s', \r\n\t\t\t\tcreator_player_id=%s, \r\n\t\t\t\trating=0, \r\n\t\t\t\tcreation_date=NOW(),\r\n\t\t\t\tplayer_count=1,\r\n\t\t\t\tmax_player_count=%s,\r\n\t\t\t\tplayers_ids='%s'", array(
  244. $allianceName,
  245. $allianceName2,
  246. $playerId,
  247. $maxPlayer,
  248. $playerId
  249. ) );
  250. $aid = $this->provider->fetchScalar( "SELECT LAST_INSERT_ID() FROM p_alliances" );
  251. $this->provider->executeQuery( "UPDATE p_players p SET p.alliance_id=%s, p.alliance_name='%s', p.alliance_roles='%s' WHERE p.id=%s", array(
  252. $aid,
  253. $allianceName,
  254. $allianceRoles,
  255. $playerId
  256. ) );
  257. $this->provider->executeQuery( "UPDATE p_villages v SET v.alliance_id=%s, v.alliance_name='%s' WHERE v.player_id=%s", array(
  258. $aid,
  259. $allianceName,
  260. $playerId
  261. ) );
  262. return $aid;
  263. }
  264. public function acceptAllianceJoining( $playerId, $allianceId )
  265. {
  266. $row = $this->provider->fetchRow( "SELECT a.* FROM p_alliances a WHERE a.id=%s", array(
  267. $allianceId
  268. ) );
  269. if ( $row == NULL )
  270. {
  271. return 0;
  272. }
  273. if ( $row['max_player_count'] <= $row['player_count'] )
  274. {
  275. return 1;
  276. }
  277. $allianceName = $row['name'];
  278. $players_ids = $row['players_ids'];
  279. if ( $players_ids != "" )
  280. {
  281. $players_ids .= ",";
  282. }
  283. $players_ids .= $playerId;
  284. $this->provider->executeQuery( "UPDATE p_alliances a SET a.player_count=a.player_count+1, a.players_ids='%s' WHERE a.id=%s", array(
  285. $players_ids,
  286. $allianceId
  287. ) );
  288. $this->provider->executeQuery( "UPDATE p_players p SET p.alliance_id=%s, p.alliance_name='%s' WHERE p.id=%s", array(
  289. $allianceId,
  290. $allianceName,
  291. $playerId
  292. ) );
  293. $this->provider->executeQuery( "UPDATE p_villages v SET v.alliance_id=%s, v.alliance_name='%s' WHERE v.player_id=%s", array(
  294. $allianceId,
  295. $allianceName,
  296. $playerId
  297. ) );
  298. return 2;
  299. }
  300. public function _getNewInvite( $invitesString, $removeId )
  301. {
  302. if ( $invitesString == "" )
  303. {
  304. return "";
  305. }
  306. $result = "";
  307. $arr = explode( "\n", $invitesString );
  308. foreach ( $arr as $invite )
  309. {
  310. $name = explode( " ", $invite[1], 2 );
  311. $id = explode( " ", $invite[0], 2 );
  312. list( $id, $name ) = $id;
  313. if ( $id == $removeId )
  314. {
  315. continue;
  316. }
  317. if ( $result != "" )
  318. {
  319. $result .= "\n";
  320. }
  321. $result .= $id." ".$name;
  322. }
  323. return $result;
  324. }
  325. public function removeAllianceInvites( $playerId, $allianceId )
  326. {
  327. $pRow = $this->provider->fetchRow( "SELECT p.name, p.invites_alliance_ids FROM p_players p WHERE p.id=%s", array(
  328. $playerId
  329. ) );
  330. $aRow = $this->provider->fetchRow( "SELECT a.name, a.invites_player_ids FROM p_alliances a WHERE a.id=%s", array(
  331. $allianceId
  332. ) );
  333. $pInvitesStr = $this->_getNewInvite( trim( $pRow['invites_alliance_ids'] ), $allianceId );
  334. $aInvitesStr = $this->_getNewInvite( trim( $aRow['invites_player_ids'] ), $playerId );
  335. $this->provider->executeQuery( "UPDATE p_players p SET p.invites_alliance_ids='%s' WHERE p.id=%s", array(
  336. $pInvitesStr,
  337. $playerId
  338. ) );
  339. $this->provider->executeQuery( "UPDATE p_alliances a SET a.invites_player_ids='%s' WHERE a.id=%s", array(
  340. $aInvitesStr,
  341. $allianceId
  342. ) );
  343. }
  344. public function getVillageData2ById( $villageId )
  345. {
  346. return $this->provider->fetchRow( "SELECT v.id, v.tribe_id, v.is_oasis, v.village_name, v.player_id, v.player_name FROM p_villages v WHERE v.id=%s", array(
  347. $villageId
  348. ) );
  349. }
  350. public function getOasesDataById( $villagesId )
  351. {
  352. return $this->provider->fetchResultSet( "SELECT v.id, v.tribe_id, v.rel_x, v.rel_y, v.troops_num, v.player_id, v.player_name FROM p_villages v WHERE v.id IN(%s)", array(
  353. $villagesId
  354. ) );
  355. }
  356. }
  357. ?>