PageRenderTime 38ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/solar27/include/game/classes/template.php

https://bitbucket.org/sebs/mosolar
PHP | 537 lines | 414 code | 60 blank | 63 comment | 22 complexity | e24bb3b94475dce6ded003027c4ce9ef MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-2.0, MIT, GPL-2.0
  1. <?php
  2. // Solar Imperium is licensed under GPL2, Check LICENSE.TXT for mode details //
  3. class Template
  4. {
  5. var $DB;
  6. var $TPL;
  7. var $page_name;
  8. var $events_data;
  9. var $notices_data;
  10. var $coord;
  11. var $events_count;
  12. var $events_height;
  13. var $ingame;
  14. var $game_id;
  15. //////////////////////////////////////////////////////////////////////
  16. // Constructor
  17. //////////////////////////////////////////////////////////////////////
  18. function Template($DB,$game_id)
  19. {
  20. $this->DB = $DB;
  21. $this->game_id = $game_id;
  22. $this->TPL = new Smarty();
  23. if (isset($_GET["XML"])) {
  24. $this->TPL->template_dir = "../templates/xml/game/";
  25. $this->TPL->compile_dir = "../templates_c/xml/game/";
  26. } else {
  27. $this->TPL->template_dir = "../templates/game/";
  28. $this->TPL->compile_dir = "../templates_c/game/";
  29. }
  30. $this->events_data = array();
  31. $this->events_data["SYSTEM"] = "";
  32. $this->events_data["WARFARE"] = "";
  33. $this->events_data["COMMUNICATION"] = "";
  34. $this->events_data["DIPLOMACY"] = "";
  35. $this->events_count = array();
  36. $this->events_count["SYSTEM"] = 0;
  37. $this->events_count["WARFARE"] = 0;
  38. $this->events_count["COMMUNICATION"] = 0;
  39. $this->events_count["DIPLOMACY"] = 0;
  40. $this->notices_data = "";
  41. $this->coord = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_coordinator");
  42. if (!$this->coord) trigger_error($this->DB->ErrorMsg());
  43. $this->coord = $this->coord->fields;
  44. $this->ingame = true;
  45. }
  46. //////////////////////////////////////////////////////////////////////
  47. // Set Page
  48. //////////////////////////////////////////////////////////////////////
  49. function setPage($page_name)
  50. {
  51. $this->page_name = $page_name;
  52. $file = explode("/",$_SERVER["SCRIPT_FILENAME"]);
  53. $file = $file[count($file)-1];
  54. if (($file == "ingame_starmap.php") || ($file == "destroy_empire.php"))
  55. $this->ingame = true;
  56. else
  57. $this->ingame = false;
  58. //
  59. //
  60. // if ((isset($_SESSION["empire_id"])) &&
  61. // ($file != "scoreboard.php")&&
  62. // ($file != "starmap.php")&&
  63. // ($file != "destroy_empire.php")&&
  64. // ($file != "show_active.php")&&
  65. // ($file != "show_empire.php")&&
  66. // ($file != "show_coalition.php")&&
  67. // ($file != "hall_of_fame.php")) $this->ingame = true; else $this->ingame = false;
  68. }
  69. //////////////////////////////////////////////////////////////////////
  70. // Render the page
  71. //////////////////////////////////////////////////////////////////////
  72. function render()
  73. {
  74. if (isset($_GET["NOTICE"])) $this->showNotice($_GET["NOTICE"],false);
  75. if (isset($_GET["WARNING"])) $this->showNotice($_GET["WARNING"],true);
  76. $file = explode("/",$_SERVER["SCRIPT_FILENAME"]);
  77. $file = $file[count($file)-1];
  78. if (($file == "show_empire.php") ||
  79. ($file == "show_active.php") ||
  80. ($file == "galaxypedia.php")) $render_banners = false;
  81. $this->TPL->assign("color_normal",TPL_COLOR_NORMAL);
  82. $this->TPL->assign("color_highlight",TPL_COLOR_HIGHLIGHT);
  83. // dynamic variables
  84. $this->TPL->assign("game_date",date("m/d/y",$this->coord["date"]));
  85. $date = time(NULL) - $this->coord["date"];
  86. $this->TPL->assign("game_life",floor($date / (60*60*24)));
  87. $online_players = $this->DB->Execute("SELECT COUNT(*) FROM game".$this->game_id."_tb_session");
  88. $this->TPL->assign("online_players",$online_players->fields[0]);
  89. $total_players = $this->DB->Execute("SELECT COUNT(*) FROM game".$this->game_id."_tb_empire WHERE active=1");
  90. $this->TPL->assign("total_players",$total_players->fields[0]);
  91. $this->TPL->assign("events_script","");
  92. if ($this->notices_data != "") {
  93. //$this->TPL->assign("events_data",$this->notices_data);
  94. $script = "<script>
  95. CustomAlert('$this->notices_data');
  96. </script>
  97. ";
  98. $this->TPL->assign("events_script",$script);
  99. } else {
  100. // $smarty = new Smarty();
  101. // $smarty->template_dir = "../templates/game/";
  102. // $smarty->compile_dir = "../templates_c/game/";
  103. if ($this->events_height > 500) $this->events_height = 500;
  104. // $smarty->assign("events_div_size",$this->events_height+50);
  105. // $smarty->assign("events_content_size",$this->events_height."px");
  106. // $smarty->assign("events_data_content_system",$this->events_data["SYSTEM"]);
  107. // $smarty->assign("events_data_content_warfare",$this->events_data["WARFARE"]);
  108. // $smarty->assign("events_data_content_communication",$this->events_data["COMMUNICATION"]);
  109. // $smarty->assign("events_data_content_diplomacy",$this->events_data["DIPLOMACY"]);
  110. // $smarty->assign("events_count_content_system",$this->events_count["SYSTEM"]);
  111. // $smarty->assign("events_count_content_warfare",$this->events_count["WARFARE"]);
  112. // $smarty->assign("events_count_content_communication",$this->events_count["COMMUNICATION"]);
  113. // $smarty->assign("events_count_content_diplomacy",$this->events_count["DIPLOMACY"]);
  114. //
  115. // if ($this->events_count["DIPLOMACY"] != 0) $smarty->assign("script_code","onClickDiplomacy();");
  116. // if ($this->events_count["COMMUNICATION"] != 0) $smarty->assign("script_code","onClickCommunication();");
  117. // if ($this->events_count["WARFARE"] != 0) $smarty->assign("script_code","onClickWarfare();");
  118. // if ($this->events_count["SYSTEM"] != 0) $smarty->assign("script_code","onClickSystem();");
  119. // if (($this->events_count["DIPLOMACY"] + $this->events_count["COMMUNICATION"] + $this->events_count["WARFARE"] + $this->events_count["SYSTEM"]) == 0)
  120. // $this->TPL->assign("events_data","");
  121. // else
  122. // $this->TPL->assign("events_data",$smarty->fetch("events_data.html").$this->notices_data);
  123. $script = "<script>
  124. events_data_content_system = Base64.decode('".base64_encode(stripslashes($this->events_data["SYSTEM"]))."');
  125. events_data_content_warfare = Base64.decode('".base64_encode(stripslashes($this->events_data["WARFARE"]))."');
  126. events_data_content_communication = Base64.decode('".base64_encode(stripslashes($this->events_data["COMMUNICATION"]))."');
  127. events_data_content_diplomacy = Base64.decode('".base64_encode(stripslashes($this->events_data["DIPLOMACY"]))."');
  128. document.getElementById('events_count_content_system').innerHTML = '".$this->events_count["SYSTEM"]."';
  129. document.getElementById('events_count_content_warfare').innerHTML = '".$this->events_count["WARFARE"]."';
  130. document.getElementById('events_count_content_communication').innerHTML = '".$this->events_count["COMMUNICATION"]."';
  131. document.getElementById('events_count_content_diplomacy').innerHTML = '".$this->events_count["DIPLOMACY"]."';
  132. renderEvents(".($this->events_height+50).");
  133. </script>
  134. ";
  135. $this->TPL->assign("events_script",$script);
  136. }
  137. // EMBED in a page
  138. if ($this->ingame) {
  139. $this->TPL->assign("page_content",$this->TPL->fetch($this->page_name));
  140. $this->TPL->assign("page_title",$file);
  141. $this->TPL->assign("version",CONF_GAME_VERSION);
  142. $this->TPL->assign("game_name",CONF_GAME_NAME);
  143. return $this->TPL->fetch("ingame.html");
  144. } else {
  145. $this->TPL->assign("page_content",$this->TPL->fetch($this->page_name));
  146. $this->TPL->assign("page_title",$file);
  147. $this->TPL->assign("version",CONF_GAME_VERSION);
  148. $this->TPL->assign("game_name",CONF_GAME_NAME);
  149. return $this->TPL->fetch("simple_frame.html");
  150. }
  151. }
  152. //////////////////////////////////////////////////////////////////////
  153. // Show notice
  154. //////////////////////////////////////////////////////////////////////
  155. function showNotice($msg,$isWarning = false)
  156. {
  157. // $smarty = new Smarty();
  158. // $smarty->template_dir = "../templates/game/";
  159. // $smarty->compile_dir = "../templates_c/game/";
  160. //
  161. // $smarty->assign("msg",stripslashes($msg));
  162. // if ($isWarning)
  163. // $this->notices_data .= $smarty->fetch("warning".($this->ingame==true?"_ingame":"").".html");
  164. // else
  165. // $this->notices_data .= $smarty->fetch("notice".($this->ingame==true?"_ingame":"").".html");
  166. // $this->notices_data = str_replace("\n","",$this->notices_data);
  167. // $this->notices_data = str_replace("\r","",$this->notices_data);
  168. $this->notices_data = str_replace("\n","",stripslashes($msg));
  169. $this->notices_data = str_replace("\r","",$this->notices_data);
  170. // unset($smarty);
  171. }
  172. //////////////////////////////////////////////////////////////////////
  173. // Show event
  174. //////////////////////////////////////////////////////////////////////
  175. function showEvent($msg,$isWarning = false, $filter = "SYSTEM")
  176. {
  177. $smarty = new Smarty();
  178. $smarty->template_dir = "../templates/game/";
  179. $smarty->compile_dir = "../templates_c/game/";
  180. $tpl_filename = "";
  181. if ($isWarning)
  182. $tpl_filename = "warning".($this->ingame==true?"_ingame":"").".html";
  183. else
  184. $tpl_filename = "notice".($this->ingame==true?"_ingame":"").".html";
  185. $smarty->assign("msg",stripslashes($msg));
  186. $data = $smarty->fetch($tpl_filename);
  187. $data = str_replace("\n","",$data);
  188. $data = str_replace("\r","",$data);
  189. $data = str_replace("'","\'",$data);
  190. $this->events_data[$filter] .= $data;
  191. $this->events_count[$filter]++;
  192. }
  193. //////////////////////////////////////////////////////////////////////
  194. // Show notice
  195. //////////////////////////////////////////////////////////////////////
  196. function getNotice($msg,$isWarning = false)
  197. {
  198. $smarty = new Smarty();
  199. $smarty->template_dir = "../templates/game/";
  200. $smarty->compile_dir = "../templates_c/game/";
  201. $msg = $msg[1];
  202. $smarty->assign("msg",stripslashes($msg));
  203. $notice_data = "";
  204. if ($isWarning)
  205. $notice_data = $smarty->fetch("warning".($this->ingame==true?"_ingame":"").".html");
  206. else
  207. $notice_data = $smarty->fetch("notice".($this->ingame==true?"_ingame":"").".html");
  208. unset($smarty);
  209. return $notice_data;
  210. }
  211. //////////////////////////////////////////////////////////////////////
  212. // Show file
  213. //////////////////////////////////////////////////////////////////////
  214. function showFile($file)
  215. {
  216. return $this->TPL->fetch($file);
  217. }
  218. //////////////////////////////////////////////////////////////////////
  219. // Set variable
  220. //////////////////////////////////////////////////////////////////////
  221. function setVar($name,$value)
  222. {
  223. $this->TPL->assign($name,$value);
  224. }
  225. //////////////////////////////////////////////////////////////////////
  226. // Set loop
  227. //////////////////////////////////////////////////////////////////////
  228. function setLoop($name,$value)
  229. {
  230. $this->TPL->assign($name,$value);
  231. }
  232. //////////////////////////////////////////////////////////////////////
  233. //
  234. //////////////////////////////////////////////////////////////////////
  235. function formatNumber($number)
  236. {
  237. $output = number_format($number);
  238. return $output;
  239. }
  240. //////////////////////////////////////////////////////////////////////
  241. //
  242. //////////////////////////////////////////////////////////////////////
  243. function formatCredits($number,$html=true)
  244. {
  245. $output = $this->formatNumber($number);
  246. if ($html)
  247. return "<b style=\"color:".TPL_COLOR_NORMAL."\">$output</b>".
  248. "&nbsp;<b style=\"color:".TPL_COLOR_HIGHLIGHT."\">Cr.</b>";
  249. else
  250. return $output." Cr.";
  251. }
  252. //////////////////////////////////////////////////////////////////////
  253. //
  254. //////////////////////////////////////////////////////////////////////
  255. function formatFood($number,$html=true)
  256. {
  257. $output = $this->formatNumber($number);
  258. if ($html)
  259. return "<b style=\"color:".TPL_COLOR_NORMAL."\">$output</b>".
  260. "&nbsp;<b style=\"color:".TPL_COLOR_HIGHLIGHT."\">Mgt.</b>";
  261. else
  262. return $output." Mgt.";
  263. }
  264. //////////////////////////////////////////////////////////////////////
  265. //
  266. //////////////////////////////////////////////////////////////////////
  267. function formatPopulation($number,$html=true)
  268. {
  269. $output = $this->formatNumber($number);
  270. if ($html)
  271. return "<b style=\"color:".TPL_COLOR_NORMAL."\">$output</b>".
  272. "&nbsp;<b style=\"color:".TPL_COLOR_HIGHLIGHT."\">M</b>";
  273. else
  274. return $output." M";
  275. }
  276. //////////////////////////////////////////////////////////////////////
  277. //
  278. //////////////////////////////////////////////////////////////////////
  279. function formatTime($date)
  280. {
  281. $days = floor($date / (60*60*24));
  282. $date -= ($days * 60*60*24);
  283. $hours = str_pad(floor($date / (60*60)),2,"0",STR_PAD_LEFT);
  284. $date -= ($hours * 60*60);
  285. $minutes = str_pad(floor($date / (60)),2,"0",STR_PAD_LEFT);
  286. return $days."d ".$hours.":".$minutes;
  287. }
  288. //////////////////////////////////////////////////////////////////////
  289. //
  290. //////////////////////////////////////////////////////////////////////
  291. function displayEmpireHTML($id,$emperor,$empire,$your_networth)
  292. {
  293. global $DB,$CONF_PREMIUM_MEMBERS;
  294. $emperor = stripslashes($emperor);
  295. $empire = stripslashes($empire);
  296. $text = "<table style=\"margin-bottom: 0px;display:inline;\" border=0 cellspacing=0 cellpadding=1><tr>";
  297. $text .= "<td><img src=\"img_logo.php?empire=$id\" border=\"1\" width=\"32\" height=\"32\" bordercolor=\"white\" style=\"border-color:white\"></td>";
  298. $query = "SELECT game".$this->game_id."_tb_coalition.logo,game".$this->game_id."_tb_member.* FROM game".$this->game_id."_tb_coalition,game".$this->game_id."_tb_member WHERE game".$this->game_id."_tb_member.empire='$id' AND game".$this->game_id."_tb_coalition.id = game".$this->game_id."_tb_member.coalition";
  299. $rs = $DB->Execute($query);
  300. if (!$rs) trigger_error($query . "<br/><br/>".$DB->ErrorMsg());
  301. if (!$rs->EOF)
  302. {
  303. $text .= "<td><img src=\"img_logo.php?data=".$rs->fields["logo"]."\" border=\"1\" width=\"32\" height=\"32\" bordercolor=\"white\" style=\"border-color:white\"></td>";
  304. }
  305. $text .= "<td>&nbsp;<a href=\"javascript:show_info($id);\" style=\"text-decoration:none\"><b style=\"color:white\">$emperor</b><b style=\"color:#ff9999\">@</b><b style=\"color:#CACACA\">$empire</b></a>";
  306. if ($your_networth!="") {
  307. $rs = $DB->Execute("SELECT networth FROM game".$this->game_id."_tb_empire WHERE id='".intval($id)."'");
  308. $enemy_networth = $rs->fields["networth"];
  309. if ($enemy_networth == 0) $enemy_networth = 1;
  310. if ($your_networth == 0) $your_networth = 1;
  311. $percent = round(($enemy_networth / $your_networth)*100,3);
  312. $star = "<img src=\"../images/common/star.gif\" border=\"0\">";
  313. $stars = $star.$star.$star;
  314. if ($percent <= 80) $stars = $star.$star;
  315. if ($percent <= 40) $stars = $star;
  316. if ($percent >= 120) $stars = $star.$star.$star.$star;
  317. if ($percent >= 140) $stars = $star.$star.$star.$star.$star;
  318. $text .= "&nbsp;$stars";
  319. }
  320. $text .="</td></tr></table>";
  321. return $text;
  322. }
  323. //////////////////////////////////////////////////////////////////////
  324. //
  325. //////////////////////////////////////////////////////////////////////
  326. function displayEmpire($emperor,$empire,$your_networth)
  327. {
  328. global $DB,$CONF_PREMIUM_MEMBERS;
  329. $emperor = stripslashes($emperor);
  330. $empire = stripslashes($empire);
  331. $text = $emperor ."@".$empire." ";
  332. if ($your_networth!="") {
  333. $rs = $DB->Execute("SELECT networth FROM game".$this->game_id."_tb_empire WHERE emperor='".addslashes($emperor)."'");
  334. $enemy_networth = $rs->fields["networth"];
  335. if ($enemy_networth == 0) $enemy_networth = 1;
  336. if ($your_networth == 0) $your_networth = 1;
  337. $percent = round(($enemy_networth / $your_networth)*100,3);
  338. $star = "*";
  339. $stars = $star.$star.$star;
  340. if ($percent <= 80) $stars = $star.$star;
  341. if ($percent <= 40) $stars = $star;
  342. if ($percent >= 120) $stars = $star.$star.$star.$star;
  343. if ($percent >= 140) $stars = $star.$star.$star.$star.$star;
  344. $text .= " $stars";
  345. }
  346. return $text;
  347. }
  348. //////////////////////////////////////////////////////////////////////
  349. //
  350. //////////////////////////////////////////////////////////////////////
  351. function showMessage($msg_data)
  352. {
  353. $params = unserialize($msg_data["params"]);
  354. $smarty = new Smarty();
  355. $smarty->template_dir = "../templates/game/";
  356. $smarty->compile_dir = "../templates_c/game/";
  357. $smarty->assign("subject",stripslashes($params["subject"]));
  358. $smarty->assign("content",stripslashes($params["content"]));
  359. $smarty->assign("date",$this->formatTime(time(NULL) - $msg_data["date"]));
  360. $rs = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_empire WHERE id='".$msg_data["event_from"]."'");
  361. $smarty->assign("author",$this->displayEmpireHTML($rs->fields["id"],$rs->fields["emperor"],$rs->fields["name"],""));
  362. $smarty->assign("id",$msg_data["id"]);
  363. if ($rs->fields["id"] == $_SESSION["empire_id"])
  364. $smarty->assign("bgcolor","#336666");
  365. else
  366. $smarty->assign("bgcolor","#447777");
  367. $msg_content = $smarty->fetch("message/body.html");
  368. unset($smarty);
  369. return $msg_content;
  370. }
  371. //////////////////////////////////////////////////////////////////////
  372. //
  373. //////////////////////////////////////////////////////////////////////
  374. function ShowProductionLevel($percentage) {
  375. $p = floor($percentage / 20);
  376. $html = "<table onmouseover=\"return escape('".T_("Current production level").": $percentage %');\" style=\"display: inline;margin:0px;padding:0px;border:0px solid black\" cellspacing=1 cellpadding=0><tr>";
  377. for ($i=0;$i<10;$i++) {
  378. $bgcolor = "#333333";
  379. if ($i <= $p) {
  380. $bgcolor = "white";
  381. if ($p <= 5) $bgcolor = "blue";
  382. if ($p <= 5) $bgcolor = "#00FF00";
  383. if ($p < 4) $bgcolor = "yellow";
  384. if ($p < 2) $bgcolor = "red";
  385. }
  386. $html .= "<td bgcolor=\"$bgcolor\"><img src=\"../images/game/placeholder.gif\" width=\"2\" height=\"10\" border=\"0\"></td>";
  387. }
  388. $html .= "</tr></table>";
  389. return $html;
  390. }
  391. }
  392. ?>