/php/citycont.php
PHP | 333 lines | 279 code | 28 blank | 26 comment | 48 complexity | e6cfd56810e7f1df8bb77b6e513924eb MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
- <?php
- /* citycont.php - Back-end utility functions for city and research views
- *
- * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
- *
- * This file is part of Selador, a browser-based fantasy strategy game
- *
- * This program is distributed under the terms of the GNU Affero General Public License.
- *
- *
- * Selador is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * Selador is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with Selador. If not, see <http://www.gnu.org/licenses/>.
- **/
- // This code is ugly and should be replaced. A ton of it is probably out of use anyway
- function Calc_Building_Dep ($bid, $level, &$build, &$research, $what)
- {
- $found_deps = 0;
- if (1 == $what)
- {
- if (isset ($build[$bid][8]))
- {
- $dep = $build[$bid][8];
- $level = $build[$bid][9];
- }
- if (isset ($build[$bid][10]))
- {
- $rdep = $build[$bid][10];
- $rlevel = $build[$bid][11];
- }
- }
- else
- {
- $dep = $research[$bid][8];
- $level = $research[$bid][9];
- $rdep = $research[$bid][10];
- $rlevel = $research[$bid][11];
- }
- if (isset ($dep))
- {
- foreach ($dep as $idx => $this_dep)
- {
- if (isset ($build[$build[1000][$this_dep]]) && ($build[$build[1000][$this_dep]][1] < $level[$idx])) // Pretty sick eh ? :)
- {
- $found_deps++;
- if ($found_deps == 5)
- $retstr .= "...";
- else
- {
- if ($found_deps < 5)
- {
- if (isset ($retstr))
- $retstr .= 'hhaamm'.$build[$build[1000][$this_dep]][0]." ".$level[$idx];
- else
- $retstr = 'hhaamm'.$build[$build[1000][$this_dep]][0]." ".$level[$idx];
- $retstr .= "<br>";
- }
- }
- }
- }
- if (isset ($rdep))
- {
- foreach ($rdep as $idx => $this_dep)
- {
- if (isset ($research[$this_dep]) && ($research[$this_dep][1] < $rlevel[$idx]))
- {
- $found_deps++;
- if ($found_deps == 5)
- $retstr .= "...";
- else
- {
- if ($found_deps < 5)
- {
- if (isset ($retstr))
- $retstr .= 'ffoorr'.$research[$this_dep][0]." ".$rlevel[$idx];
- else
- $retstr = 'ffoorr'.$research[$this_dep][0]." ".$rlevel[$idx];
- $retstr .= "<br>";
- }
- }
- }
- }
- }
- }
- if (isset ($retstr))
- return $retstr;
- else
- return false;
- }
- function city_fetch_db ($activevill)
- {
- global $me;
- $query = "select * from villbuild where villageid=".$activevill;
- $res = mysql_query ($query);
- while ($row = mysql_fetch_array ($res))
- {
- $buildlvl[$row['type']] = $row['level'];
- }
- $query = "select * from buildings where race=".$me->race." order by display_order";
- $res = mysql_query ($query);
- while ($row = mysql_fetch_array ($res))
- {
- $bid = $row['type'];
- $build[1000][$row['id']] = $bid; // inverse lookup - ugly implementation
- $build[$bid][0]=$row['name'];
- if (isset ($buildlvl[$bid]) && ($buildlvl[$bid] > 0))
- $build[$bid][1]=$buildlvl[$bid];
- else
- $build[$bid][1]=0;
- $build[$bid][2]=$row['costs1'];
- $build[$bid][3]=$row['costs2'];
- $build[$bid][4]=$row['costs3'];
- $build[$bid][15]=$row['costs4'];
- $build[$bid][5]=$row['oneliner'];
- $build[$bid][6]=$row['description'];
- $build[$bid][7]=$row['id'];
- for ($temp = 0; $temp < 4; $temp++)
- {
- if (isset ($row['bdep'.$temp]))
- if ($row['bdep'.$temp] > 0)
- {
- $build[$bid][8][$temp] = $row['bdep'.$temp];
- $build[$bid][9][$temp] = $row['bdeplevel'.$temp];
- }
- if (isset ($row['ddep'.$temp]))
- if ($row['ddep'.$temp] > 0)
- {
- $build[$bid][10][$temp] = $row['ddep'.$temp];
- $build[$bid][11][$temp] = $row['ddeplevel'.$temp];
- }
- }
- $build[$bid][12]=$row['pic'];
- }
- mysql_free_result ($res);
- return $build;
- }
- function research_fetch_db ($uid)
- {
- global $me;
- $query = "select * from userresearch where uid=".$uid;
- $res = mysql_query ($query);
- while ($row = mysql_fetch_array ($res))
- {
- $researchlvl[$row['resid']] = $row['level'];
- }
- $query = "select * from research where race=0 or race=".$me->race." order by display_order";
- $result = mysql_query ($query);
- while ($row = mysql_fetch_array ($result))
- {
- $rid = $row['id'];
- $research[$rid][0]=$row['name'];
- if (isset ($researchlvl[$rid]) && ($researchlvl[$rid] > 0))
- $research[$rid][1]=$researchlvl[$rid];
- else
- $research[$rid][1]=0;
- $research[$rid][2]=$row['costs1'];
- $research[$rid][3]=$row['costs2'];
- $research[$rid][4]=$row['costs3'];
- $research[$rid][15]=$row['costs4'];
- $research[$rid][5]=$row['oneliner'];
- $research[$rid][6]=$row['description'];
- for ($temp = 0; $temp < 4; $temp++)
- {
- if (isset ($row['bdep'.$temp]) && ($row['bdep'.$temp] > 0))
- {
- $research[$rid][8][$temp] = $row['bdep'.$temp];
- $research[$rid][9][$temp] = $row['bdeplevel'.$temp];
- }
- if (isset ($row['ddep'.$temp]) && ($row['ddep'.$temp] > 0))
- {
- $research[$rid][10][$temp] = $row['ddep'.$temp];
- $research[$rid][11][$temp] = $row['ddeplevel'.$temp];
- }
- }
- $research[$rid][12]=$row['pic'];
- }
- mysql_free_result ($result);
- return $research;
- }
- /* What = 1 for buildings, 2 for research */
- function city_calc_contents ($activevill, $villagename, $precalcpath, $uid, $buildspeed, $what)
- {
- global $gui;
- $display_rows = 8;
- $rowcounter = 0;
- $outstr = "";
- $build = city_fetch_db ($activevill);
- $research = research_fetch_db ($uid);
- if (1 == $what)
- $list =& $build;
- else
- $list =& $research;
- $pages = ceil (count ($list) / $display_rows);
- // echo "Pages: ".$pages." (".count($list);
- foreach ($list as $bid => $thisbuild)
- {
- // evil hack for the dependancy embedding in the array :(
- if ($bid == 1000)
- continue;
- if ($rowcounter % $display_rows == 0)
- {
- if (isset ($outfile))
- fclose ($outfile);
- $page = (int)($rowcounter / $display_rows);
- if (!($outfile = fopen ($precalcpath."-".$page.".php", "w")))
- {
- log_err ("Can't open file: ".$precalcpath."-".$page.".php");
- return (0);
- }
- if (1 == $what)
- fwrite ($outfile, phpit ('if (count($gui->building_queue) < 2) $slotsfree = true; else $slotsfree = false;'));
- else
- fwrite ($outfile, phpit ('if (!$gui->research_string) $slotsfree = true; else $slotsfree = false;'));
- // if ($page == 0)
- // fwrite ($outfile, "<div id=\"pfeil_links\"><img src=\"gfx/pfeil_links_ausgegraut.png\"></div>\n");
- if (0 != $page)
- fwrite ($outfile, "<div id=\"pfeil_links\"><a href=\"".$_SERVER['PHP_SELF']."?p=".($page-1)."\">Nach links blättern</a></div>\n");
- // if ($page == ($pages-1))
- // fwrite ($outfile, "<div id=\"pfeil_rechts\"><img src=\"gfx/pfeil_ausgegraut.png\"></div>\n");
- if ($page != ($pages-1))
- fwrite ($outfile, "<div id=\"pfeil_rechts\"><a href=\"".$_SERVER['PHP_SELF']."?p=".($page+1)."\">Nach rechts blättern</a></div>\n");
- if (1 == $what)
- {
- fwrite ($outfile, "<div id=\"stadt\"><center><b>Gebäude-Ü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");
- fwrite ($outfile, "<th id=\"rundrum\" align=\"center\">Gebäude</th><th width=\"40\" id=\"rundrum\">Stufe</th><th width=\"240\" id=\"rundrum\">Kosten für die nächste Stufe</th><th width=\"120\" id=\"rundrum\">Ausbauen</th>");
- $actstr = "Ausbau";
- }
- else
- {
- fwrite ($outfile, "<div id=\"stadt\"><center><b>Forschungs-Ü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");
- fwrite ($outfile, "<th id=\"rundrum\" colspan=\"2\" align=\"center\">Forschung</th><th id=\"rundrum\" width=\"40\">Stufe</th><th width=\"240\">Kosten für die nächste Stufe</th><th id=\"rundrum\">Forschen</th>");
- $actstr = "Forschen";
- }
- }
- $rowcounter++;
- $tolevel = $list[$bid][1] + 1;
- if (1 == $what)
- if (isset($gui->buildid_queue))
- {
- foreach ($gui->buildid_queue as $this_build)
- {
- if ($this_build == $build[$bid][7])
- {
- $tolevel++;
- }
- }
- }
- $levelfac = $tolevel * pow (NEXTLEVEL_COSTS, $tolevel - 1);
- $outstr .= "</tr><tr height=\"42\">\n";
- $outstr .= "<td id=\"rundrum\"> ".$list[$bid][0]."</td><td id=\"rundrum\" style=\"text-align:center;\">".$list[$bid][1]."</td>\n";
- $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";');
- $outstr .= "<br>".$list[$bid][5]."</td>\n";
- if ($errstr = Calc_Building_Dep ($bid, $list[$bid][1], $build, $research, $what))
- {
- if (strlen ($errstr) > 50)
- $errstr = substr ($errstr, 0, 47)."...";
- $outstr .= "<td id=\"rundrum\"><span id=\"bug\">Benötigt \n".$errstr."</span></td>\n";
- }
- else
- {
- $this_cost = (int)(($list[$bid][2] + $list[$bid][3] + $list[$bid][4] + $list[$bid][15]) * $levelfac / WORLD_SPEED);
- $this_dur = external_calcdur ((int)($this_cost * $buildspeed));
- $unb_dur = external_calcdur ($this_cost);
- $outstr .= '<'.'?php'."\n".'if (!$tmp_notenough)'."\n{\n";
- $outstr .= ' if ($slotsfree)'."\n ";
- $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";
- $outstr .= " else\n";
- if (1 == $what)
- $outstr .= ' echo "<td id=\"rundrum\"><span id=\"busy\">Bauschleife voll!</span><br><span id=\"busy\">Dauer: '.$this_dur.'</span></td>\n";'."\n";
- else
- $outstr .= ' echo "<td id=\"rundrum\"><font color=\"AA3333\">Es wird bereits geforscht!</font><br><font color=\"#A0A0A0\">Dauer: '.$this_dur.'</font></td>\n";'."\n";
- $outstr .= "}\nelse\n";
- $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";
- }
- // $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>";
- fwrite ($outfile, $outstr);
- $outstr = "";
- }
- fclose ($outfile);
- }