PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/classes/PlayerUtil.class.php

https://bitbucket.org/VolCh/2moons
PHP | 655 lines | 526 code | 99 blank | 30 comment | 47 complexity | 83f707efe4af2631d69111004685f96f MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0, GPL-2.0, Apache-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * 2Moons
  4. * Copyright (C) 2012 Jan Krรถpke
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package 2Moons
  20. * @author Jan Krรถpke <info@2moons.cc>
  21. * @copyright 2012 Jan Krรถpke <info@2moons.cc>
  22. * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
  23. * @version 1.7.2 (2013-03-18)
  24. * @info $Id$
  25. * @link http://2moons.cc/
  26. */
  27. class PlayerUtil
  28. {
  29. static public function cryptPassword($password)
  30. {
  31. $salt = NULL;
  32. // @see: http://www.phpgangsta.de/schoener-hashen-mit-bcrypt
  33. require 'includes/config.php';
  34. if(!CRYPT_BLOWFISH || is_null($salt)) {
  35. return md5($password);
  36. } else {
  37. return crypt($password, '$2a$09$'.$salt.'$');
  38. }
  39. }
  40. static public function isPositionFree($universe, $galaxy, $system, $position, $type = 1)
  41. {
  42. $db = Database::get();
  43. $sql = "SELECT COUNT(*) as record
  44. FROM %%PLANETS%%
  45. WHERE universe = :universe
  46. AND galaxy = :galaxy
  47. AND system = :system
  48. AND planet = :position
  49. AND planet_type = :type;";
  50. $count = $db->selectSingle($sql, array(
  51. ':universe' => $universe,
  52. ':galaxy' => $galaxy,
  53. ':system' => $system,
  54. ':position' => $position,
  55. ':type' => $type,
  56. ), 'record');
  57. return $count == 0;
  58. }
  59. static public function isNameValid($name)
  60. {
  61. if(UTF8_SUPPORT) {
  62. return preg_match('/^[\p{L}\p{N}_\-. ]*$/u', $name);
  63. } else {
  64. return preg_match('/^[A-z0-9_\-. ]*$/', $name);
  65. }
  66. }
  67. static public function isMailValid($address) {
  68. if(function_exists('filter_var')) {
  69. return filter_var($address, FILTER_VALIDATE_EMAIL) !== FALSE;
  70. } else {
  71. return preg_match('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', $address);
  72. }
  73. }
  74. static public function checkPosition($universe, $galaxy, $system, $position)
  75. {
  76. $config = Config::get($universe);
  77. return !(1 > $galaxy
  78. || 1 > $system
  79. || 1 > $position
  80. || $config->max_galaxy < $galaxy
  81. || $config->max_system < $system
  82. || $config->max_planets < $position);
  83. }
  84. static public function createPlayer($universe, $userName, $userPassword, $userMail, $userLanguage = NULL, $galaxy = NULL, $system = NULL, $position = NULL, $name = NULL, $authlevel = 0, $userIpAddress = NULL)
  85. {
  86. $config = Config::get($universe);
  87. if (isset($universe, $galaxy, $system, $position))
  88. {
  89. if (self::checkPosition($universe, $galaxy, $system, $position) === false)
  90. {
  91. throw new Exception(sprintf("Try to create a planet at position: %s:%s:%s!", $galaxy, $system, $position));
  92. }
  93. if (self::isPositionFree($universe, $galaxy, $system, $position) === false)
  94. {
  95. throw new Exception(sprintf("Position is not empty: %s:%s:%s!", $galaxy, $system, $position));
  96. }
  97. } else {
  98. $galaxy = $config->LastSettedGalaxyPos;
  99. $system = $config->LastSettedSystemPos;
  100. $planet = $config->LastSettedPlanetPos;
  101. do {
  102. $position = mt_rand(round($config->max_planets * 0.2), round($config->max_planets * 0.8));
  103. if ($planet < 3) {
  104. $planet += 1;
  105. } else {
  106. if ($system >= $config->max_system) {
  107. $system = 1;
  108. if($galaxy >= $config->max_galaxy) {
  109. $galaxy = 1;
  110. } else {
  111. $galaxy += 1;
  112. }
  113. } else {
  114. $system += 1;
  115. }
  116. }
  117. } while (self::isPositionFree($universe, $galaxy, $system, $position) === false);
  118. // Update last coordinates to config table
  119. $config->LastSettedGalaxyPos = $planet;
  120. $config->LastSettedSystemPos = $system;
  121. $config->LastSettedPlanetPos = $galaxy;
  122. }
  123. $resourceQuery = array();
  124. $params = array(
  125. ':username' => $userName,
  126. ':email' => $userMail,
  127. ':email2' => $userMail,
  128. ':authlevel' => $authlevel,
  129. ':universe' => $universe,
  130. ':language' => $userLanguage,
  131. ':registerAddress' => (!empty($userIpAddress) ? $userIpAddress : $_SERVER['REMOTE_ADDR']),
  132. ':onlinetime' => TIMESTAMP,
  133. ':registerTimestamp' => TIMESTAMP,
  134. ':password' => $userPassword,
  135. ':dpath' => DEFAULT_THEME,
  136. ':timezone' => $config->timezone,
  137. ':nameLastChanged' => 0,
  138. );
  139. foreach($GLOBALS['reslist']['resstype'][3] as $elementID) {
  140. $key = $GLOBALS['resource'][$elementID];
  141. $params[':'.$key] = $config->{$key.'_start'};
  142. $resourceQuery[] = '`'.$key.'` = :'.$key;
  143. }
  144. $sql = 'INSERT INTO %%USERS%% SET
  145. username = :username,
  146. email = :email,
  147. email_2 = :email2,
  148. authlevel = :authlevel,
  149. universe = :universe,
  150. lang = :language,
  151. ip_at_reg = :registerAddress,
  152. onlinetime = :onlinetime,
  153. register_time = :registerTimestamp,
  154. password = :password,
  155. dpath = :dpath,
  156. timezone = :timezone,
  157. uctime = :nameLastChanged,
  158. '.implode(', ', $resourceQuery).';';
  159. $db = Database::get();
  160. $db->insert($sql, $params);
  161. $userId = $db->lastInsertId();
  162. $planetId = self::createPlanet($galaxy, $system, $position, $universe, $userId, $name, true, $authlevel);
  163. $currentUserAmount = $config->users_amount + 1;
  164. $config->users_amount = $currentUserAmount;
  165. $sql = "UPDATE %%USERS%% SET
  166. galaxy = :galaxy,
  167. system = :system,
  168. planet = :position,
  169. id_planet = :planetId
  170. WHERE id = :userId;";
  171. $db->update($sql, array(
  172. ':galaxy' => $galaxy,
  173. ':system' => $system,
  174. ':position' => $position,
  175. ':planetId' => $planetId,
  176. ':userId' => $userId,
  177. ));
  178. $sql = "SELECT MAX(total_rank) as rank FROM %%STATPOINTS%% WHERE universe = :universe AND stat_type = :type;";
  179. $rank = $db->selectSingle($sql, array(
  180. ':universe' => $universe,
  181. ':type' => 1,
  182. ), 'rank');
  183. $sql = "INSERT INTO %%STATPOINTS%% SET
  184. id_owner = :userId,
  185. universe = :universe,
  186. stat_type = :type,
  187. tech_rank = :rank,
  188. build_rank = :rank,
  189. defs_rank = :rank,
  190. fleet_rank = :rank,
  191. total_rank = :rank;";
  192. $db->insert($sql, array(
  193. ':universe' => $universe,
  194. ':userId' => $userId,
  195. ':type' => 1,
  196. ':rank' => $rank + 1,
  197. ));
  198. $config->save();
  199. return array($userId, $planetId);
  200. }
  201. static public function createPlanet($galaxy, $system, $position, $universe, $userId, $name = NULL, $isHome = false, $authlevel = 0)
  202. {
  203. global $LNG;
  204. if (self::checkPosition($universe, $galaxy, $system, $position) === false)
  205. {
  206. throw new Exception(sprintf("Try to create a planet at position: %s:%s:%s!", $galaxy, $system, $position));
  207. }
  208. if (self::isPositionFree($universe, $galaxy, $system, $position) === false)
  209. {
  210. throw new Exception(sprintf("Position is not empty: %s:%s:%s!", $galaxy, $system, $position));
  211. }
  212. $planetData = array();
  213. require 'includes/PlanetData.php';
  214. $config = Config::get($universe);
  215. $dataIndex = (int) ceil($position / ($config->max_planets / count($planetData)));
  216. $maxTemperature = $planetData[$dataIndex]['temp'];
  217. $minTemperature = $maxTemperature - 40;
  218. if($isHome) {
  219. $maxFields = $config->initial_fields;
  220. } else {
  221. $maxFields = (int) floor($planetData[$dataIndex]['fields'] * $config->planet_factor);
  222. }
  223. $diameter = (int) floor(1000 * sqrt($maxFields));
  224. $imageNames = array_keys($planetData[$dataIndex]['image']);
  225. $imageNameType = $imageNames[array_rand($imageNames)];
  226. $imageName = $imageNameType;
  227. $imageName .= 'planet';
  228. $imageName .= $planetData[$dataIndex]['image'][$imageNameType] < 10 ? '0' : '';
  229. $imageName .= $planetData[$dataIndex]['image'][$imageNameType];
  230. if(empty($name))
  231. {
  232. $name = $isHome ? $LNG['fcm_mainplanet'] : $LNG['fcp_colony'];
  233. }
  234. $params = array(
  235. ':name' => $name,
  236. ':universe' => $universe,
  237. ':userId' => $userId,
  238. ':galaxy' => $galaxy,
  239. ':system' => $system,
  240. ':position' => $position,
  241. ':updateTimestamp' => TIMESTAMP,
  242. ':type' => 1,
  243. ':imageName' => $imageName,
  244. ':diameter' => $diameter,
  245. ':maxFields' => $maxFields,
  246. ':minTemperature' => $minTemperature,
  247. ':maxTemperature' => $maxTemperature,
  248. );
  249. $resourceQuery = array();
  250. foreach($GLOBALS['reslist']['resstype'][1] as $elementID) {
  251. $key = $GLOBALS['resource'][$elementID];
  252. $params[':'.$key] = $config->{$key.'_start'};
  253. $resourceQuery[] = '`'.$key.'` = :'.$key;
  254. }
  255. $sql = 'INSERT INTO %%PLANETS%% SET
  256. name = :name,
  257. universe = :universe,
  258. id_owner = :userId,
  259. galaxy = :galaxy,
  260. system = :system,
  261. planet = :position,
  262. last_update = :updateTimestamp,
  263. planet_type = :type,
  264. image = :imageName,
  265. diameter = :diameter,
  266. field_max = :maxFields,
  267. temp_min = :minTemperature,
  268. temp_max = :maxTemperature,
  269. '.implode(', ', $resourceQuery).';';
  270. $db = Database::get();
  271. $db->insert($sql, $params);
  272. return $db->lastInsertId();
  273. }
  274. static public function createMoon($universe, $galaxy, $system, $position, $userId, $chance, $diameter = NULL, $moonName = NULL)
  275. {
  276. global $LNG;
  277. $db = Database::get();
  278. $sql = "SELECT id_luna, planet_type, id, name, temp_max, temp_min
  279. FROM %%PLANETS%%
  280. WHERE universe = :universe
  281. AND galaxy = :galaxy
  282. AND system = :system
  283. AND planet = :position
  284. AND planet_type = :type;";
  285. $parentPlanet = $db->selectSingle($sql, array(
  286. ':universe' => $universe,
  287. ':galaxy' => $galaxy,
  288. ':system' => $system,
  289. ':position' => $position,
  290. ':type' => 1,
  291. ));
  292. if ($parentPlanet['id_luna'] != 0)
  293. {
  294. return false;
  295. }
  296. if(is_null($diameter))
  297. {
  298. $diameter = floor(pow(mt_rand(10, 20) + 3 * $chance, 0.5) * 1000); # New Calculation - 23.04.2011
  299. }
  300. $maxTemperature = $parentPlanet['temp_max'] - mt_rand(10, 45);
  301. $minTemperature = $parentPlanet['temp_min'] - mt_rand(10, 45);
  302. if(empty($moonName))
  303. {
  304. $moonName = $LNG['type_planet'][3];
  305. }
  306. $sql = "INSERT INTO %%PLANETS%% SET
  307. name = :name,
  308. id_owner = :owner,
  309. universe = :universe,
  310. galaxy = :galaxy,
  311. system = :system,
  312. planet = :planet,
  313. last_update = :updateTimestamp,
  314. planet_type = :type,
  315. image = :image,
  316. diameter = :diameter,
  317. field_max = :fields,
  318. temp_min = :minTemperature,
  319. temp_max = :maxTemperature,
  320. metal = :metal,
  321. metal_perhour = :metPerHour,
  322. crystal = :crystal,
  323. crystal_perhour = :cryPerHour,
  324. deuterium = :deuterium,
  325. deuterium_perhour = :deuPerHour;";
  326. $db->insert($sql, array(
  327. ':name' => $moonName,
  328. ':owner' => $userId,
  329. ':universe' => $universe,
  330. ':galaxy' => $galaxy,
  331. ':system' => $system,
  332. ':planet' => $position,
  333. ':updateTimestamp' => TIMESTAMP,
  334. ':type' => 3,
  335. ':image' => 'mond',
  336. ':diameter' => $diameter,
  337. ':fields' => 1,
  338. ':minTemperature' => $minTemperature,
  339. ':maxTemperature' => $maxTemperature,
  340. ':metal' => 0,
  341. ':metPerHour' => 0,
  342. ':crystal' => 0,
  343. ':cryPerHour' => 0,
  344. ':deuterium' => 0,
  345. ':deuPerHour' => 0,
  346. ));
  347. $moonId = $db->lastInsertId();
  348. $sql = "UPDATE %%PLANETS%% SET id_luna = :moonId WHERE id = :planetId;";
  349. $db->update($sql, array(
  350. ':moonId' => $moonId,
  351. ':planetId' => $parentPlanet['id'],
  352. ));
  353. return $moonId;
  354. }
  355. static public function deletePlayer($userId)
  356. {
  357. if(ROOT_USER == $userId) {
  358. // superuser can not be deleted.
  359. return false;
  360. }
  361. $db = Database::get();
  362. $sql = 'SELECT universe, ally_id FROM %%USERS%% WHERE id = :userId;';
  363. $userData = $db->selectSingle($sql, array(
  364. ':userId' => $userId
  365. ));
  366. if (empty($userData))
  367. {
  368. return false;
  369. }
  370. if (!empty($userData['ally_id']))
  371. {
  372. $sql = 'SELECT ally_members FROM %%ALLIANCE%% WHERE id = :allianceId;';
  373. $memberCount = $db->selectSingle($sql, array(
  374. ':allianceId' => $userData['ally_id']
  375. ), 'ally_members');
  376. if ($memberCount > 1)
  377. {
  378. $sql = 'UPDATE %%ALLIANCE%% SET ally_members = ally_members - 1 WHERE id = :allianceId;';
  379. $db->update($sql, array(
  380. ':allianceId' => $userData['ally_id']
  381. ));
  382. }
  383. else
  384. {
  385. $sql = 'DELETE FROM %%ALLIANCE%% WHERE id = :allianceId;';
  386. $db->delete($sql, array(
  387. ':allianceId' => $userData['ally_id']
  388. ));
  389. $sql = 'DELETE FROM %%STATPOINTS%% WHERE stat_type = :type AND id_owner = :allianceId;';
  390. $db->delete($sql, array(
  391. ':allianceId' => $userData['ally_id'],
  392. ':type' => 2
  393. ));
  394. $sql = 'UPDATE %%STATPOINTS%% SET id_ally = :resetId WHERE id_ally = :allianceId;';
  395. $db->update($sql, array(
  396. ':allianceId' => $userData['ally_id'],
  397. ':resetId' => 0
  398. ));
  399. }
  400. }
  401. $sql = 'DELETE FROM %%ALLIANCE_REQUEST%% WHERE userID = :userId;';
  402. $db->delete($sql, array(
  403. ':userId' => $userId
  404. ));
  405. $sql = 'DELETE FROM %%BUDDY%% WHERE owner = :userId OR sender = :userId;';
  406. $db->delete($sql, array(
  407. ':userId' => $userId
  408. ));
  409. $sql = 'DELETE %%FLEETS%%, %%FLEETS_EVENT%%
  410. FROM %%FLEETS%% LEFT JOIN %%FLEETS_EVENT%% on fleet_id = fleetId
  411. WHERE fleet_owner = :userId;';
  412. $db->delete($sql, array(
  413. ':userId' => $userId
  414. ));
  415. $sql = 'DELETE FROM %%MESSAGES%% WHERE message_owner = :userId;';
  416. $db->delete($sql, array(
  417. ':userId' => $userId
  418. ));
  419. $sql = 'DELETE FROM %%NOTES%% WHERE owner = :userId;';
  420. $db->delete($sql, array(
  421. ':userId' => $userId
  422. ));
  423. $sql = 'DELETE FROM %%PLANETS%% WHERE id_owner = :userId;';
  424. $db->delete($sql, array(
  425. ':userId' => $userId
  426. ));
  427. $sql = 'DELETE FROM %%USERS%% WHERE id = :userId;';
  428. $db->delete($sql, array(
  429. ':userId' => $userId
  430. ));
  431. $sql = 'DELETE FROM %%STATPOINTS%% WHERE stat_type = :type AND id_owner = :userId;';
  432. $db->delete($sql, array(
  433. ':userId' => $userId,
  434. ':type' => 1
  435. ));
  436. $fleetIds = $db->select('SELECT fleet_id FROM %%FLEETS%% WHERE fleet_target_owner = :userId;', array(
  437. ':userId' => $userId
  438. ));
  439. foreach($fleetIds as $fleetId)
  440. {
  441. FleetFunctions::SendFleetBack($userId, $fleetId);
  442. }
  443. $sql = 'UPDATE %%UNIVERSE%% SET userAmount = userAmount - 1 WHERE universe = :universe;';
  444. $db->update($sql, array(
  445. ':universe' => $userData['universe']
  446. ));
  447. Cache::get()->flush('universe');
  448. return true;
  449. }
  450. static public function deletePlanet($planetId)
  451. {
  452. $db = Database::get();
  453. $sql = 'SELECT id_owner, planet_type FROM %%PLANETS%% WHERE id = :planetId AND id NOT IN (SELECT id_planet FROM %%USERS%%);';
  454. $planetData = $db->selectSingle($sql, array(
  455. ':planetId' => $planetId
  456. ));
  457. if(empty($planetType))
  458. {
  459. return false;
  460. }
  461. $sql = 'SELECT fleet_id FROM %%FLEETS%% WHERE fleet_end_id = :planetId;';
  462. $fleetIds = $db->select($sql, array(
  463. ':planetId' => $planetId
  464. ));
  465. foreach($fleetIds as $fleetId)
  466. {
  467. FleetFunctions::SendFleetBack($planetData['id_owner'], $fleetId);
  468. }
  469. if ($planetData['planet_type'] == 3) {
  470. $sql = 'DELETE FROM %%PLANETS%% WHERE id = :planetId;';
  471. $db->delete($sql, array(
  472. ':planetId' => $planetId
  473. ));
  474. $sql = 'UPDATE %%PLANETS%% SET id_luna = :resetId WHERE id_luna = :planetId;';
  475. $db->update($sql, array(
  476. ':resetId' => 0,
  477. ':planetId' => $planetId
  478. ));
  479. } else {
  480. $sql = 'DELETE FROM %%PLANETS%% WHERE id = :planetId; OR id_luna = :planetId;';
  481. $db->delete($sql, array(
  482. ':planetId' => $planetId
  483. ));
  484. }
  485. return true;
  486. }
  487. static public function maxPlanetCount($USER)
  488. {
  489. global $resource;
  490. $config = Config::get($USER['universe']);
  491. $planetPerTech = $config->planets_tech;
  492. $planetPerBonus = $config->planets_officier;
  493. if($config->min_player_planets == 0)
  494. {
  495. $planetPerTech = 999;
  496. }
  497. if($config->min_player_planets == 0)
  498. {
  499. $planetPerBonus = 999;
  500. }
  501. // http://owiki.de/index.php/Astrophysik#.C3.9Cbersicht
  502. return (int) ceil($config->min_player_planets + min($planetPerTech, $USER[$resource[124]] * $config->planets_per_tech) + min($planetPerBonus, $USER['factor']['Planets']));
  503. }
  504. static public function allowPlanetPosition($position, $USER)
  505. {
  506. // http://owiki.de/index.php/Astrophysik#.C3.9Cbersicht
  507. global $resource;
  508. $config = Config::get($USER['universe']);
  509. switch($position) {
  510. case 1:
  511. case ($config->max_planets):
  512. return $USER[$resource[124]] >= 8;
  513. break;
  514. case 2:
  515. case ($config->max_planets-1):
  516. return $USER[$resource[124]] >= 6;
  517. break;
  518. case 3:
  519. case ($config->max_planets-2):
  520. return $USER[$resource[124]] >= 4;
  521. break;
  522. default:
  523. return $USER[$resource[124]] >= 1;
  524. break;
  525. }
  526. }
  527. static public function sendMessage($userId, $senderId, $senderName, $messageType, $subject, $text, $time, $parentID = NULL, $unread = 1, $universe = NULL)
  528. {
  529. if(is_null($universe))
  530. {
  531. $universe = Universe::current();
  532. }
  533. $db = Database::get();
  534. $sql = "INSERT INTO %%MESSAGES%% SET
  535. message_owner = :userId,
  536. message_sender = :sender,
  537. message_time = :time,
  538. message_type = :type,
  539. message_from = :from,
  540. message_subject = :subject,
  541. message_text = :text,
  542. message_unread = :unread,
  543. message_universe = :universe;";
  544. $db->insert($sql, array(
  545. ':userId' => $userId,
  546. ':sender' => $senderId,
  547. ':time' => $time,
  548. ':type' => $messageType,
  549. ':from' => $senderName,
  550. ':subject' => $subject,
  551. ':text' => $text,
  552. ':unread' => $unread,
  553. ':universe' => $universe,
  554. ));
  555. }
  556. }