1
<?php
2
/**
3
 *   File functions:
4
 *   Outposts - a game in game.
5
 *
6
 *   @name				 : outposts.php
7
 *   @copyright			: (C) 2004,2005,2006,2007 Vallheru Team based on Gamers-Fusion ver 2.5
8
 *   @author			   : thindil <thindil@users.sourceforge.net>
9
 *   @author			   : eyescream <tduda@users.sourceforge.net>
10
 *   @version			  : 1.4
11
 *   @since				: 19.07.2007
12
 *
13
 */
14
15
// Published under GNU GPL 2 or later. See /install/README file for details.
16
// $Id$
17
18
$title = 'Strażnica';
19
/**
20
 * If it's first click on outpost link or outpost just has been bought: prepare AJAX-powered page backbone (tabs).
21
 * Else get only the really required information (without Navigation menu etc).
22
 */
23
require_once(empty($_GET) || isset($_GET['action'])? 'includes/head.php' : 'includes/minihead.php');
24
require_once('languages/'.$player -> lang.'/outposts.php');
25
26
/**
27
 * Check basic errors.
28
 */
29
if($player -> location != 'Altara' && $player -> location != 'Ardulith')
30
	error (NOT_IN_CITY);
31
if ($player -> hp <= 0 && !isset($_GET['listing']))
32
	error(YOU_DEAD.' (<a href="city.php">'.BACK.'</a>)');
33
if ($player -> race == '' || $player -> clas == '')
34
	error(YOU_UNFINISHED.' (<a href="city.php">'.BACK.'</a>)');
35
36
$db -> SetFetchMode(ADODB_FETCH_NUM);
37
require_once('class/outpost_class.php');
38
require_once('includes/security.php');
39
40
/**
41
* Get information about outposts from database
42
*/
43
$out = & new Outpost($player -> id, $db);
44
/**
45
* If player doesn't have a outpost - give him ability to buy one.
46
*/
47
if (!$out -> id && !isset ($_GET['action']))
48
	error(NO_OUTPOST);
