PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/missions/MissionCaseSpy.php

https://bitbucket.org/VolCh/2moons
PHP | 197 lines | 134 code | 37 blank | 26 comment | 16 complexity | 19c8bbd834a609eee16a651e051ece59 MD5 | raw file
  1<?php
  2
  3/**
  4 *  2Moons
  5 *  Copyright (C) 2012 Jan Kr??pke
  6 *
  7 * This program is free software: you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation, either version 3 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19 *
 20 * @package 2Moons
 21 * @author Jan Kr??pke <info@2moons.cc>
 22 * @copyright 2012 Jan Kr??pke <info@2moons.cc>
 23 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 24 * @version 1.7.1 (2013-01-18)
 25 * @info $Id$
 26 * @link http://2moons.cc/
 27 */
 28
 29class MissionCaseSpy extends MissionFunctions
 30{
 31		
 32	function __construct($Fleet)
 33	{
 34		$this->_fleet	= $Fleet;
 35	}
 36	
 37	function TargetEvent()
 38	{
 39		global $pricelist, $reslist, $resource;		
 40		$senderUser		= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = ".$this->_fleet['fleet_owner'].";");
 41		$senderPlanet	= $GLOBALS['DATABASE']->getFirstRow("SELECT galaxy, system, planet, name FROM ".PLANETS." WHERE id = ".$this->_fleet['fleet_start_id'].";");
 42		$senderUser['factor']	= getFactors($senderUser, 'basic', $this->_fleet['fleet_start_time']);
 43		$ownSpyLvl		= max($senderUser['spy_tech'], 1);
 44		
 45		$LNG			= $this->getLanguage($senderUser['lang']);
 46		
 47		$targetUser		= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = ".$this->_fleet['fleet_target_owner'].";");
 48		$targetPlanet	= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".PLANETS." WHERE id = ".$this->_fleet['fleet_end_id'].";");
 49		
 50		$targetSpyLvl	= max($targetUser['spy_tech'], 1);
 51		
 52		$targetUser['factor']				= getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
 53		$PlanetRess 						= new ResourceUpdate();
 54		list($targetUser, $targetPlanet)	= $PlanetRess->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);
 55
 56		$targetStayFleets	= $GLOBALS['DATABASE']->query("SELECT * FROM ".FLEETS." WHERE fleet_end_id = ".$this->_fleet['fleet_end_id']." AND fleet_mission = 5 AND fleet_end_stay > ".$this->_fleet['fleet_start_time'].";");
 57		
 58		while($fleetRow = $GLOBALS['DATABASE']->fetch_array($targetStayFleets))
 59		{
 60			$temp = explode(';', $fleetRow['fleet_array']);
 61			foreach ($temp as $temp2)
 62			{
 63				$temp2 = explode(',', $temp2);
 64				if (!isset($targetPlanet[$resource[$temp2[0]]]))
 65				{
 66					$targetPlanet[$resource[$temp2[0]]] = 0;
 67				}
 68				
 69				$targetPlanet[$resource[$temp2[0]]] += $temp2[1];
 70			}
 71		}
 72		
 73		$GLOBALS['DATABASE']->free_result($targetStayFleets);
 74		
 75		$fleetAmount	= $this->_fleet['fleet_amount'] * (1 + $senderUser['factor']['SpyPower']);
 76		
 77		$Diffence		= abs($ownSpyLvl - $targetSpyLvl);
 78		$MinAmount		= ($ownSpyLvl > $targetSpyLvl ? -1 : 1) * pow($Diffence * SPY_DIFFENCE_FACTOR, 2);
 79		$SpyFleet		= $fleetAmount >= $MinAmount;
 80		$SpyDef			= $fleetAmount >= $MinAmount + 1 * SPY_VIEW_FACTOR;
 81		$SpyBuild		= $fleetAmount >= $MinAmount + 3 * SPY_VIEW_FACTOR;
 82		$SpyTechno		= $fleetAmount >= $MinAmount + 5 * SPY_VIEW_FACTOR;
 83			
 84
 85		$classIDs[900]	= array_merge($reslist['resstype'][1], $reslist['resstype'][2]);
 86				
 87		if($SpyFleet) 
 88		{
 89			$classIDs[200]	= $reslist['fleet'];
 90		}
 91		
 92		if($SpyDef) 
 93		{
 94			$classIDs[400]	= $reslist['defense'];
 95		}
 96		
 97		if($SpyBuild) 
 98		{
 99			$classIDs[0]	= $reslist['build'];
100		}
101		
102		if($SpyTechno) 
103		{
104			$classIDs[100]	= $reslist['tech'];
105		}
106		
107		$targetChance 	= mt_rand(0, min(($fleetAmount/4) * ($targetSpyLvl / $ownSpyLvl), 100));
108		$spyChance  	= mt_rand(0, 100);
109		
110		foreach($classIDs as $classID => $elementIDs)
111		{
112			foreach($elementIDs as $elementID)
113			{
114				if($classID == 100)
115				{
116					$spyData[$classID][$elementID]	= $targetUser[$resource[$elementID]];
117				}
118				else 
119				{
120					$spyData[$classID][$elementID]	= $targetPlanet[$resource[$elementID]];
121				}
122			}
123		
124			if($senderUser['spyMessagesMode'] == 1)
125			{
126				$spyData[$classID]	= array_filter($spyData[$classID]);
127			}
128		}
129		
130		// I'm use template class here, because i want to exclude HTML in PHP.
131		
132		require_once(ROOT_PATH.'includes/classes/class.template.php');
133		
134		$template	= new template;
135		
136		$template->caching		= true;
137		$template->compile_id	= $senderUser['lang'];
138		$template->loadFilter('output', 'trimwhitespace');
139		list($tplDir)	= $template->getTemplateDir();
140		$template->setTemplateDir($tplDir.'game/');
141		$template->assign_vars(array(
142			'spyData'		=> $spyData,
143			'targetPlanet'	=> $targetPlanet,
144			'targetChance'	=> $targetChance,
145			'spyChance'		=> $spyChance,
146			'isBattleSim'	=> ENABLE_SIMULATOR_LINK == true && isModulAvalible(MODULE_SIMULATOR),
147			'title'			=> sprintf($LNG['sys_mess_head'], $targetPlanet['name'], $targetPlanet['galaxy'], $targetPlanet['system'], $targetPlanet['planet'], _date($LNG['php_tdformat'], $this->_fleet['fleet_end_time'], $targetUser['timezone'], $LNG)),
148		));
149		
150		$template->assign_vars(array(
151			'LNG'			=> $LNG
152		), false);
153				
154		$spyRaport	= $template->fetch('shared.mission.spyraport.tpl');
155
156		SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_start_time'], 0, $LNG['sys_mess_qg'], $LNG['sys_mess_spy_report'], $spyRaport);
157		
158		$LNG			= $this->getLanguage($targetUser['lang']);
159		$targetMessage  = $LNG['sys_mess_spy_ennemyfleet'] ." ". $senderPlanet['name'];
160
161		if($this->_fleet['fleet_start_type'] == 3)
162			$targetMessage .= $LNG['sys_mess_spy_report_moon'].' ';
163
164		$targetMessage .= '<a href="game.php?page=galaxy&amp;galaxy='.$senderPlanet["galaxy"].'&amp;system='.$senderPlanet["system"].'">'.
165						  '['.$senderPlanet['galaxy'].':'.$senderPlanet['system'].':'.$senderPlanet['planet'].']</a> '.
166						  $LNG['sys_mess_spy_seen_at'].' '.$targetPlanet['name'].
167						  ' ['. $targetPlanet['galaxy'].':'.$targetPlanet['system'].':'.$targetPlanet['planet'].'] '.$LNG['sys_mess_spy_seen_at2'].'.';
168
169		SendSimpleMessage($this->_fleet['fleet_target_owner'], 0, $this->_fleet['fleet_start_time'], 0, $LNG['sys_mess_spy_control'], $LNG['sys_mess_spy_activity'], $targetMessage);
170
171		if ($targetChance >= $spyChance)
172		{
173			$CONF		= Config::getAll(NULL, $this->_fleet['fleet_universe']);
174			$WhereCol	= $this->_fleet['fleet_end_type'] == 3 ? "id_luna" : "id";		
175			$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET
176			der_metal = der_metal + ".($fleetAmount * $pricelist[210]['cost'][901] * (Config::get('Fleet_Cdr') / 100)).", 
177			der_crystal = der_crystal + ".($fleetAmount * $pricelist[210]['cost'][902] * (Config::get('Fleet_Cdr') / 100))." 
178			WHERE ".$WhereCol." = ".$this->_fleet['fleet_end_id'].";");
179			$this->KillFleet();
180		}
181		else
182		{
183			$this->setState(FLEET_RETURN);
184			$this->SaveFleet();
185		}
186	}
187	
188	function EndStayEvent()
189	{
190		return;
191	}
192	
193	function ReturnEvent()
194	{	
195		$this->RestoreFleet();
196	}
197}