PageRenderTime 62ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/php/citycont.php

https://bitbucket.org/obsidian/selador
PHP | 333 lines | 279 code | 28 blank | 26 comment | 48 complexity | e6cfd56810e7f1df8bb77b6e513924eb MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. /* citycont.php - Back-end utility functions for city and research views
  3. *
  4. * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
  5. *
  6. * This file is part of Selador, a browser-based fantasy strategy game
  7. *
  8. * This program is distributed under the terms of the GNU Affero General Public License.
  9. *
  10. *
  11. * Selador is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * any later version.
  15. *
  16. * Selador is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with Selador. If not, see <http://www.gnu.org/licenses/>.
  23. **/
  24. // This code is ugly and should be replaced. A ton of it is probably out of use anyway
  25. function Calc_Building_Dep ($bid, $level, &$build, &$research, $what)
  26. {
  27. $found_deps = 0;
  28. if (1 == $what)
  29. {
  30. if (isset ($build[$bid][8]))
  31. {
  32. $dep = $build[$bid][8];
  33. $level = $build[$bid][9];
  34. }
  35. if (isset ($build[$bid][10]))
  36. {
  37. $rdep = $build[$bid][10];
  38. $rlevel = $build[$bid][11];
  39. }
  40. }
  41. else
  42. {
  43. $dep = $research[$bid][8];
  44. $level = $research[$bid][9];
  45. $rdep = $research[$bid][10];
  46. $rlevel = $research[$bid][11];
  47. }
  48. if (isset ($dep))
  49. {
  50. foreach ($dep as $idx => $this_dep)
  51. {
  52. if (isset ($build[$build[1000][$this_dep]]) && ($build[$build[1000][$this_dep]][1] < $level[$idx])) // Pretty sick eh ? :)
  53. {
  54. $found_deps++;
  55. if ($found_deps == 5)
  56. $retstr .= "...";
  57. else
  58. {
  59. if ($found_deps < 5)
  60. {
  61. if (isset ($retstr))
  62. $retstr .= 'hhaamm'.$build[$build[1000][$this_dep]][0]." ".$level[$idx];
  63. else
  64. $retstr = 'hhaamm'.$build[$build[1000][$this_dep]][0]." ".$level[$idx];
  65. $retstr .= "<br>";
  66. }
  67. }
  68. }
  69. }
  70. if (isset ($rdep))
  71. {
  72. foreach ($rdep as $idx => $this_dep)
  73. {
  74. if (isset ($research[$this_dep]) && ($research[$this_dep][1] < $rlevel[$idx]))
  75. {
  76. $found_deps++;
  77. if ($found_deps == 5)
  78. $retstr .= "...";
  79. else
  80. {
  81. if ($found_deps < 5)
  82. {
  83. if (isset ($retstr))
  84. $retstr .= 'ffoorr'.$research[$this_dep][0]." ".$rlevel[$idx];
  85. else
  86. $retstr = 'ffoorr'.$research[$this_dep][0]." ".$rlevel[$idx];
  87. $retstr .= "<br>";
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. if (isset ($retstr))
  95. return $retstr;
  96. else
  97. return false;
  98. }
  99. function city_fetch_db ($activevill)
  100. {
  101. global $me;
  102. $query = "select * from villbuild where villageid=".$activevill;
  103. $res = mysql_query ($query);
  104. while ($row = mysql_fetch_array ($res))
  105. {
  106. $buildlvl[$row['type']] = $row['level'];
  107. }
  108. $query = "select * from buildings where race=".$me->race." order by display_order";
  109. $res = mysql_query ($query);
  110. while ($row = mysql_fetch_array ($res))
  111. {
  112. $bid = $row['type'];
  113. $build[1000][$row['id']] = $bid; // inverse lookup - ugly implementation
  114. $build[$bid][0]=$row['name'];
  115. if (isset ($buildlvl[$bid]) && ($buildlvl[$bid] > 0))
  116. $build[$bid][1]=$buildlvl[$bid];
  117. else
  118. $build[$bid][1]=0;
  119. $build[$bid][2]=$row['costs1'];
  120. $build[$bid][3]=$row['costs2'];
  121. $build[$bid][4]=$row['costs3'];
  122. $build[$bid][15]=$row['costs4'];
  123. $build[$bid][5]=$row['oneliner'];
  124. $build[$bid][6]=$row['description'];
  125. $build[$bid][7]=$row['id'];
  126. for ($temp = 0; $temp < 4; $temp++)
  127. {
  128. if (isset ($row['bdep'.$temp]))
  129. if ($row['bdep'.$temp] > 0)
  130. {
  131. $build[$bid][8][$temp] = $row['bdep'.$temp];
  132. $build[$bid][9][$temp] = $row['bdeplevel'.$temp];
  133. }
  134. if (isset ($row['ddep'.$temp]))
  135. if ($row['ddep'.$temp] > 0)
  136. {
  137. $build[$bid][10][$temp] = $row['ddep'.$temp];
  138. $build[$bid][11][$temp] = $row['ddeplevel'.$temp];
  139. }
  140. }
  141. $build[$bid][12]=$row['pic'];
  142. }
  143. mysql_free_result ($res);
  144. return $build;
  145. }
  146. function research_fetch_db ($uid)
  147. {
  148. global $me;
  149. $query = "select * from userresearch where uid=".$uid;
  150. $res = mysql_query ($query);
  151. while ($row = mysql_fetch_array ($res))
  152. {
  153. $researchlvl[$row['resid']] = $row['level'];
  154. }
  155. $query = "select * from research where race=0 or race=".$me->race." order by display_order";
  156. $result = mysql_query ($query);
  157. while ($row = mysql_fetch_array ($result))
  158. {
  159. $rid = $row['id'];
  160. $research[$rid][0]=$row['name'];
  161. if (isset ($researchlvl[$rid]) && ($researchlvl[$rid] > 0))
  162. $research[$rid][1]=$researchlvl[$rid];
  163. else
  164. $research[$rid][1]=0;
  165. $research[$rid][2]=$row['costs1'];
  166. $research[$rid][3]=$row['costs2'];
  167. $research[$rid][4]=$row['costs3'];
  168. $research[$rid][15]=$row['costs4'];
  169. $research[$rid][5]=$row['oneliner'];
  170. $research[$rid][6]=$row['description'];
  171. for ($temp = 0; $temp < 4; $temp++)
  172. {
  173. if (isset ($row['bdep'.$temp]) && ($row['bdep'.$temp] > 0))
  174. {
  175. $research[$rid][8][$temp] = $row['bdep'.$temp];
  176. $research[$rid][9][$temp] = $row['bdeplevel'.$temp];
  177. }
  178. if (isset ($row['ddep'.$temp]) && ($row['ddep'.$temp] > 0))
  179. {
  180. $research[$rid][10][$temp] = $row['ddep'.$temp];
  181. $research[$rid][11][$temp] = $row['ddeplevel'.$temp];
  182. }
  183. }
  184. $research[$rid][12]=$row['pic'];
  185. }
  186. mysql_free_result ($result);
  187. return $research;
  188. }
  189. /* What = 1 for buildings, 2 for research */
  190. function city_calc_contents ($activevill, $villagename, $precalcpath, $uid, $buildspeed, $what)
  191. {
  192. global $gui;
  193. $display_rows = 8;
  194. $rowcounter = 0;
  195. $outstr = "";
  196. $build = city_fetch_db ($activevill);
  197. $research = research_fetch_db ($uid);
  198. if (1 == $what)
  199. $list =& $build;
  200. else
  201. $list =& $research;
  202. $pages = ceil (count ($list) / $display_rows);
  203. // echo "Pages: ".$pages." (".count($list);
  204. foreach ($list as $bid => $thisbuild)
  205. {
  206. // evil hack for the dependancy embedding in the array :(
  207. if ($bid == 1000)
  208. continue;
  209. if ($rowcounter % $display_rows == 0)
  210. {
  211. if (isset ($outfile))
  212. fclose ($outfile);
  213. $page = (int)($rowcounter / $display_rows);
  214. if (!($outfile = fopen ($precalcpath."-".$page.".php", "w")))
  215. {
  216. log_err ("Can't open file: ".$precalcpath."-".$page.".php");
  217. return (0);
  218. }
  219. if (1 == $what)
  220. fwrite ($outfile, phpit ('if (count($gui->building_queue) < 2) $slotsfree = true; else $slotsfree = false;'));
  221. else
  222. fwrite ($outfile, phpit ('if (!$gui->research_string) $slotsfree = true; else $slotsfree = false;'));
  223. // if ($page == 0)
  224. // fwrite ($outfile, "<div id=\"pfeil_links\"><img src=\"gfx/pfeil_links_ausgegraut.png\"></div>\n");
  225. if (0 != $page)
  226. fwrite ($outfile, "<div id=\"pfeil_links\"><a href=\"".$_SERVER['PHP_SELF']."?p=".($page-1)."\">Nach links bl&auml;ttern</a></div>\n");
  227. // if ($page == ($pages-1))
  228. // fwrite ($outfile, "<div id=\"pfeil_rechts\"><img src=\"gfx/pfeil_ausgegraut.png\"></div>\n");
  229. if ($page != ($pages-1))
  230. fwrite ($outfile, "<div id=\"pfeil_rechts\"><a href=\"".$_SERVER['PHP_SELF']."?p=".($page+1)."\">Nach rechts bl&auml;ttern</a></div>\n");
  231. if (1 == $what)
  232. {
  233. fwrite ($outfile, "<div id=\"stadt\"><center><b>Geb&auml;ude-&Uuml;bersicht von ".$villagename." Seite ".($page+1)." von ".($pages)."</b></center><br>\n<table class=\"inhalt\" cellspacing=\"0\" cellpadding=\"0\" id=\"rundrum\" width=\"98%\"><tr>\n");
  234. fwrite ($outfile, "<th id=\"rundrum\" align=\"center\">Geb&auml;ude</th><th width=\"40\" id=\"rundrum\">Stufe</th><th width=\"240\" id=\"rundrum\">Kosten f&uuml;r die n&auml;chste Stufe</th><th width=\"120\" id=\"rundrum\">Ausbauen</th>");
  235. $actstr = "Ausbau";
  236. }
  237. else
  238. {
  239. fwrite ($outfile, "<div id=\"stadt\"><center><b>Forschungs-&Uuml;bersicht von ".$villagename." Seite ".($page+1)." von 3</b></center><br>\n<table class=\"inhalt\" cellspacing=\"0\" cellpadding=\"0\" id=\"rundrum\" width=\"98%\"><tr>\n");
  240. fwrite ($outfile, "<th id=\"rundrum\" colspan=\"2\" align=\"center\">Forschung</th><th id=\"rundrum\" width=\"40\">Stufe</th><th width=\"240\">Kosten f&uuml;r die n&auml;chste Stufe</th><th id=\"rundrum\">Forschen</th>");
  241. $actstr = "Forschen";
  242. }
  243. }
  244. $rowcounter++;
  245. $tolevel = $list[$bid][1] + 1;
  246. if (1 == $what)
  247. if (isset($gui->buildid_queue))
  248. {
  249. foreach ($gui->buildid_queue as $this_build)
  250. {
  251. if ($this_build == $build[$bid][7])
  252. {
  253. $tolevel++;
  254. }
  255. }
  256. }
  257. $levelfac = $tolevel * pow (NEXTLEVEL_COSTS, $tolevel - 1);
  258. $outstr .= "</tr><tr height=\"42\">\n";
  259. $outstr .= "<td id=\"rundrum\">&nbsp;".$list[$bid][0]."</td><td id=\"rundrum\" style=\"text-align:center;\">".$list[$bid][1]."</td>\n";
  260. $outstr .= phpit ('$tmp_notenough = false; $thiscost = array (1=>'.(int)($list[$bid][2]*$levelfac).', 2=>'.(int)($list[$bid][3]*$levelfac).', 3=>'.(int)($list[$bid][4]*$levelfac).', 4=>'.(int)($list[$bid][15]*$levelfac).'); echo "<td id=\"rundrum\">".$me->activevillage->display_money($thiscost)."\n";');
  261. $outstr .= "<br>".$list[$bid][5]."</td>\n";
  262. if ($errstr = Calc_Building_Dep ($bid, $list[$bid][1], $build, $research, $what))
  263. {
  264. if (strlen ($errstr) > 50)
  265. $errstr = substr ($errstr, 0, 47)."...";
  266. $outstr .= "<td id=\"rundrum\"><span id=\"bug\">Benötigt \n".$errstr."</span></td>\n";
  267. }
  268. else
  269. {
  270. $this_cost = (int)(($list[$bid][2] + $list[$bid][3] + $list[$bid][4] + $list[$bid][15]) * $levelfac / WORLD_SPEED);
  271. $this_dur = external_calcdur ((int)($this_cost * $buildspeed));
  272. $unb_dur = external_calcdur ($this_cost);
  273. $outstr .= '<'.'?php'."\n".'if (!$tmp_notenough)'."\n{\n";
  274. $outstr .= ' if ($slotsfree)'."\n ";
  275. $outstr .= 'echo "<td id=\"rundrum\"><a href=\"'.$_SERVER['PHP_SELF'].'?r='.$bid.'&l='.$tolevel.'&p='.$page.'\" class=\"dark\">'.$actstr.' Stufe '.$tolevel.'</a><br>Dauer: '.$this_dur.'</td>";'."\n";
  276. $outstr .= " else\n";
  277. if (1 == $what)
  278. $outstr .= ' echo "<td id=\"rundrum\"><span id=\"busy\">Bauschleife voll!</span><br><span id=\"busy\">Dauer: '.$this_dur.'</span></td>\n";'."\n";
  279. else
  280. $outstr .= ' echo "<td id=\"rundrum\"><font color=\"AA3333\">Es wird bereits geforscht!</font><br><font color=\"#A0A0A0\">Dauer: '.$this_dur.'</font></td>\n";'."\n";
  281. $outstr .= "}\nelse\n";
  282. $outstr .= ' echo "<td id=\"rundrum\"><span id=\"bug\">Nicht genügend Rohstoffe</span><br><span id=\"zero\">Dauer: '.$this_dur.'</font></td>\n";'."\n?".">\n";
  283. }
  284. // $outstr .= "</tr><tr height=\"4\"><td colspan=\"4\" style=\"position:absolute; height:4px; overflow:visible;\"><img style=\"position:relative; top:-4px; left:80px;\" height=\"4\" width=\"560\" src=\"gfx/line_thin.png\"></td></tr>";
  285. fwrite ($outfile, $outstr);
  286. $outstr = "";
  287. }
  288. fclose ($outfile);
  289. }