49
if (isset ($_GET['action']) && $_GET['action'] == 'buy')
50
{
51
	if ($out -> id)
52
		error (YOU_BUY);
53
	if ($player -> credits < 500)
54
		error(NO_MONEY);
55
	buy($player -> id, $player -> clas, 500, $db);
56
}
57
58
/**
59
* Add gold to outpost or take it from outpost
60
*/
61
if (isset ($_GET['view']) && $_GET['view'] == 'gold')
62
{
63
   /**
64
	* Get gold from outpost
65
	*/
66
	if (isset($_POST['zeton']))
67
	{
68
		if (!uint32($_POST['zeton']))
69
			error(ERROR);
70
		$smarty -> assign ('Message', YOU_CHANGE.$_POST['zeton'].GOLD_ON.($out -> goldBalance(-$_POST['zeton'])).GOLD_ON2);
71
	}
72
	/**
73
	* Add gold to outpost
74
	*/
75
	if (isset($_POST['sztuki']))
76
	{
77
		if (!uint32($_POST['sztuki']))
78
			error(ERROR);
79
		$out -> goldBalance($_POST['sztuki'], $player -> credits);
80
		$smarty -> assign ('Message', YOU_ADD.' '.$_POST['sztuki'].' '.TO_OUT);
81
	}
82
	$smarty -> assign(array('Treasury' => $out -> gold,
83
							'GoldInHand' => $player -> credits));
84
}
85
86
/**
87
* Informations about outpost and gaining leadership ability
88
*/
89
if (isset ($_GET['view']) && $_GET['view'] == 'my')
90
{
91
	/**
92
	* Gain leadership bonus
93
	*/
94
	$arrSQLNames = array('battack', 'bdefense', 'btax', 'blost', 'bcost');
95
	if (isset($_GET['type']) && in_array($_GET['type'], $arrSQLNames) && isset($_GET['bonus']) && strictInt($_GET['bonus']) && $_GET['bonus'] > 0 && $out -> bonusAttack + $out -> bonusDefense + $out -> bonusTax + $out -> bonusLost + $out -> bonusGold + $_GET['bonus'] - 5 <= $player -> leadership)
96
	{
97
		if ($_GET['type'] == 'bcost' && $out -> bonusGold > 49 || $_GET['type'] == 'blost' && $out -> bonusLost > 49)
98
			error(MAX_LEVEL);
99
		$db -> Execute('UPDATE `outposts` SET `'.$_GET['type'].'`=`'.$_GET['type'].'`+'.$_GET['bonus'].' WHERE `id`='.$out -> id);
100
		$arrNames = array('bonusAttack', 'bonusDefense', 'bonusTax', 'bonusLost', 'bonusGold');
101
		$out -> $arrNames[array_search($_GET['type'], $arrSQLNames)] += $_GET['bonus'];
102
	}
103
	if ($out -> morale > 49)
104
		$strMorale = MORALE1;
105
	elseif ($out -> morale > -49)
106
		$strMorale = MORALE2;
107
	else
108
		$strMorale = MORALE3;
109
	$arrInfo = & $out -> info();
110
	$smarty -> assign ( array('User' => $player -> user,
111
							  'Size' => $out -> size,
112
							  'Turns' => $out -> turns,
113
							  'Gold' => $out -> gold,
114
							  'Warriors' => $out -> warriors,
115
							  'Barricades' => $out -> barricades,
116
							  'Archers' => $out -> archers,
117
							  'Catapults' => $out -> catapults,
118
							  'Maxtroops' => $arrInfo[0],
119
							  'Maxequip' => $arrInfo[1],
120
							  'Cost' => $arrInfo[7],
121
							  'Attack' => $out -> bonusAttack,
122
							  'Defense' => $out -> bonusDefense,
123
							  'Tax' => $out -> bonusTax,
124
							  'Lost' => $out -> bonusLost,
125
							  'Bcost' => $out -> bonusGold,
126
							  'Fence' => $out -> lairs,
127
							  'Maxfence' => $arrInfo[2],
128
							  'Monsters' => $arrInfo[4],
129
							  'Barracks' => $out -> barracks,
130
							  'Maxbarracks' => $arrInfo[3],
131
							  'Veterans' => $arrInfo[5],
132
							  'Maxveterans' => $out -> barracks - count($arrInfo[5]),
133
							  'Fatigue' => $arrInfo[6],
134
							  'Morale' => $out -> morale,
135
							  'Moralename' => $strMorale,
136
							  'Leadership' => $player -> leadership));
137
}
138
139
/**
140
* Get tax from villages
141
*/
142
if (isset ($_GET['view']) && $_GET['view'] == 'taxes')
143
{
144
	if ($out -> turns < 1)
145
		error(NO_EN_AP);
146
	if (!($out -> warriors + $out -> archers))
147
		error(NO_SOLDIERS);
148
	if (isset($_POST['amount']))
149
	{
150
		if (uint32($_POST['amount']) < 1)
151
		   error (ERROR);
152
		$smarty -> assign ('Message', YOU_ARMY.$_POST['amount'].TIMES_FOR.($out -> getTaxes($_POST['amount'])).GOLD_COINS);
153
	}
154
	$smarty -> assign ('AttackPoints', $out -> turns);
155
}
156
157
/**
158
* Outposts list.
159
* Result array is unified no mather the search method (level, owner's name, ID or tribe) and contains in each row:
160
* size, number of attacks, owner ID, owner name.
161
*/
162
if (isset ($_GET['view']) && $_GET['view'] == 'listing')
163
{
164
	$arrMax = $db -> GetRow('SELECT MAX(size) from `outposts`');
165
	$smarty -> assign('MaxLevel', $arrMax[0]);
166
	if (isset($_POST['slevel']) && isset($_POST['elevel']))
167
	{
168
		if (!strictInt($_POST['slevel']) || !strictInt($_POST['elevel']) || ($_POST['slevel'] > $_POST['elevel']))
169
			error (ERROR);
170
		$arrOutpost = $db -> GetAll('SELECT `size`, `attacks`, `owner` FROM `outposts` WHERE `size`>='.$_POST['slevel'].' AND `size`<='.$_POST['elevel'].' AND `id`!='.$out -> id.' ORDER BY `size` DESC');
171
		for ($i = 0, $intMax = count($arrOutpost); $i < $intMax; $i++)
172
		{
173
			$arrTemp = $db -> GetRow('SELECT `user` FROM `players` WHERE `id`='.$arrOutpost[$i][2]);
174
			$arrOutpost[$i][3] = $arrTemp[0];
175
		}
176
	}
177
	elseif (isset($_POST['id']) && strictInt($_POST['id']) && $_POST['id'] != 0 && $_POST['id'] != $player -> id)
178
	{
179
		$arrOutpost =  $db -> GetAll('SELECT `size`, `attacks` FROM `outposts` WHERE `owner`='.$_POST['id'].' ORDER BY `size` DESC');
180
		if (!empty($arrOutpost[0]))
181
		{
182
			$arrOutpost[0][2] = $_POST['id'];
183
			$arrTemp = $db -> GetRow('SELECT `user` FROM `players` WHERE `id`='.$_POST['id']);
184
			$arrOutpost[0][3] = $arrTemp[0];
185
		}
186
		else
187
			unset($arrOutpost);
188
	}
189
	elseif (isset($_POST['searched']))
190
	{
191
		sqlLikeString($_POST['searched']);
192
		$arrTest = $db -> GetAll('SELECT `id`, `user` FROM `players` WHERE `user` LIKE '.$_POST['searched']);
193
		for ($i = 0, $j = 0, $intMax = count($arrTest), $arrOutpost = array(); $i < $intMax; $i++)
194
		{
195
			if ($arrTest[$i][0] != $player -> id)
196
			{
197
				$arrTemp = $db -> GetRow('SELECT `size`, `attacks` FROM `outposts` WHERE `owner`='.$arrTest[$i][0]);
198
				if (isset($arrTemp[0]))
199
				{
200
					$arrOutpost[$j][0] = $arrTemp[0];
201
					$arrOutpost[$j][1] = $arrTemp[1];
202
					$arrOutpost[$j][2] = $arrTest[$i][0];
203
					$arrOutpost[$j++][3] = $arrTest[$i][1];
204
				}
205
			}
206
		}
207
	}
208
	elseif(isset($_POST['tribe']))
209
	{
210
		sqlLikeString($_POST['tribe']);
211
		$arrTest = $db -> GetAll('SELECT `id` FROM `tribes` WHERE `name` LIKE '.$_POST['tribe']);
212
		for ($i = 0, $j=0, $intMax1 = count($arrTest), $arrOutpost = array(); $i < $intMax1; $i++)
213
		{
214
			$arrTest2 = $db -> GetAll('SELECT `id`, `user` FROM `players` WHERE `tribe`='.$arrTest[$i][0]);
215
			for ($k=0, $intMax2 = count($arrTest2); $k < $intMax2; $k++)
216
			{
217
				if ($arrTest2[$k][0] != $player -> id)
218
				{
219
					$arrTemp = $db -> GetRow('SELECT `size`, `attacks` FROM `outposts` WHERE `owner`='.$arrTest2[$k][0]);
220
					if (isset($arrTemp[0]))
221
					{
222
						$arrOutpost[$j][0] = $arrTemp[0];
223
						$arrOutpost[$j][1] = $arrTemp[1];
224
						$arrOutpost[$j][2] = $arrTest2[$k][0];
225
						$arrOutpost[$j++][3] = $arrTest2[$k][1];
226
					}
227
				}
228
			}
229
		}
230
	}
231
	if(isset($arrOutpost))
232
	{
233
		$smarty -> assign_by_ref('Outposts', $arrOutpost);
234
		$smarty -> assign('MinSize', round($out -> size / 2) - 1);
235
	}
236
}
237
238
/**
239
* Battle of outposts
240
*/
241
if (isset ($_GET['view']) && $_GET['view'] == 'battle')
242
{
243
	if (isset($_POST['oid']) && strictInt($_POST['oid']) && isset($_POST['amount']) && strictInt($_POST['amount']))
244
	{
245
		if ($out -> fatigue == 20)
246
			error(TOO_FAT);
247
		if ($out -> turns < $_POST['amount'])
248
			error (NO_AP);
249
		if ($_POST['oid'] == $out -> ownerId)
250
			error (ITS_YOUR);
251
252
		$myOut = &$out; // rename variable
253
		$enemyOut = & new Outpost($_POST['oid'], $db);
254
		if (!$enemyOut -> id)
255
			error (NO_OUT);
256
		$arrEnemy = $db -> getRow('SELECT `user`, `leadership` FROM `players` WHERE `id`='.$enemyOut -> ownerId);
257
258
		if ($_POST['amount'] > 3 || $enemyOut -> attacks > 2)
259
			error(TOO_MUCH_A);
260
261
		/**
262
		* Make few attacks
263
		*/
264
		$arrAttackerInfo = array(YOU_ATTACKED.' <a href="view.php?view='.$_POST['oid'].'">'.$arrEnemy[0].'</a> '.$_POST['amount'].' '.TIMES);
265
		$arrDefenderInfo = array(YOU_WERE_ATTACKED_BY.' <a href="view.php?view='.$player -> id.'">'.$player -> user.'</a> '.$_POST['amount'].' '.TIMES);
266
		for ($k = 0; $k < $_POST['amount']; ++$k)
267
		{
268
			/**
269
			* Count attack and defense values of outposts and perform one attack.
270
			*/
271
			$myOut -> stats();
272
			$enemyOut -> stats();
273
			$myOut -> attack($enemyOut);
274
275
			$arrAttackerInfo[] = '<b>'.($k+1).'</b> '.STATS_WERE.': '.$myOut -> attack.' '.ATTACK.' '.$enemyOut -> defense.' '.DEFENSE.' '.RESULT.' '.(($myOut -> attack > $enemyOut -> defense) ? YOU_WON : YOU_LOST);
276
			$arrDefenderInfo[] = '<b>'.($k+1).'</b> '.STATS_WERE.': '.$myOut -> attack.' '.ATTACK.' '.$enemyOut -> defense.' '.DEFENSE.' '.RESULT.' '.(($myOut -> attack > $enemyOut -> defense) ? YOU_LOST : YOU_WON);
277
278
			$myOut -> lostspecials();
279
			$enemyOut -> lostspecials();
280
			$strMy = $myOut -> battlereport(false);
281
			$strEnemy = $enemyOut -> battlereport(true);
282
			$arrAttackerInfo[] = YOU_LOSE.$strMy;
283
			$arrAttackerInfo[] = HE_LOSSES.$strEnemy;
284
			$arrDefenderInfo[] = YOU_LOSE.$strEnemy;
285
			$arrDefenderInfo[] = HE_LOSSES.$strMy;
286
287
			if ($myOut -> fatigue == 20)
288
			{
289
				$arrAttackerInfo[] = YOUR_ARMY_IS_TOO_TIRED;
290
				break;
291
			}
292
			if ($enemyOut -> attacks > 2)
293
			{
294
				$arrAttackerInfo[] = YOU_CANNOT_ATTACK_HIM_MORE;
295
				break;
296
			}
297
		}
298
		$arrAttackerInfo[] = (($myOut -> intGaingold > 0) ? WON_GOLD : LOST_GOLD).': '.abs($myOut -> intGaingold).'. '.GAIN_LEADERSHIP.': '.round($myOut -> fltGainLeadership, 2);
299
		$arrDefenderInfo[] = (($enemyOut -> intGaingold < 0) ? LOST_GOLD : WON_GOLD).': '.abs($enemyOut -> intGaingold).'. '.GAIN_LEADERSHIP.': '.round($enemyOut -> fltGainLeadership, 2);
300
301
		$myOut -> writedata(true);
302
		$enemyOut -> writedata(true);
303
304
		$db -> Execute('INSERT INTO `log` (`owner`, `log`, `czas`) VALUES('.$_POST['oid'].','.$db -> qstr(implode($arrDefenderInfo, "")).', '.$db -> DBDate($newdate).')');
305
		$smarty -> assign_by_ref('AttackerInfo', $arrAttackerInfo);
306
	}
307
}
308
309
/**
310
* Buy army and new buildings to outpost
311
*/
312
if (isset ($_GET['view']) && $_GET['view'] == 'shop')
313
{
314
	$arrMinerals = $db -> GetRow('SELECT `pine`, `crystal`, `adamantium`, `meteor` FROM `minerals` WHERE `owner`='.$player -> id);
315
	if(empty($arrMinerals))
316
		$arrMinerals = array(0,0,0,0);
317
318
	// Increase outpost's level.
319
	if (isset($_POST['level']) && strictInt($_POST['level']) > 0)
320
		$smarty -> assign( 'Message', $out -> upgrade('size', $_POST['level'], $player -> platinum, $arrMinerals[0]));
321
	// Compute max available level. If done after upgrading - will get new value, so no need to refresh page.
322
	$intMaxLevel = $out -> checkresources ('size', $player -> platinum, $arrMinerals[0]);
323
324
	if (isset($_POST['lairs']) && strictInt($_POST['lairs']) > 0)
325
		$smarty -> assign( 'Message', $out -> upgrade('lairs', $_POST['lairs'], $arrMinerals[3], $arrMinerals[1]));
326
	$intMaxLair = $out -> checkresources ('lairs', $arrMinerals[3], $arrMinerals[1]);
327
328
	if (isset($_POST['barracks']) && strictInt($_POST['barracks']) > 0)
329
		$smarty -> assign( 'Message', $out -> upgrade('barracks', $_POST['barracks'], $arrMinerals[3], $arrMinerals[2]));
330
	$intMaxBarracks = $out -> checkresources ('barracks', $arrMinerals[3], $arrMinerals[2]);
331
332
	if (isset($_POST['buy']))
333
	{
334
		$arrNames = array('warriors', 'archers', 'barricades', 'catapults');
335
		for($i = 0, $arrInput = array(); $i < 4; ++$i)
336
			$arrInput[$i] = (isset($_POST[$arrNames[$i]]) && uint32($_POST[$arrNames[$i]])) ? $_POST[$arrNames[$i]] : 0;
337
		$smarty -> assign('Message', YOU_HAVE_SPENT.($out -> buyArmy($arrInput)).GOLD_COINS.ON.': '.$arrInput[0].' '.WARRIORS.', '.$arrInput[1].' '.ARCHERS.', '.$arrInput[2].' '.BARRICADES.', '.$arrInput[3].' '.CATAPULTS.'.');
338
	}
339
	$maxtroops = floor(min(($out -> size * 20) - $out -> warriors - $out -> archers, $out -> gold / 25));
340
	$maxequips = floor(min(($out -> size * 10) - $out -> catapults - $out -> barricades, $out -> gold / 35));
341
342
	$smarty -> assign (array('Size' => $out -> size,
343
							 'Gold' => $out -> gold,
344
							 'BaseLairs' => $out -> lairs,
345
							 'BaseBarracks' => $out -> barracks,
346
							 'Platinum' => $player -> platinum,
347
							 'Pine' => $arrMinerals[0],
348
							 'Crystal' => $arrMinerals[1],
349
							 'Adamantium' => $arrMinerals[2],
350
							 'Meteor' => $arrMinerals[3],
351
							 'MaxPossibleLevel' => $intMaxLevel ? $intMaxLevel : 0,
352
							 'MaxPossibleLair' => $intMaxLair ? $intMaxLair : 0,
353
							 'MaxPossibleBarrack' => $intMaxBarracks ? $intMaxBarracks : 0,
354
							 'Maxtroops' => $maxtroops,
355
							 'Maxequips' => $maxequips));
356
}
357
358
/**
359
* View details about outpost's monsters and hire new from Familiars' Glade (core.php).
360
*/
361
if (isset ($_GET['view']) && $_GET['view'] == 'beasts')
362
{
363
	$arrMonsters = $out -> getBeasts();
364
	$arrCores = $out -> getFamiliars($names); // $names - from language file.
365
	$intSum = count($arrMonsters);
366
	if (isset($_POST['id']))
367
		$smarty -> assign('Message', $out -> addBeast(strictInt($_POST['id']), $arrMonsters, $arrCores, $names));
368
	$smarty -> assign_by_ref('Monsters', $arrMonsters);
369
	$smarty -> assign_by_ref('Cores', $arrCores);
370
	$smarty -> assign(array('Lairs' => $out -> lairs,
371
							'Freelairs' => $out -> lairs - $intSum));
372
}
373
374
/**
375
* View details about outpost's veterans, hire new ones and equip them.
376
*/
377
if (isset ($_GET['view']) && $_GET['view'] == 'veterans')
378
{
379
/*  On first viewing player sees empty work area (<table> with images-dropzones), first tab of his equipment and possibly list of his veterans. Work area contains empty fields and zeroes in places for items ID's.
380
	To add new veteran player must click "send" button, possibly dropping first items on appropriate images.
381
	To edit a existing veteran, he must drag him on the work area. This generates a problem with item's which veteran already has. Their ID's are lost, so what should we use as a identifier? I decided to use:
382
	0 - to indicate that "this" item should be removed from veteran
383
	any non-zero value - to indicate ID of normal added item, from `equipment` database table
384
	2147483647 - max signed integer - to indicate "leave this part of veterans equipment as is"
385
	This way, whenever max int is changed, I know I must update accordingly, else it can be left alone.
386
*/
387
	$arrVeterans = $out -> getVeterans();   // id, name, power and defense of all veterans
388
	$arrVeteran = array(0, NEW_NAME, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '', 0);  // detailed info about one (real data when he is edited, else dummy data)
389
	$intSum = count($arrVeterans);
390
	if (isset($_POST['vid']))
391
	{
392
		if ($_POST['vid'] == 0)
393
/// Add new veteran.
394
			$smarty -> assign('Message', $out -> addVeteran($arrVeterans));
395
	   elseif (count($_POST) == 1)
396
/// Get info about selected veteran for edition.
397
		{
398
			$arrVeteran = $out -> getVeteran(strictInt($_POST['vid']));
399
			for ($i = 2; $i < 17; $i += 2)
400
				if ($arrVeteran[$i] != '')
401
				{
402
					$arrVeteran[$i] .= ' <b>+'.$arrVeteran[$i+1].'</b>';  // add "power" to item's name
403
					$arrVeteran[$i+1] = 2147483647;					 // and set "don't change" information
404
				}
405
		}
406
		else
407
/// Finalize edition of selected veteran.
408
			$smarty -> assign('Message', $out -> modifyVeteran($arrVeterans));
409
	}
410
	$smarty -> assign_by_ref('Veterans', $arrVeterans);
411
	$smarty -> assign_by_ref('VetDetails', $arrVeteran);
412
	$smarty -> assign(array('Barracks' => $out -> barracks,
413
							'Freebarracks' => $out -> barracks - $intSum));
414
}
415
416
/**
417
* Display player's equipment in tabs. Useful for veterans.
418
*/
419
if (isset ($_GET['view']) && $_GET['view'] == 'equip' && isset($_GET['type']) && in_array($_GET['type'], array('W', 'B', 'A', 'S', 'H', 'L', 'I')))
420
{
421
	$smarty -> assign_by_ref('Equipment', $out -> getEquipment($_GET['type']));
422
	$arrTypes = array('W' => 'sidearm',
423
					  'B' => 'bow',
424
					  'A' => 'armor',
425
					  'S' => 'shield',
426
					  'H' => 'helmet',
427
					  'L' => 'legs',
428
					  'I' => 'ring');
429
	$smarty -> assign('Type', $arrTypes[$_GET['type']]);
430
	unset($arrTypes);
431
}
432
/**
433
* Display page.
434
*/
435
$smarty -> display('outposts.tpl');
436
require_once(empty($_GET) || isset($_GET['action']) ? 'includes/foot.php' : 'includes/minifoot.php');
437
?>