/php/base/EventHandler.inc.php
PHP | 1501 lines | 1142 code | 198 blank | 161 comment | 276 complexity | f519452a994c26d3d52ca9bd82f5a55e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /* EventHandler.inc.php - Event handler
- *
- * 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/>.
- **/
- // Producer-consumer pattern. Acceptable style and design.
- require_once ("event.php");
- require_once ("eventmap.php");
- class EventHandler
- {
- private $users = array ();
- private $queued_villages = array ();
- private $events = array ();
- private $mainuid;
- private $mainuser;
- private $running = false;
- private $deleids = array ();
- private $seen_eids = array ();
- private $user_row = null;
- private $refresh_villages = array ();
- private $startvillage = 0;
- public $gui = null;
- public $dummygui = null;
- public $fails = false;
- public function __construct ($user, $gui, &$user_row, $db, $startvillage=0)
- {
- $this->users[$user->uid] =& $user;
- $this->mainuid = $user->uid;
- $this->mainuser =& $this->users[$this->mainuid];
- $this->gui =& $gui;
- $this->user_row =& $user_row;
- $this->db = $db;
- if ($startvillage)
- $this->startvillage = $startvillage;
- else
- $this->startvillage = $this->mainuser->activevillageid;
- /* $query = "lock tables event write";
- if (!mysql_query ($query))
- {
- $this->mainuser->log_err ("Cannot lock table events. Query: ".$query.", ".mysql_error ());
- } */
- /*$query = "unlock tables event write";
- if (!mysql_query ($query))
- {
- $this->mainuser->log_err ("Cannot unlock table events. Query: ".$query.", ".mysql_error ());
- } */
- }
- public function run ()
- {
- return ($this->fetchmyevents ($this->mainuid, $this->startvillage));
- }
- private function log_this_event ($str, $dif, $eid, $ev)
- {
- $this->mainuser->log_debug ($str." event with DIF ".$dif." EID ".$eid." has uid1 ".$ev['uid1']." vid1 ".$ev['village1']." uid2 ".$ev['uid2']." vid2 ".$ev['village2']." stamp ".$ev['timestamp']." type ".$ev['type']." param ".$ev['param']." param2 ".$ev['param2']." score ".$ev['score']." flag ".$ev['flag']);
- }
- private function log_events ($str)
- {
- foreach ($this->events as $dif=>$timegroup)
- {
- foreach ($timegroup as $eid=>$ev)
- {
- $this->log_this_event ($str, $dif, $eid, $ev);
- }
- }
- }
- public function fetchmyevents ($uid, $village)
- {
- if (!isset ($this->users[$uid]) || !isset ($this->users[$uid]->villages[$village]))
- {
- $this->mainuser->log_err ("Cannot find user ".$this->mainuid." or his village ".$village." in fetchevents");
- return (false);
- }
- // Now check to see if this village is up-to-date
- if ($this->users[$uid]->villages[$village]->dirty)
- $this->users[$uid]->villages[$village]->update_to_latest (true, $this->user_row['time']);
- $this->users[$uid]->villages[$village]->updated = true;
- $query = "select A.*, time_to_sec(timediff(A.timestamp,\"" . $this->user_row['time'] . "\")) as dif, time(A.timestamp) as time_arrival from event A where (A.flag=1 and A.village1>0 and (A.timestamp<=\"" . $this->user_row['time'] . "\" or A.uid1=".$this->mainuid.")) or A.village1=".$village." or A.uid2=".$this->mainuid." for update";
- // $query = "select *, time_to_sec(timediff(timestamp,now())) as dif from event where (flag=1 and village1>0 and (timestamp<=now() or uid1=".$this->mainuid.")) or village1=".$this->mainuid." or uid2=".$this->mainuid." order by dif asc for update";
- if (!($evres = $this->db->query ($query)))
- {
- $this->mainuser->log_err ("Cannot calc events. Query: ".$query.", error: ".mysql_error ());
- // selects often fail in the events table because of the high level of locking.
- // This is not fatal. Just roll the transaction back and start over
- if ($this->db->isTimeOut ())
- {
- $this->mainuser->log_err ("fetchmyevents: Rolling back");
- $this->db->query ("rollback");
- $this->fails = true;
- }
- return false;
- }
- else
- {
- while ($row = mysql_fetch_array ($evres))
- {
- if (!isset ($this->seen_eids[$row['eid']]))
- {
- $this->seen_eids[$row['eid']] = true;
- $this->events[$row['dif']][$row['eid']] = $row;
- }
- }
- ksort ($this->events);
- $this->queued_villages[$village] = true;
- $this->log_events ("Queued ");
- return ($this->run_queue ());
- }
- }
- public function query_secondary_events ($uid, $newvillage)
- {
- if (count ($this->users[$uid]->villages[$newvillage]->outposts))
- $targetstr = " in (".$newvillage.",".implode (",", array_keys ($this->users[$uid]->villages[$newvillage]->outposts)).")";
- else
- $targetstr = "=".$newvillage;
- $query = "select *, time_to_sec(timediff(A.timestamp,\"" . $this->user_row['time'] . "\")) as dif, time(A.timestamp) as time_arrival from event A where (((A.uid1=".$uid." and A.village1=".$newvillage.") or (A.uid2=".$uid." and A.village2".$targetstr.")) and A.timestamp<=\"" . $this->user_row['time'] . "\") for update";
- return $query;
- }
- private function check_user ($uid, $log=false, $logstr="")
- {
- if (!isset ($this->users[$uid]))
- {
- // Deleting old events may fail because of table locking concurrency in events
- if (!$this->delete_old_events ())
- return false;
- $victim = new User ();
- $victim->BecomeUserByUid ($uid);
- $victim->invoked_by = $this->mainuid; // This is not exactly correct :)
- $victim->fetch_villages();
- $victim->master_time = $this->user_row['time'];
- $this->users[$uid] =& $victim;
- if ($log)
- $this->mainuser->log_debug ("Didn't have user ".$logstr." ".$uid." while running event queue");
- }
- return true;
- }
- private function check_village1 (&$evrow)
- {
- if ($evrow['village1'] > 0)
- {
- $this->mainuser->log_debug ("check_village1: ".$evrow['uid1'].", ".$evrow['village1']);
- return ($this->check_village ($evrow['uid1'], $evrow['village1']));
- }
- }
- private function check_village2 (&$evrow)
- {
- if (($evrow['uid2'] > 0) && ($evrow['village2'] > 0))
- {
- $this->mainuser->log_debug ("check_village2: ".$evrow['uid2'].", ".$evrow['village2']);
- return ($this->check_village ($evrow['uid2'], $evrow['village2']));
- }
- }
- // Check to see if this village is up-to-date, and also if it's events have been queued
- private function check_village (&$uid, $vid)
- {
- // Don't run queries if we run into deadlock already
- if ($this->fails)
- return (false);
- if (!isset ($this->users[$uid]))
- {
- if (!$this->check_user ($uid))
- {
- if ($this->db->isTimeOut ())
- {
- $this->mainuser->log_err ("Rolling back, cannot delete old events");
- $this->db->query ("rollback");
- $this->fails = true;
- return false;
- }
- }
- }
- if (!isset ($this->users[$uid]->villages[$vid]))
- {
- $targetvill = $this->users[$uid]->lookup_outpost ($vid);
- if (!$targetvill)
- {
- $this->users[$uid]->log_err ("Blech in check_village: Can't find village ".$vid." for ".$uid);
- // This is a serious problem. For now we read the true owner of the village/outpost from the village table
- // and then pretend this is his event.
- $uid = 0;
- return (false);
- // FIXME: Don't return false here but just use the uid from the village table
- }
- $outpostid = $vid;
- $vid = $targetvill;
- }
- if ($this->users[$uid]->villages[$vid]->dirty)
- $this->users[$uid]->villages[$vid]->update_to_latest (true, $this->user_row['time']);
- if (!isset ($this->queued_villages[$vid]))
- {
- $this->mainuser->log_debug ("village ".$vid." not queued");
- // Differentiate here between mainuid's events and other peoples events
- // mainuids events already have attack-type events included, so we only need
- // events for this village for them
- if ($uid == $this->mainuid)
- $query = "select *, time_to_sec(timediff(timestamp,\"" . $this->user_row['time'] . "\")) as dif, time(timestamp) as time_arrival from event where uid1=".$uid." and village1=".$vid." order by dif asc for update";
- else
- $query = $this->query_secondary_events ($uid, $vid);
- if (!($evres = $this->db->query ($query)))
- {
- $this->users[$uid]->log_err ("Cannot calc events when checking village. Query: ".$query.", error: ".$this->db->error ());
- // selects often fail in the events table because of the high level of locking.
- // This is not fatal. Just roll the transaction back and start over
- if ($this->db->isTimeOut ())
- {
- $this->mainuser->log_err ("Rolling back");
- $this->db->query ("rollback");
- $this->fails = true;
- return false;
- }
- }
- else
- {
- while ($row = mysql_fetch_array ($evres))
- {
- if (!isset ($this->seen_eids[$row['eid']]))
- {
- $this->seen_eids[$row['eid']] = true;
- $this->events[$row['dif']][$row['eid']] = $row;
- }
- }
- ksort ($this->events);
- $this->queued_villages[$vid] = true;
- return (true);
- }
- }
- return (false);
- }
- private function delete_this_eid ($evrow)
- {
- if ($evrow['eid'] > 0)
- $this->deleids[] = $evrow['eid'];
- }
- private function delete_old_events ()
- {
- if (count ($this->deleids) > 0)
- {
- $query = "delete from event where eid in (".implode (",", $this->deleids).")\n";
- $this->mainuser->log_debug ("delquery: ".$query);
- if (!mysql_query ($query))
- {
- $this->mainuser->log_err ("Cannot delete old events. Query: ".$query.", ".$this->db->error ());
- return false;
- }
- else
- $this->deleids = array ();
- }
- return true;
- }
- private function update_all_users ()
- {
- $dummygui = false;
- foreach ($this->users as $this_user)
- {
- // Only the calling user gets his activity upped
- if ($this_user->uid == $this->mainuid)
- $this_user->save_score (true);
- else
- $this_user->save_score (false);
- $this_user->hero->regenerate ();
- $deleids_to_add = array ();
- foreach ($this_user->villages as $this_village)
- {
- if (count ($this_village->recruiting))
- {
- if (($this_user->uid == $this->mainuid) && ($this_village->villageid == $this->startvillage))
- $this_user->update_score ($this_village->recruit_handler ($this->gui, $deleids_to_add));
- else
- $this_user->update_score ($this_village->recruit_handler ($dummygui, $deleids_to_add));
- $this_village->recruiting = array ();
- }
- if ($this_village->updated)
- {
- $this_village->do_income (true);
- if ($this_village->villageid != $this->mainuser->activevillageid)
- $this_village->update_stamp_nounlock (true, "nounlock", $this->user_row['time']);
- }
- }
- $this->deleids = array_merge ($deleids_to_add, $this->deleids);
- }
- }
- // Iterate through all queued events and handle each of them in chronological order
- public function run_queue ()
- {
- $this->running = true;
- // We might have to start the process multiple times
- while ($this->running)
- {
- $this->delete_old_events ();
- $this->running = false;
- foreach ($this->events as $dif=>$eventlist)
- {
- foreach ($eventlist as $eid=>$ev)
- {
- if ($ev['dif'] <= 0) // Events in the past, to be parsed - what a laugh
- {
- // First, we need to check some preconditions
- $this->check_user ($ev['uid1'], true, $logstr="uid1");
- if ($ev['uid2'] > 0)
- {
- $this->check_user ($ev['uid2'], true, $logstr="uid2");
- }
- // Check if we have run into the deadlock or lock timeout
- if ($this->fails)
- {
- // Yes, return and start over
- $this->mainuser->log_debug ("Returning from run_queue because of deadlock");
- return false;
- }
- $this->log_this_event ("Handling ", $dif, $eid, $ev);
- // Now start the run through the queue
- if ($this->handle_past ($ev))
- {
- $this->running = true;
- $this->mainuser->log_debug ("run_queue: Starting over!!");
- break 2;
- }
- else
- {
- unset ($this->events[$dif][$eid]);
- // Record an eventlog entry for all history
- // FIXME: Do this in BATCH!!!!
- if (($ev['type'] != RECRUITING) && ($ev['type'] != BUILDING_DELEGATE) && ($ev['type'] != RESEARCH_DELEGATE) && ($ev['type'] != RESSOURCE_DELEGATE))
- {
- $query = "insert into eventlog values (".$ev['eid'].", ".$ev['uid1'].", ".$ev['village1'].", ".$ev['uid2'].", ".$ev['village2'].", now(), '".$ev['timestamp']."', ".$ev['type'].", ".$ev['param'].", ".$ev['param2'].", ".$ev['score'].", 1)";
- if (!mysql_query ($query))
- $this->mainuser->log_err ("Cannot update eventlog. Query: ".$query.", ".mysql_error());
- }
- }
- }
- else
- {
- // Future events that don't match up with pre-loaded users are of no interest to us
- if (($ev['uid1'] == $this->mainuid) || ($ev['uid2'] == $this->mainuid))
- $this->handle_future ($ev);
- }
- }
- }
- }
- $this->update_all_users ();
- $this->delete_old_events ();
- return (true);
- }
- // Handle one past event
- // Returns true if the list of events might have changed and we have to re-iterate over all
- private function handle_past ($evrow)
- {
- if ($this->check_village1 ($evrow))
- return (true);
- // check_village might have told us this entry is botched
- if ($evrow['uid1'] == 0)
- $this->mainuser->log_err ("Skipping event ".$evrow['eid'].": Cannot find uid1/vid1 combo");
- switch ($evrow['type'])
- {
- // Building construction
- // Params:
- // village1 = village_id where construction takes place
- // village2 = building_id (not building type!!)
- // param = level that building is raised to
- // param2 = builing id
- case BUILDING:
- $this->handle_past_building ($evrow);
- $this->delete_this_eid ($evrow);
- break;
- case BUILDING_DELEGATE:
- if ($evrow['uid1'] == $this->mainuid)
- {
- $this->handle_past_building_delegate ($evrow);
- $this->delete_this_eid ($evrow);
- }
- break;
- case RL_ALLY_DIPL:
- // Reload diplomacy for my ally_reload_diplomacy
- if ($this->users[$evrow['uid1']]->aid && ($evrow['uid1'] == $this->mainuid))
- {
- $this->handle_past_diplomacy_reload ($evrow);
- $this->delete_this_eid ($evrow);
- }
- break;
- case 100:
- if ($evrow['uid1'] == $this->mainuid)
- {
- $this->mainuser->CalcCommanders ();
- $this->mainuser->log_info ("Recalced Commanders");
- $this->delete_this_eid ($evrow);
- }
- break;
- case RESEARCH:
- $this->handle_past_research ($evrow);
- $this->delete_this_eid ($evrow);
- break;
- case RESEARCH_DELEGATE:
- if ($evrow['uid1'] == $this->mainuid)
- {
- $this->mainuser->CalcResearch(true);
- $this->delete_this_eid ($evrow);
- }
- break;
- case RECRUITING:
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->recruiting[] = $evrow;
- break;
- case RETURNING:
- // Will this save the village later on???
- $this->users[$evrow['uid1']]->merge_troops ($evrow['village1'], $evrow['village1'], $evrow['param'], $evrow['param2'], abs ($evrow['dif']));
- $this->delete_this_eid ($evrow);
- break;
- case MOVE:
- $this->users[$evrow['uid1']]->merge_troops ($evrow['village2'], $evrow['village2'], $evrow['param'], 0, abs ($evrow['dif']));
- $this->delete_this_eid ($evrow);
- break;
- case SUPPORT:
- $this->users[$evrow['uid1']]->merge_troops ($evrow['village1'], $evrow['village2'], $evrow['param']);
- $this->delete_this_eid ($evrow);
- break;
- case GROUPATTACK_PART:
- $this->delete_this_eid ($evrow);
- break;
- case ATTACK:
- case GROUPATTACK:
- $fightrounds = 6;
- case SIEGE:
- if ($evrow['type'] == SIEGE) // Looks stupid, I know :)
- $fightrounds = 9;
- case RAID:
- if ($evrow['type'] == RAID) // Looks stupid, I know :)
- $fightrounds = 3;
- case SPY:
- if ($this->check_village2 ($evrow))
- return (true);
- else
- {
- // check_village might have told us this entry is botched
- if ($evrow['uid2'] == 0)
- $this->mainuser->log_err ("Skipping type ".$evrow['type']." event ".$evrow['eid']." uid1:".$evrow['uid1'].", village1: ".$evrow['village1'].": Cannot find uid2/vid2 combo");
- else
- {
- $retev = $this->handle_past_attack ($evrow, $fightrounds);
- if (is_array ($retev) && count($retev))
- {
- return (true);
- }
- }
- }
- break;
- case TRANSPORT:
- $olduid2 = $evrow['uid2'];
- $oldvid2 = $evrow['village2'];
- if ($this->check_village2 ($evrow))
- return (true);
- else
- {
- // check_village might have told us this entry is botched
- // if ($evrow['uid2'] == 0)
- // $this->mainuser->log_err ("Skipping transport event ".$evrow['eid']." uid1:".$evrow['uid1'].", village1: ".$evrow['village1'].": uid2:".$olduid2.", village2: ".$oldvid2."; Cannot find uid2/vid2 combo");
- if ($this->handle_past_transport ($evrow))
- {
- return (true);
- }
- }
- break;
- // Ressource fields
- case 1000:
- case 1001:
- case 1002:
- case 1003:
- case 1004:
- $this->handle_past_ressource_event ($evrow);
- $this->delete_this_eid ($evrow);
- break;
- case RESSOURCE_DELEGATE:
- if ($evrow['uid1'] == $this->mainuid)
- {
- if (isset ($this->mainuser->villages[$evrow['param2']]))
- $this->mainuser->villages[$evrow['param2']]->update_production();
- else
- $this->mainuser->villages[$evrow['village1']]->update_production();
- $this->delete_this_eid ($evrow);
- }
- break;
- }
- return (false);
- }
- private function handle_past_building ($evrow)
- {
- if (!is_object ($this->users[$evrow['uid1']]))
- {
- $this->mainuser->log_error ("User ".$evrow['uid1']." not instantiated for eid ".$evrow['eid']);
- }
- $this->users[$evrow['uid1']]->log_debug ("Fertig: building level for uid ".$evrow['uid1'].", village ".$evrow['village1'].", buildid ".$evrow['param2'].", level ".$evrow['param']);
- $query = "replace into villbuild values (".$evrow['village1'].", ".$evrow['param2'].", ".$evrow['param'].")";
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_error ("Cannot insert new building level for uid ".$evrow['uid1'].", village ".$evrow['village1'].", buildid ".$evrow['param2'].", level ".$evrow['param'].". Query: ".$query.", error ".mysql_error());
- // Delete the cached files
- $city_path="precity/";
- $precalcpath = $city_path.$evrow['village1'];
- $files = glob ($precalcpath."*");
- if (count ($files))
- {
- foreach ($files as $file)
- while (!@unlink ($file)) usleep (2000);
- }
- // Update the score
- $this->users[$evrow['uid1']]->update_score ($evrow['score'], $evrow['village1']);
- // All events that might affect values in the session object must add a delegate event
- if ($evrow['uid1'] != $this->mainuid)
- $this->users[$evrow['uid1']]->add_delegate (BUILDING_DELEGATE, false, $evrow['param2'], $evrow['village1']);
- // Some buildings have an effect on other events, like the alchemist or zunfthaus
- // Others change global flags, like storage. This is the building type we are
- // looking at
- switch ($evrow['param2'])
- {
- case 2: // Marketplace
- $merchants_to_add = ($evrow['param'] * 5) + 10;
- $query = "update village set merchants=merchants+".$merchants_to_add." where villageid=".$evrow['village1'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event marketplace update failed: ".$query.": ".mysql_error());
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->merchants += $merchants_to_add; // refresh merchants
- break;
- case 1: // Bergfried
- // Every 4 levels we gain an expansion slot
- if (($evrow['param'] % 4) == 0)
- {
- if ($evrow['param'] > 16)
- {
- $query = "update village set out_expansion=out_expansion+1 where villageid=".$evrow['village1'];
- $this->users[$evrow['uid1']]->log_debug ("Updating outpost expansion slots. Slots now: ".$this->users[$evrow['uid1']]->villages[$evrow['village1']]->outpost_expansion.", query: ".$query);
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event bergfried update failed: ".$query.": ".mysql_error ());
- }
- else
- {
- $query = "update village set expansion=expansion+1 where villageid=".$evrow['village1'];
- $this->users[$evrow['uid1']]->log_debug ("Updating expansion slots. Slots now: ".$this->users[$evrow['uid1']]->villages[$evrow['village1']]->expansion_slots.", query: ".$query);
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event bergfried update failed: ".$query.": ".mysql_error ());
- }
- if ($evrow['uid1'] == $this->mainuid)
- {
- $query = "select expansion, out_expansion from village where villageid=".$evrow['village1'];
- if (!($res = mysql_query ($query)))
- $this->users[$evrow['uid1']]->log_err ("Cannot read back expansion slots. Query: ".$query.", ".mysql_error());
- else
- {
- if (!($row = mysql_fetch_row ($res)))
- $this->users[$evrow['uid1']]->log_err ("No rows returned while reading back expansion slots. Query: ".$query);
- else
- {
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->expansion_slots = $row[0];
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->outpost_expansion = $row[1];
- }
- }
- }
- }
- break;
- case 5: // zunfthaus
- $factor = ConstructionTime ($evrow['param']);
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->speed_build = $factor;
- $query = "update village set buildspeed=\"".$factor."\" where villageid=".$evrow['village1'];
- // echo $query;
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event buildspeed update failed: ".$query.": ".mysql_error());
- break;
- case 6: // Storage
- // Storage space for level 1 is special
- if ($evrow['param'] == 1)
- $newstorage = LVL1_STORAGE;
- else
- $newstorage = LevelGadgetNice ($evrow['param'], LVL2_STORAGE);
- $query = "update village set resmax=".$newstorage." where villageid=".$evrow['village1'];
- // echo $query;
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event storage update failed: ".$query.": ".mysql_error());
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->ress_max = $newstorage; // refresh ressource production - could be handled more efficiently FIXME
- if ($this->mainuid != $evrow['uid1'])
- {
- $query = "update user set flags=flags|8 where uid=".$evrow['uid1'];
- if (!mysql_query($query))
- $this->users[$evrow['uid1']]->log_err ("Could not update user flags for village storage refresh");
- }
- break;
- case 10: // alchemist
- $factor = ConstructionTime ($evrow['param']);
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->speed_research = $factor;
- $query = "update village set resspeed=\"".$factor."\" where villageid=".$evrow['village1'];
- // echo $query;
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Event resspeed update failed: ".$query.": ".mysql_error());
- break;
- case 3: // garrison
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_recrspeed (1, $evrow);
- break;
- case 19: // stables
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_recrspeed (2, $evrow);
- break;
- case 18: // archery yard
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_recrspeed (3, $evrow);
- break;
- case 9: // workshop
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_recrspeed (4, $evrow);
- break;
- case 20: // castle
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_recrspeed (5, $evrow);
- break;
- }
- }
- private function handle_past_building_delegate ($evrow)
- {
- switch ($evrow['param2'])
- {
- case 2: // Marketplace
- $this->mainuser->villages[$evrow['village1']]->merchants += $evrow['param'] + 10; // refresh merchants
- break;
- case 1: // Bergfried
- $query = "select expansion from village where villageid=".$evrow['village1'];
- if (!($res = mysql_query ($query)))
- $this->mainuser->log_err ("Cannot read back expansion slots. Query: ".$query.", ".mysql_error());
- else
- {
- if (!($row = mysql_fetch_row ($res)))
- $this->mainuser->log_err ("No rows returned while reading back expansion slots. Query: ".$query);
- else
- $this->mainuser->villages[$evrow['village1']]->expansion_slots = $row[0];
- }
- break;
- case 5:
- case 6:
- case 10:
- case 3:
- case 19:
- case 18:
- case 9:
- case 20:
- $query = "select * from village where villageid=".$evrow['village1'];
- if (!($res = mysql_query ($query)))
- $this->mainuser->log_err ("Cannot read in delegate info: speeds. Query: ".$query.", ".mysql_error ());
- else
- {
- if (!($villageInfo = mysql_fetch_array ($res)))
- $this->mainuser->log_err ("No rows returned for delegate info: speeds. Query: ".$query.", ".mysql_error ());
- else
- {
- $this->mainuser->villages[$evrow['village1']]->speed_build = $villageInfo['buildspeed'];
- $this->mainuser->villages[$evrow['village1']]->speed_research = $villageInfo['resspeed'];
- $this->mainuser->villages[$evrow['village1']]->speed_recruit[1] = $villageInfo['recrspeed1']; // foot
- $this->mainuser->villages[$evrow['village1']]->speed_recruit[2] = $villageInfo['recrspeed2']; // horse
- $this->mainuser->villages[$evrow['village1']]->speed_recruit[3] = $villageInfo['recrspeed3']; // bow
- $this->mainuser->villages[$evrow['village1']]->speed_recruit[4] = $villageInfo['recrspeed4']; // siege
- $this->mainuser->villages[$evrow['village1']]->speed_recruit[5] = $villageInfo['recrspeed5']; // castle
- }
- }
- break;
- }
- }
- private function handle_past_diplomacy_reload ($evrow)
- {
- require_once ("ally.inc.php");
- $this->mainuser->allymeta = array ();
- if ($this->mainuser->aid)
- $this->mainuser->allymeta[] = $this->mainuser->aid;
- $this->mainuser->allydipl = ally_reload_diplomacy ($this->mainuser->aid, $this->mainuser->allymeta);
- $newallywars = ally_reload_wars ($this->mainuser->aid);
- // print_r ($newallywars);
- $this->mainuser->allywars = $newallywars;
- }
- private function handle_past_research ($evrow)
- {
- $this->users[$evrow['uid1']]->log_debug ("Fertig: research level for uid ".$evrow['uid1'].", village ".$evrow['village1'].", resid ".$evrow['param2'].", level ".$evrow['param']);
- $query = "replace into userresearch values (".$evrow['uid1'].", ".$evrow['param2'].", ".$evrow['param'].")";
- mysql_query ($query);
- // Update the score
- $this->users[$evrow['uid1']]->update_score ($evrow['score']);
- // Delete the cached city files
- $city_path="precity/";
- $precalcpath = $city_path.$evrow['village1'];
- @unlink ($precalcpath."-0.php");
- @unlink ($precalcpath."-1.php");
- @unlink ($precalcpath."-2.php");
- // We should recalc all research influenced variables
- $this->users[$evrow['uid1']]->CalcResearch(true, $evrow['dif']);
- // All events that might affect values in the session object must add a delegate event
- if ($evrow['uid1'] != $this->mainuid)
- {
- $query = "update user set flags=flags|2 where uid=".$evrow['uid1'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Cannot add research delegate. Query: ".$query.", ".mysql_error ());
- }
- }
- private function handle_past_attack ($evrow, $fightrounds)
- {
- global $debug, $user_row;
- $dummygui = false;
- $report_spying = false;
- // Big special here: People are able to attack outposts
- // So check if the target is an outpost
- if (!isset ($this->users[$evrow['uid2']]->villages[$evrow['village2']]))
- {
- $targetvill = $this->users[$evrow['uid2']]->lookup_outpost ($evrow['village2']);
- $this->users[$evrow['uid2']]->log_debug ("Looked up ".$evrow['village2'].", found ".$targetvill);
- $outpost_obj = $this->users[$evrow['uid2']]->villages[$targetvill]->outposts[$evrow['village2']];
- }
- else
- {
- $targetvill = $evrow['village2'];
- $outpost_obj = null;
- }
- $report_spying = false;
- $this->users[$evrow['uid2']]->log_debug ("Being attacked (type ".$evrow['type'].") in ".$evrow['village2']." - ".$targetvill);
- // Decrease yield, so that only income up to the point of attack is calculated
- $this->users[$evrow['uid2']]->villages[$targetvill]->do_income_partial (abs ($evrow['dif']), true);
- $this->delete_this_eid ($evrow);
- // Then check if this guy has some units to recruit
- if (count ($this->users[$evrow['uid2']]->villages[$evrow['village2']]->recruiting))
- {
- // We have to differentiate here again: Only use the "real" gui instance if this is the active village
- // of the main user
- if ($evrow['village2'] == $this->startvillage)
- $this->users[$evrow['uid2']]->update_score ($this->users[$evrow['uid2']]->villages[$evrow['village2']]->recruit_handler ($this->gui, $this->deleids, -$evrow['dif']));
- else
- $this->users[$evrow['uid2']]->update_score ($this->users[$evrow['uid2']]->villages[$evrow['village2']]->recruit_handler ($dummygui, $this->deleids, -$evrow['dif']));
- }
- $this->users[$evrow['uid2']]->hero->regenerate (-$evrow['dif']);
- // Check if this event is happening in an attack break
- if (START_OF_BREAK && END_OF_BREAK)
- {
- if (($_SERVER['REQUEST_TIME'] >= START_OF_BREAK) && ($_SERVER['REQUEST_TIME'] < END_OF_BREAK) && !is_ghost($evrow['uid2']))
- {
- $name1 = ghostify_name ($evrow['uid1'], $this->users[$evrow['uid1']]->user);
- $name2 = ghostify_name ($evrow['uid2'], $this->users[$evrow['uid2']]->user);
- // Yes, so just insert a dummy event with seasons greetings.
- // Technically, this is an entry in reports with a special type
- $query = "insert into battles values (NULL, 7)";
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings battle insert failed: ".$query.": ".mysql_error());
- $battleid = mysql_insert_id();
- $query = 'insert into reports (battleid, touid, uid1, village1, uid2, village2, stamp, type, subject, toaid, msg) values ('.$battleid.', '.$evrow['uid1'].', '.$evrow['uid1'].', '.$evrow['village1'].', '.$evrow['uid2'].', '.$evrow['village2'].', now(), 7, "'.sprintf (BREAK_SUBJECT, $name1, $name2).'", '.(int)$this->users->$evrow['uid2']->aid.', "'.sprintf (BREAK_MESSAGE, $name1, $name2).'")';
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings report insert1 failed: ".$query.": ".mysql_error());
- $query = 'insert into reports (battleid, touid, uid1, village1, uid2, village2, stamp, type, subject, toaid, msg) values ('.$battleid.', '.$evrow['uid2'].', '.$evrow['uid1'].', '.$evrow['village1'].', '.$evrow['uid2'].', '.$evrow['village2'].', now(), 7, "';
- $query .= sprintf (BREAK_SUBJECT, $name1, $name2);
- $query .= '", '.(int)$this->users->$evrow['uid2']->aid.', "'.sprintf (BREAK_MESSAGE, $name1, $name2).'")';
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings report insert failed: ".$query.": ".mysql_error());
- $query = "update armies set flag=10 where troopid=".$evrow['param'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings army update failed: ".$query.": ".mysql_error());
- $query = "insert into event (uid1, village1, uid2, village2, timestamp, type, param, param2, flag) values (".$evrow['uid1'].", ".$evrow['village1'].", 0, ".$evrow['village2'].", addtime(now(), sec_to_time(".($evrow['dif'] + $evrow['param2']).")), 10, ".$evrow['param'].", ".$battleid.", 0)";
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings event insert failed: ".$query.": ".mysql_error());
- $query = "update user set changed=changed,reports=reports+1 where uid in (".$evrow['uid1'].", ".$evrow['uid2'].")";
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Greetings user update failed: ".$query.": ".mysql_error());
- // Update local cached report number
- if (($this->mainuid == $evrow['uid1']) || ($this->mainuid == $evrow['uid1']))
- {
- $user_row[2]++;
- }
- return (false);
- }
- }
- if ($evrow['type'] == SPY)
- {
- $this->users[$evrow['uid2']]->log_debug ("I'm being spyed!!");
- require_once ("php/spy.php");
- if (!do_spying ($evrow, $this->users[$evrow['uid2']], $this->users[$evrow['uid1']], $targetvill))
- {
- $evrow['type'] = RAID;
- $fightrounds = 3;
- $report_spying = true;
- }
- }
- if ($evrow['type'] != SPY)
- {
- $this->users[$evrow['uid2']]->log_debug ("I'm being attacked!! Fightrounds: ".$fightrounds);
- require_once ("php/battle.php");
- $idmap = array ();
- $capacity = array ();
- $armyinfo = array ();
- $in_units = prepare_battle ($evrow['uid1'], $evrow['param'], $evrow['village2'], $this->users[$evrow['uid2']]->hero, $armyinfo, $report_spying, $evrow['type'], $evrow['score']);
- $num_units = checkup_battle ($in_units);
- $extra_result = array ();
- if ($debug || $_SESSION['always_debug'])
- {
- print_r ($num_units);
- $bflags = 1;
- }
- else
- $bflags = 0;
- // Out-take: We took this out because if there are catapults involved, there should always be a battle...
- // if (($num_units[0] > 0) && ($num_units[1] > 0)) // FIXME: Can this really be correct?
- {
- $survivors = do_battle ($in_units, $idmap, $capacity, $fightrounds, $armyinfo, $num_units, $bflags, $extra_result, $report_spying);
- $result = $survivors[count($survivors)];
- require_once ("php/postbattle.php");
- $post_battle = new Postbattle ($evrow['uid1'], $evrow['village1'], $evrow['uid2'], $evrow['village2'], $evrow['type'], $evrow['dif'], $in_units, $result, $idmap, $evrow['param2'], $capacity, $this->users[$evrow['uid2']]->villages[$targetvill], $report_spying, $armyinfo, true, $extra_result, $evrow['param'], $evrow['score'], $outpost_obj, $this->users[$evrow['uid1']], $this->users[$evrow['uid2']]);
- $retev = $post_battle->post_battle ();
- // Finished sieges need special consideration
- if ($evrow['type'] == SIEGE)
- {
- if ($post_battle->siege_successful)
- {
- // Set delegate flags and also move around the score
- $query = "update user set changed=changed,flags=flags|8,score=score-(select score from village where villageid=".$evrow['village2'].") where uid=".$evrow['uid2'];
- $this->mainuser->log_debug ($query);
- if (!mysql_query ($query))
- $this->mainuser->log_err ("Cannot reduce score in postsiege. Query: ".$query.", ".mysql_error ());
- $query = "update user set changed=changed,flags=flags|8,score=score+(select score from village where villageid=".$evrow['village2'].") where uid=".$evrow['uid1'];
- $this->mainuser->log_debug ($query);
- if (!mysql_query ($query))
- $this->mainuser->log_err ("Cannot add score in postsiege. Query: ".$query.", ".mysql_error ());
- $this->mainuser->log_debug ("Siege successful. Refreshing both participants (".$evrow['uid1']." and ".$evrow['uid2'].") and changing slots.");
- // First move village score to new owner
- $this->users[$evrow['uid1']]->update_score ($score_move);
- $this->users[$evrow['uid2']]->update_score (-$score_move);
- // Reload villages
- $this->users[$evrow['uid1']]->refresh_villages ();
- $this->users[$evrow['uid2']]->refresh_villages ();
- // Now zero the upgrade slots for the besieged village, we will recalculate it anyway
- $query = "update village set expansion=0 where villageid=".$evrow['village2'];
- if (!mysql_query ($query))
- $this->mainuser->log_err ("Cannot zero expansion slots for target after siege. Query: ".$query.", ".mysql_error ());
- $num_vill = count ($this->users[$evrow['uid1']]->villages);
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->recalculate_upgrade ($evrow['village2'], $num_vill);
- // Up outexpansion slots for victim
- $query = "update village set out_expansion=out_expansion+1 where villageid=".$targetvill;
- $this->users[$evrow['uid2']]->log_debug ("Post-Siege expansion: ".$query);
- if (!mysql_query ($query))
- {
- $this->mainuser->log_err ("Cannot reduce outpost expansion slots in postsiege. Query: ".$query.", ".mysql_error ());
- }
- $this->users[$evrow['uid2']]->villages[$targetvill]->outpost_expansion++;
- //finally, give building events from the stolen outpost to its new owner
- $query = 'update event set uid1='.$evrow['uid1'].',village1='.$evrow['village1'].' where (type >= 1000) AND param2='.$evrow['village2'];
- if (!mysql_query($query))
- $this->mainuser->log_err ('could not update building events for stolen outpost: '.$query);
- }
- else
- {
- // Up outexpansion slots for attacker
- $query = "update village set out_expansion=out_expansion+1 where villageid=".$evrow['village1'];
- $this->mainuser->log_debug ("Siege failed: ".$query);
- if (!mysql_query ($query))
- {
- log_err ("Cannot reduce outpost expansion slots in postsiege. Query: ".$query.", ".mysql_error ());
- }
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->outpost_expansion++;
- }
- }
- // increase unread message count if the main user is involved in this combat
- if (($this->mainuid == $evrow['uid1']) || ($this->mainuid == $evrow['uid2']))
- $this->user_row[2]++;
- }
- if (is_array ($retev) && count ($retev))
- {
- unset ($this->events[$evrow['dif']][$evrow['eid']]);
- foreach ($retev as $this_retevent)
- {
- $this->events[$this_retevent['dif']][] = $this_retevent;
- }
- }
- return ($retev);
- }
- return (array ());
- }
- // Handle a transport
- // FIXME: No GUI stuff at the moment, but maybe we will do this with delegates?
- private function handle_past_transport ($evrow)
- {
- // Did the transport just return?
- if ($evrow['uid2'] == 0)
- {
- // Then just delete the transport record
- $query = "delete from transport where merchantid = ".$evrow['param'];
- if (!mysql_query ($query))
- {
- $this->users[$evrow['uid2']]->log_err ("Cannot delete transport entry in event handler. Query: ".$query.", ".mysql_error ());
- }
- // We didn't delete the event before, because the blacklist might have deleted them too early
- $this->delete_this_eid ($evrow);
- return (false);
- }
- $sender = $this->users[$evrow['uid1']]->user;
- // We didn't delete the event before, because the blacklist might have deleted them too early
- $this->delete_this_eid ($evrow);
- // Decrease yield, so that only income up to the point of transport is calculated
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->do_income_partial (abs ($evrow['dif']), true);
- // Fetch the details of the transport
- $query = "select * from transport where merchantid = ".$evrow['param'];
- if (!($res = mysql_query ($query)))
- {
- $this->users[$evrow['uid2']]->log_err ("Cannot fetch transport details in event handler. Query: ".$query.", ".mysql_error ());
- }
- else
- {
- if (!($row = mysql_fetch_array ($res)))
- {
- $this->users[$evrow['uid2']]->log_err ("No transport details found in event handler. Query: ".$query.", ".mysql_error ());
- }
- else
- {
- $this->users[$evrow['uid2']]->log_debug ("Transport: Ress before: ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[1].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[2].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[3].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[4]);
- $happy_message = $row['trade_msg'];
- // Hand 'em off
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->spend (-$row['ress1'], -$row['ress2'], -$row['ress3'], -$row['ress4']);
- /*
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[1] = min ($row['ress1'] + $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[1], $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress_max);
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[2] = min ($row['ress2'] + $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[2], $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress_max);
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[3] = min ($row['ress3'] + $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[3], $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress_max);
- $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[4] = min ($row['ress4'] + $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[4], $this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress_max); */
- $this->users[$evrow['uid2']]->log_debug ("Transport: Ress after: ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[1].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[2].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[3].", ".$this->users[$evrow['uid2']]->villages[$evrow['village2']]->ress[4]);
- // Log the transport
- $query = "insert into old_transport select * from transport where merchantid=".$evrow['param'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot save old transport. Query: ".$query.", ".mysql_error());
- else
- {
- // Check param2 to see if the return is within the timeframe of this event handlement
- // and if so, push the event on the local event stack instead of the DB
- if ($evrow['param2'] > abs ($evrow['dif']))
- {
- // No, we need to issue a return event
- $query = "insert into event (uid1, village1, uid2, village2, type, param, param2, timestamp, flag) values (".$evrow['uid1'].", ".$evrow['village1'].", 0, ".$evrow['village2'].", ".TRANSPORT.", ".$evrow['param'].", ".$evrow['param2'].", addtime(addtime(now(), sec_to_time(".$evrow['dif'].")), sec_to_time(".$evrow['param2'].")), 0)";
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot set transport return event. Query: ".$query.", ".mysql_error ());
- // Then send the merchants home empty-handed
- $query = "update transport set ress1=0,ress2=0,ress3=0,ress4=0,flag=1 where merchantid=".$evrow['param'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot set transport return data. Query: ".$query.", ".mysql_error());
- // In this case, we also want the transport displayed in the merchant overview
- $evrow['dif'] += $evrow['param2'];
- $this->add_past_transport_gui_item ($evrow);
- }
- else
- {
- // Yes, the merchants should return immediately (this trick doesn't disturb anything)
- $query = "delete from transport where merchantid=".$evrow['param'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot delete transport while instant-home-transporting. Query: ".$query.", ".mysql_error ());
- }
- $query = "insert into battles values (NULL, 5)";
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot insert transport misnamed battle entry. Query: ".$query.", ".mysql_error());
- else
- {
- $battleid = mysql_insert_id ();
- $query_end = ", ".$evrow['uid1'].", ".$evrow['village1'].", ".$evrow['uid2'].", ".$evrow['village2'].", \"".$evrow['timestamp']."\", 5, 0, 0,";
- $subject = mysql_real_escape_string (profile_link ($evrow['uid1'], $sender).' beliefert <a href="land.php?z='.$this->users[$evrow['uid2']]->villages[$evrow['village2']]->give_z ().'">'.$this->users[$evrow['uid2']]->villages[$evrow['village2']]->name.'</a> von '.profile_link ($evrow['uid2'], $this->users[$evrow['uid2']]->user));
- if ($evrow['uid1'] == $evrow['uid2'])
- $query = 'insert into reports values (NULL, '.$battleid.', '.$evrow['uid1'].$query_end.' "'.$subject.'", 0, "'.$happy_message.'")';
- else
- $query = 'insert into reports values (NULL, '.$battleid.', '.$evrow['uid1'].$query_end.' "'.$subject.'", 0, "'.$happy_message.'"),(NULL, '.$battleid.', '.$evrow['uid2'].$query_end.' "'.$subject.'",0, "'.$happy_message.'")';
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot insert transport reports. Query: ".$query.", ".mysql_error());
- else
- {
- $query = "insert into rep_loot values (".$battleid.", 0, ".$evrow['uid2'].", ".$row['ress1']."), (".$battleid.", 1, ".$evrow['uid2'].", ".$row['ress2']."), (".$battleid.", 2, ".$evrow['uid2'].", ".$row['ress3']."), (".$battleid.", 3, ".$evrow['uid2'].", ".$row['ress4'].")";
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot insert transport report loot entry for uid1. Query: ".$query.", ".mysql_error ());
- else
- {
- // Bump the unread reports count for the user in the DB...
- $query = "update user set changed=changed,reports=reports+1 where uid in (".$evrow['uid1'].", ".$evrow['uid2'].")";
- if (!mysql_query ($query))
- $this->users[$evrow['uid2']]->log_err ("Cannot update report count after transport. Query: ".$query.", ".mysql_error());
- // ...and also in the row read from the DB if the main user is either sender or receiver of the transport
- if (($this->mainuid == $evrow['uid1']) || ($this->mainuid == $evrow['uid2']))
- $this->user_row[2]++;
- }
- }
- }
- }
- }
- mysql_free_result ($res);
- }
- }
- private function handle_past_ressource_event ($evrow)
- {
- global $world;
- $up_z = $evrow['village2'];
- $x = ($up_z % $world->sizex);
- $y = ((int)($up_z / $world->sizex));
- if (FINITE_RESSOURCES)
- {
- $this->users[$evrow['uid1']]->UpdateResTile ($x, $y, $evrow['param'], $evrow['dif']);
- }
- else
- {
- // First update the tile level in the DB, just to be sure
- $query = "update tileres set level=".$evrow['param']." where x=".$x." and y=".$y;
- $this->users[$evrow['uid1']]->log_debug ("Fertig: Ausbau Feld ".$evrow['type']." in ".$up_z." auf Stufe ".$evrow['param']." vor ".$evrow['dif']." Sekunden");
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Cannot update tileres after resstile is done. Query: ".$query.", ".mysql_error ());
- }
- // Now make ressource ticks up to the time of the enhancement deadline
- // $this->villages[$did]->do_income_partial (abs ($evrow['dif']), true);
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->do_income_partial (abs ($evrow['dif']), true);
- // It this ress field is in an outpost, special consideration is needed
- if ($evrow['param2'])
- {
- // Yes, 'tis an outpost
- if (!isset ($this->users[$evrow['uid1']]->villages[$evrow['param2']]))
- {
- $num_vill = count ($this->users[$evrow['uid1']]->villages);
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->recalculate_upgrade ($evrow['param2'], $num_vill);
- }
- // Count the score towards the outpost, not the village
- $query = "update village set stamp=stamp,score=score+".$evrow['score']." where villageid=".$evrow['param2'];
- // Also give the man his due, of course :)
- $this->users[$evrow['uid1']]->update_score ($evrow['score']);
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Cannot update outpost score after resstile is done. Query: ".$query.", ".mysql_error ());
- $this->users[$evrow['uid1']]->CalculateVillageProd ($evrow['param2']);
- if (isset ($this->users[$evrow['uid1']]->villages[$evrow['param2']]))
- $this->users[$evrow['uid1']]->villages[$evrow['param2']]->update_production();
- else
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->update_production();
- }
- else
- {
- // Count the score towards the village then
- $this->users[$evrow['uid1']]->update_score ($evrow['score'], $evrow['village1']);
- // This is a hack.
- // If this is not an outpost, we count the number of used ressource tiles.
- // If it 8 or greater, outposts can be founded. This is recorded as expansion_slots >= 127,
- // but only if it is not already greater of course :)
- $this->users[$evrow['uid1']]->log_debug ("Checking expansion slots for outpost enabling for vid ".$evrow['village1'].": ".$this->users[$evrow['uid1']]->villages[$evrow['village1']]->expansion_slots);
- if ($this->users[$evrow['uid1']]->villages[$evrow['village1']]->expansion_slots < 127)
- {
- $query = "select count(villageid) from tileres where level>0 and villageid=".$evrow['village1'];
- if (!($res = mysql_query ($query)))
- {
- $this->users[$evrow['uid1']]->log_err ("Cannot fetch tile count after upgrading res field. Query: ".$query.", ".mysql_error ());
- }
- else
- {
- if (($row = mysql_fetch_row ($res)))
- {
- $this->users[$evrow['uid1']]->log_debug ("Checking tile count for outpost enabling for vid ".$evrow['village1'].": ".$row[0]);
- // Now check if there are already 8 tiles
- if ($row[0] >= 8)
- {
- $query = "update village set expansion=expansion+127 where villageid=".$evrow['village1'];
- if (!mysql_query ($query))
- $this->users[$evrow['uid1']]->log_err ("Cannot update slots for outpost settling after upgrading res field. Query: ".$query.", ".mysql_error ());
- else
- $this->users[$evrow['uid1']]->villages[$evrow['village1']]->expansion_slots += 127;
- }
- }
- else
- $this->users[$evrow['uid1']]->log_err ("No tile count after upgrading res field. Query: ".$query.", …
Large files files are truncated, but you can click here to view the full file