Welcome to the Browser-Base Game Zone forums!
I can send you pics of my cocks if you want reference.
It would be nice if someone could create a tutorial or something about this, because I will be needing this if I have made a bid more progress in the stuff I'm coding right now.
Quote from: jannesiera on October 21, 2008, 09:47:55 AMIt would be nice if someone could create a tutorial or something about this, because I will be needing this if I have made a bid more progress in the stuff I'm coding right now. The discussion and The Tick will give you an idea how to do it. If you want more info I can post here my update class when my laptop comes back from repair.
<?phpabstract class update extends db{function city($city){$timenow = time();$stats=$this->get("city_production", "name='$city'"); //I get everything in the database foreach ($stats as $akey => $aval) { //lets assign them to the class $this -> {$akey} = $aval; }//foreachif($this->farm>0){ //simple production building $foodmulti=$timenow - $this->time_food; //we compare time between now and last update $mod=($this->farm*2)-($this->population*0.01); //in this case we do a small adjustment to the producting amount, people have to eat after all. The $this->farm*2 shows how much food one farm is producing and $this->population*0.01 how much people are eating in one hour. if ($mod!=0){ //we can't divide with zero after all $foodmodi=3600/$mod; //calculate how much we are producing in a hour. Time is in seconds so 60*60 seconds is an hour. $food_amount=floor($foodmulti/$foodmodi); //here we calculate how many food has been produced since last update. $food_time=($food_amount*$foodmodi)+$this->time_food; //Lets set new last update time. If we haven't produced the even number of foods we need to set it lower than time is now. $food_amount+=$this->food; //needed for my other buildings } else{ $food_amount=$this->food; $food_time=$timenow; } //if the modifier is 0 we are producing just enough food to keep everyone happy, so we don't need to update the amount}else{ $food_amount=$this->food-($this->population*0.01); // We have no farms, so our food stock is getting smaller$food_time=$timenow; } //set the time//Other buildings removed, they are just the same as farm.//ok, now for the building that uses food to produce horses!if($this->stable>0){ //check do we have any stables $horsemulti=$timenow - $this->time_horse; //same as farm above $horsemodi=3600/($this->stable*2); //2 horses per hour per stable $horse_amount=floor($horsemulti/$horsemodi); $horse_time=($horse_amount*$horsemodi)+$this->time_horse;//ok, now begins the different things; if($food_amount<$horse_amount*.1){ //if we don't have enough food to produce as many horses that the stables could produce $horse_amount=$food_amount*.1; //we produce as many we can $food_amount=0; //we used all the food $horse_time=$food_time; //we set the time to the time when we last produced food } else { $food_amount=round($food_amount-($horse_amount*.1));} //we have enough food for all the horses, so we need to calculate how much we are using. $horse_amount+=$this->horse;}else{$horse_amount=$this->horse; $horse_time=$time_now;}$set_time= $this->set("city_production", "time_weapon='$weapon_time', weapon='$weapon_amount', time_food='$food_time', food='$food_amount', time_horse='$horse_time', horse='$horse_amount', time_wood='$wood_time', wood='$wood_amount', time_lux='$lux_time', luxury='$lux_amount',time_stone='$stone_time', stone='$stone_amount', time_gem='$gem_time', gem='$gem_amount'", "name='$city'"); //update the db.}//city?>
Code: ( My update class) [Select]<?phpabstract class update extends db{function city($city){$timenow = time();$stats=$this->get("city_production", "name='$city'"); //I get everything in the database foreach ($stats as $akey => $aval) { //lets assign them to the class $this -> {$akey} = $aval; }//foreachif($this->farm>0){ //simple production building $foodmulti=$timenow - $this->time_food; //we compare time between now and last update $mod=($this->farm*2)-($this->population*0.01); //in this case we do a small adjustment to the producting amount, people have to eat after all. The $this->farm*2 shows how much food one farm is producing and $this->population*0.01 how much people are eating in one hour. if ($mod!=0){ //we can't divide with zero after all $foodmodi=3600/$mod; //calculate how much we are producing in a hour. Time is in seconds so 60*60 seconds is an hour. $food_amount=floor($foodmulti/$foodmodi); //here we calculate how many food has been produced since last update. $food_time=($food_amount*$foodmodi)+$this->time_food; //Lets set new last update time. If we haven't produced the even number of foods we need to set it lower than time is now. $food_amount+=$this->food; //needed for my other buildings } else{ $food_amount=$this->food; $food_time=$timenow; } //if the modifier is 0 we are producing just enough food to keep everyone happy, so we don't need to update the amount}else{ $food_amount=$this->food-($this->population*0.01); // We have no farms, so our food stock is getting smaller$food_time=$timenow; } //set the time//Other buildings removed, they are just the same as farm.//ok, now for the building that uses food to produce horses!if($this->stable>0){ //check do we have any stables $horsemulti=$timenow - $this->time_horse; //same as farm above $horsemodi=3600/($this->stable*2); //2 horses per hour per stable $horse_amount=floor($horsemulti/$horsemodi); $horse_time=($horse_amount*$horsemodi)+$this->time_horse;//ok, now begins the different things; if($food_amount<$horse_amount*.1){ //if we don't have enough food to produce as many horses that the stables could produce $horse_amount=$food_amount*.1; //we produce as many we can $food_amount=0; //we used all the food $horse_time=$food_time; //we set the time to the time when we last produced food } else { $food_amount=round($food_amount-($horse_amount*.1));} //we have enough food for all the horses, so we need to calculate how much we are using. $horse_amount+=$this->horse;}else{$horse_amount=$this->horse; $horse_time=$time_now;}$set_time= $this->set("city_production", "time_weapon='$weapon_time', weapon='$weapon_amount', time_food='$food_time', food='$food_amount', time_horse='$horse_time', horse='$horse_amount', time_wood='$wood_time', wood='$wood_amount', time_lux='$lux_time', luxury='$lux_amount',time_stone='$stone_time', stone='$stone_amount', time_gem='$gem_time', gem='$gem_amount'", "name='$city'"); //update the db.}//city?>There you go. I didn't post the whole class, because I want to keep it to myself but that should clear things up. I heavily commented it, but if you have any questions feel free to ask.$this->(resource name) is amount of named resource the city has, and $this->time_(resource name) is the time last time it was updated. All times are UNIX timestamps.Table Structure is simple, I have time_(resource name) (int(20)) for the latest update and (resource name) (int(11)) for the each resource.$this->get and $this->set are just database queries.
Code: ( My update class) [Select]<?php$timenow = time(); $foodmulti=$timenow - $this->time_food; //we compare time between now and last update ?>....Table Structure is simple, I have time_(resource name) (int(20)) for the latest update ...
<?php$timenow = time(); $foodmulti=$timenow - $this->time_food; //we compare time between now and last update ?>