28.01.2012, 08:31
Gefixt:
PHP Code:
<?php
function msec(){}
class botvillagedata
{
var $villagedata;
var $villagedata_recruit;
var $villagedata_bot;
var $villagedata_builds;
//Achtung Reihenfolge: $vid,$bh(!!),$wood,$stone,$iron
function add_ressource_or_bh($vid,$bh=0,$wood=0,$stone=0,$iron=0){
$this->villagedata[$vid]['r_bh'] += $bh;
$this->villagedata[$vid]['r_stone'] += $stone;
$this->villagedata[$vid]['r_iron'] += $iron;
$this->villagedata[$vid]['r_wood'] += $wood;
}
function get_array($name,$detail=''){
$var = $this->{$name};
if(!empty($detail))
{
$var = $this->{$name}[$detail];
}
return $var;
}
function select_village_as_from_bot($vid){
$this->villages[] = $vid;
$this->allvillages++;
}
function set_type($vid,$type){
$this->type[$vid] = $type;
switch($type){
case 'def':
$this->type_def[] = $vid;
break;
case 'off':
$this->type_off[] = $vid;
break;
case 'spy':
$this->type_spy[] = $vid;
break;
}
}
function reload_villagedata(){
global $db,
$botconfig,
$cl_botvillages,
$cl_action;
$this->villagedata = array();
$sql = "SELECT * FROM `villages` WHERE `userid` ='".$botconfig['id']."'";
$res = $db->query($sql);
$count_villages = $db->numRows($res);
if($count_villages == 0){
$x = 0;
if (empty($botconfig['minvillages'])){
while($x < $config['minvillages'])
{
create_village($botconfig['id'],$botconfig['villagenames'],'random');
$x++;
}
} else {
while($x < $botconfig['minvillages'])
{
create_village($botconfig['id'],$botconfig['villagenames'],'random');
$x++;
}
}
die('Bitte neuladen...');
}
while ($row = $db->fetch($res))
{
$ress = ressis($row);
$wood = $ress['r_wood'] - $row['r_wood'];
$iron = $ress['r_stone']- $row['r_stone'];
$wood = $ress['r_iron'] - $row['r_iron'];
$this->select_village_as_from_bot($row['id']);
//$villagedata anlegen (tabelle: `villages`)
$this->villagedata[$row['id']] = $row;
//$villagedata_bot anlegen (tabelle: `bot`)
$this->read_from_db($row);
//$villagedata_builds anlegen (tabelle: `builds`)
$this->villagedata_builds($row['id']);
//$villagedata_recruit anlegen (tabelle: `recruit`)
$this->villagedata_recruit($row['id']);
//Rohstoffe aktualisieren
$this->add_ressource_or_bh($vid=$botconfig['id'],$bh=0,$wood=$wood,$stone=$stone,$iron=$iron);
//Nächstes zu bauende Gebäude auswählen
$cl_action->nextbuild_math($row);
}
}
function read_from_db($villagedata){
global $botconfig,
$db;
$this->villagedata_bot = array();
$sql = 'SELECT * FROM `bot`';
$res = $db->query($sql);
while($row = $db->fetch($res)){
$this->villagedata_bot[ $row['villageid'] ] = $row;
switch($row['type']){
case 'def':
$this->set_type($row['villageid'],'def');
break;
case 'off':
$this->set_type($row['villageid'],'off');
break;
case 'spy':
$this->set_type($row['villageid'],'spy');
break;
}
}
$arr = $this->villagedata_bot;
for($x=0; $x < $this->allvillages; $x++){
if(!array_key_exists($this->villages[$x],$arr)){
if($x == 0){
$this->new_village('def',$villagedata);
}
elseif (($x % $botconfig['spy_villages']) == 0){
$this->new_village('spy',$villagedata);
}
elseif ($x % $botconfig['deffoff'] == 0){
$this->new_village('off',$villagedata);
}
else {
$this->new_village('def',$villagedata);
}
}
}
}
/* villagedata_builds($vid) Zählt alle Gebäude die gerade in der Bauschleife sind zu den normalen Gebäuden
Eingabe: $vid : Village-id
Ausgabe: -Keine-
*/
function villagedata_builds($vid){
global $cl_builds,
$db;
$builds = $cl_builds->get_array("dbname");
foreach ($builds as $key=>$value){
$this->villagedata_builds[$vid][$value] = $this->villagedata[$vid][$value];
}
$result = $db->query("SELECT id,building,end_time,build_time from build where villageid='".$vid."' order by id");
$quantity = $db->numRows($result);
$this->villagedata_builds[$vid]['quantity'] = $quantity;
while($row=$db->Fetch($result)){
$this->villagedata_builds[$vid][$row['building']]++;
$this->villagedata_builds[$vid]['end_time'] = $row['end_time'];
}
}
/* villagedata_recruit($vid) Erfasst alle rekrutierungen
Eingabe: $vid : Village-id
Ausgabe: -Keine-
*/
function villagedata_recruit($vid){
global $db;
$sql = "SELECT unit,num_unit FROM recruit WHERE villageid=".$vid;
$res = $db->query($sql);
while($row =$db->fetch($res)){
$this->villagedata_recruit[ $vid ][ $row['unit'] ] += $row['num_unit'];
}
}
function select_villages_as_finish($vid){
$db->query("UPDATE bot SET finish='y' WHERE id=$vid");
}
/* new_village($type,$villagedata) Legt einen neuen Eintrag der DB an und schreibt neue Dörfer in $villagedata_bot
Eingabe: $type: Typ des Dorfes ('def','off','spy')
$villagedata: Alle Einträge von Tabelle 'villages'
Ausgabe: -Keine-
*/
function new_village($type,$villagedata){
global $botconfig,
$db,
$cl_builds,
$cl_botvillagedata;
$arr_builds = $cl_builds->get_array('dbname');
$botval = $botconfig['buildings']['values'];
$sql = 'insert into `bot`
(villageid,type,finish_build,finish_tec,next_build';
//) values (';
foreach($arr_builds AS $dbname){
$sql .= ", $dbname";
$value[] = $botval[$dbname] - $villagedata[$dbname];
}
$sql .= ") values ('".$villagedata['id']."','".$type."','n','n','','";
$count = count($value) -1;
$y = 0;
for($x=0;$x<$count;$x++)
{
$sql .= $value[$x]."','";
$y++;
}
$sql .= $value[$y]."')";
$db->query($sql);
unset($sql);
$villagedata_bot_ = array('villageid'=>$villagedata['id']);
foreach($cl_builds->get_array('dbname') AS $key=>$val){
$villagedata_bot_[$val] = ($botval[$val]-$villagedata[$val]);
}
$villagedata_bot_['type'] = $type;
$villagedata_bot_['finish_build'] = 'n';
$villagedata_bot_['finish_tec'] = 'n';
$this->type[$villagedata['id']] = $type;
$this->villagedata_bot[ $villagedata_bot_['villageid'] ] = $villagedata_bot_;
}
}
/*
Klasse zum Bauen, Forschen und allem anderen.
Statt den 3 einzelnen Klassen gibt es nun nur noch diese!
*/
class Botvillages extends botvillagedata
{
//Allgemeiner Dörfercounter, Array mit allen Dörfern, Array mit allen Dörfern nach den Type(off,def,spy)
function recruit_build_research()
{
global $cl_botvillagedata, //Eigene Klassen
$cl_action,
$botconfig, //Eigene Konfig
$cl_builds,//Lan-klassen
$cl_units,
$cl_techs,
$db,
$arr_farm;
//Off,Def,Spy dörfer durchgehen.
foreach($cl_botvillagedata->type AS $vid=>$type){
//Angriffe auf ein Botdorf ? Wenn ja def oder spy schicken!
$villagedata = $cl_botvillagedata->get_array('villagedata',$vid);
$villagedata_bot = $cl_botvillagedata->get_array('villagedata_bot',$vid);
//TECHS ERFORSCHEN
//Ist das erforschen auf null gesetzt?
$fast_sql = NULL;
if($villagedata_bot['finish_tec'] == 'n'){
//Alle Typentechs auslesen
$techs = $cl_techs->get_attType($type);
foreach($techs as $value){
//$value = was soll erforscht werden
if($villagedata_bot["tec_".$value] == 'y'){
if($villagedata["unit_".$value."_tec_level"] != $botconfig[$type]['tec'][$value]){
//Es wurde eingestellt das noch mehr erforscht werden muss...
$research = $cl_action->research($value,$vid);
if(!$research){
if($cl_techs->error =="max_stage"){
$fast_sql['tec_'.$value] = 'n';
}
}
} else {
$fast_sql['tec_'.$value] = 'n';
}
}
}
if(is_array($fast_sql))
{
$sql = "UPDATE `bot` SET ";
foreach($fast_sql AS $building=>$val){
$sql .= "$building='$val' ";
}
$sql .= "WHERE villageid='".$vid."'";
$db->query($sql);
$fast_sql = NULL;
}
usleep(100);
}
$cl_action->build_building($cl_action->next_build[$vid]["building"]);
usleep(100);
//Wie viele Einheiten stehen bereits im Dorf?
$querys = array();
foreach($cl_units->get_array('dbname') AS $dbname) {
$querys[] = "all_".$dbname;
}
$result = $db->query("SELECT ".implode(",",$querys)." from villages where id=".$vid."");
$village_all = $db->fetch($result);
$farm_datas['own_units'] = 0;
//Alle eigenen Truppen auslesen
foreach($cl_units->get_array('dbname') AS $dbname) {
$farm_datas['own_units'] += $village_all['all_'.$dbname];
}
if($botconfig['max_units'] <= $farm_datas['own_units']) return false;
//KI-Überprüfung Wenn die Punkte des Dorfes größer sind als der noch freie Bauernhof
if($villagedata['points'] > $arr_farm[$villagedata['farm']] - $villagedata['r_bh']){
if((random_generator($botconfig['ki']['create_units_villagepoints'] + 1))){
$units = $cl_units->get_attType($type);
foreach($units as $value){
$cl_action->recruit($value,$villagedata,$cl_units->recruit_in[$value]);
}
}
} else {
$units = $cl_units->get_attType($type);
foreach($units as $value){
$cl_action->recruit($value,$villagedata,$cl_units->recruit_in[$value]);
}
}
usleep(100);
}
}
}
/* Diese Funktion erzeugt eine Zufallszahl (1-100) , die für einen Botlauf bleibt, danach wenn größer dann true wenn nicht dann false! */
function random_generator($number_value){
static $r_number;
if(empty($r_number)){
$r_number = rand(0,101);
}
if($r_number > $number_value){
return false;
}
return true;
}
/*
class away
{
var village;
function write_away($vid,$units)
}
*/
class action
{
var $last_village;
var $last_recruit;
var $last_error;
var $sql_bot = NULL;
var $error;
var $next_build;
var $builds;
var $buildings_by_vid;
var $attack;
/*
Überprüft ob jemand ein Dorf angreift.
Gibt bei Angriff TRUE zurück, ansonsten FALSE
*/
function check_attack($vid)
{
global $db;
$sql = "SELECT * FROM `movements` WHERE `type` ='attack' AND `send_to_village`='".$vid."'";
$res = $db->query($sql);
while($row = $db->Fetch($res))
{
$this->attack[$vid][] = $row;
}
if($db->numRows($res) > 0)
return true;
return false;
}
function research($unit,$vid) //Bei Unit wird nur der Unitname erwartet! Kein "unit_" vornedran!
{
global $cl_techs,
$cl_botvillages,
$cl_botvillagedata;
$this->last_village = $cl_botvillagedata->get_array('villagedata',$vid);
// Schauen, ob noch nicht geforscht wird:
if(!empty($this->last_village['smith_tec'])){
$error = "Es wird bereits geforscht.";
return false;
}
// Allgemeiner Einheitencheck:
$cl_techs->check_tech($unit,$villagedata);
switch($cl_techs->get_last_error()){
case "tech_not_found":
$error = "t";
return false;
break;
case "max_stage":
$error = "m";
return false;
break;
case "not_fulfilled":
$error = "G";
return false;
break;
case "not_enough_storage":
$error = "S";
return false;
break;
case "not_enough_ress":
$error = "R";
return false;
break;
}
// Wenn kein Fehler da ist, dann forschen:
$cl_techs->research($unit,$villagedata);
$wood = round($cl_techs->get_wood($unit,$villagedata['unit_'.$unit.'_tec_level']+1));
$stone = round($cl_techs->get_stone($unit,$villagedata['unit_'.$unit.'_tec_level']+1));
$iron = round($cl_techs->get_iron($unit,$villagedata['unit_'.$unit.'_tec_level']+1));
$this->last_village['r_wood'] -= $wood;
$this->last_village['r_stone'] -= $stone;
$this->last_village['r_iron'] -= $iron;
return true;
}
function recruit($unit,$buildname)
{
global $cl_units,
$cl_botvillages,
$botconfig,
$arr_farm,
$db;
$villagedata = $last_village;
# Einheiten rekrutieren:
#Rohstoffe überprüfen
$cl_units->check_needed($unit,$villagedata);
if (is_numeric($cl_units->last_error)){
$quantity = $cl_units->last_error >> 1;
}
$input = (int) $quantity;
// Weitere Überprüfungen
$wood = $cl_units->get_woodprice($unit) *$input;
$stone = $cl_units->get_stoneprice($unit)*$input;
$iron = $cl_units->get_ironprice($unit) *$input;
$bh = $cl_units->get_bhprice($unit) *$input;
//Existiert überhaupt das Gebäude zum Bauen in dem Dorf?
if($villagedata[$buildname] == 0){
$check = "building does not exits";
return false;
}
//Rohstoffe vorhanden?
elseif ($wood>$villagedata['r_wood'] OR $stone>$villagedata['r_stone'] OR $iron>$villagedata['r_iron']){
$check = "to_many_units";
return false;
}
//Bauernhof?
elseif(($arr_farm[$village['farm']]-$villagedata['r_bh']-$bh) >0){
$check = "to_many_bh";
return false;
}
//Alles möglich also rekrutieren
if ($input>$botconfig['min_units_for_recruit']){
// Ress abziehen:
$db->query("UPDATE villages SET r_wood=r_wood-'$wood',r_stone=r_stone-'$stone',r_iron=r_iron-'$iron',r_bh=r_bh+'$bh' where id='".$villagedata['id']."'");
// Nun kann die Einheit rekrutiert werden:
$cl_units->recruit_units($unit,$input,$buildname,$villagedata[$buildname],$villagedata['id']);
$this->last_village['r_bh'] += $bh;
$this->last_village['r_wood'] -= $wood;
$this->last_village['r_stone'] -= $stone;
$this->last_village['r_iron'] -= $iron;
return true;
}
}
function nextbuild_math($villagedata){
global $botconfig,
$db,
$cl_botvillagedata,
$db,
$cl_builds;
//Bot Tabelle
$villagedata_bot = $cl_botvillagedata->get_array('villagedata_bot',$villagedata['id']);
if(!empty($villagedata_bot['next_build'])){
$this->next_build[$villagedata['id']]['building'] = $villagedata_bot['next_build'];
$this->sql_bot['check_needed']['next_build'] ="''";
$db->query($sql);
} else {
foreach($cl_builds->get_array('dbname') AS $dbname){
srand ( (double)microtime () * 1000000 );
$this->check_needed($dbname,$villagedata);
$villagedata_bot[ $dbname ] += rand(0,5);
if ($villagedata_bot[ $dbname ] > $this->next_build[$villagedata['id']]['value']){
$this->next_build[ $villagedata['id'] ]["building"] = $dbname;
$this->next_build[ $villagedata['id'] ]["value"] = $villagedata_bot[ $dbname ];
}
}
$this->sql('check_needed',$villagedata['id']);
}
}
function sql($name_function,$vid){
global $db;
//SQL zusammenstellen
$sql = "UPDATE bot SET ";
$x = 0;
if(is_array($this->sql_bot)){
foreach($this->sql_bot[$name_function] AS $building=>$val){
if($x == 0) $sql .= "$building=$val";
else $sql .= ", $building=$val";
$x++;
}
$sql .= " WHERE villageid=".$vid;
$db->query($sql);
}
$this->sql_bot[$name_function] = array();
}
function check_needed($building,$villagedata){
global $db,
$cl_builds,
$botconfig,
$arr_maxstorage,
$arr_farm;
$this->sql_bot['check_needed'] = false;
//Preise auslesen...
$no_needed = false;
$stage = $villagedata[$building] + 1; //Stufe auslesen
$wood = $cl_builds->get_wood($building,$stage); //Holzpreis
$stone = $cl_builds->get_stone($building,$stage); //Lehmpreis
$iron = $cl_builds->get_iron($building,$stage); //Eisenpreis
$maxstorage = $arr_maxstorage[$villagedata["storage"]]; //Fassungsvermögen des Speichers...
$maxstage = $cl_builds->get_maxstage($building); //Maximale Stufe...
$needed = $cl_builds->get_needed_by_dbname($building); //Einzelne Gebäudestufen
//Alle Gebäude durchgehen...
foreach($cl_builds->get_array('dbname') AS $dbname){
if($needed[ $dbname ] > $villagedata[ $dbname ]){
//Das Dorf entspricht nicht den Anforderungen zum Baudes Gebäudes...
$this->sql_bot['check_needed'][$building] = -50;
return true;
}
}
//Was wird am meisten gebraucht?
$need_iron = $iron - $villagedata['r_iron'];
$need_stone = $stone - $villagedata['r_stone'];
$need_wood = $wood - $villagedata['r_wood'];
if($need_iron > $need_stone && $need_iron > $need_wood){
if(random_generator($botconfig['ki']['not_enough_ress'])){
$this->sql_bot['check_needed']['next_build'] = "'iron'";
return true;
}
}
//Die nächsten zwei überprüfen das gleiche, nur für andere Rohstoffe deshalb eingeklappt:
if($need_stone > $need_iron && $need_stone > $need_wood){ if(random_generator($botconfig['ki']['not_enough_ress'])){ $this->sql_bot['check_needed']['next_build'] = "'stone'"; return true; } }
if($need_wood > $need_iron && $need_wood > $need_stone){ if(random_generator($botconfig['ki']['not_enough_ress'])){ $this->sql_bot['check_needed']['next_build'] = "'wood'"; return true; } }
//Der Preis für eine Ressource ist höher als das Fassungsvermögen...
if($iron > $maxstorage || $stone > $maxstorage || $wood >$maxstorage){
$this->sql_bot['check_needed'][$building] = -50;
$this->sql_bot['check_needed']['next_build'] = "'storage'";
return true;
}
//Nur wenn $botconfig['manual_maxstage'] auf true gesetzt ist!
if($botconfig['manual_maxstage']){
if($villagedata[$building] == $botconfig['maxstage'][$building]){
//Maximale Stufe wurde heruntergesetzt
$this->sql_bot['check_needed'][$building] = -50;
return true;
}
}
if($maxstage == $villagedata[$building]){
//Falls das Gebäude voll ausgebaut ist..
$this->sql_bot['check_needed'][$building] = -50;
return true;
}
if(($arr_farm[$villagedata['farm']] - $villagedata['r_bh']) < 5)
{
//Weniger als 5 freie Bauernplätze / Bauernhof als nächstes Bauen...
$this->sql_bot['check_needed']['next_build'] = "'farm'";
return true;
}
//Da diese Funktion bei allen Gebäuden ausgefhrt wird..
$minus = $botconfig['buildings']['values'][$building] - $villagedata[$building];
$this->sql_bot['check_needed'][$building] = $minus;
}
function build_building($building){
global $botconfig,
$db,
$cl_builds,
$cl_botvillages,
$cl_botvillagedata;
// Rohstoff preise & BH ermitteln:
if(empty($building)){
die('FEHLER - Kein Gebaeude');
}
$build_village = $cl_botvillagedata->get_array('villagedata_builds',$villagedata['id']);
$auftrage = $build_village['quantity'];
$villagedata = $this->last_village;
$plus_costs = ($cl_builds->get_buildsharpens_costs($auftrage)/100)+1;
$id_array = array_flip($cl_builds->get_array('dbname'));
if ($plus_costs < $botconfig['max_order']){
// Diese Methode überprüft nur, der Methodenname verwirrt etwas.
$cl_builds->build($villagedata,$id_array[$building],$build_village,$plus_costs);
$error = '';
switch($cl_builds->get_build_error2()){
case 'not_enough_ress':
$error = 'Rohstoffe';
break;
case 'not_enough_ress_plus':
$error = 'Rohstoffe Bauschleife';
break;
case 'max_stage':
$error = 'hoechste stufe';
break;
case 'not_enough_bh':
$error = 'Bauernhof';
break;
case 'not_enough_storage':
$error = 'Speicher';
break;
case 'not_fulfilled':
$error = 'Gebaeude.';
break;
}
if(!empty($error))
return false;
// Wenn kein Fehler ausgetreten ist, so kann das Gebäude gebaut werden:
// Rohstoff preise & BH ermitteln:
$wood = $cl_builds->get_wood($building,$build_village[$building]+1);
$stone = $cl_builds->get_stone($building,$build_village[$building]+1);
$iron = $cl_builds->get_iron($building,$build_village[$building]+1);
$bh = $cl_builds->get_bh($building,$build_village[$building]+1);
$time = $cl_builds->get_time($villagedata['main'],$building,$build_village[$building]+1);
$onlytime = $cl_builds->get_time($villagedata['main'],$building,$build_village[$building]+1);
// Wenn die letzte Abfrage keinen Datensatz gefunden hat, dann ist noch kein Auftrag vorhandne.
if ($db->affectedRows()==0){
// Kein Auftrag vorhanden:
$time += time();
// In Villages den ersten Auftrag speichern:
$add_village_sql = ",main_build='".$building.",$time' ";
} else {
// Bereits ein Auftrag vorhanden, in die Schleife:
$time += $build_village['end_time'];
}
// Nun mal die Rohstoffkosten abziehen bzw, BH dazuzählen UND Gebäude bauen aktiv aktivieren:
$db->query("UPDATE villages SET r_wood=r_wood-'".round($wood*$plus_costs)."',
r_stone=r_stone-'".round($stone*$plus_costs)."',
r_iron=r_iron-'".round($iron*$plus_costs)."',
r_bh=r_bh+'$bh' $add_village_sql where id='".$villagedata['id']."'");
// Nun den Auftrag noch in die Tabelle "build" rein:
$db->query("INSERT into build (building,villageid,end_time,build_time)
VALUES ('".$building."','".$villagedata['id']."','$time','$onlytime')");
$buildid = $db->getLastID();
// In Events den Bauauftrag speichern
$db->query("INSERT into events (event_time,event_type,event_id,user_id,villageid) VALUES ('$time','build','$buildid','".$botconfig['id']."','".$villagedata['id']."')");
//VILLAGEDATA AKTUALISIEREN
$new_data=array (
'r_bh'=>$bh,
'r_wood'=>$villagedata['r_wood'] - round($wood*$plus_costs),
'r_stone'=>$villagedata['r_stone'] - round($stone*$plus_costs),
'r_iron'=>$villagedata['r_iron'] - round($iron*$plus_costs)
);
$this->last_village = $new_data;
}
}
}
?>