PageRenderTime 41ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/izariam/controllers/actions.php

https://github.com/D3ATHLY/IZARIAM
PHP | 2052 lines | 1855 code | 66 blank | 131 comment | 321 complexity | c852051459f234ee0235fb01f81bd491 MD5 | raw file
  1. <?php
  2. /*
  3. * Project: iZariam
  4. * Edited: 25/02/2012
  5. * By: ZZJHONS
  6. * Info: zzjhons@gmail.com
  7. */
  8. require_once "game.php";
  9. /**
  10. * The controller action
  11. */
  12. class Actions extends Controller
  13. {
  14. function Actions()
  15. {
  16. parent::Controller();
  17. $this->load->model('Player_Model');
  18. if (!$this->session->userdata('login'))
  19. {
  20. $this->Player_Model->Error($this->lang->line('session_expired'));
  21. }
  22. else
  23. {
  24. // Load the user
  25. $this->Player_Model->Load_Player($this->session->userdata('id'));
  26. $this->Player_Model->Load_New_Town_Messages();
  27. $this->Player_Model->Load_New_User_To_Messages();
  28. $this->load->model('View_Model');
  29. }
  30. //$this->game = new Game;
  31. }
  32. function abolishColony()
  33. {
  34. if($this->Player_Model->town_id != $this->Player_Model->capital_id and SizeOf($this->Player_Model->missions) == 0)
  35. {
  36. $this->db->delete($this->session->userdata('universe').'_towns', array('id' => $this->Player_Model->town_id));
  37. $this->db->delete($this->session->userdata('universe').'_army', array('city' => $this->Player_Model->town_id));
  38. $this->db->delete($this->session->userdata('universe').'_town_messages', array('town' => $this->Player_Model->town_id));
  39. $this->db->delete($this->session->userdata('universe').'_town_messages', array('town' => $this->Player_Model->town_id));
  40. $this->db->set('city'.$this->Player_Model->now_town->position, 0);
  41. $this->db->where(array('id' => $this->Player_Model->now_town->island));
  42. $this->db->update($this->session->userdata('universe').'_islands');
  43. $this->db->query('UPDATE '.$this->session->userdata('universe').'_trade_routes SET `from`=0, `send_count`=0, `send_resource`=0, `send_time`=0 WHERE `from`='.$this->Player_Model->town_id);
  44. $this->db->query('UPDATE '.$this->session->userdata('universe').'_trade_routes SET `to`=0, `send_count`=0, `send_resource`=0, `send_time`=0 WHERE `to`='.$this->Player_Model->town_id);
  45. $this->db->set('town', $this->Player_Model->capital_id);
  46. $this->db->where(array('id' => $this->Player_Model->user->id));
  47. $this->db->update($this->session->userdata('universe').'_users');
  48. }
  49. else
  50. {
  51. $this->Error($this->lang->line('unable_leave_colony'));
  52. }
  53. redirect($this->config->item('base_url').'game/city/', 'refresh');
  54. }
  55. function abortBuildings($town = 0)
  56. {
  57. $id = -1;
  58. for ($i = 0; $i < SizeOf($this->Player_Model->towns); $i++)
  59. {
  60. if ($this->Player_Model->towns[$i]->id == $town) { $id = $i; }
  61. }
  62. if($id >= 0 and $this->Player_Model->towns[$id]->build_line != '')
  63. {
  64. $this->Player_Model->now_town->build_line = '';
  65. $this->Player_Model->now_town->build_start = 0;
  66. $this->db->set('build_line', '');
  67. $this->db->set('build_start', 0);
  68. $this->db->where(array('id' => $this->Player_Model->town_id));
  69. $this->db->update($this->session->userdata('universe').'_towns');
  70. }
  71. $this->show('city', $town);
  72. }
  73. function abortFleet($mission = 0, $position = 0, $redirect = 'port')
  74. {
  75. if (isset($this->Player_Model->missions[$mission]))
  76. {
  77. if ($this->Player_Model->missions[$mission]->mission_start == 0)
  78. {
  79. // colonization
  80. // If you have not loaded remove the town and return resources
  81. if($this->Player_Model->missions[$mission]->mission_type == 1)
  82. {
  83. // remove city
  84. $town_query = $this->db->get_where($this->session->userdata('universe').'_towns', array('id' => $this->Player_Model->missions[$mission]->to));
  85. $town = $town_query->row();
  86. $this->db->set('city'.$town->position, 0);
  87. $this->db->where(array('id' => $town->island));
  88. $this->db->update($this->session->userdata('universe').'_islands');
  89. $this->db->query('DELETE FROM '.$this->session->userdata('universe').'_towns where `id`="'.$this->Player_Model->missions[$mission]->to.'"');
  90. }
  91. // return resources
  92. $this->db->set('wood', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->wood + $this->Player_Model->missions[$mission]->wood);
  93. $this->db->set('wine', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->wine + $this->Player_Model->missions[$mission]->wine);
  94. $this->db->set('marble', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->marble + $this->Player_Model->missions[$mission]->marble);
  95. $this->db->set('crystal', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->crystal + $this->Player_Model->missions[$mission]->crystal);
  96. $this->db->set('sulfur', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->sulfur + $this->Player_Model->missions[$mission]->sulfur);
  97. $this->db->set('peoples', $this->Player_Model->towns[$this->Player_Model->missions[$mission]->from]->peoples + $this->Player_Model->missions[$mission]->peoples);
  98. $this->db->where(array('id' => $this->Player_Model->missions[$mission]->from));
  99. $this->db->update($this->session->userdata('universe').'_towns');
  100. $this->db->set('gold', $this->Player_Model->user->gold + $this->Player_Model->missions[$mission]->gold);
  101. $this->db->set('transports', $this->Player_Model->user->transports + $this->Player_Model->missions[$mission]->ship_transport);
  102. $this->db->where(array('id' => $this->Player_Model->user->id));
  103. $this->db->update($this->session->userdata('universe').'_users');
  104. $this->db->query('DELETE FROM '.$this->session->userdata('universe').'_missions where `id`="'.$this->Player_Model->missions[$mission]->id.'"');
  105. }
  106. else
  107. {
  108. // If you just loaded, expand
  109. if($this->Player_Model->missions[$mission]->return_start == 0)
  110. {
  111. $cost = $this->Data_Model->army_cost_by_type(23, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  112. $x1 = $this->Data_Model->temp_islands_db[$this->Data_Model->temp_towns_db[$this->Player_Model->missions[$mission]->from]->island]->x;
  113. $x2 = $this->Data_Model->temp_islands_db[$this->Data_Model->temp_towns_db[$this->Player_Model->missions[$mission]->to]->island]->x;
  114. $y1 = $this->Data_Model->temp_islands_db[$this->Data_Model->temp_towns_db[$this->Player_Model->missions[$mission]->from]->island]->y;
  115. $y2 = $this->Data_Model->temp_islands_db[$this->Data_Model->temp_towns_db[$this->Player_Model->missions[$mission]->to]->island]->y;
  116. $time = $this->Data_Model->time_by_coords($x1,$x2,$y1,$y2,$cost['speed']);
  117. $elapsed = time() - $this->Player_Model->missions[$mission]->mission_start;
  118. $ostalos = ($time - $elapsed >= 0) ? $time - $elapsed : 0;
  119. $return_time = $time - $elapsed;
  120. $this->Player_Model->missions[$mission]->percent = 1 - $return_time/$time;
  121. $this->db->set('percent', $this->Player_Model->missions[$mission]->percent);
  122. $this->db->set('return_start', time());
  123. $this->db->where(array('id' => $this->Player_Model->missions[$mission]->id));
  124. $this->db->update($this->session->userdata('universe').'_missions');
  125. }
  126. }
  127. }
  128. redirect($this->config->item('base_url').'game/'.$redirect.'/'.$position.'/', 'refresh');
  129. }
  130. function abortShips($position = 0)
  131. {
  132. if($this->Player_Model->armys[$this->Player_Model->town_id]->ships_line != '')
  133. {
  134. // update the army
  135. $this->Player_Model->armys[$this->Player_Model->town_id]->ships_line = '';
  136. $this->Player_Model->armys[$this->Player_Model->town_id]->ships_start = 0;
  137. $this->Player_Model->ships_line[$this->Player_Model->town_id] = $this->Data_Model->load_army_line($this->Player_Model->armys[$this->Player_Model->town_id]->army_line);
  138. $this->db->set('ships_line', '');
  139. $this->db->set('ships_start', 0);
  140. $this->db->where(array('city' => $this->Player_Model->town_id));
  141. $this->db->update($this->session->userdata('universe').'_army');
  142. }
  143. $this->show('shipyard', $position);
  144. }
  145. function abortUnits($position = 0)
  146. {
  147. if($this->Player_Model->armys[$this->Player_Model->town_id]->army_line != '')
  148. {
  149. // update the army
  150. $this->Player_Model->armys[$this->Player_Model->town_id]->army_line = '';
  151. $this->Player_Model->armys[$this->Player_Model->town_id]->army_start = 0;
  152. $this->Player_Model->army_line[$this->Player_Model->town_id] = $this->Data_Model->load_army_line($this->Player_Model->armys[$this->Player_Model->town_id]->army_line);
  153. $this->db->set('army_line', '');
  154. $this->db->set('army_start', 0);
  155. $this->db->where(array('city' => $this->Player_Model->town_id));
  156. $this->db->update($this->session->userdata('universe').'_army');
  157. }
  158. $this->show('barracks', $position);
  159. }
  160. function army($id = 0)
  161. {
  162. $position = $this->Data_Model->get_position(5, $this->Player_Model->now_town);
  163. if (($this->Player_Model->now_town->build_line == '' or $this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] != 5) and
  164. (strlen($this->Player_Model->armys[$this->Player_Model->town_id]->army_line) <= $this->config->item('army_queue_size')*4) )
  165. {
  166. if ($position > 0 and $position == $id)
  167. {
  168. $all_cost['wood'] = 0;
  169. $all_cost['wine'] = 0;
  170. $all_cost['crystal'] = 0;
  171. $all_cost['sulfur'] = 0;
  172. $all_cost['peoples'] = 0;
  173. //$all_cost['gold'] = 0;
  174. $army_line = $this->Player_Model->armys[$this->Player_Model->town_id]->army_line;
  175. $army_start = ($this->Player_Model->armys[$this->Player_Model->town_id]->army_start > 0) ? $this->Player_Model->armys[$this->Player_Model->town_id]->army_start : time();
  176. // Process the data
  177. for ($i = 1; $i <= 14; $i++)
  178. {
  179. $class = $this->Data_Model->army_class_by_type($i);
  180. $$class = (isset($_POST[$i])) ? floor($_POST[$i]) : 0 ;
  181. $cost = $this->Data_Model->army_cost_by_type($i, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  182. $all_cost['wood'] = $all_cost['wood'] + $cost['wood']*$$class;
  183. $all_cost['wine'] = $all_cost['wine'] + $cost['wine']*$$class;
  184. $all_cost['crystal'] = $all_cost['crystal'] + $cost['crystal']*$$class;
  185. $all_cost['sulfur'] = $all_cost['sulfur'] + $cost['sulfur']*$$class;
  186. $all_cost['peoples'] = $all_cost['peoples'] + $cost['peoples']*$$class;
  187. if ($$class > 0)
  188. {
  189. if ($army_line != '')
  190. {
  191. $army_line .= ';';
  192. }
  193. $army_line .= $i.','.$$class;
  194. }
  195. }
  196. // Calculate the remainder
  197. $wood = $this->Player_Model->now_town->wood - $all_cost['wood'];
  198. $wine = $this->Player_Model->now_town->wine - $all_cost['wine'];
  199. $crystal = $this->Player_Model->now_town->crystal - $all_cost['crystal'];
  200. $sulfur = $this->Player_Model->now_town->sulfur - $all_cost['sulfur'];
  201. $peoples = $this->Player_Model->now_town->peoples - $all_cost['peoples'];
  202. // If you have enough resources
  203. if ($wood >= 0 and $wine >= 0 and $crystal >= 0 and $sulfur >= 0 and $peoples >= 0)
  204. {
  205. // update the city
  206. $this->Player_Model->now_town->wood = $wood;
  207. $this->Player_Model->now_town->wine = $wine;
  208. $this->Player_Model->now_town->crystal = $crystal;
  209. $this->Player_Model->now_town->sulfur = $sulfur;
  210. $this->Player_Model->now_town->peoples = $peoples;
  211. $this->db->set('wood', $wood);
  212. $this->db->set('wine', $wine);
  213. $this->db->set('crystal', $crystal);
  214. $this->db->set('sulfur', $sulfur);
  215. $this->db->set('peoples', $peoples);
  216. $this->db->where(array('id' => $this->Player_Model->town_id));
  217. $this->db->update($this->session->userdata('universe').'_towns');
  218. // update the army
  219. $this->Player_Model->armys[$this->Player_Model->town_id]->army_line = $army_line;
  220. $this->Player_Model->armys[$this->Player_Model->town_id]->army_start = $army_start;
  221. $this->Player_Model->army_line[$this->Player_Model->town_id] = $this->Data_Model->load_army_line($this->Player_Model->armys[$this->Player_Model->town_id]->army_line);
  222. $this->db->set('army_line', $army_line);
  223. $this->db->set('army_start', $army_start);
  224. $this->db->where(array('city' => $this->Player_Model->town_id));
  225. $this->db->update($this->session->userdata('universe').'_army');
  226. // Training - employment lancers
  227. if ($this->Player_Model->user->tutorial <= 10)
  228. {
  229. $this->tutorials('set', 11);
  230. }
  231. }
  232. }
  233. }
  234. $this->show('barracks',$id);
  235. }
  236. function armyEdit($type = '')
  237. {
  238. $peoples_army = 0;
  239. for ($i = 1; $i <= 22; $i++)
  240. {
  241. $class = $this->Data_Model->army_class_by_type($i);
  242. $$class = (isset($_POST[$i])) ? floor($_POST[$i]) : 0 ;
  243. $cost = $this->Data_Model->army_cost_by_type($i, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  244. if ($this->Player_Model->armys[$this->Player_Model->town_id]->$class >= $$class)
  245. {
  246. $peoples_army = $peoples_army + ($$class*$cost['peoples']);
  247. $this->Player_Model->armys[$this->Player_Model->town_id]->$class = $this->Player_Model->armys[$this->Player_Model->town_id]->$class - $$class;
  248. }
  249. $this->db->set($class, $this->Player_Model->armys[$this->Player_Model->town_id]->$class);
  250. }
  251. $this->db->where(array('city' => $this->Player_Model->town_id));
  252. $this->db->update($this->session->userdata('universe').'_army');
  253. $this->Player_Model->now_town->peoples = $this->Player_Model->now_town->peoples + $peoples_army;
  254. // update population
  255. $this->db->set('peoples', $this->Player_Model->now_town->peoples);
  256. $this->db->where(array('id' => $this->Player_Model->town_id));
  257. $this->db->update($this->session->userdata('universe').'_towns');
  258. $this->show($type);
  259. }
  260. function branchOffice($position)
  261. {
  262. if (isset($_POST['type']) and isset($_POST['searchResource']))
  263. {
  264. $type = ($_POST['type'] <= 1) ? floor($_POST['type']) : 0;
  265. switch($_POST['searchResource'])
  266. {
  267. case 1: $resource = 1; break;
  268. case 2: $resource = 2; break;
  269. case 3: $resource = 3; break;
  270. case 4: $resource = 4; break;
  271. default: $resource = 0; break;
  272. }
  273. $this->Player_Model->now_town->branch_search_type = $type;
  274. $this->Player_Model->now_town->branch_search_resource = $resource;
  275. $this->Player_Model->now_town->branch_search_radius = floor($_POST['range']);
  276. $this->db->set('branch_search_type', $type);
  277. $this->db->set('branch_search_resource', $resource);
  278. $this->db->set('branch_search_radius', floor($_POST['range']));
  279. $this->db->where(array('id' => $this->Player_Model->town_id));
  280. $this->db->update($this->session->userdata('universe').'_towns');
  281. }
  282. if(isset($_POST['resource']) and isset($_POST['tradegood1']) and isset($_POST['tradegood2']) and isset($_POST['tradegood3']) and isset($_POST['tradegood4']))
  283. {
  284. $wood_count = floor($_POST['resource']);
  285. $wine_count = floor($_POST['tradegood1']);
  286. $marble_count = floor($_POST['tradegood2']);
  287. $crystal_count = floor($_POST['tradegood3']);
  288. $sulfur_count = floor($_POST['tradegood4']);
  289. $gold_wood = floor($_POST['resourcePrice']);
  290. $gold_wine = floor($_POST['tradegood1Price']);
  291. $gold_marble = floor($_POST['tradegood2Price']);
  292. $gold_crystal = floor($_POST['tradegood3Price']);
  293. $gold_sulfur = floor($_POST['tradegood4Price']);
  294. $capacity = $this->Data_Model->branchOffice_capacity_by_level($this->Player_Model->levels[$this->Player_Model->town_id][12]);
  295. $all_trade_resources = 0;
  296. if($_POST['resourceTradeType'] == 1){ $all_trade_resources = $all_trade_resources + $wood_count; }
  297. if($_POST['tradegood1TradeType'] == 1){ $all_trade_resources = $all_trade_resources + $wine_count; }
  298. if($_POST['tradegood2TradeType'] == 1){ $all_trade_resources = $all_trade_resources + $marble_count; }
  299. if($_POST['tradegood3TradeType'] == 1){ $all_trade_resources = $all_trade_resources + $crystal_count; }
  300. if($_POST['tradegood4TradeType'] == 1){ $all_trade_resources = $all_trade_resources + $sulfur_count; }
  301. for($i = 0; $i <= 4; $i++)
  302. {
  303. $resource_name = $this->Data_Model->resource_class_by_type($i);
  304. $branch_type = 'branch_trade_'.$resource_name.'_type';
  305. $branch_count = 'branch_trade_'.$resource_name.'_count';
  306. $branch_cost = 'branch_trade_'.$resource_name.'_cost';
  307. switch($i)
  308. {
  309. case 1: $post_name = 'tradegood1'; $count = $wine_count; $gold = $gold_wine; break;
  310. case 2: $post_name = 'tradegood2'; $count = $marble_count; $gold = $gold_marble; break;
  311. case 3: $post_name = 'tradegood3'; $count = $crystal_count; $gold = $gold_crystal; break;
  312. case 4: $post_name = 'tradegood4'; $count = $sulfur_count; $gold = $gold_sulfur; break;
  313. default: $post_name = 'resource'; $count = $wood_count; $gold = $gold_wood; break;
  314. }
  315. if ($count == 0){ $gold = 0; }
  316. if ($gold == 0){ $count = 0; }
  317. if ($_POST[$post_name.'TradeType'] == 1 and $all_trade_resources <= $capacity)
  318. {
  319. if($this->Player_Model->now_town->$branch_type == 1){
  320. $this->Player_Model->now_town->$resource_name = $this->Player_Model->now_town->$resource_name + $this->Player_Model->now_town->$branch_count - $count;
  321. }else{
  322. $this->Player_Model->user->gold = $this->Player_Model->user->gold + ($this->Player_Model->now_town->$branch_count*$this->Player_Model->now_town->$branch_cost);
  323. $this->Player_Model->now_town->$resource_name = $this->Player_Model->now_town->$resource_name - $count;
  324. }
  325. $this->Player_Model->now_town->$branch_type = floor($_POST[$post_name.'TradeType']);
  326. $this->Player_Model->now_town->$branch_count = $count;
  327. $this->Player_Model->now_town->$branch_cost = $gold;
  328. }
  329. elseif ($_POST[$post_name.'TradeType'] == 0)
  330. {
  331. if($this->Player_Model->now_town->$branch_type == 0){
  332. $this->Player_Model->user->gold = $this->Player_Model->user->gold + ($this->Player_Model->now_town->$branch_count*$this->Player_Model->now_town->$branch_cost) - ($gold*$count);
  333. }
  334. else
  335. {
  336. $this->Player_Model->now_town->$resource_name = $this->Player_Model->now_town->$resource_name + $this->Player_Model->now_town->$branch_count;
  337. $this->Player_Model->user->gold = $this->Player_Model->user->gold - ($gold*$count);
  338. }
  339. $this->Player_Model->now_town->$branch_type = floor($_POST[$post_name.'TradeType']);
  340. $this->Player_Model->now_town->$branch_count = $count;
  341. $this->Player_Model->now_town->$branch_cost = $gold;
  342. }
  343. }
  344. if($this->Player_Model->user->gold >= 0 and
  345. $this->Player_Model->now_town->wood >= 0 and
  346. $this->Player_Model->now_town->wine >= 0 and
  347. $this->Player_Model->now_town->marble >= 0 and
  348. $this->Player_Model->now_town->crystal >= 0 and
  349. $this->Player_Model->now_town->sulfur >= 0)
  350. {
  351. for($i = 0; $i <= 4; $i++)
  352. {
  353. $resource_name = $this->Data_Model->resource_class_by_type($i);
  354. $branch_type = 'branch_trade_'.$resource_name.'_type';
  355. $branch_count = 'branch_trade_'.$resource_name.'_count';
  356. $branch_cost = 'branch_trade_'.$resource_name.'_cost';
  357. $this->db->set($resource_name, $this->Player_Model->now_town->$resource_name);
  358. $this->db->set($branch_type, $this->Player_Model->now_town->$branch_type);
  359. $this->db->set($branch_count, $this->Player_Model->now_town->$branch_count);
  360. $this->db->set($branch_cost, $this->Player_Model->now_town->$branch_cost);
  361. }
  362. $this->db->where(array('id' => $this->Player_Model->town_id));
  363. $this->db->update($this->session->userdata('universe').'_towns');
  364. $this->db->set('gold', $this->Player_Model->user->gold);
  365. $this->db->where(array('id' => $this->Player_Model->user->id));
  366. $this->db->update($this->session->userdata('universe').'_users');
  367. }
  368. }
  369. $this->show('branchOffice', $position);
  370. }
  371. /**
  372. * Construction of buildings
  373. * @param <int> $position
  374. * @param <int> $id
  375. * @param <bool> $redirect
  376. */
  377. function build($position, $id, $redirect = 'city')
  378. {
  379. $id = floor($id);
  380. $position = floor($position);
  381. $redirect = strip_tags($redirect);
  382. $level_text = 'pos'.$position.'_level';
  383. $type_text = 'pos'.$position.'_type';
  384. $level = $this->Player_Model->now_town->$level_text;
  385. $type = $this->Player_Model->now_town->$type_text;
  386. $class = $this->Data_Model->building_class_by_type($id);
  387. $already_position = $this->Data_Model->get_position($id, $this->Player_Model->now_town);
  388. if ((($already_position == 0 or $already_position == $position) or $id == 6) and
  389. $class != 'buildingGround' and
  390. ($id != 4 or ($id == 4 and $this->Player_Model->armys[$this->Player_Model->town_id]->ships_line == '')) and
  391. ($id != 5 or ($id == 5 and $this->Player_Model->armys[$this->Player_Model->town_id]->army_line == '')) and
  392. ($id != 13 or $this->Player_Model->research->res2_13 > 0) and
  393. ($id != 14 or ($id == 14 and $this->Player_Model->now_town->spyes_start == 0)) and
  394. ($type == 0 or $type == $id) and
  395. (SizeOf($this->Player_Model->build_line[$this->Player_Model->town_id]) <= $this->config->item('town_queue_size')) and (SizeOf($this->Player_Model->build_line[$this->Player_Model->town_id]) > 0 and $this->Player_Model->user->premium_account > 0) or SizeOf($this->Player_Model->build_line[$this->Player_Model->town_id]) == 0)
  396. {
  397. // Get the price
  398. $cost = $this->Data_Model->building_cost($id, $level, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  399. // Count the remainder
  400. $wood = $this->Player_Model->now_town->wood - $cost['wood'];
  401. $wine = $this->Player_Model->now_town->wine - $cost['wine'];
  402. $marble = $this->Player_Model->now_town->marble - $cost['marble'];
  403. $crystal = $this->Player_Model->now_town->crystal - $cost['crystal'];
  404. $sulfur = $this->Player_Model->now_town->sulfur - $cost['sulfur'];
  405. // If an acceptable balance
  406. if ($wood >= 0 and $wine >= 0 and $marble >= 0 and $crystal >= 0 and $sulfur >= 0)
  407. {
  408. if ($this->Player_Model->now_town->build_line == '')
  409. {
  410. // Update the resources in the database and model
  411. $this->Player_Model->now_town->wood = $wood;
  412. $this->Player_Model->now_town->wine = $wine;
  413. $this->Player_Model->now_town->marble = $marble;
  414. $this->Player_Model->now_town->crystal = $crystal;
  415. $this->Player_Model->now_town->sulfur = $sulfur;
  416. $this->db->set('wood', $wood);
  417. $this->db->set('wine', $wine);
  418. $this->db->set('marble', $marble);
  419. $this->db->set('crystal', $crystal);
  420. $this->db->set('sulfur', $sulfur);
  421. }
  422. // A string of text just like in the database
  423. if ($this->Player_Model->now_town->build_line != '')
  424. {
  425. $this->db->set('build_line', $this->Player_Model->now_town->build_line.';'.$position.','.$id);
  426. $this->Player_Model->now_town->build_line = $this->Player_Model->now_town->build_line.';'.$position.','.$id;
  427. }
  428. else
  429. {
  430. $this->db->set('build_line', $position.','.$id);
  431. $this->Player_Model->now_town->build_line = $position.','.$id;
  432. }
  433. // Set the start time if no
  434. if ($this->Player_Model->now_town->build_start == 0)
  435. {
  436. $this->db->set('build_start', time());
  437. $this->Player_Model->now_town->build_start = time();
  438. }
  439. $this->db->where(array('id' => $this->Player_Model->town_id));
  440. $this->db->update($this->session->userdata('universe').'_towns');
  441. // The building is added to the queue
  442. // Update the training if there is
  443. if ($id == 3 and $this->Player_Model->user->tutorial <= 4)
  444. {
  445. // built the academy
  446. $this->tutorials('set', 5);
  447. }
  448. if ($id == 5 and $this->Player_Model->user->tutorial <= 8)
  449. {
  450. // built barracks
  451. $this->tutorials('set', 9);
  452. }
  453. if ($id == 7 and $this->Player_Model->user->tutorial <= 11)
  454. {
  455. // built a wall
  456. $this->tutorials('set', 12);
  457. }
  458. if ($id == 2 and $this->Player_Model->user->tutorial <= 13)
  459. {
  460. // built a port
  461. $this->tutorials('set', 14);
  462. }
  463. if ($level > 0 and $this->Player_Model->user->tutorial <= 15)
  464. {
  465. // Upgrading the building
  466. $this->tutorials('set', 16);
  467. }
  468. $this->Player_Model->correct_buildings();
  469. $this->show($redirect, $position);
  470. }
  471. else
  472. {
  473. $this->Error($this->lang->line('construction_enough_resources'));
  474. }
  475. }
  476. else
  477. {
  478. $this->show($redirect, $position);
  479. }
  480. }
  481. function changeCapital($town = 0)
  482. {
  483. if($town > 0 and isset($this->Player_Model->towns[$town]) and $town != $this->Player_Model->capital_id)
  484. {
  485. $palace_position = $this->Data_Model->get_position(10, $this->Player_Model->towns[$this->Player_Model->capital_id]);
  486. $colony_position = $this->Data_Model->get_position(15, $this->Player_Model->towns[$town]);
  487. if ($palace_position > 0 and $colony_position > 0)
  488. {
  489. $building_line = array();
  490. if (SizeOf($this->Player_Model->build_line[$town]) > 1)
  491. for ($i = 0; $i < SizeOf($this->Player_Model->build_line[$town]); $i++)
  492. {
  493. if ($this->Player_Model->build_line[$town][$i]['type'] != 10)
  494. {
  495. $building = array($this->Player_Model->build_line[$town][$i]['position'], $this->Player_Model->build_line[$town][$i]['type']);
  496. $building_line[] = implode(",", $building);
  497. }
  498. }
  499. $buildings_line = implode(";", $building_line);
  500. $this->db->set('build_line', $buildings_line);
  501. $this->db->set('pos'.$colony_position.'_type', 10);
  502. $this->db->where(array('id' => $town));
  503. $this->db->update($this->session->userdata('universe').'_towns');
  504. $this->db->set('pos'.$palace_position.'_type', 0);
  505. $this->db->set('pos'.$palace_position.'_level', 0);
  506. $this->db->where(array('id' => $this->Player_Model->capital_id));
  507. $this->db->update($this->session->userdata('universe').'_towns');
  508. $this->db->set('capital', $town);
  509. $this->db->where(array('id' => $this->Player_Model->user->id));
  510. $this->db->update($this->session->userdata('universe').'_users');
  511. redirect($this->config->item('base_url').'game/palace/', 'refresh');
  512. }
  513. else
  514. {
  515. redirect($this->config->item('base_url').'game/palaceColony/', 'refresh');
  516. }
  517. }
  518. else
  519. {
  520. redirect($this->config->item('base_url').'game/palaceColony/', 'refresh');
  521. }
  522. }
  523. function colonize($id = 0, $position = -1)
  524. {
  525. $id = floor($id);
  526. $position = floor($position);
  527. if ($this->Player_Model->now_town->actions > 0)
  528. {
  529. if ($id > 0 and $position >= 0)
  530. {
  531. $this->load->model('Island_Model');
  532. $this->Island_Model->Load_Island($id);
  533. if(isset($_POST['action']))
  534. {
  535. $city_text = 'city'.$position;
  536. if($_POST['action'] == $this->lang->line('move') and isset($this->Player_Model->towns[$_POST['cityId']]) and $this->Player_Model->user->ambrosy >= 200)
  537. {
  538. $now_position = -1;
  539. for ($i = 0; $i <= 15; $i++)
  540. {
  541. $city_now = 'city'.$i;
  542. if ($this->Player_Model->islands[$this->Player_Model->towns[$_POST['cityId']]->island]->$city_now == $_POST['cityId']){$now_position = $i;}
  543. }
  544. if($this->Island_Model->island->$city_text == 0 and $now_position >= 0)
  545. {
  546. // Remove the old mark
  547. $city_text = 'city'.$now_position;
  548. $this->Player_Model->towns[$_POST['cityId']]->$city_text = 0;
  549. $this->db->set('city'.$now_position, 0);
  550. $this->db->where(array('id' => $this->Player_Model->towns[$_POST['cityId']]->island));
  551. $this->db->update($this->session->userdata('universe').'_islands');
  552. // Writing a new mark
  553. $city_text = 'city'.$position;
  554. $this->Player_Model->towns[$_POST['cityId']]->$city_text = $_POST['cityId'];
  555. $this->db->set('city'.$position, $_POST['cityId']);
  556. $this->db->where(array('id' => $id));
  557. $this->db->update($this->session->userdata('universe').'_islands');
  558. // Writing a new island in the city
  559. $this->Player_Model->towns[$_POST['cityId']]->island = $id;
  560. $this->Player_Model->towns[$_POST['cityId']]->position = $position;
  561. $this->db->set('island', $id);
  562. $this->db->set('position', $position);
  563. $this->db->where(array('id' => $_POST['cityId']));
  564. $this->db->update($this->session->userdata('universe').'_towns');
  565. // Taking the ambrosia
  566. $this->Player_Model->user->ambrosy = $this->Player_Model->user->ambrosy - 200;
  567. $this->db->set('ambrosy', $this->Player_Model->user->ambrosy);
  568. $this->db->where(array('id' => $this->Player_Model->user->id));
  569. $this->db->update($this->session->userdata('universe').'_users');
  570. }
  571. }
  572. else
  573. {
  574. if(!isset($_POST['cityId']))
  575. {
  576. if(SizeOf($this->Player_Model->towns)-1 < $this->Player_Model->levels[$this->Player_Model->capital_id][10])
  577. {
  578. $sendresource = floor($_POST['sendresource']);
  579. $sendwine = floor($_POST['sendwine']);
  580. $sendmarble = floor($_POST['sendmarble']);
  581. $sendcrystal = floor($_POST['sendcrystal']);
  582. $sendsulfur = floor($_POST['sendsulfur']);
  583. $transporters = floor($_POST['transporters']);
  584. $wood = $this->Player_Model->now_town->wood - $sendresource - 1250;
  585. $wine = $this->Player_Model->now_town->wine - $sendwine;
  586. $marble = $this->Player_Model->now_town->marble - $sendmarble;
  587. $crystal = $this->Player_Model->now_town->crystal - $sendcrystal;
  588. $sulfur = $this->Player_Model->now_town->sulfur - $sendsulfur;
  589. $peoples = $this->Player_Model->now_town->peoples - 40;
  590. $gold = $this->Player_Model->user->gold - 9000;
  591. $transports = $this->Player_Model->user->transports - $transporters;
  592. if(($this->Player_Model->user->transports >= $transporters) and ($wood >= 0) and ($wine >= 0) and ($crystal >= 0) and ($sulfur >= 0) and ($peoples >= 0) and ($gold >= 0) and ($transports >= 0) and ($transporters*$this->config->item('transport_capacity') >= $sendresource + $sendwine + $sendmarble + $sendcrystal + $sendsulfur + 1250 + 40))
  593. {
  594. if($this->Island_Model->island->$city_text == 0)
  595. {
  596. // add a city
  597. $this->db->insert($this->session->userdata('universe').'_towns', array('user' => $this->Player_Model->user->id, 'island' => $this->Island_Model->island->id, 'position' => $position, 'pos0_level' => 0));
  598. // We find a city in the database
  599. $town_query = $this->db->get_where($this->session->userdata('universe').'_towns', array('island' => $this->Island_Model->island->id, 'position' => $position));
  600. $town = $town_query->row();
  601. // update the island
  602. $city_text = 'city'.$position;
  603. $this->Island_Model->island->$city_text = $town->id;
  604. $this->db->set('city'.$position, $town->id);
  605. $this->db->where(array('id' => $this->Island_Model->island->id));
  606. $this->db->update($this->session->userdata('universe').'_islands');
  607. // subtract the resources
  608. $this->Player_Model->now_town->wood = $wood;
  609. $this->Player_Model->now_town->wine = $wine;
  610. $this->Player_Model->now_town->marble = $marble;
  611. $this->Player_Model->now_town->crystal = $crystal;
  612. $this->Player_Model->now_town->sulfur = $sulfur;
  613. $this->Player_Model->now_town->peoples = $peoples;
  614. $this->Player_Model->now_town->actions = $this->Player_Model->now_town->actions - 1;
  615. $this->db->set('wood', $wood);
  616. $this->db->set('wine', $wine);
  617. $this->db->set('marble', $marble);
  618. $this->db->set('crystal', $crystal);
  619. $this->db->set('sulfur', $sulfur);
  620. $this->db->set('peoples', $peoples);
  621. $this->db->set('actions', $this->Player_Model->now_town->actions);
  622. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  623. $this->db->update($this->session->userdata('universe').'_towns');
  624. $this->Player_Model->user->gold = $gold;
  625. $this->Player_Model->user->transports = $transports;
  626. $this->db->set('gold', $gold);
  627. $this->db->set('transports', $transports);
  628. $this->db->where(array('id' => $this->Player_Model->user->id));
  629. $this->db->update($this->session->userdata('universe').'_users');
  630. // add a mission
  631. $this->db->insert($this->session->userdata('universe').'_missions', array('user' => $this->Player_Model->user->id, 'from' => $this->Player_Model->now_town->id, 'to' => $town->id, 'loading_from_start' => time(), 'mission_type' => 1, 'wood' => $sendresource+1250, 'wine' => $sendwine, 'marble' => $sendmarble, 'crystal' => $sendcrystal, 'sulfur' => $sendsulfur, 'gold' => 9000, 'peoples' => 40, 'ship_transport' => $transporters));
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. $this->show('island', $id, $position);
  640. }
  641. else
  642. {
  643. $this->Error($this->lang->line('enough_action_points'));
  644. }
  645. }
  646. /**
  647. * Lowering the level of the building
  648. * @param <int> $position
  649. */
  650. function demolition($position)
  651. {
  652. $position = floor($position);
  653. if ($position > 0 or $this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] == 1)
  654. {
  655. $level_text = 'pos'.$position.'_level';
  656. $type_text = 'pos'.$position.'_type';
  657. // The level of the building
  658. $level = $this->Player_Model->now_town->$level_text;
  659. $type = $this->Player_Model->now_town->$type_text;
  660. if ($type == 0){ $type = $this->Player_Model->build_line[$this->Player_Model->town_id][0]['type']; }
  661. // Get the price
  662. if ($this->Player_Model->now_town->build_line != '' and $this->Player_Model->build_line[$this->Player_Model->town_id][0]['position'] == $position)
  663. {
  664. $cost = $this->Data_Model->building_cost($type,$level, $this->Player_Model->research,$this->Player_Model->levels[$this->Player_Model->town_id]);
  665. }
  666. else
  667. {
  668. $cost = $this->Data_Model->building_cost($type,$level-1,$this->Player_Model->research,$this->Player_Model->levels[$this->Player_Model->town_id]);
  669. $level = ($level > 0) ? $level - 1 : $level;
  670. }
  671. // If it's zero out Academy of Scientists
  672. if ($type == 3 and $level == 0)
  673. {
  674. $this->Player_Model->now_town->peoples = $this->Player_Model->now_town->peoples + $this->Player_Model->now_town->scientists;
  675. $this->Player_Model->now_town->scientists = 0;
  676. $this->db->set('peoples', $this->Player_Model->now_town->peoples);
  677. $this->db->set('scientists', $this->Player_Model->now_town->scientists);
  678. }
  679. // Add 90% of resources
  680. $wood = $this->Player_Model->now_town->wood + ($cost['wood']*0.9);
  681. $wine = $this->Player_Model->now_town->wine + ($cost['wine']*0.9);
  682. $marble = $this->Player_Model->now_town->marble + ($cost['marble']*0.9);
  683. $crystal = $this->Player_Model->now_town->crystal + ($cost['crystal']*0.9);
  684. $sulfur = $this->Player_Model->now_town->sulfur + ($cost['sulfur']*0.9);
  685. $points = ($cost['wood'] + $cost['wine'] + $cost['marble'] + $cost['crystal'] + $cost['sulfur'])*0.01;
  686. // If there is a queue, and the building it
  687. if ($this->Player_Model->now_town->build_line != '' and $this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] == $type)
  688. {
  689. // Remove the building from the queue
  690. if (sizeof($this->Player_Model->build_line[$this->Player_Model->town_id]) > 1)
  691. {
  692. if ($this->Player_Model->build_line[$this->Player_Model->town_id][0]['position'] < 10)
  693. {
  694. $build_line = ($this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] < 10) ? substr($this->Player_Model->now_town->build_line, 4) : substr($this->Player_Model->now_town->build_line, 5);
  695. }
  696. else
  697. {
  698. $build_line = ($this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] < 10) ? substr($this->Player_Model->now_town->build_line, 5) : substr($this->Player_Model->now_town->build_line, 6);
  699. }
  700. $build_start = $this->Player_Model->now_town->build_start;
  701. }
  702. else
  703. {
  704. $build_line = '';
  705. $build_start = 0;
  706. }
  707. // If you still have a place
  708. if ($build_line != '')
  709. {
  710. $do = true;
  711. while ($do)
  712. {
  713. // The cost of trail. building
  714. $buildings = $this->Data_Model->load_build_line($build_line);
  715. $level_text = 'pos'.$buildings[0]['position'].'_level';
  716. $type_text = 'pos'.$buildings[0]['position'].'_type';
  717. $type = $this->Player_Model->now_town->$type_text;
  718. $next_level = $this->Player_Model->now_town->$level_text;
  719. $cost = $this->Data_Model->building_cost($type, $next_level, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  720. // If you have enough resources
  721. if (($wood - $cost['wood']) >= 0 and ($wine - $cost['wine']) >= 0 and ($marble - $cost['marble']) >= 0 and ($crystal - $cost['crystal']) >= 0 and ($sulfur - $cost['sulfur']) >= 0)
  722. {
  723. $wood = $wood - $cost['wood'];
  724. $wine = $wine - $cost['wine'];
  725. $marble = $marble - $cost['marble'];
  726. $crystal = $crystal - $cost['crystal'];
  727. $sulfur = $sulfur - $cost['sulfur'];
  728. $do = false;
  729. break;
  730. }
  731. else
  732. {
  733. // If there is no decrease of all
  734. if ($buildings[0]['position'] < 10)
  735. {
  736. $build_line = ($type < 10) ? substr($build_line, 4) : substr($build_line, 5);
  737. }
  738. else
  739. {
  740. $build_line = ($type < 10) ? substr($build_line, 5) : substr($build_line, 6);
  741. }
  742. }
  743. }
  744. }
  745. // Data validation, not to write too much into the database
  746. if (strlen($build_line) < 3){ $build_line = ''; }
  747. if ($build_line == ''){ $build_start = 0; }
  748. // Write to the database
  749. $this->Player_Model->now_town->build_line = $build_line;
  750. $this->Player_Model->now_town->build_start = $build_start;
  751. $this->db->set('build_line', $build_line);
  752. $this->db->set('build_start', $build_start);
  753. }
  754. // If the level is not, then we carry the card
  755. if ($level <= 0){ $this->Player_Model->now_town->$type_text = 0; }
  756. // Write to the database
  757. $level_text = 'pos'.$position.'_level';
  758. $this->Player_Model->now_town->$level_text = $level;
  759. $this->Player_Model->now_town->wood = $wood;
  760. $this->Player_Model->now_town->wine = $wine;
  761. $this->Player_Model->now_town->marble = $marble;
  762. $this->Player_Model->now_town->crystal = $crystal;
  763. $this->Player_Model->now_town->sulfur = $sulfur;
  764. $this->db->set('pos'.$position.'_level', $level);
  765. $this->db->set('pos'.$position.'_type', $this->Player_Model->now_town->$type_text);
  766. $this->db->set('wood', $wood);
  767. $this->db->set('wine', $wine);
  768. $this->db->set('marble', $marble);
  769. $this->db->set('crystal', $crystal);
  770. $this->db->set('sulfur', $sulfur);
  771. $this->db->where(array('id' => $this->Player_Model->town_id));
  772. $this->db->update($this->session->userdata('universe').'_towns');
  773. $this->Player_Model->user->points_buildings = $this->Player_Model->user->points_buildings - $points;
  774. $this->Player_Model->user->points_levels = $this->Player_Model->user->points_levels - 1;
  775. $this->db->set('points_buildings', $this->Player_Model->user->points_buildings);
  776. $this->db->set('points_levels', $this->Player_Model->user->points_levels);
  777. $this->db->where(array('id' => $this->Player_Model->user->id));
  778. $this->db->update($this->session->userdata('universe').'_users');
  779. $this->Player_Model->correct_buildings();
  780. $this->show('city');
  781. }
  782. else
  783. {
  784. $this->Error($this->lang->line('impossible_reduce_level'));
  785. }
  786. }
  787. function doResearch($way = 0, $id = 0)
  788. {
  789. $way = floor($way);
  790. $id = floor($id);
  791. if($way > 0 and $way <= 4)
  792. {
  793. $parametr = 'res'.$way.'_'.$id;
  794. $data = $this->Data_Model->get_research($way,$id,$this->Player_Model->research);
  795. if ($this->Player_Model->research->points >= $data['points'])
  796. {
  797. $this->Player_Model->research->points = $this->Player_Model->research->points - $data['points'];
  798. $way_text = 'way'.$way.'_checked';
  799. $this->Player_Model->research->$way_text = 0;
  800. $this->db->set('points', $this->Player_Model->research->points);
  801. $this->db->set('way'.$way.'_checked', 0);
  802. $this->Player_Model->research->$parametr = $this->Player_Model->research->$parametr + 1;
  803. $this->db->set($parametr, $this->Player_Model->research->$parametr);
  804. $this->db->where(array('user' => $this->Player_Model->user->id));
  805. $this->db->update($this->session->userdata('universe').'_research');
  806. $this->Player_Model->user->points_research = $this->Player_Model->user->points_research + $data['points']*0.01;
  807. $this->Player_Model->user->points_complete = $this->Player_Model->user->points_complete + 4;
  808. $this->db->set('points_research', $this->Player_Model->user->points_research);
  809. $this->db->set('points_complete', $this->Player_Model->user->points_complete);
  810. $this->db->where(array('id' => $this->Player_Model->user->id));
  811. $this->db->update($this->session->userdata('universe').'_users');
  812. // Wealth
  813. if($way == 2 and $id == 3)
  814. {
  815. $this->Player_Model->now_town->wood = $this->Player_Model->now_town->wood + 130;
  816. $this->Player_Model->now_town->marble = $this->Player_Model->now_town->marble + 130;
  817. $this->Player_Model->now_town->wine = $this->Player_Model->now_town->wine + 130;
  818. $this->Player_Model->now_town->crystal = $this->Player_Model->now_town->crystal + 130;
  819. $this->Player_Model->now_town->sulfur = $this->Player_Model->now_town->sulfur + 130;
  820. $this->db->set('wood', $this->Player_Model->now_town->wood);
  821. $this->db->set('marble', $this->Player_Model->now_town->marble);
  822. $this->db->set('wine', $this->Player_Model->now_town->wine);
  823. $this->db->set('crystal', $this->Player_Model->now_town->crystal);
  824. $this->db->set('sulfur', $this->Player_Model->now_town->sulfur);
  825. $this->db->where(array('id' => $this->Player_Model->town_id));
  826. $this->db->update($this->session->userdata('universe').'_towns');
  827. }
  828. }
  829. }
  830. $this->Player_Model->Load_Ways();
  831. $this->show('researchAdvisor');
  832. }
  833. /**
  834. * Go to page errors
  835. * @param <string> $error
  836. */
  837. function Error($error = '')
  838. {
  839. $this->show('error', $error);
  840. }
  841. function espionage($town = 0, $spy = 0, $mission = 0)
  842. {
  843. $msg_id = 0;
  844. $town = floor($town);
  845. $spy = floor($spy);
  846. $mission = floor($mission);
  847. if (isset($this->Player_Model->spyes[$town][$spy]) and $mission >= 3 and $mission <= 10)
  848. {
  849. if($this->Player_Model->user->gold >= $this->Data_Model->spy_gold_by_mission($mission))
  850. {
  851. $this->Data_Model->Load_Town($this->Player_Model->spyes[$town][$spy]->to);
  852. $spy = $this->Player_Model->spyes[$town][$spy];
  853. $town = $this->Data_Model->temp_towns_db[$spy->to];
  854. $to_position = $this->Data_Model->get_position(14, $town);
  855. $to_text = 'pos'.$to_position.'_level';
  856. $to_level = ($to_position > 0) ? $town->$to_text : 0;
  857. $risk = (5*$town->spyes)+(2*$to_level)-(2*$town->pos0_level)-(2*$this->Player_Model->levels[$this->Player_Model->town_id][14]);
  858. if ($risk < 0){ $risk = 0; }
  859. $risk = $risk + $this->Data_Model->spy_risk_by_mission($spy->mission_type) + $spy->risk;
  860. $chance = rand(0, 100);
  861. if ($chance >= $risk)
  862. {
  863. $time = time();
  864. $text = '';
  865. switch($mission)
  866. {
  867. case 3:
  868. $this->Data_Model->Load_User($town->user);
  869. $user = $this->Data_Model->temp_users_db[$town->user];
  870. $text = $this->lang->line('available_this_town').' '.number_format($user->gold).' <img alt="'.$this->lang->line('gold').'" src="'.$this->config->item('style_url').'skin/resources/icon_gold.gif">';
  871. break;
  872. case 4:
  873. $text = '<table cellpadding="0" cellspacing="0" class="reportTable" id="resources">
  874. <tr>
  875. <th class="unitname">'.$this->lang->line('resource').'</th>
  876. <th class="count">'.$this->lang->line('quantity').'</th>
  877. </tr>
  878. <tr>
  879. <td class="unitname"><img src="'.$this->config->item('style_url').'skin/resources/icon_wood.gif" alt="'.$this->lang->line('wood').'" title="'.$this->lang->line('wood').'"></td>
  880. <td class="count">'.number_format($town->wood).'</td>
  881. </tr>
  882. <tr>
  883. <td class="unitname"><img src="'.$this->config->item('style_url').'skin/resources/icon_wine.gif" alt="'.$this->lang->line('wine').'" title="'.$this->lang->line('wine').'"></td>
  884. <td class="count">'.number_format($town->wine).'</td>
  885. </tr>
  886. <tr>
  887. <td class="unitname"><img src="'.$this->config->item('style_url').'skin/resources/icon_marble.gif" alt="'.$this->lang->line('marble').'" title="'.$this->lang->line('marble').'"></td>
  888. <td class="count">'.number_format($town->marble).'</td>
  889. </tr>
  890. <tr>
  891. <td class="unitname"><img src="'.$this->config->item('style_url').'skin/resources/icon_glass.gif" alt="'.$this->lang->line('crystal').'" title="'.$this->lang->line('crystal').'"></td>
  892. <td class="count">'.number_format($town->crystal).'</td>
  893. </tr>
  894. <tr>
  895. <td class="unitname"><img src="'.$this->config->item('style_url').'skin/resources/icon_sulfur.gif" alt="'.$this->lang->line('sulfur').'" title="'.$this->lang->line('sulfur').'"></td>
  896. <td class="count">'.number_format($town->sulfur).'</td>
  897. </tr>
  898. </table>';
  899. break;
  900. case 5:
  901. $this->Data_Model->Load_Research($town->user);
  902. $this->research = $this->Data_Model->temp_research_db[$town->user];
  903. for($i = 1; $i < 14; $i++){
  904. $parametr = 'res1_'.$i;
  905. if ($this->research->$parametr == 0){$this->ways[1] = $this->Data_Model->get_research(1,$i,$this->research);break;}}
  906. if ($this->research->res1_14 > 0){$this->ways[1] = $this->Data_Model->get_research(1,14,$this->research);}
  907. if(isset($this->ways[1]) and $this->ways[1]['points'] <= $this->research->points and $this->research->way1_checked == 0){$this->research_advisor = true;}
  908. for($i = 1; $i < 15; $i++){
  909. $parametr = 'res2_'.$i;
  910. if ($this->research->$parametr == 0){$this->ways[2] = $this->Data_Model->get_research(2,$i,$this->research);break;}}
  911. if ($this->research->res2_15 > 0){$this->ways[2] = $this->Data_Model->get_research(2,15,$this->research);}
  912. if(isset($this->ways[2]) and $this->ways[2]['points'] <= $this->research->points and $this->research->way2_checked == 0){$this->research_advisor = true;}
  913. for($i = 1; $i < 16; $i++){
  914. $parametr = 'res3_'.$i;
  915. if ($this->research->$parametr == 0){$this->ways[3] = $this->Data_Model->get_research(3,$i,$this->research);break;}}
  916. if ($this->research->res3_16 > 0){$this->ways[3] = $this->Data_Model->get_research(3,16,$this->research);}
  917. if(isset($this->ways[3]) and $this->ways[3]['points'] <= $this->research->points and $this->research->way3_checked == 0){$this->research_advisor = true;}
  918. for($i = 1; $i < 14; $i++){
  919. $parametr = 'res4_'.$i;
  920. if ($this->research->$parametr == 0){$this->ways[4] = $this->Data_Model->get_research(4,$i,$this->research);break;}}
  921. if ($this->research->res4_14 > 0){$this->ways[4] = $this->Data_Model->get_research(4,14,$this->research);}
  922. if(isset($this->ways[4]) and $this->ways[4]['points'] <= $this->research->points and $this->research->way4_checked == 0){$this->research_advisor = true;}
  923. $ways = array();
  924. for ($i = 1; $i <= 4; $i++)
  925. if ($this->ways[$i]['need_id'] > 0)
  926. {
  927. $need = $this->Data_Model->get_research($this->ways[$i]['need_way'],$this->ways[$i]['need_id'],$this->research);
  928. $ways_names[$i] = $need['name'];
  929. }
  930. else
  931. {
  932. $ways_names[$i] = $this->ways[$i]['name'];
  933. }
  934. $text = '<table cellpadding="0" cellspacing="0" class="reportTable">
  935. <tr>
  936. <th class="unitname">'.$this->lang->line('research_areas').'</th>
  937. <th class="count">'.$this->lang->line('current_research').'</th>
  938. </tr>
  939. <tr>
  940. <td class="unitname">'.$this->lang->line('way1_name').'</td>
  941. <td class="count">'.$ways_names[1].'</td>
  942. </tr>
  943. <tr>
  944. <td class="unitname">'.$this->lang->line('way2_name').'</td>
  945. <td class="count">'.$ways_names[2].'</td>
  946. </tr>
  947. <tr>
  948. <td class="unitname">'.$this->lang->line('way3_name').'</td>
  949. <td class="count">'.$ways_names[3].'</td>
  950. </tr>
  951. <tr>
  952. <td class="unitname">'.$this->lang->line('way4_name').'</td>
  953. <td class="count">'.$ways_names[4].'</td>
  954. </tr>
  955. </table>';
  956. break;
  957. case 6:
  958. $this->Data_Model->Load_User($town->user);
  959. $user = $this->Data_Model->temp_users_db[$town->user];
  960. $text = ((time() - $user->last_visit) <= 300) ? $this->lang->line('leader_online') : $this->lang->line('leader_offline');
  961. break;
  962. case 7:
  963. $this->Data_Model->Load_Army($town->id);
  964. $army = $this->Data_Model->temp_army_db[$town->id];
  965. $army_names = '';
  966. $army_counts = '';
  967. $ship_names = '';
  968. $ship_counts = '';
  969. $text = 'Войска в '.$town->name.'<table cellpadding="0" cellspacing="0" class="reportTable">
  970. <tr>
  971. <th class="unitname">'.$this->lang->line('unit').'</th>
  972. <th class="count">'.$this->lang->line('number').'</th>
  973. </tr>
  974. <tr>';
  975. for ($i = 1; $i <= 14; $i++)
  976. {
  977. $class = $this->Data_Model->army_class_by_type($i);
  978. if ($army->$class > 0)
  979. {
  980. $army_names .= '<p>'.$this->Data_Model->army_name_by_type($i).'</p>';
  981. $army_counts .= '<p>'.$army->$class.'</p>';
  982. }
  983. }
  984. for ($i = 16; $i <= 22; $i++)
  985. {
  986. $class = $this->Data_Model->army_class_by_type($i);
  987. if ($army->$class > 0)
  988. {
  989. $ship_names .= '<p>'.$this->Data_Model->army_name_by_type($i).'</p>';
  990. $ship_counts .= '<p>'.$army->$class.'</p>';
  991. }
  992. }
  993. if ($army_names == '')
  994. {
  995. $text .= '<td class="unitname">'.$this->lang->line('no_troops').'</td>';
  996. $text .= '<td class="count"></td>';
  997. }
  998. else
  999. {
  1000. $text .= '<td class="unitname">'.$army_names.'</td>';
  1001. $text .= '<td class="count">'.$army_counts.'</td>';
  1002. }
  1003. $text .= '</tr>
  1004. </table>
  1005. <tr>
  1006. <td></td>
  1007. <td></td>
  1008. </tr>
  1009. </td>
  1010. </tr>
  1011. <tr>
  1012. <td></td>
  1013. <td class="report">
  1014. '.$this->lang->line('fleets_in').' '.$town->name.'<table cellpadding="0" cellspacing="0" class="reportTable">
  1015. <tr>
  1016. <th class="unitname">'.$this->lang->line('ships').'</th>
  1017. <th class="count">'.$this->lang->line('number').'</th>
  1018. </tr>
  1019. <tr>';
  1020. if ($ship_names == '')
  1021. {
  1022. $text .= '<td class="unitname">'.$this->lang->line('no_troops').'</td>';
  1023. $text .= '<td class="count"></td>';
  1024. }
  1025. else
  1026. {
  1027. $text .= '<td class="unitname">'.$ship_names.'</td>';
  1028. $text .= '<td class="count">'.$ship_counts.'</td>';
  1029. }
  1030. $text .= '</tr>
  1031. </table>
  1032. <tr>
  1033. <td></td>
  1034. <td class="report">
  1035. '.$this->lang->line('blocking_fleet_in').' '.$town->name.'<table cellpadding="0" cellspacing="0" class="reportTable">
  1036. <tr>
  1037. <th class="unitname">'.$this->lang->line('ships').'</th>
  1038. <th class="count">'.$this->lang->line('number').'</th>
  1039. </tr>
  1040. <tr>
  1041. <td class="unitname">'.$this->lang->line('no_troops').'</td>
  1042. <td class="count"></td>
  1043. </tr>
  1044. </table> ';
  1045. break;
  1046. case 8:
  1047. $text = '<table cellpadding="0" cellspacing="0" class="reportTable">
  1048. <tr>
  1049. <th>'.$this->lang->line('no_fleet_movements').'</th>
  1050. </tr>
  1051. </table>';
  1052. break;
  1053. case 9:
  1054. $text = '<table cellpadding="0" cellspacing="0" class="reportTable" width="100%">
  1055. <tr>
  1056. <th class="unitname"><strong>'.$this->lang->line('sender').':</strong></th>
  1057. <th class="unitname"><strong>'.$this->lang->line('subject').':</strong></th>
  1058. <th class="unitname"><strong>'.$this->lang->line('date').':</strong></th>
  1059. <th class="unitname"><strong>'.$this->lang->line('recipient').':</strong></th>
  1060. </tr>';
  1061. $text .= '<tr>
  1062. <td>'.$this->lang->line('no_messages').'</td>
  1063. <td></td>
  1064. <td></td>
  1065. <td></td>
  1066. </tr> ';
  1067. $text .= '</table>';
  1068. break;
  1069. case 10:
  1070. $this->db->set('mission_start', time());
  1071. $this->db->set('mission_type', 2);
  1072. $this->db->where(array('id' => $spy->id));
  1073. $this->db->update($this->session->userdata('universe').'_spyes');
  1074. break;
  1075. }
  1076. if ($mission < 10)
  1077. {
  1078. $desc = $this->Data_Model->spy_mission_name_by_type($mission);
  1079. if ($mission == 6) { $desc .= ' '.$town->name; }
  1080. $spy_message= array(
  1081. 'user' => $spy->user,
  1082. 'spy' => $spy->id,
  1083. 'from' => $spy->from,
  1084. 'to' => $spy->to,
  1085. 'mission' => $mission,
  1086. 'date' => $time,
  1087. 'desc' => $desc,
  1088. 'text' => $text
  1089. );
  1090. $this->db->insert($this->session->userdata('universe').'_spy_messages', $spy_message);
  1091. }
  1092. $this->db->set('risk', $risk);
  1093. $this->db->where(array('id' => $spy->id));
  1094. $this->db->update($this->session->userdata('universe').'_spyes');
  1095. $spy_query = $this->db->get_where($this->session->userdata('universe').'_spy_messages', array('spy' => $spy->id, 'date' => $time));
  1096. if ($spy_query->num_rows == 1)
  1097. {
  1098. $spy_msg = $spy_query->row();
  1099. $msg_id = $spy_msg->id;
  1100. }
  1101. }
  1102. else
  1103. {
  1104. $chance = rand(0, 1);
  1105. if($chance)
  1106. {
  1107. $chance = (100-$risk) < 0 ? 0 : 100-$risk;
  1108. $spy_message= array(
  1109. 'user' => $spy->user,
  1110. 'spy' => $spy->id,
  1111. 'from' => $spy->from,
  1112. 'to' => $spy->to,
  1113. 'mission' => 0,
  1114. 'date' => time(),
  1115. 'desc' => $this->lang->line('no_get_touch'),
  1116. 'text' => $this->lang->line('no_get_touch_txt').': '.$chance.' %.'
  1117. );
  1118. $this->db->insert($this->session->userdata('universe').'_spy_messages', $spy_message);
  1119. $this->db->delete($this->session->userdata('universe').'_spyes', array('id' => $spy->id));
  1120. unset($this->Player_Model->spyes[$this->Player_Model->town_id][$spy->id]);
  1121. }
  1122. else
  1123. {
  1124. $spy_message = array(
  1125. 'user' => $spy->user,
  1126. 'spy' => $spy->id,
  1127. 'from' => $spy->from,
  1128. 'to' => $spy->to,
  1129. 'mission' => 0,
  1130. 'date' => time(),
  1131. 'desc' => $this->lang->line('job_canceled'),
  1132. 'text' => $this->lang->line('discovered_bet_returns')
  1133. );
  1134. $this->db->insert($this->session->userdata('universe').'_spy_messages', $spy_message);
  1135. $this->Player_Model->spyes[$this->Player_Model->town_id][$spy->id]->mission_type = 2;
  1136. $this->db->set('mission_start', time());
  1137. $this->db->set('mission_type', 2);
  1138. $this->db->where(array('id' => $spy->id));
  1139. $this->db->update($this->session->userdata('universe').'_spyes');
  1140. }
  1141. }
  1142. $this->db->set('gold', $this->Player_Model->user->gold - $this->Data_Model->spy_gold_by_mission($mission));
  1143. $this->db->where(array('id' => $this->Player_Model->user->id));
  1144. $this->db->update($this->session->userdata('universe').'_users');
  1145. }
  1146. }
  1147. if ($msg_id > 0)
  1148. {
  1149. redirect($this->config->item('base_url').'game/safehouseReports/'.$msg_id.'/', 'refresh');
  1150. }
  1151. else
  1152. {
  1153. redirect($this->config->item('base_url').'game/safehouse/', 'refresh');
  1154. }
  1155. }
  1156. function fleet($id = 0)
  1157. {
  1158. $position = $this->Data_Model->get_position(4, $this->Player_Model->now_town);
  1159. if (($this->Player_Model->now_town->build_line == '' or $this->Player_Model->build_line[$this->Player_Model->town_id][0]['type'] != 4) and
  1160. (strlen($this->Player_Model->armys[$this->Player_Model->town_id]->ships_line) <= $this->config->item('army_queue_size')*4) )
  1161. {
  1162. if ($position > 0 and $position == $id)
  1163. {
  1164. $all_cost['wood'] = 0;
  1165. $all_cost['wine'] = 0;
  1166. $all_cost['crystal'] = 0;
  1167. $all_cost['sulfur'] = 0;
  1168. $all_cost['peoples'] = 0;
  1169. //$all_cost['gold'] = 0;
  1170. $army_line = $this->Player_Model->armys[$this->Player_Model->town_id]->ships_line;
  1171. $army_start = ($this->Player_Model->armys[$this->Player_Model->town_id]->ships_start > 0) ? $this->Player_Model->armys[$this->Player_Model->town_id]->ships_start : time();
  1172. // Process the data
  1173. for ($i = 16; $i <= 22; $i++)
  1174. {
  1175. $class = $this->Data_Model->army_class_by_type($i);
  1176. $$class = (isset($_POST[$i])) ? floor($_POST[$i]) : 0 ;
  1177. $cost = $this->Data_Model->army_cost_by_type($i, $this->Player_Model->research, $this->Player_Model->levels[$this->Player_Model->town_id]);
  1178. $all_cost['wood'] = $all_cost['wood'] + $cost['wood']*$$class;
  1179. $all_cost['wine'] = $all_cost['wine'] + $cost['wine']*$$class;
  1180. $all_cost['crystal'] = $all_cost['crystal'] + $cost['crystal']*$$class;
  1181. $all_cost['sulfur'] = $all_cost['sulfur'] + $cost['sulfur']*$$class;
  1182. $all_cost['peoples'] = $all_cost['peoples'] + $cost['peoples']*$$class;
  1183. if ($$class > 0)
  1184. {
  1185. if ($army_line != '')
  1186. {
  1187. $army_line .= ';';
  1188. }
  1189. $army_line .= $i.','.$$class;
  1190. }
  1191. }
  1192. // Calculate the remainder
  1193. $wood = $this->Player_Model->now_town->wood - $all_cost['wood'];
  1194. $wine = $this->Player_Model->now_town->wine - $all_cost['wine'];
  1195. $crystal = $this->Player_Model->now_town->crystal - $all_cost['crystal'];
  1196. $sulfur = $this->Player_Model->now_town->sulfur - $all_cost['sulfur'];
  1197. $peoples = $this->Player_Model->now_town->peoples - $all_cost['peoples'];
  1198. // If you have enough resources
  1199. if ($wood >= 0 and $wine >= 0 and $crystal >= 0 and $sulfur >= 0 and $peoples >= 0)
  1200. {
  1201. // update the city
  1202. $this->Player_Model->now_town->wood = $wood;
  1203. $this->Player_Model->now_town->wine = $wine;
  1204. $this->Player_Model->now_town->crystal = $crystal;
  1205. $this->Player_Model->now_town->sulfur = $sulfur;
  1206. $this->Player_Model->now_town->peoples = $peoples;
  1207. $this->db->set('wood', $wood);
  1208. $this->db->set('wine', $wine);
  1209. $this->db->set('crystal', $crystal);
  1210. $this->db->set('sulfur', $sulfur);
  1211. $this->db->set('peoples', $peoples);
  1212. $this->db->where(array('id' => $this->Player_Model->town_id));
  1213. $this->db->update($this->session->userdata('universe').'_towns');
  1214. // update the army
  1215. $this->Player_Model->armys[$this->Player_Model->town_id]->ships_line = $army_line;
  1216. $this->Player_Model->armys[$this->Player_Model->town_id]->ships_start = $army_start;
  1217. $this->Player_Model->ships_line[$this->Player_Model->town_id] = $this->Data_Model->load_army_line($this->Player_Model->armys[$this->Player_Model->town_id]->ships_line);
  1218. $this->db->set('ships_line', $army_line);
  1219. $this->db->set('ships_start', $army_start);
  1220. $this->db->where(array('city' => $this->Player_Model->town_id));
  1221. $this->db->update($this->session->userdata('universe').'_army');
  1222. }
  1223. }
  1224. }
  1225. $this->show('shipyard',$id);
  1226. }
  1227. function leaveConstructionList($id = 1)
  1228. {
  1229. if(isset($this->Player_Model->build_line[$this->Player_Model->town_id][$id]) and $id > 0)
  1230. {
  1231. if (SizeOf($this->Player_Model->build_line[$this->Player_Model->town_id]) > 1)
  1232. for ($i = 0; $i < SizeOf($this->Player_Model->build_line[$this->Player_Model->town_id]); $i++)
  1233. {
  1234. if ($i != $id)
  1235. {
  1236. $building = array($this->Player_Model->build_line[$this->Player_Model->town_id][$i]['position'], $this->Player_Model->build_line[$this->Player_Model->town_id][$i]['type']);
  1237. $building_line[] = implode(",", $building);
  1238. }
  1239. }
  1240. $buildings_line = implode(";", $building_line);
  1241. $this->Player_Model->now_town->build_line = $buildings_line;
  1242. $this->db->set('build_line', $buildings_line);
  1243. $this->db->where(array('id' => $this->Player_Model->town_id));
  1244. $this->db->update($this->session->userdata('universe').'_towns');
  1245. }
  1246. redirect($this->config->item('base_url').'game/city/', 'refresh');
  1247. }
  1248. function messages($action = '', $id = 0, $relocation = 'diplomacyAdvisor')
  1249. {
  1250. $this->Player_Model->Load_User_Messages();
  1251. switch($action)
  1252. {
  1253. case 'send':
  1254. if (isset($_POST['msgType']) and isset($_POST['content']))
  1255. {
  1256. $msg_type = floor($_POST['msgType']);
  1257. $content = strip_tags($_POST['content']);
  1258. $this->Data_Model->Load_User($id);
  1259. if ($msg_type > 0 and $msg_type <= 1 and isset($this->Data_Model->temp_users_db[$id]))
  1260. {
  1261. $this->db->insert($this->session->userdata('universe').'_user_messages', array('from' => $this->Player_Model->user->id, 'to' => $id, 'type' => $msg_type, 'date' => time(), 'text' => $content));
  1262. }
  1263. }
  1264. break;
  1265. case 'delete':
  1266. if (isset($this->Player_Model->to_user_messages[$id]))
  1267. {
  1268. $this->db->set('deleted_to', time());
  1269. $this->db->where(array('id' => $id));
  1270. $this->db->update($this->session->userdata('universe').'_user_messages');
  1271. }
  1272. else
  1273. {
  1274. if(isset($_POST['deleteId']))
  1275. {
  1276. foreach($this->Player_Model->from_user_messages as $message)
  1277. {
  1278. if (($message->from = $this->Player_Model->user->id) and
  1279. (isset($_POST['deleteId'][$message->id]) and $_POST['deleteId'][$message->id] == 1))
  1280. {
  1281. $this->db->set('deleted_from', time());
  1282. $this->db->where(array('id' => $message->id));
  1283. $this->db->update($this->session->userdata('universe').'_user_messages');
  1284. }
  1285. }
  1286. foreach($this->Player_Model->to_user_messages as $message)
  1287. {
  1288. if (($message->to = $this->Player_Model->user->id) and
  1289. (isset($_POST['deleteId'][$message->id]) and $_POST['deleteId'][$message->id] == 'read'))
  1290. {
  1291. $this->db->set('deleted_to', time());
  1292. $this->db->where(array('id' => $message->id));
  1293. $this->db->update($this->session->userdata('universe').'_user_messages');
  1294. }
  1295. }
  1296. }
  1297. }
  1298. break;
  1299. case 'read':
  1300. if (isset($this->Player_Model->to_user_messages[$id]))
  1301. {
  1302. $this->db->set('checked_to', time());
  1303. $this->db->where(array('id' => $id));
  1304. $this->db->update($this->session->userdata('universe').'_user_messages');
  1305. }
  1306. break;
  1307. }
  1308. redirect($this->config->item('base_url').'game/'.$relocation.'/', 'refresh');
  1309. }
  1310. function premium($type = '')
  1311. {
  1312. $cost = $this->Data_Model->premium_cost($type);
  1313. if ($this->Player_Model->user->ambrosy >= $cost)
  1314. {
  1315. switch($type)
  1316. {
  1317. case 'account': if($this->Player_Model->user->premium_account > 0){ $this->Player_Model->user->premium_account = $this->Player_Model->user->premium_account+604800; } else { $this->Player_Model->user->premium_account = time()+604800; } break;
  1318. case 'wood': if($this->Player_Model->user->premium_wood > 0){ $this->Player_Model->user->premium_wood = $this->Player_Model->user->premium_wood+604800; } else { $this->Player_Model->user->premium_wood = time()+604800; } break;
  1319. case 'wine': if($this->Player_Model->user->premium_wine > 0){ $this->Player_Model->user->premium_wine = $this->Player_Model->user->premium_wine+604800; } else { $this->Player_Model->user->premium_wine = time()+604800; } break;
  1320. case 'marble': if($this->Player_Model->user->premium_marble > 0){ $this->Player_Model->user->premium_marble = $this->Player_Model->user->premium_marble+604800; } else { $this->Player_Model->user->premium_marble = time()+604800; } break;
  1321. case 'crystal': if($this->Player_Model->user->premium_crystal > 0){ $this->Player_Model->user->premium_crystal = $this->Player_Model->user->premium_crystal+604800; } else { $this->Player_Model->user->premium_crystal = time()+604800; } break;
  1322. case 'sulfur': if($this->Player_Model->user->premium_sulfur > 0){ $this->Player_Model->user->premium_sulfur = $this->Player_Model->user->premium_sulfur+604800; } else { $this->Player_Model->user->premium_sulfur = time()+604800; } break;
  1323. case 'capacity': if($this->Player_Model->user->premium_capacity > 0){ $this->Player_Model->user->premium_capacity = $this->Player_Model->user->premium_capacity+604800; } else { $this->Player_Model->user->premium_capacity = time()+604800; } break;
  1324. }
  1325. $this->Player_Model->user->ambrosy = $this->Player_Model->user->ambrosy - $cost;
  1326. $this->db->set('premium_account', $this->Player_Model->user->premium_account);
  1327. $this->db->set('premium_wood', $this->Player_Model->user->premium_wood);
  1328. $this->db->set('premium_wine', $this->Player_Model->user->premium_wine);
  1329. $this->db->set('premium_marble', $this->Player_Model->user->premium_marble);
  1330. $this->db->set('premium_crystal', $this->Player_Model->user->premium_crystal);
  1331. $this->db->set('premium_sulfur', $this->Player_Model->user->premium_sulfur);
  1332. $this->db->set('premium_capacity', $this->Player_Model->user->premium_capacity);
  1333. $this->db->set('ambrosy', $this->Player_Model->user->ambrosy);
  1334. $this->db->where(array('id' => $this->Player_Model->user->id));
  1335. $this->db->update($this->session->userdata('universe').'_users');
  1336. }
  1337. $this->show('premium');
  1338. }
  1339. function options($type = '')
  1340. {
  1341. switch($type)
  1342. {
  1343. case 'user':
  1344. if (isset($_POST['name']))
  1345. {
  1346. $login = strip_tags($_POST['name']);
  1347. if ($login != $this->Player_Model->user->login and $login != '')
  1348. {
  1349. $query = $this->db->get_where($this->session->userdata('universe').'_users', array('login' => $login));
  1350. // If no such player
  1351. if ($query->num_rows == 0)
  1352. {
  1353. $this->Player_Model->user->login = $login;
  1354. $this->db->set('login', $login);
  1355. $this->db->where(array('id' => $this->Player_Model->user->id));
  1356. $this->db->update($this->session->userdata('universe').'_users');
  1357. }
  1358. else
  1359. {
  1360. $this->session->set_flashdata(array('options_error' => $this->lang->line('error')));
  1361. $this->session->set_flashdata(array('options_error_login' => $this->lang->line('username').' '.$login.' '.$this->lang->line('taken')));
  1362. }
  1363. }
  1364. $position = 'account';
  1365. }
  1366. if (isset($_POST['oldPassword']) and isset($_POST['newPassword']) and isset($_POST['newPasswordConfirm']))
  1367. {
  1368. $old = md5(strip_tags($_POST['oldPassword']));
  1369. $new = md5(strip_tags($_POST['newPassword']));
  1370. $new2 = md5(strip_tags($_POST['newPasswordConfirm']));
  1371. if ($old != $new and $old != '' and $new != '')
  1372. {
  1373. if ($this->Player_Model->user->password == $old)
  1374. {
  1375. if ($new == $new2)
  1376. {
  1377. $this->Player_Model->user->password = $new;
  1378. $this->db->set('password', $new);
  1379. $this->db->where(array('id' => $this->Player_Model->user->id));
  1380. $this->db->update($this->session->userdata('universe').'_users');
  1381. }
  1382. else
  1383. {
  1384. $this->session->set_flashdata(array('options_error' => $this->lang->line('error')));
  1385. $this->session->set_flashdata(array('options_error_login' => $this->lang->line('error_password')));
  1386. }
  1387. }
  1388. else
  1389. {
  1390. $this->session->set_flashdata(array('options_error' => $this->lang->line('error')));
  1391. $this->session->set_flashdata(array('options_error_login' => $this->lang->line('error_login_password')));
  1392. }
  1393. }
  1394. $position = 'account';
  1395. }
  1396. if (isset($_POST['citySelectOptions']))
  1397. {
  1398. $city_select = floor($_POST['citySelectOptions']);
  1399. if ($city_select != $this->Player_Model->user->options_select and $city_select >=0 and $city_select <= 2)
  1400. {
  1401. $this->Player_Model->user->options_select = $city_select;
  1402. $this->db->set('options_select', $city_select);
  1403. $this->db->where(array('id' => $this->Player_Model->user->id));
  1404. $this->db->update($this->session->userdata('universe').'_users');
  1405. }
  1406. $position = 'game';
  1407. }
  1408. if (isset($_POST['tutorialOptions']))
  1409. {
  1410. if ($this->Player_Model->user->tutorial < 999 and $_POST['tutorialOptions'] == -2)
  1411. {
  1412. $this->Player_Model->user->tutorial = 999;
  1413. $this->db->set('tutorial', 999);
  1414. $this->db->where(array('id' => $this->Player_Model->user->id));
  1415. $this->db->update($this->session->userdata('universe').'_users');
  1416. }
  1417. $position = 'game';
  1418. }
  1419. redirect($this->config->item('base_url').'game/options/'.$position.'/', 'refresh');
  1420. break;
  1421. case 'validationEmail':
  1422. $this->load->library('email');
  1423. $this->load->helper('email');
  1424. $config['mailtype'] = 'html'; // Type text or html emails
  1425. $this->email->initialize($config);
  1426. if ($this->config->item('game_email'))
  1427. {
  1428. $message = '
  1429. <html>
  1430. <body>
  1431. <p>
  1432. '.$this->lang->line('register_email_text_1').' '.$this->Player_Model->user->login.', <br>
  1433. <br>'.$this->lang->line('register_email_text_2').' '.$this->session->userdata('universe').'!<br>
  1434. <br>'.$this->lang->line('register_email_text_3').':<br>
  1435. <br><a href="'.$this->config->item('base_url').'main/validate/'.$this->session->userdata('universe').'/'.$this->Player_Model->user->register_key.'/" target="_blank">'.$this->config->item('base_url').'main/validate/'.$this->session->userdata('universe').'/'.$this->Player_Model->user->register_key.'</a><br>
  1436. <br>'.$this->lang->line('register_email_text_4').':
  1437. <br>'.$this->lang->line('username').': '.$this->Player_Model->user->login.'
  1438. <br>'.$this->lang->line('password').': *****
  1439. <br>'.$this->lang->line('world').': '.$this->session->userdata('universe').'<br>
  1440. <br>'.$this->lang->line('register_email_text_5').' ('.$this->config->item('forum_url').').<br>
  1441. <br>'.$this->lang->line('register_email_text_6').',
  1442. <br>'.$this->lang->line('register_email_text_7').'
  1443. </p>
  1444. </body>
  1445. </html>';
  1446. $this->email->from($this->config->item('email_from'), 'ZZJHONS');
  1447. $this->email->to($this->Player_Model->user->email);
  1448. $this->email->subject($this->lang->line('register_email_title'));
  1449. $this->email->message($message);
  1450. $this->email->send();
  1451. }
  1452. break;
  1453. }
  1454. $this->show('options');
  1455. }
  1456. /**
  1457. * All the islands
  1458. * @param <int> $xmin
  1459. * @param <int> $xmax
  1460. * @param <int> $ymin
  1461. * @param <int> $ymax
  1462. */
  1463. function getJSONArea($xmin = 0, $xmax = 15, $ymin = 0, $ymax = 15)
  1464. {
  1465. $data = '{"request":{"x_min":'.$xmin.',"x_max":'.$xmax.',"y_min":'.$ymin.',"y_max":'.$ymax.'},"data":{';
  1466. for ($i = $xmin; $i <= $xmax; $i++)
  1467. {
  1468. $query_x = $this->db->query('SELECT * FROM '.$this->session->userdata('universe').'_islands WHERE x='.$i.' and y>'.$ymin.' and y<'.$ymax);
  1469. $data .= '"'.$i.'":{';
  1470. $j = 1;
  1471. foreach ($query_x->result() as $island)
  1472. {
  1473. $towns = 0;
  1474. for ($p = 0; $p <= 15; $p++)
  1475. {
  1476. $parametr = 'city'.$p;
  1477. if ($island->$parametr > 0){ $towns = $towns + 1; }
  1478. }
  1479. $data .= '"'.$island->y.'":["'.$island->id.'","'.$island->name.'","'.$island->trade_resource.'","'.$island->wonder.'","0","'.$island->type.'","0","'.$towns.'"]';
  1480. $data .= ($j == $query_x->num_rows) ? '' : ',' ;
  1481. $j = $j + 1;
  1482. }
  1483. $data .= ($i == $xmax) ? '}' : '},' ;
  1484. }
  1485. $data .= '}}';
  1486. echo $data;
  1487. }
  1488. /**
  1489. * Concrete island
  1490. * @param <int> $x
  1491. * @param <int> $y
  1492. */
  1493. function getJSONIsland($x = 0, $y = 0)
  1494. {
  1495. $x = floor($x);
  1496. $y = floor($y);
  1497. echo '{"request":{"x":'.$x.',"y":'.$y.'},"data":[]}';
  1498. }
  1499. /**
  1500. * Rename the city
  1501. */
  1502. function rename()
  1503. {
  1504. if (isset($_POST['name']) and strip_tags($_POST['name']) != '')
  1505. {
  1506. $this->Player_Model->now_town->name = strip_tags($_POST['name']);
  1507. $this->db->set('name', strip_tags($_POST['name']));
  1508. $this->db->where(array('id' => $this->Player_Model->town_id));
  1509. $this->db->update($this->session->userdata('universe').'_towns');
  1510. $this->show('townHall', 0);
  1511. }
  1512. }
  1513. function resources($type = 'resource', $id = 0)
  1514. {
  1515. $count = isset($_POST['donation']) ? floor($_POST['donation']) : 0;
  1516. if($this->Player_Model->now_town->wood >= $count and $count > 0 and $this->Player_Model->island_id == $id)
  1517. {
  1518. // Update the city
  1519. if ($type == 'resource')
  1520. {
  1521. $this->Player_Model->now_town->workers_wood = $this->Player_Model->now_town->workers_wood + $count;
  1522. $this->db->set('workers_wood', $this->Player_Model->now_town->workers_wood);
  1523. }
  1524. else
  1525. {
  1526. $this->Player_Model->now_town->tradegood_wood = $this->Player_Model->now_town->tradegood_wood + $count;
  1527. $this->db->set('tradegood_wood', $this->Player_Model->now_town->tradegood_wood + $count);
  1528. }
  1529. $this->Player_Model->now_town->wood = $this->Player_Model->now_town->wood - $count;
  1530. $this->db->set('wood', $this->Player_Model->now_town->wood);
  1531. $this->db->where(array('id' => $this->Player_Model->town_id));
  1532. $this->db->update($this->session->userdata('universe').'_towns');
  1533. // Update the island
  1534. if ($type == 'resource')
  1535. {
  1536. $this->db->query('UPDATE `'.$this->session->userdata('universe').'_islands'.'` SET `wood_count`=`wood_count`+'.$count.' WHERE `id`="'.$id.'"');
  1537. }
  1538. else
  1539. {
  1540. $this->db->query('UPDATE `'.$this->session->userdata('universe').'_islands'.'` SET `trade_count`=`trade_count`+'.$count.' WHERE `id`="'.$id.'"');
  1541. }
  1542. }
  1543. $this->load->model('Island_Model');
  1544. $this->Island_Model->Load_Island($id);
  1545. $this->show($type, $id);
  1546. }
  1547. function saveAvatarNotes()
  1548. {
  1549. $notes = strip_tags($_POST['notes']);
  1550. if (strlen($notes <= $this->config->item('notes_default')) or (strlen($notes <= $this->config->item('notes_premium') and $this->Player_Model->user->premium_account > 0)))
  1551. {
  1552. $this->db->set('text', $notes);
  1553. $this->db->where(array('user' => $this->Player_Model->user->id));
  1554. $this->db->update($this->session->userdata('universe').'_notes');
  1555. }
  1556. }
  1557. function show($location, $param1 = 0, $param2 = 0, $param3 = 0)
  1558. {
  1559. $this->load->view('game_index',array('page' => $location, 'param1' => $param1, 'param2' => $param2, 'param3' => $param3));
  1560. }
  1561. function spyes($action = 'buy', $island = 0, $town = 0)
  1562. {
  1563. $all_spyes = SizeOf($this->Player_Model->spyes[$this->Player_Model->town_id])+$this->Player_Model->now_town->spyes;
  1564. if ($action == 'buy' and $this->Player_Model->user->gold >= 150 and $this->Player_Model->now_town->crystal >= 80 and $this->Player_Model->now_town->spyes_start == 0 and ($this->Player_Model->levels[$this->Player_Model->town_id][14]-$all_spyes) > 0)
  1565. {
  1566. $this->db->set('gold', $this->Player_Model->user->gold - 150);
  1567. $this->db->where(array('id' => $this->Player_Model->user->id));
  1568. $this->db->update($this->session->userdata('universe').'_users');
  1569. $this->db->set('crystal', $this->Player_Model->now_town->crystal - 80);
  1570. $this->db->set('spyes_start', time());
  1571. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  1572. $this->db->update($this->session->userdata('universe').'_towns');
  1573. }
  1574. if ($action == 'send' and $this->Player_Model->user->gold >= 30 and $this->Player_Model->now_town->spyes > 0)
  1575. {
  1576. if ($island == 0)
  1577. {
  1578. $island = $this->Player_Model->island_id;
  1579. }
  1580. $this->load->model('Island_Model');
  1581. $this->Island_Model->Load_Island($island);
  1582. $this->Data_Model->Load_Town($town);
  1583. if(isset($this->Data_Model->temp_towns_db[$town]) and $this->Data_Model->temp_towns_db[$town]->island == $this->Island_Model->island->id)
  1584. {
  1585. $this->db->insert($this->session->userdata('universe').'_spyes', array('user' => $this->Player_Model->user->id, 'from' => $this->Player_Model->now_town->id, 'to' => $town, 'mission_type' => 1, 'mission_start' => time()));
  1586. $this->db->set('spyes', $this->Player_Model->now_town->spyes-1);
  1587. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  1588. $this->db->update($this->session->userdata('universe').'_towns');
  1589. $this->db->set('gold', $this->Player_Model->user->gold-30);
  1590. $this->db->where(array('id' => $this->Player_Model->user->id));
  1591. $this->db->update($this->session->userdata('universe').'_users');
  1592. }
  1593. }
  1594. if ($action == 'return' and isset($this->Player_Model->spyes[$town][$island]))
  1595. {
  1596. $this->db->set('mission_type', 2);
  1597. $this->db->set('mission_start', time());
  1598. $this->db->where(array('id' => $island));
  1599. $this->db->update($this->session->userdata('universe').'_spyes');
  1600. }
  1601. redirect($this->config->item('base_url').'game/safehouse/', 'refresh');
  1602. }
  1603. function tavern($position = 0)
  1604. {
  1605. $position = floor($position);
  1606. if(isset($_POST['amount']))
  1607. {
  1608. $type_text = 'pos'.$position.'_type';
  1609. $level_text = 'pos'.$position.'_level';
  1610. if ($this->Player_Model->now_town->$type_text == 8 and $this->Player_Model->now_town->$level_text >= floor($_POST['amount']))
  1611. {
  1612. $this->Player_Model->now_town->tavern_wine = floor($_POST['amount']);
  1613. $this->db->set('tavern_wine', $this->Player_Model->now_town->tavern_wine);
  1614. $this->db->where(array('id' => $this->Player_Model->town_id));
  1615. $this->db->update($this->session->userdata('universe').'_towns');
  1616. }
  1617. }
  1618. $this->show('tavern', $position);
  1619. }
  1620. function trade($town = 0, $type = 0)
  1621. {
  1622. if ($this->Player_Model->now_town->actions > 0)
  1623. {
  1624. if ($town > 0)
  1625. {
  1626. // read the data
  1627. $wood_count = (isset($_POST['cargo_resource']) and floor($_POST['cargo_resource']) > 0) ? floor($_POST['cargo_resource']) : 0;
  1628. $wine_count = (isset($_POST['cargo_tradegood1']) and floor($_POST['cargo_tradegood1']) > 0) ? floor($_POST['cargo_tradegood1']) : 0;
  1629. $marble_count = (isset($_POST['cargo_tradegood2']) and floor($_POST['cargo_tradegood2']) > 0) ? floor($_POST['cargo_tradegood2']) : 0;
  1630. $crystal_count = (isset($_POST['cargo_tradegood3']) and floor($_POST['cargo_tradegood3']) > 0) ? floor($_POST['cargo_tradegood3']) : 0;
  1631. $sulfur_count = (isset($_POST['cargo_tradegood4']) and floor($_POST['cargo_tradegood4']) > 0) ? floor($_POST['cargo_tradegood4']) : 0;
  1632. $wood_cost = (isset($_POST['resourcePrice']) and floor($_POST['resourcePrice']) > 0) ? floor($_POST['resourcePrice']) : 0;
  1633. $wine_cost = (isset($_POST['tradegood1Price']) and floor($_POST['tradegood1Price']) > 0) ? floor($_POST['tradegood1Price']) : 0;
  1634. $marble_cost = (isset($_POST['tradegood2Price']) and floor($_POST['tradegood2Price']) > 0) ? floor($_POST['tradegood2Price']) : 0;
  1635. $crystal_cost = (isset($_POST['tradegood3Price']) and floor($_POST['tradegood3Price']) > 0) ? floor($_POST['tradegood3Price']) : 0;
  1636. $sulfur_cost = (isset($_POST['tradegood4Price']) and floor($_POST['tradegood4Price']) > 0) ? floor($_POST['tradegood4Price']) : 0;
  1637. $gold_need = floor(($wood_count*$wood_cost) + ($wine_count*$wine_cost) + ($marble_count*$marble_cost) + ($crystal_count*$crystal_cost) + ($sulfur_count*$sulfur_cost));
  1638. $transporters = floor($_POST['transporters']);
  1639. $transports = $this->Player_Model->user->transports - $transporters;
  1640. $this->Data_Model->Load_Town($town);
  1641. if(isset($this->Data_Model->temp_towns_db[$town]))
  1642. {
  1643. if ($this->Player_Model->user->gold > 0 and $transports > 0)
  1644. {
  1645. if ($type == 1)
  1646. {
  1647. $this->Player_Model->user->gold = $this->Player_Model->user->gold - $gold_need;
  1648. if ($this->Player_Model->user->gold > 0)
  1649. {
  1650. $trade_town = $this->Data_Model->temp_towns_db[$town];
  1651. $this->Player_Model->user->transports = $transports;
  1652. $this->db->set('gold', $this->Player_Model->user->gold);
  1653. $this->db->set('transports', $transports);
  1654. $this->db->where(array('id' => $this->Player_Model->user->id));
  1655. $this->db->update($this->session->userdata('universe').'_users');
  1656. $this->db->insert($this->session->userdata('universe').'_missions', array('user' => $this->Player_Model->user->id, 'from' => $this->Player_Model->now_town->id, 'to' => $town, 'loading_from_start' => time(), 'mission_start' => time(), 'mission_type' => 3, 'trade_wood_count' => $wood_count, 'trade_wine_count' => $wine_count, 'trade_marble_count' => $marble_count, 'trade_crystal_count' => $crystal_count, 'trade_sulfur_count' => $sulfur_count, 'gold' => $gold_need, 'trade_wood_cost' => $wood_cost, 'trade_wine_cost' => $wine_cost, 'trade_marble_cost' => $marble_cost, 'trade_crystal_cost' => $crystal_cost, 'trade_sulfur_cost' => $sulfur_cost, 'ship_transport' => $transporters));
  1657. $this->Player_Model->now_town->actions = $this->Player_Model->now_town->actions - 1;
  1658. $this->db->set('actions', $this->Player_Model->now_town->actions);
  1659. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  1660. $this->db->update($this->session->userdata('universe').'_towns');
  1661. }
  1662. }
  1663. if($type == 0)
  1664. {
  1665. $trade_town = $this->Data_Model->temp_towns_db[$town];
  1666. // subtract the remainder
  1667. $wood = $this->Player_Model->now_town->wood - $wood_count;
  1668. $wine = $this->Player_Model->now_town->wine - $wine_count;
  1669. $marble = $this->Player_Model->now_town->marble - $marble_count;
  1670. $crystal = $this->Player_Model->now_town->crystal - $crystal_count;
  1671. $sulfur = $this->Player_Model->now_town->sulfur - $sulfur_count;
  1672. if ($wood >= 0 and $wine >=0 and $marble >= 0 and $crystal >= 0 and $sulfur >=0)
  1673. {
  1674. $this->Player_Model->user->transports = $transports;
  1675. $this->db->set('transports', $transports);
  1676. $this->db->where(array('id' => $this->Player_Model->user->id));
  1677. $this->db->update($this->session->userdata('universe').'_users');
  1678. $this->db->insert($this->session->userdata('universe').'_missions', array('user' => $this->Player_Model->user->id, 'from' => $this->Player_Model->now_town->id, 'to' => $town, 'loading_from_start' => time(), 'mission_type' => 4, 'wood' => $wood_count, 'wine' => $wine_count, 'marble' => $marble_count, 'crystal' => $crystal_count, 'sulfur' => $sulfur_count, 'trade_wood_cost' => $wood_cost, 'trade_wine_cost' => $wine_cost, 'trade_marble_cost' => $marble_cost, 'trade_crystal_cost' => $crystal_cost, 'trade_sulfur_cost' => $sulfur_cost, 'ship_transport' => $transporters));
  1679. $this->Player_Model->now_town->wood = $wood;
  1680. $this->Player_Model->now_town->wine = $wine;
  1681. $this->Player_Model->now_town->marble = $marble;
  1682. $this->Player_Model->now_town->crystal = $crystal;
  1683. $this->Player_Model->now_town->sulfur = $sulfur;
  1684. $this->Player_Model->now_town->aactions = $this->Player_Model->now_town->actions - 1;
  1685. $this->db->set('wood', $wood);
  1686. $this->db->set('wine', $wine);
  1687. $this->db->set('marble', $marble);
  1688. $this->db->set('crystal', $crystal);
  1689. $this->db->set('sulfur', $sulfur);
  1690. $this->db->set('actions', $this->Player_Model->now_town->actions);
  1691. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  1692. $this->db->update($this->session->userdata('universe').'_towns');
  1693. }
  1694. }
  1695. }
  1696. }
  1697. }
  1698. $this->show('branchOffice');
  1699. }
  1700. else
  1701. {
  1702. $this->Error($this->lang->line('enough_action_points'));
  1703. }
  1704. }
  1705. function tradeRoute($delete_id = 0)
  1706. {
  1707. if ($delete_id > 0)
  1708. {
  1709. if (isset($this->Player_Model->trade_routes[$delete_id]))
  1710. {
  1711. $this->db->delete($this->session->userdata('universe').'_trade_routes', array('id' => $delete_id));
  1712. }
  1713. }
  1714. else
  1715. {
  1716. if (isset($_POST['city1Id']) and isset($_POST['city2Id']) and $_POST['city1Id'] > 0 and $_POST['city2Id'] > 0 and $_POST['city1Id'] != $_POST['city2Id'])
  1717. {
  1718. $from = floor($_POST['city1Id']);
  1719. $to = floor($_POST['city2Id']);
  1720. $tradegood = floor($_POST['tradegood']);
  1721. $time = floor($_POST['time']);
  1722. $number = floor($_POST['number']);
  1723. $update_time = route_time(time(), $time);
  1724. if ($from > 0 and $to > 0 and $tradegood >= 0 and $tradegood <= 4 and $time >= 0 and $time <= 24 and $number > 0)
  1725. {
  1726. if(!isset($_POST['save']))
  1727. {
  1728. if (SizeOf($this->Player_Model->trade_routes) == 0)
  1729. {
  1730. $this->db->insert($this->session->userdata('universe').'_trade_routes', array('user' => $this->Player_Model->user->id, 'from' => $from, 'to' => $to, 'start_time' => time(), 'update_time' => $update_time, 'send_resource' => $tradegood, 'send_time' => $time, 'send_count' => $number));
  1731. }
  1732. else
  1733. {
  1734. if($this->Player_Model->user->ambrosy >= 10)
  1735. {
  1736. $this->db->insert($this->session->userdata('universe').'_trade_routes', array('user' => $this->Player_Model->user->id, 'from' => $from, 'to' => $to, 'start_time' => time(), 'update_time' => $update_time, 'send_resource' => $tradegood, 'send_time' => $time, 'send_count' => $number));
  1737. $this->db->set('ambrosy', $this->Player_Model->user->ambrosy - 10);
  1738. $this->db->where(array('id' => $this->Player_Model->user->id));
  1739. $this->db->update($this->session->userdata('universe').'_users');
  1740. }
  1741. }
  1742. }
  1743. else
  1744. {
  1745. if (isset($_POST['route']) and isset($this->Player_Model->trade_routes[floor($_POST['route'])]))
  1746. {
  1747. $this->db->set('from', $from);
  1748. $this->db->set('to', $to);
  1749. $this->db->set('update_time', $update_time);
  1750. $this->db->set('send_resource', $tradegood);
  1751. $this->db->set('send_time', $time);
  1752. $this->db->set('send_count', $number);
  1753. $this->db->where(array('id' => floor($_POST['route'])));
  1754. $this->db->update($this->session->userdata('universe').'_trade_routes');
  1755. }
  1756. }
  1757. }
  1758. }
  1759. else
  1760. {
  1761. $this->Error($this->lang->line('select_city'));
  1762. }
  1763. }
  1764. redirect($this->config->item('base_url').'game/tradeAdvisorTradeRoute/', 'refresh');
  1765. }
  1766. function transport($island = 0, $town = 0)
  1767. {
  1768. $island = floor($island);
  1769. $town = floor($town);
  1770. if ($this->Player_Model->now_town->actions > 0)
  1771. {
  1772. if ($island > 0 and $town >= 0)
  1773. {
  1774. $this->Data_Model->Load_Town($town);
  1775. $this->load->model('Island_Model');
  1776. $this->Island_Model->Load_Island($island);
  1777. // Get the details
  1778. $cargo_wood = isset($_POST['cargo_resource']) ? $_POST['cargo_resource'] : 0;
  1779. $cargo_wine = isset($_POST['cargo_tradegood1']) ? $_POST['cargo_tradegood1'] : 0;
  1780. $cargo_marble = isset($_POST['cargo_tradegood2']) ? $_POST['cargo_tradegood2'] : 0;
  1781. $cargo_crystal = isset($_POST['cargo_tradegood3']) ? $_POST['cargo_tradegood3'] : 0;
  1782. $cargo_sulfur = isset($_POST['cargo_tradegood4']) ? $_POST['cargo_tradegood4'] : 0;
  1783. $transporters = isset($_POST['transporters']) ? $_POST['transporters'] : 0;
  1784. // Count the resources of
  1785. $wood = $this->Player_Model->now_town->wood - $cargo_wood;
  1786. $wine = $this->Player_Model->now_town->wine - $cargo_wine;
  1787. $marble = $this->Player_Model->now_town->marble - $cargo_marble;
  1788. $crystal = $this->Player_Model->now_town->crystal - $cargo_crystal;
  1789. $sulfur = $this->Player_Model->now_town->sulfur - $cargo_sulfur;
  1790. $transports = $this->Player_Model->user->transports - $transporters;
  1791. if(isset($this->Data_Model->temp_towns_db[$town]) and ($this->Player_Model->user->transports >= $transporters) and ($transporters != 0) and ($wood >= 0) and ($wine >= 0) and ($crystal >= 0) and ($sulfur >= 0) and ($transports >= 0) and ($transporters*$this->config->item('transport_capacity') >= $cargo_wood + $cargo_wine + $cargo_marble + $cargo_crystal + $cargo_sulfur) and ($cargo_wood + $cargo_wine + $cargo_marble + $cargo_crystal + $cargo_sulfur != 0))
  1792. {
  1793. // subtract the resources
  1794. $this->Player_Model->now_town->wood = $wood;
  1795. $this->Player_Model->now_town->wine = $wine;
  1796. $this->Player_Model->now_town->marble = $marble;
  1797. $this->Player_Model->now_town->crystal = $crystal;
  1798. $this->Player_Model->now_town->sulfur = $sulfur;
  1799. $this->Player_Model->now_town->actions = $this->Player_Model->now_town->actions - 1;
  1800. $this->Player_Model->now_town->wood = $wood;
  1801. $this->Player_Model->now_town->wood = $wood;
  1802. $this->db->set('wood', $wood);
  1803. $this->db->set('wine', $wine);
  1804. $this->db->set('marble', $marble);
  1805. $this->db->set('crystal', $crystal);
  1806. $this->db->set('sulfur', $sulfur);
  1807. $this->db->set('actions', $this->Player_Model->now_town->actions);
  1808. $this->db->where(array('id' => $this->Player_Model->now_town->id));
  1809. $this->db->update($this->session->userdata('universe').'_towns');
  1810. $this->Player_Model->user->transports = $transports;
  1811. $this->db->set('transports', $transports);
  1812. $this->db->where(array('id' => $this->Player_Model->user->id));
  1813. $this->db->update($this->session->userdata('universe').'_users');
  1814. // add a mission
  1815. $this->db->insert($this->session->userdata('universe').'_missions', array('user' => $this->Player_Model->user->id, 'from' => $this->Player_Model->now_town->id, 'to' => $town, 'loading_from_start' => time(), 'mission_type' => 2, 'wood' => $cargo_wood, 'wine' => $cargo_wine, 'marble' => $cargo_marble, 'crystal' => $cargo_crystal, 'sulfur' => $cargo_sulfur, 'ship_transport' => $transporters));
  1816. // Bring the player to the port
  1817. $this->show('port');
  1818. }
  1819. else
  1820. {
  1821. $this->show('error', $this->lang->line('trade_fleet_no_freight'));
  1822. }
  1823. }
  1824. else
  1825. {
  1826. $this->show('port');
  1827. }
  1828. }
  1829. else
  1830. {
  1831. $this->Error($this->lang->line('enough_action_points'));
  1832. }
  1833. }
  1834. function transporter($position = 0)
  1835. {
  1836. $cost = $this->Data_Model->transport_cost_by_count($this->Player_Model->all_transports);
  1837. if ($cost > 0 and $this->Player_Model->user->gold >= $cost)
  1838. {
  1839. $this->Player_Model->user->gold = $this->Player_Model->user->gold - $cost;
  1840. $this->Player_Model->user->points_transports = $this->Player_Model->user->points_transports + $cost/100;
  1841. $this->Player_Model->user->transports++;
  1842. $this->Player_Model->all_transports++;
  1843. $this->db->set('gold', $this->Player_Model->user->gold);
  1844. $this->db->set('transports', $this->Player_Model->user->transports);
  1845. $this->db->set('points_transports', $this->Player_Model->user->points_transports);
  1846. $this->db->where(array('id' => $this->Player_Model->user->id));
  1847. $this->db->update($this->session->userdata('universe').'_users');
  1848. }
  1849. $this->show('port', $position);
  1850. }
  1851. /**
  1852. * Tutorial: Go to next learning
  1853. * @param <string> $action
  1854. */
  1855. function tutorials($action, $id = 0)
  1856. {
  1857. $id = floor($id);
  1858. switch($action)
  1859. {
  1860. // The next phase of learning
  1861. case 'next':
  1862. //$this->Player_Model->user->tutorial = $this->Player_Model->user->tutorial + 1;
  1863. $this->db->query('UPDATE `'.$this->session->userdata('universe').'_users'.'` SET `tutorial`=`tutorial`+1 WHERE `id`="'.$this->session->userdata('id').'"');
  1864. break;
  1865. // Installation step
  1866. case 'set':
  1867. //$this->Player_Model->user->tutorial = $id;
  1868. $this->db->query('UPDATE `'.$this->session->userdata('universe').'_users'.'` SET `tutorial`='.$id.' WHERE `id`="'.$this->session->userdata('id').'"');
  1869. break;
  1870. }
  1871. }
  1872. /**
  1873. * Improving buildings
  1874. * @param <int> $position
  1875. */
  1876. function upgrade($position)
  1877. {
  1878. $position = floor($position);
  1879. $level_text = 'pos'.$position.'_level';
  1880. $type_text = 'pos'.$position.'_type';
  1881. if ($this->Player_Model->now_town->$level_text > 0){
  1882. $this->build($position, $this->Player_Model->now_town->$type_text, $this->Data_Model->building_class_by_type($this->Player_Model->now_town->$type_text));
  1883. }
  1884. else
  1885. {
  1886. $this->Error($this->lang->line('position_of').' '.$position.' '.$this->lang->line('no_build_improve'));
  1887. }
  1888. }
  1889. function winter($special)
  1890. {
  1891. setcookie('Winter', $special);
  1892. redirect($this->config->item('base_url').'game/city/', 'refresh');
  1893. }
  1894. /**
  1895. * Update the work
  1896. * @param <string> $type
  1897. * @param <int> $id
  1898. */
  1899. function workers($type = 'resource', $id = 0)
  1900. {
  1901. // Education - hiring workers at a sawmill
  1902. if ($type == 'resource' and $this->Player_Model->user->tutorial <= 2)
  1903. {
  1904. $this->tutorials('set', 3);
  1905. }
  1906. if ($type == 'academy' and $this->Player_Model->user->tutorial <= 6)
  1907. {
  1908. $this->tutorials('set', 7);
  1909. }
  1910. // Workers
  1911. if (isset($_POST['rw']))
  1912. {
  1913. $level = $this->Player_Model->now_island->wood_level;
  1914. $cost = $this->Data_Model->island_cost(0, $level);
  1915. if ($this->Player_Model->research->res2_5 > 0)
  1916. {
  1917. $cost['workers'] = $cost['workers']*1.5;
  1918. }
  1919. if ($cost['workers'] >= $_POST['rw'])
  1920. {
  1921. $all = $this->Player_Model->now_town->workers + $this->Player_Model->now_town->peoples;
  1922. if ($all >= $_POST['rw'])
  1923. {
  1924. $this->Player_Model->now_town->workers = floor($_POST['rw']);
  1925. $this->Player_Model->now_town->peoples = $all - floor($_POST['rw']);
  1926. $this->db->set('workers', $this->Player_Model->now_town->workers);
  1927. $this->db->set('peoples', $this->Player_Model->now_town->peoples);
  1928. $this->db->where(array('id' => $this->Player_Model->town_id));
  1929. $this->db->update($this->session->userdata('universe').'_towns');
  1930. }
  1931. }
  1932. }
  1933. // Workers 2
  1934. if (isset($_POST['tw']))
  1935. {
  1936. $level = $this->Player_Model->now_island->trade_level;
  1937. $cost = $this->Data_Model->island_cost(1, $level);
  1938. if ($this->Player_Model->research->res2_5 > 0)
  1939. {
  1940. $cost['workers'] = $cost['workers']*1.5;
  1941. }
  1942. if ($cost['workers'] >= $_POST['tw'])
  1943. {
  1944. $all = $this->Player_Model->now_town->tradegood + $this->Player_Model->now_town->peoples;
  1945. if ($all >= $_POST['tw'])
  1946. {
  1947. $this->Player_Model->now_town->tradegood = floor($_POST['tw']);
  1948. $this->Player_Model->now_town->peoples = $all - floor($_POST['tw']);
  1949. $this->db->set('tradegood', $this->Player_Model->now_town->tradegood);
  1950. $this->db->set('peoples', $this->Player_Model->now_town->peoples);
  1951. $this->db->where(array('id' => $this->Player_Model->town_id));
  1952. $this->db->update($this->session->userdata('universe').'_towns');
  1953. }
  1954. }
  1955. }
  1956. // Scientists
  1957. if (isset($_POST['s']) and $id > 0 and $this->Player_Model->already_build[$this->Player_Model->town_id][3])
  1958. {
  1959. $level_text = 'pos'.$id.'_level';
  1960. $type_text = 'pos'.$id.'_type';
  1961. $level = $this->Player_Model->now_town->$level_text;
  1962. $max_scientists = $this->Data_Model->scientists_by_level($level);
  1963. if ($max_scientists >= $_POST['s'])
  1964. {
  1965. $all = $this->Player_Model->now_town->scientists + $this->Player_Model->now_town->peoples;
  1966. if ($all >= $_POST['s'] )
  1967. {
  1968. $this->Player_Model->now_town->scientists = floor($_POST['s']);
  1969. $this->Player_Model->now_town->peoples = $all - floor($_POST['s']);
  1970. $this->db->set('scientists', $this->Player_Model->now_town->scientists);
  1971. $this->db->set('peoples', $this->Player_Model->now_town->peoples);
  1972. $this->db->where(array('id' => $this->Player_Model->town_id));
  1973. $this->db->update($this->session->userdata('universe').'_towns');
  1974. }
  1975. }
  1976. }
  1977. if (isset($_POST['rw']) or isset($_POST['tw']))
  1978. {
  1979. $this->load->model('Island_Model');
  1980. $this->Island_Model->Load_Island($id);
  1981. }
  1982. $this->show($type, $id);
  1983. }
  1984. }
  1985. /* End of file actions.php */
  1986. /* Location: ./system/application/controllers/actions.php */