예제 #1
0
function production()
{
    // Orbital Start
    $sth = mysql_query("select * from o_production where time=1 and pos=1");
    if (!$sth) {
        echo "Database failure!\n";
        return 0;
    }
    while ($orbital = mysql_fetch_array($sth)) {
        $sth1 = mysql_query("insert into constructions (pid,prod_id,type) values ('" . $orbital["planet_id"] . "','" . $orbital["prod_id"] . "',1)");
        if (!$sth1) {
            echo "Database failure!";
        }
        $sth1 = mysql_query("select uid from planets where id=" . $orbital["planet_id"]);
        if (!$sth1) {
            echo "Datasbe failure!";
        }
        list($uid) = mysql_fetch_row($sth1);
        $sth1 = mysql_query("select name from production where prod_id=" . $orbital["prod_id"]);
        if (!$sth1) {
            echo "Database failure!";
        }
        $blub = mysql_fetch_array($sth1);
        $planetname = get_planetname($orbital["planet_id"]);
        ticker($uid, "*lproduction.php?act=build&pid=" . $orbital["planet_id"] . "*" . $planetname . ": " . $blub["name"] . " in orbit constructed.", "w");
        $sth1 = mysql_query("delete from o_production where planet_id='" . $orbital["planet_id"] . "' and prod_id='" . $orbital["prod_id"] . "'");
        if (!$sth1) {
            echo "Database failure!";
            return 0;
        }
        $sth1 = mysql_query("select typ from production where prod_id=" . $orbital["prod_id"] . " and typ='R'");
        if (mysql_num_rows($sth1) > 0) {
            $sth1 = mysql_query("select uid from planets where id=" . $orbital["planet_id"]);
            list($uid) = mysql_fetch_array($sth1);
            $sth1 = mysql_query("delete from tradestations where uid={$uid}");
            $sth1 = mysql_query("insert into tradestations (metal,energy,mopgas,erkunum,gortium,susebloom,uid,pid) values ('0','0','0','0','0','0','{$uid}','" . $orbital["planet_id"] . "')");
        }
        $sth1 = mysql_query("select prod_id from production where prod_id='" . $orbital["prod_id"] . "' and special='S'");
        if (!$sth1) {
            echo "Database failure!";
        }
        if (mysql_num_rows($sth1) > 0) {
            $sth1 = mysql_query("select sid from planets where id=" . $orbital["planet_id"]);
            if (!$sth1) {
                echo "Database failure!";
            }
            $sid = mysql_fetch_row($sth1);
            $sth1 = mysql_query("select * from jumpgatevalues where prod_id=" . $orbital["prod_id"]);
            if (!$sth1) {
                echo "Databaser failure!";
            }
            if (mysql_num_rows($sth1) > 0) {
                $password = crypt(md5(uniqid(mt_rand())));
                $sth1 = mysql_query("insert into jumpgates (prod_id,password,sid,pid) values ('" . $orbital["prod_id"] . "','" . $password . "','" . $sid[0] . "','" . $orbital["planet_id"] . "')");
                if (!$sth1) {
                    echo "Dtasabser failuert!";
                }
                $sth1 = mysql_query("select uid from planets where id=" . $orbital["planet_id"]);
                if (!$sth1) {
                    echo "Dtasber faileu8r!";
                }
                $uid = mysql_fetch_row($sth1);
                mail_to_uid($uid[0], "Password for your jumpgate in system " . $sid[0], $password);
            }
        }
    }
    $sth = mysql_query("update o_production set time=time-'1' where time>1 and pos=1");
    if (!$sth) {
        echo "Database failure!";
        return 0;
    }
    $sth = mysql_query("select distinct p1.planet_id from o_production as p1 left join o_production as p2 on p2.pos=1 and p2.planet_id=p1.planet_id where p1.pos!=1 and p2.pos is NULL");
    if (!$sth) {
        echo "Database failure!";
    }
    while ($pids = mysql_fetch_row($sth)) {
        $next_prod_id = next_in_queue(0, $pids[0]);
        if ($next_prod_id) {
            $uid = get_uid_by_pid($pids[0]);
            if (has_ressources($uid, $next_prod_id)) {
                $sth1 = mysql_query("update o_production set pos=1 where planet_id=" . $pids[0] . " and prod_id={$next_prod_id}");
                $sth1 = mysql_query("select metal,energy,mopgas,erkunum,gortium,susebloom from production where prod_id={$next_prod_id}");
                list($metal, $energy, $mopgas, $erkunum, $gortium, $susebloom) = mysql_fetch_row($sth1);
                $sth1 = mysql_query("update ressources set metal=metal-{$metal},energy=energy-{$energy},mopgas=mopgas-{$mopgas},erkunum=erkunum-{$erkunum},gortium=gortium-{$gortium},susebloom=susebloom-{$susebloom} where uid={$uid}");
            }
        }
    }
    // Orbital Ende
    // Planet production
    $sth = mysql_query("select * from p_production where time=1 and pos=1");
    if (!$sth) {
        echo "Database failure!\n";
        return 0;
    }
    while ($buildings = mysql_fetch_array($sth)) {
        $sth1 = mysql_query("select uid,name from planets where id=" . $buildings["planet_id"]);
        if (!$sth1) {
            echo "Database failure!";
        }
        $uid = mysql_fetch_array($sth1);
        if ($uid["name"] == "Unnamed") {
            $uid["name"] = get_planetname($buildings["planet_id"]);
        }
        $sth1 = mysql_query("select name from production where prod_id=" . $buildings["prod_id"]);
        // Schilde einbauen
        list($value, $shield) = dselect("CONV(SUBSTRING(special FROM 2),10,10), shield", "shipvalues", "special LIKE 'H%' AND prod_id=" . $buildings["prod_id"]);
        if ($value) {
            $janne = mysql_query("REPLACE INTO planetary_shields (pid, prod_id, value, max_value, regeneration, \n        regeneration_bonus) VALUES (" . $buildings["planet_id"] . ", " . $buildings["prod_id"] . ", {$shield}, {$shield},\n          {$value}, 0)");
        }
        if (!$sth1) {
            echo "Database failure!";
        }
        $blub = mysql_fetch_array($sth1);
        ticker($uid["uid"], "*lproduction.php?act=build&pid=" . $buildings["planet_id"] . "*" . $uid["name"] . ": " . $blub["name"] . " constructed.", "w");
        $sth1 = mysql_query("insert into constructions (pid,prod_id,type) values ('" . $buildings["planet_id"] . "','" . $buildings["prod_id"] . "',0)");
        if (!$sth1) {
            echo "Database failure! (CREATING BUILDING ON PLANET)";
        }
        $sth1 = mysql_query("delete from p_production where planet_id='" . $buildings["planet_id"] . "' and prod_id='" . $buildings["prod_id"] . "'");
        if (!$sth1) {
            echo "Database failure! (DELETING PLANETAR PRODUCTION)";
        }
    }
    $sth = mysql_query("update p_production set time=time-'1' where time>1 and pos=1");
    if (!$sth) {
        echo "Database failure!";
        return 0;
    }
    $sth = mysql_query("select distinct p1.planet_id from p_production as p1 left join p_production as p2 on p2.pos=1 and p2.planet_id=p1.planet_id where p1.pos!=1 and p2.pos is NULL");
    if (!$sth) {
        echo "Database failure!";
    }
    while ($pids = mysql_fetch_row($sth)) {
        $next_prod_id = next_in_queue(1, $pids[0]);
        if ($next_prod_id) {
            $uid = get_uid_by_pid($pids[0]);
            if (has_ressources($uid, $next_prod_id)) {
                $sth1 = mysql_query("update p_production set pos=1 where planet_id=" . $pids[0] . " and prod_id={$next_prod_id}");
                $sth1 = mysql_query("select metal,energy,mopgas,erkunum,gortium,susebloom from production where prod_id={$next_prod_id}");
                list($metal, $energy, $mopgas, $erkunum, $gortium, $susebloom) = mysql_fetch_row($sth1);
                $sth1 = mysql_query("update ressources set metal=metal-{$metal},energy=energy-{$energy},mopgas=mopgas-{$mopgas},erkunum=erkunum-{$erkunum},gortium=gortium-{$gortium},susebloom=susebloom-{$susebloom} where uid={$uid}");
            }
        }
    }
    // Planetar Ende
    // Fleet Production Start
    $uids = array();
    $sth = mysql_query("select s.planet_id,s.prod_id,s.count,s.time,s.priority,\nr.uid,r.metal as rmetal,r.energy as renergy,r.mopgas as rmopgas,r.erkunum as rerkunum,r.gortium as rgortium,r.susebloom as rsusebloom,\npl.id,p.metal,p.energy,p.mopgas,p.erkunum,p.gortium,p.susebloom,\nsum(ps.tonnage) as slots,\nsv.tonnage,\np.com_time\nfrom s_production s,planets pl,production p,ressources r,production_slots ps,constructions c,shipvalues sv where s.planet_id=pl.id\nand s.prod_id=p.prod_id\nand p.prod_id=sv.prod_id\nand ps.prod_id=c.prod_id\nand c.pid=pl.id\nand r.uid=pl.uid\ngroup by s.planet_id,s.prod_id,s.time\norder by pl.uid,s.priority DESC,s.time DESC");
    if (!$sth) {
        echo "ERR::GET S_PRODUCTION";
    } else {
        while ($data = mysql_fetch_assoc($sth)) {
            if (!$uids[$data["uid"]]) {
                $uids[$data["uid"]] = array("production_slots" => $data["slots"], "ressources" => array("metal" => $data["rmetal"], "energy" => $data["renergy"], "mopgas" => $data["rmopgas"], "erkunum" => $data["rerkunum"], "gortium" => $data["rgortium"], "susebloom" => $data["rsusebloom"]));
            }
            // mop: maximale anzahl der produzierbaren schiffe ermitteln (durch slots limitiert)
            $prod_count = floor($uids[$data["uid"]]["production_slots"] / $data["tonnage"]);
            $real_count = min($data["count"], $prod_count);
            if ($real_count > 0) {
                // mop: jetzt koenen uns nur noch die ressourcen aufhalten
                foreach (array("metal", "energy", "mopgas", "erkunum", "gortium", "susebloom") as $res) {
                    if ($data[$res]) {
                        $real_count = min($real_count, floor($uids[$data["uid"]]["ressources"][$res] / ceil($data[$res] / $data["com_time"])));
                    }
                }
                if ($real_count > 0) {
                    // mop: alle schiffe produzierbar?
                    if ($real_count == $data["count"]) {
                        // mop: alles easy
                        $sth1 = mysql_query("update s_production s,ressources r set s.time=s.time+1,\n                r.metal=r.metal-" . ceil($real_count * $data["metal"] / $data["com_time"]) . ",\n                r.energy=r.energy-" . ceil($real_count * $data["energy"] / $data["com_time"]) . ",\n                r.mopgas=r.mopgas-" . ceil($real_count * $data["mopgas"] / $data["com_time"]) . ",\n                r.erkunum=r.erkunum-" . ceil($real_count * $data["erkunum"] / $data["com_time"]) . ",\n                r.gortium=r.gortium-" . ceil($real_count * $data["gortium"] / $data["com_time"]) . ",\n                r.susebloom=r.susebloom-" . ceil($real_count * $data["susebloom"] / $data["com_time"]) . "\n                where s.prod_id=" . $data["prod_id"] . " and s.time=" . $data["time"] . " and s.planet_id=" . $data["planet_id"] . " and r.uid=" . $data["uid"]);
                        if (!$sth1) {
                            echo "ERR::UPDATE S_PROD 1";
                        }
                    } else {
                        // mop: stress...aufsplitten
                        $sth1 = mysql_query("insert into s_production set prod_id=" . $data["prod_id"] . ",planet_id=" . $data["planet_id"] . ",count=" . $real_count . ",time=" . ($data["time"] + 1) . ",priority=" . $data["priority"] . " on duplicate key update count=count+" . $real_count . ",priority=" . $data["priority"]);
                        if (!$sth1) {
                            echo "ERR::INSERT SPLIT S_PROD";
                        }
                        // mop: update des rests und der ressourcen
                        $sth1 = mysql_query("update s_production s,ressources r set s.count=s.count-" . $real_count . ",\n                r.metal=r.metal-" . ceil($real_count * $data["metal"] / $data["com_time"]) . ",\n                r.energy=r.energy-" . ceil($real_count * $data["energy"] / $data["com_time"]) . ",\n                r.mopgas=r.mopgas-" . ceil($real_count * $data["mopgas"] / $data["com_time"]) . ",\n                r.erkunum=r.erkunum-" . ceil($real_count * $data["erkunum"] / $data["com_time"]) . ",\n                r.gortium=r.gortium-" . ceil($real_count * $data["gortium"] / $data["com_time"]) . ",\n                r.susebloom=r.susebloom-" . ceil($real_count * $data["susebloom"] / $data["com_time"]) . "\n                where s.prod_id=" . $data["prod_id"] . " and s.time=" . $data["time"] . " and s.planet_id=" . $data["planet_id"] . " and r.uid=" . $data["uid"]);
                        if (!$sth1) {
                            echo "ERR::UPDATE REST S_PROD";
                        }
                    }
                    $uids[$data["uid"]]["production_slots"] -= $real_count * $data["tonnage"];
                    foreach (array("metal", "energy", "mopgas", "erkunum", "gortium", "susebloom") as $res) {
                        $uids[$data["uid"]]["ressources"][$res] -= ceil($real_count * $data[$res] / $data["com_time"]);
                    }
                }
            }
        }
    }
    // mop: rundungsfehler fuer die dinger, die jetzt fertig werden ausgleichen
    $fragments = array();
    foreach (array("metal", "energy", "mopgas", "erkunum", "gortium", "susebloom") as $res) {
        $fragments[] = "r." . $res . "=r." . $res . "+ceil(p." . $res . "*s.count/p.com_time)*p.com_time-p." . $res . "*s.count";
    }
    $sth = mysql_query("update s_production s,production p,planets pl,ressources r set " . implode($fragments, ",") . " where r.uid=pl.uid and pl.id=s.planet_id and s.prod_id=p.prod_id and p.com_time-s.time=0");
    if (!$sth) {
        echo "ERR::CORRECT CEILINGS\n";
    }
    $sth = mysql_query("select * from s_production sp,production p where p.com_time-sp.time=0 and sp.prod_id=p.prod_id");
    if (!$sth) {
        echo "Database failure!\n";
        return 0;
    }
    while ($ships = mysql_fetch_assoc($sth)) {
        $sth1 = mysql_query("select uid,name from planets where id=" . $ships["planet_id"]);
        if (!$sth1) {
            echo "Datasbe failure!";
        }
        $uid = mysql_fetch_assoc($sth1);
        ticker($uid["uid"], "*lproduction.php?pid=" . $ships["planet_id"] . "*" . $uid["name"] . ": " . $ships["count"] . " " . $ships["name"] . "(s) produced.", "w");
        $sth1 = mysql_query("select sid from planets where id=" . $ships["planet_id"]);
        if (!$sth1) {
            echo "Dtabase failuer!";
        }
        $system = mysql_fetch_array($sth1);
        $fids = get_fids_by_pid($ships["planet_id"], $uid["uid"]);
        // Keine Flotte auf dem Planet?
        if (!$fids) {
            $new_fleet = new fleet();
            $new_fleet->add_ships_arr(array($ships["prod_id"] => array($ships["count"], 0)));
            $new_fleet->uid = $uid["uid"];
            $new_fleet->pid = $ships["planet_id"];
            $new_fleet->sid = $system["sid"];
            $new_fleet->create_fleet();
        } else {
            $prod_id_in_fid = false;
            for ($i = 0; $i <= sizeof($fids) - 1; $i++) {
                $fleet = new fleet($fids[$i]);
                if ($fleet->ships[$ships["prod_id"]]) {
                    $prod_id_in_fid =& $fleet;
                    // Das is ne referenz :) Irgendwann musste ich die einfach mal benutzen:)
                    break;
                }
            }
            if ($prod_id_in_fid) {
                $prod_id_in_fid->ships[$ships["prod_id"]][0] += $ships["count"];
                $prod_id_in_fid->update_prod_id($ships["prod_id"]);
            } else {
                $fleet->add_ships_arr(array($ships["prod_id"] => array($ships["count"], 0)));
                if (!$fleet->update_prod_id($ships["prod_id"])) {
                    echo "Error assigning new prod id to fleet " . $fleet->fid . "\n";
                }
            }
        }
        $sth1 = mysql_query("delete from s_production where planet_id='" . $ships["planet_id"] . "' and prod_id='" . $ships["prod_id"] . "' and count='" . $ships["count"] . "' and time=" . $ships["time"]);
        if (!$sth1) {
            echo "Dtaabase failuer!";
        }
    }
    // Fleet Ende
    // Infantery Production Start
    $sth = mysql_query("select * from i_production where time=1");
    if (!$sth) {
        echo "Database failure!\n";
        return 0;
    }
    while ($soldiers = mysql_fetch_array($sth)) {
        $sth1 = mysql_query("select uid,name from planets where id=" . $soldiers["planet_id"]);
        if (!$sth1) {
            echo "Datasbe failure!";
        }
        $uid = mysql_fetch_array($sth1);
        $sth1 = mysql_query("select name from production where prod_id=" . $soldiers["prod_id"]);
        if (!$sth1) {
            echo "Database failure!";
        }
        $blub = mysql_fetch_array($sth1);
        ticker($uid["uid"], "*lproduction.php?pid=" . $soldiers["planet_id"] . "*" . $uid["name"] . ": " . $soldiers["count"] . " " . $blub["name"] . "(s) produced.", "w");
        $sth1 = mysql_query("select prod_id from infantery where prod_id=" . $soldiers["prod_id"] . " and pid=" . $soldiers["planet_id"]);
        if (!$sth1) {
            echo "Dazsu failu!";
        }
        if (mysql_num_rows($sth1) == 0) {
            $sth1 = mysql_query("insert into infantery (prod_id,pid,count,uid) values ('" . $soldiers["prod_id"] . "','" . $soldiers["planet_id"] . "','" . $soldiers["count"] . "','" . $uid[0] . "')");
            if (!$sth1) {
                echo "Database failuerfleet!";
            }
        } else {
            $sth1 = mysql_query("update infantery set count=count+" . $soldiers["count"] . " where pid='" . $soldiers["planet_id"] . "' and prod_id='" . $soldiers["prod_id"] . "' and uid='" . $uid["uid"] . "' limit 1");
            if (!$sth1) {
                echo "Database failure!";
            }
        }
        $sth1 = mysql_query("delete from i_production where planet_id='" . $soldiers["planet_id"] . "' and prod_id='" . $soldiers["prod_id"] . "' and count='" . $soldiers["count"] . "' and time=1");
        if (!$sth1) {
            echo "Dtaabase failuer!";
        }
    }
    $sth = mysql_query("select * from infbattle" . $soldier["planet_id"]);
    if ($sth) {
        $sth = mysql_query("insert into infbattle" . $soldier["planet_id"] . " (prod_id,count,initiative,side) values ('" . $soldier["prod_id"] . "','" . $soldier["count"] . "','0','0')");
    }
    $sth = mysql_query("update i_production set time=time-'1' where time>1");
    if (!$sth) {
        echo "Database failure!";
        //      return 0;
    }
    // Infantery Ende
}