/php/spy.php
PHP | 405 lines | 336 code | 29 blank | 40 comment | 28 complexity | 8c60ca1862585aafd4444807ebe0016d MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
- <?php
- /* spy.php - Back-end code for spying on cities
- *
- * 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/>.
- **/
- // Fairly ok but needs more documentation
- require_once ("php/battle.php");
- function do_spying ($evrow, $defuser, $attuser, $targetvill, $always_successfull=false)
- {
- global $debug;
- $attvillage = $evrow['village1'];
- $targetvillage = $evrow['village2'];
- // var_dump ($defuser);
- // var_dump ($attfuser);
- if ($targetvill == $evrow['village2'])
- {
- $tvillage = $defuser->villages[$targetvill];
- $outpost = false;
- }
- else
- {
- $tvillage = $defuser->villages[$targetvill]->outposts[$evrow['village2']];
- $outpost = true;
- }
- // We need the attackers spying level
- $query = "select A.level, B.class, B.hero_health from user B left join userresearch A on (B.uid=A.uid and A.resid in (2, 46)) where B.uid=".$evrow['uid1'];
- if (!($res = mysql_query ($query)))
- log_err ("Cannot fetch spy level. Query: ".$query.", ".mysql_error());
- else
- {
- //echo $query;
- // echo "<br>hiro!<br>".$hero_health;
- if (!($row = mysql_fetch_row ($res)))
- $att_spy = 0;
- else
- {
- $att_spy = (int)$row[0];
- $hero_health = $row[2];
- // Thieves spy better :)
- if ($row[1] == CLASS_THIEF)
- {
- $attacker_is_thief = true;
- $att_spy += 2;
- }
- }
- }
- mysql_free_result ($res);
- $armyinfo = array ();
- // Now the numbers of all involved units;
- $in_units = prepare_battle ($evrow['uid1'], $evrow['param'], $evrow['village2'], $defuser->hero, $armyinfo, false);
- $num_units = checkup_battle ($in_units);
- // The third root of the village score is the detection base
- if ($tvillage->score == 0)
- $villsize = 1;
- else
- $villsize = $tvillage->score;
- if (!$always_successfull)
- $detection = $num_units[0] / pow ($villsize, 1/4); // Formel: scouts/pow(groesse,1/3)
- else
- $detection = 0;
- // If the village has troops in it, the chance of detection rises
- if ($num_units[1] > 0)
- $detection *= (1 + sqrt ($num_units[1] / $villsize * 4)); // sqrt ($defuser->villages[$targetvillage]->score);
- $dice = rand (0, 99);
- log_debug ("Spy attempt. Hero-spyspy: ".$attuser->hero->spyspy_bonus.", Hero-spydef: ".$defuser->hero->spydef_bonus.", Detection: ".$detection." (".($detection * $attuser->hero->spyspy_bonus * $defuser->hero->spydef_bonus)."), ".$dice.", Num units: ".$num_units[1].", num spies: ".$num_units[0].", villsize: ".$villsize);
- $detection *= $attuser->hero->spyspy_bonus * $defuser->hero->spydef_bonus;
- if ($debug)
- {
- echo "Detection: ".$detection."\n";
- print_r ($num_units);
- echo "Attspy: ".$att_spy.", Defspy: ".$defuser->antispy_level.", dice: ".$dice."\n";
- }
- // Lets see how far these guys can see
- // if ($defuser->spy_level > 0)
- $espied = $att_spy - $defuser->antispy_level + 1 + (int)($num_units[0] / 10);
- // else
- // $espied = $att_spy + 1 + (int)($num_units[0] / 10);
- if ($always_successfull)
- $espied = rand (1, 6);
- if ($espied > 6)
- $espied = 6;
- // Where we seen?
- if ($dice <= ceil ($detection))
- {
- return (false);
- }
- log_debug ("Attspy: ".$att_spy.", Defspy: ".$defuser->antispy_level.", espied: ".$espied);
- $query = "";
- if ($debug)
- echo "Espied: ".$espied."\n";
- $query = "insert into battles values (NULL, 1)";
- if (!mysql_query ($query))
- log_err ("Cannot insert new battle entry while spying. Query: ".$query.", ".mysql_error());
- else
- {
- $battleid = mysql_insert_id ();
- // Now insert an army entry for each battle participant
- foreach ($in_units as $this_troopid => $his_values)
- {
- unset ($query);
- $this_uid = $his_values[2001];
- if ($his_values[2000] == 1) // Is this a defender?
- {
- if ($espied > 1)
- {
- $spy_units = true;
- if (!isset ($baids[$this_uid]))
- $query = "insert into bat_armies values (".$battleid.", NULL, 1, ".$this_uid.", ".$targetvillage.")";
- }
- else
- {
- // $query = "insert into bat_armies values (".$battleid.", NULL, 1, ".$this_uid.", ".$targetvillage.")";
- $spy_units = false;
- }
- }
- else
- {
- $spy_units = true;
- if (!isset ($baids[$this_uid]))
- $query = "insert into bat_armies values (".$battleid.", NULL, 0, ".$this_uid.", ".$attvillage.")";
- }
- if ($spy_units)
- {
- if (isset ($query))
- {
- if ($debug)
- echo "Bat query: ".$query."\n";
- if (!mysql_query ($query))
- {
- log_err ("Cannot write bat_army entry. ".$query.", error: ".mysql_error());
- return (false);
- }
- // Now we need the troop entry for this army
- $reparmyid = $baids[$this_uid] = mysql_insert_id ();
- }
- else
- $reparmyid = $baids[$this_uid];
- unset ($query);
- foreach ($his_values as $unitid=>$resultid)
- {
- if ($unitid < 1000)
- {
- $repqueries[$this_uid][$unitid] += $in_units[$this_troopid][$unitid];
- }
- else
- {
- if ($debug)
- echo "<br><br>You're supposed to be a hero! ".$hero_health;
- if ($unitid == 1000)
- $repqueries[$this_uid][1000] = (int)($resultid*100);
- //$repqueries[$this_uid][1000] = (int)($hero_health*100);
- }
- // print_r ($repqueries);
- }
- } // did we spy units?
- } // foreach
- if (count ($repqueries))
- {
- unset ($query);
- foreach ($repqueries as $repuid=>$unitentry)
- {
- foreach ($unitentry as $repunitid=>$entry)
- {
- if (isset ($query))
- $query .= ", (".$baids[$repuid].", ".$repunitid.", ".$entry.", 0)";
- else
- $query = "(".$baids[$repuid].", ".$repunitid.", ".$entry.", 0)";
- }
- }
- $query = "insert into bat_troops values ".$query;
- if (!mysql_query ($query))
- {
- log_err ("Cannot insert troops in spy.php. Query: ".$query.", error: ".mysql_error ());
- return (false);
- }
- }
- switch ($espied)
- {
- case 6: // items
- case 5: // hero stats
- case 4: // research
- if (!$outpost)
- {
- $query = "select resid, level from userresearch where uid=".$evrow['uid2'];
- if (!($res = mysql_query ($query)))
- log_err ("Cannot read user research levels in spy. Query: ".$query." ", mysql_error());
- else
- {
- if (isset ($lquery))
- $lquery .= ", (".$battleid.", 21, 0, 0)";
- else
- $lquery = "(".$battleid.", 21, 0, 0)";
- while ($row = mysql_fetch_row ($res))
- {
- $lquery .= ", (".$battleid.", 21, ".$row[0].", ".$row[1].")";
- }
- }
- }
- case 3: // city buildings
- if (!$outpost)
- {
- $query = "select type, level from villbuild where villageid=".$targetvillage;
- if (!($res = mysql_query ($query)))
- log_err ("Cannot read city building levels in spy. Query: ".$query." ", mysql_error());
- else
- {
- while ($row = mysql_fetch_row ($res))
- {
- if (isset ($lquery))
- $lquery .= ", (".$battleid.", 20, ".$row[0].", ".$row[1].")";
- else
- $lquery = "(".$battleid.", 20, ".$row[0].", ".$row[1].")";
- }
- }
- }
- case 2: // army
- case 1: // ressources
- if (WORLD_SPEED > 4)
- $hidden_div = sqrt (WORLD_SPEED / 2);
- else
- $hidden_div = 1;
- if (!$outpost)
- {
- foreach ($tvillage->ress_prod as $currency => $amount)
- $hidden[$currency] = min ($amount * 5, ($amount / $hidden_div) + (pow ($tvillage->ress_max, 0.36) * 10));
- log_debug ("Storage level ".$row[0].", hidden ".$hidden[1].", ".$hidden[2].", ".$hidden[3].", ".$hidden[4].", ress are ".$defuser->villages[$targetvillage]->ress_val (1).", ".$defuser->villages[$targetvillage]->ress_val (2).", ".$defuser->villages[$targetvillage]->ress_val (3).", ".$defuser->villages[$targetvillage]->ress_val (4));
- for ($currency = 1; $currency <= 4; ++$currency)
- {
- $amount = $defuser->villages[$targetvillage]->ress_val ($currency);
- if ($currency == 4)
- {
- if ($attacker_is_thief)
- $this_loot = max (0, (int)(($amount - $hidden[$currency]) / 2));
- else
- $this_loot = max (0, (int)(($amount - $hidden[$currency]) / 10));
- }
- else
- $this_loot = $amount - $hidden[$currency];
- if (isset ($lquery))
- $lquery .= ", (".$battleid.", ".($currency-1).", ".$evrow['uid2'].", ".$this_loot.")";
- else
- $lquery = "(".$battleid.", ".($currency-1).", ".$evrow['uid2'].", ".$this_loot.")";
- }
- }
- else
- {
- foreach ($tvillage->ress_prod as $ressid=>$amount)
- {
- if ($ressid == 4)
- {
- if ($attacker_is_thief)
- $amount = (int)($amount / 2);
- else
- $amount = (int)($amount / 10);
- }
- if (isset ($lquery))
- $lquery .= ", (".$battleid.", ".($ressid-1).", ".$evrow['uid2'].", ".$amount.")";
- else
- $lquery = "(".$battleid.", ".($ressid-1).", ".$evrow['uid2'].", ".$amount.")";
- }
- }
- break;
- }
- if (isset ($lquery))
- {
- // If this report is because we have questioned foreign spies, add a message about this to the event
- if ($always_successfull)
- $lquery .= ",(".$battleid.", 72, ".$evrow['uid1'].", 1)";
- $lquery = "insert into rep_loot values ".$lquery;
- if ($debug)
- echo "Lquery: ".$lquery;
- if (!mysql_query ($lquery))
- log_err ("Cannot insert loot entries when spying. Query: ".$lquery.", ".mysql_error());
- }
- $query = "select user from user where uid=".$evrow['uid1'];
- if (!($res = mysql_query ($query)))
- log_err ("Cannot read username for attacker when spying. Query: ".$query.", ".mysql_error());
- else
- {
- if (!$row = mysql_fetch_row ($res))
- {
- log_err ("No username found for attacker when spying. Query: ".$query);
- $attackername = "(gelĂśscht)";
- }
- else
- $attackername = $row[0];
- }
- if (!$always_successfull)
- $subject = mysql_real_escape_string (profile_link ($evrow['uid1'], $attackername)." späht <a href=\"land.php?z=".$tvillage->give_z ().'">'.$tvillage->name.'</a> von '.profile_link ($defuser->uid, $defuser->user)." aus");
- else
- $subject = mysql_real_escape_string (profile_link ($evrow['uid1'], $attackername)."'s Gefangenenbericht über <a href=\"land.php?z=".$tvillage->give_z ().'">'.$tvillage->name.'</a> von '.profile_link ($defuser->uid, $defuser->user));
- // print_r($armyinfo);
- $query = "insert into reports values (NULL, ".$battleid.", ".$evrow['uid1'].", ".$evrow['uid1'].", ".$evrow['village1'].", ".$evrow['uid2'].", ".$evrow['village2'].", addtime(now(),sec_to_time(".$evrow['dif'].")), 1, 0, 0, '".$subject."', ".(int)$attuser->aid.", '')";
- $receip_uids[] = $evrow['uid1'];
- if ($dice <= ceil ($detection))
- {
- $receip_uids[] = $evrow['uid2'];
- $query .= ",(NULL, ".$battleid.", ".$evrow['uid2'].", ".$evrow['uid1'].", ".$evrow['village1'].", ".$evrow['uid2'].", ".$evrow['village2'].", addtime(now(),sec_to_time(".$evrow['dif'].")), 1, 0, 0, '".$subject."', ".(int)$defuser->aid.", '')";
- }
- if ($debug)
- echo "Reportquery: ".$query."\n";
- if (!mysql_query ($query))
- log_err ("Cannot insert reports when spying. Query: ".$query.", ".mysql_error());
- // And give them all an update on their unread report count
- $query = "update user set reports=reports+1 where uid in (".implode (",", $receip_uids).")";
- if (!mysql_query ($query))
- log_err ("Cannot update unread report cound in spying. Query: ".$query.", ".mysql_error());
- } // insert into battles
- if (!$always_successfull)
- {
- // Now set the troops on the path towards home
- $query = "update armies set flag=10 where troopid=".$evrow['param'];
- if ($debug)
- echo "\n".$query;
- if (!mysql_query ($query))
- {
- log_err ("Cannot set troops to RETURNING state in spy ".$battleid.". Query: ".$query.", ".mysql_error ());
- }
- // Insert the event for the trip home
- $query = "insert into event (uid1, village1, uid2, village2, timestamp, type, param, param2, flag) values (".$evrow['uid1'].", ".$evrow['village1'].", ".$evrow['uid1'].", ".$evrow['village1'].", addtime(addtime(now(), sec_to_time(".$evrow['dif'].")), sec_to_time(".$evrow['param2'].")), 10, ".$evrow['param'].", ".$battleid.", 0)";
- if ($debug)
- echo "\n".$query;
- if (!@mysql_query ($query))
- {
- log_err ("Cannot insert return event when spying: ".$query.", error: ".mysql_error ());
- }
- }
- return (true);
- }