PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/missions/MissionCaseSpy.class.php

https://bitbucket.org/VolCh/2moons
PHP | 235 lines | 163 code | 46 blank | 26 comment | 14 complexity | 67ab7a361f953f47058aefc349a738e4 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.2 (2013-03-18)
 25 * @info $Id$
 26 * @link http://2moons.cc/
 27 */
 28
 29class MissionCaseSpy extends MissionFunctions implements Mission
 30{
 31		
 32	function __construct($Fleet)
 33	{
 34		$this->_fleet	= $Fleet;
 35	}
 36	
 37	function TargetEvent()
 38	{
 39		global $pricelist, $reslist, $resource;
 40
 41		$db				= Database::get();
 42
 43		$sql			= 'SELECT * FROM %%USERS%% WHERE id = :userId;';
 44		$senderUser		= $db->selectSingle($sql, array(
 45			':userId'	=> $this->_fleet['fleet_owner']
 46		));
 47
 48		$targetUser		= $db->selectSingle($sql, array(
 49			':userId'	=> $this->_fleet['fleet_target_owner']
 50		));
 51
 52		$sql			= 'SELECT * FROM %%PLANETS%% WHERE id = :planetId;';
 53		$targetPlanet	= $db->selectSingle($sql, array(
 54			':planetId'	=> $this->_fleet['fleet_end_id']
 55		));
 56
 57		$sql				= 'SELECT name FROM %%PLANETS%% WHERE id = :planetId;';
 58		$senderPlanetName	= $db->selectSingle($sql, array(
 59			':planetId'	=> $this->_fleet['fleet_start_id']
 60		), 'name');
 61
 62		$LNG			= $this->getLanguage($senderUser['lang']);
 63
 64		$senderUser['factor']	= getFactors($senderUser, 'basic', $this->_fleet['fleet_start_time']);
 65		$targetUser['factor']	= getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
 66
 67		$planetUpdater 						= new ResourceUpdate();
 68		list($targetUser, $targetPlanet)	= $planetUpdater->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);
 69
 70		$sql	= 'SELECT * FROM %%FLEETS%% WHERE fleet_end_id = :planetId AND fleet_mission = 5 AND fleet_end_stay > :time;';
 71
 72		$targetStayFleets	= $db->select($sql, array(
 73			':planetId'	=> $this->_fleet['fleet_end_id'],
 74			':time'		=> $this->_fleet['fleet_start_time'],
 75		));
 76
 77		foreach($targetStayFleets as $fleetRow)
 78		{
 79			$fleetData	= FleetFunctions::unserialize($fleetRow['fleet_array']);
 80			foreach($fleetData as $shipId => $shipAmount)
 81			{
 82				$targetPlanet[$resource[$shipId]]	+= $shipAmount;
 83			}
 84		}
 85		
 86		$fleetAmount	= $this->_fleet['fleet_amount'] * (1 + $senderUser['factor']['SpyPower']);
 87
 88		$senderSpyTech	= max($senderUser['spy_tech'], 1);
 89		$targetSpyTech	= max($targetUser['spy_tech'], 1);
 90
 91		$techDifference	= abs($senderSpyTech - $targetSpyTech);
 92		$MinAmount		= ($senderSpyTech > $targetSpyTech ? -1 : 1) * pow($techDifference * SPY_DIFFENCE_FACTOR, 2);
 93		$SpyFleet		= $fleetAmount >= $MinAmount;
 94		$SpyDef			= $fleetAmount >= $MinAmount + 1 * SPY_VIEW_FACTOR;
 95		$SpyBuild		= $fleetAmount >= $MinAmount + 3 * SPY_VIEW_FACTOR;
 96		$SpyTechno		= $fleetAmount >= $MinAmount + 5 * SPY_VIEW_FACTOR;
 97			
 98
 99		$classIDs[900]	= array_merge($reslist['resstype'][1], $reslist['resstype'][2]);
100				
101		if($SpyFleet) 
102		{
103			$classIDs[200]	= $reslist['fleet'];
104		}
105		
106		if($SpyDef) 
107		{
108			$classIDs[400]	= array_merge($reslist['defense'], $reslist['missile']);
109		}
110		
111		if($SpyBuild) 
112		{
113			$classIDs[0]	= $reslist['build'];
114		}
115		
116		if($SpyTechno) 
117		{
118			$classIDs[100]	= $reslist['tech'];
119		}
120		
121		$targetChance 	= mt_rand(0, min(($fleetAmount/4) * ($targetSpyTech / $senderSpyTech), 100));
122		$spyChance  	= mt_rand(0, 100);
123		$spyData		= array();
124
125		foreach($classIDs as $classID => $elementIDs)
126		{
127			foreach($elementIDs as $elementID)
128			{
129				if(isset($targetUser[$resource[$elementID]]))
130				{
131					$spyData[$classID][$elementID]	= $targetUser[$resource[$elementID]];
132				}
133				else 
134				{
135					$spyData[$classID][$elementID]	= $targetPlanet[$resource[$elementID]];
136				}
137			}
138		
139			if($senderUser['spyMessagesMode'] == 1)
140			{
141				$spyData[$classID]	= array_filter($spyData[$classID]);
142			}
143		}
144		
145		// I'm use template class here, because i want to exclude HTML in PHP.
146		
147		require_once 'includes/classes/class.template.php';
148		
149		$template	= new template;
150		
151		$template->caching		= true;
152		$template->compile_id	= $senderUser['lang'];
153		$template->loadFilter('output', 'trimwhitespace');
154		list($tplDir)	= $template->getTemplateDir();
155		$template->setTemplateDir($tplDir.'game/');
156		$template->assign_vars(array(
157			'spyData'		=> $spyData,
158			'targetPlanet'	=> $targetPlanet,
159			'targetChance'	=> $targetChance,
160			'spyChance'		=> $spyChance,
161			'isBattleSim'	=> ENABLE_SIMULATOR_LINK == true && isModulAvalible(MODULE_SIMULATOR),
162			'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)),
163		));
164		
165		$template->assign_vars(array(
166			'LNG'			=> $LNG
167		), false);
168				
169		$spyReport	= $template->fetch('shared.mission.spyReport.tpl');
170
171		PlayerUtil::sendMessage($this->_fleet['fleet_owner'], 0, $LNG['sys_mess_qg'], 0, $LNG['sys_mess_spy_report'],
172			$spyReport, $this->_fleet['fleet_start_time'], NULL, 1, $this->_fleet['fleet_universe']);
173		
174		$LNG			= $this->getLanguage($targetUser['lang']);
175		$targetMessage  = $LNG['sys_mess_spy_ennemyfleet'] ." ". $senderPlanetName;
176
177		if($this->_fleet['fleet_start_type'] == 3)
178		{
179			$targetMessage .= $LNG['sys_mess_spy_report_moon'].' ';
180		}
181
182		$text	= '<a href="game.php?page=galaxy&amp;galaxy=%1$s&amp;system=%2$s">[%1$s:%2$s:%3$s]</a> %7$s
183		%8$s <a href="game.php?page=galaxy&amp;galaxy=%4$s&amp;system=%5$s">[%4$s:%5$s:%6$s]</a> %9$s';
184
185		$targetMessage .= sprintf($text,
186			$this->_fleet['fleet_start_galaxy'],
187			$this->_fleet['fleet_start_system'],
188			$this->_fleet['fleet_start_planet'],
189			$this->_fleet['fleet_end_galaxy'],
190			$this->_fleet['fleet_end_system'],
191			$this->_fleet['fleet_end_planet'],
192			$LNG['sys_mess_spy_seen_at'],
193			$targetPlanet['name'],
194			$LNG['sys_mess_spy_seen_at2']
195		);
196
197
198		PlayerUtil::sendMessage($this->_fleet['fleet_target_owner'], 0, $LNG['sys_mess_spy_control'], 0,
199			$LNG['sys_mess_spy_activity'], $targetMessage, $this->_fleet['fleet_start_time'], NULL, 1, $this->_fleet['fleet_universe']);
200
201		if ($targetChance >= $spyChance)
202		{
203			$config		= Config::get($this->_fleet['fleet_universe']);
204			$whereCol	= $this->_fleet['fleet_end_type'] == 3 ? "id_luna" : "id";
205
206			$sql		= 'UPDATE %%PLANETS%% SET
207			der_metal	= der_metal + :metal,
208			der_crystal = der_crystal + :crystal
209			WHERE '.$whereCol.' = :planetId;';
210
211			$db->update($sql, array(
212				':metal'	=> $fleetAmount * $pricelist[210]['cost'][901] * $config->Fleet_Cdr / 100,
213				':crystal'	=> $fleetAmount * $pricelist[210]['cost'][902] * $config->Fleet_Cdr / 100,
214				':planetId'	=> $this->_fleet['fleet_end_id']
215			));
216
217			$this->KillFleet();
218		}
219		else
220		{
221			$this->setState(FLEET_RETURN);
222			$this->SaveFleet();
223		}
224	}
225	
226	function EndStayEvent()
227	{
228		return;
229	}
230	
231	function ReturnEvent()
232	{	
233		$this->RestoreFleet();
234	}
235}