PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/units/units_send.php

https://bitbucket.org/obsidian/selador
PHP | 658 lines | 544 code | 76 blank | 38 comment | 186 complexity | 74e6bdc731b49dfb4cdbbeecaade166a MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. $no_reload = 1;
  3. if ($me->activeoutpostid)
  4. $here = $me->activeoutpostid;
  5. else
  6. $here = $me->activevillageid;
  7. if (isset ($_REQUEST['x']))
  8. $x = $_REQUEST['x'];
  9. if (isset ($_REQUEST['y']))
  10. $y = $_REQUEST['y'];
  11. if (isset ($_REQUEST['u']) && isset ($_REQUEST['send']) && isset ($_REQUEST['stage']))
  12. {
  13. $u = $_REQUEST['u'];
  14. $stage = (int)$_REQUEST['stage'];
  15. $hero_health_irrelevant = false;
  16. // Check if enough units all in all have been selected
  17. if (array_sum ($u) > 0)
  18. {
  19. if (isset ($_REQUEST['c']))
  20. {
  21. $troopid = (int)$_REQUEST['c'];
  22. $query = "select A.troopid, A.uid, A.villageid, B.unitid, B.amount, C.speed, C.flags, D.villageid as fromvid, D.name as fromvill, D.uid as touser, D.x as fromx, D.y as fromy, E.villageid as tovid, E.name as tovill, E.uid as touser, E.x as tox, E.y as toy, F.user as ownername from armies A join troops B on (A.troopid=B.troopid) left join units C on (B.unitid=C.unitid) join village D on (A.location=D.villageid) join village E on (A.villageid=E.villageid) join user F on (A.uid=F.uid) where A.flag=0 and A.troopid=".$troopid." and (A.villageid=".$here." or A.location=".$here.")";
  23. $hero_health_irrelevant = true;
  24. if ($debug)
  25. echo $query;
  26. }
  27. else
  28. $query = "select A.troopid, B.unitid, B.amount, C.speed, C.flags from armies A join troops B on (A.troopid=B.troopid) left join units C on (C.speed>0 and B.unitid=C.unitid) where A.flag=0 and A.villageid=".$me->activevillageid." and A.location=".$me->activevillageid;
  29. if (!($res = mysql_query ($query)))
  30. log_err ("Troop sending query failed. Query: ".$query.", ".mysql_error ());
  31. while ($row = mysql_fetch_array ($res))
  32. {
  33. $home_troopid = $row['troopid'];
  34. $units_here[$row['unitid']] = $row['amount'];
  35. if ($row['speed'])
  36. $unit_speed[$row['unitid']] = (int)($row['speed'] * (1+$me->speed_bonus));
  37. else
  38. $unit_speed[$row['unitid']] = $me->hero->speed;
  39. if (isset ($troopid))
  40. $lastrow = $row; // Pass this information on for later
  41. }
  42. mysql_free_result ($res);
  43. $slowest_speed = 10000;
  44. $commander_there = false;
  45. $all_gone = true;
  46. $total_units = 0;
  47. $all_commanders_coming = false;
  48. $hero_cometh = false;
  49. //print_r($u);
  50. foreach ($u as $idx => $this_unit)
  51. {
  52. if ($this_unit > 0)
  53. {
  54. if ($idx == 1000)
  55. {
  56. if (($units_here[1000] >= 1) && ($this_unit == 1) && ((round ($me->hero->health) > 0) || $hero_health_irrelevant))
  57. {
  58. $commander_there = 1;
  59. $hero_cometh = true;
  60. }
  61. else
  62. {
  63. $send_err = "Der Held ist nicht in dieser Stadt. - Cheater ;)";
  64. }
  65. }
  66. if ((int)$this_unit > $units_here[$idx])
  67. {
  68. $send_err = "Nicht genügend Einheiten vorhanden!";
  69. }
  70. $total_units += (int)$this_unit;
  71. if (($idx == 4||$idx == 19) && ((int)$this_unit > 0))
  72. {
  73. $commander_there = 2;
  74. if ($units_here[$idx] == (int)$this_unit)
  75. $all_commanders_coming = true;
  76. }
  77. if ($unit_speed[$idx] < $slowest_speed)
  78. {
  79. if ($debug)
  80. echo "Replacing old speed ".$slowest_speed." with ".$unit_speed[$idx]." ";
  81. $slowest_speed = $unit_speed[$idx];
  82. }
  83. }
  84. }
  85. if (((int)$units_here[4] - $u[4] == 0) && ((int)$units_here[19] - $u[19] == 0)
  86. && ((int)$units_here[1000] - $u[1000] == 0))//...and no hero
  87. $all_commanders_coming = true;
  88. else
  89. $all_commanders_coming = false;
  90. if (isset ($troopid))
  91. {
  92. foreach ($units_here as $idx=>$amount)
  93. {
  94. if ($amount > (int)$u[$idx])
  95. {
  96. // if ($debug)
  97. // log_debug ("ID: ".$idx." here ".$amount.", to go ".(int)$u[$idx].", so not all gone");
  98. $all_gone = false;
  99. }
  100. }
  101. }
  102. $sendidx = $_REQUEST['sendtroops'];
  103. $cost_fac = 2;
  104. switch (key($sendidx))
  105. {
  106. case GROUPATTACK_PART:
  107. $type = GROUPATTACK_PART;
  108. $what = "Teilnahme an Gruppenangriff";
  109. $commander_needed = true;
  110. $setup_time = SETUP_GROUP_PART;
  111. $allflag = 1;
  112. $cost_fac = 2;
  113. break;
  114. case GROUPATTACK:
  115. $type = GROUPATTACK;
  116. $what = "Gruppenangriff";
  117. $commander_needed = true;
  118. $setup_time = SETUP_GROUP_LEAD;
  119. $allflag = 1;
  120. $cost_fac = 2;
  121. break;
  122. case RETURNING:
  123. $type = RETURNING;
  124. $what = "Heimschicken zu ";
  125. $commander_needed = true;
  126. $allflag = 0;
  127. $setup_time = SETUP_RETURN;
  128. $cost_fac = 0;
  129. break;
  130. case SIEGE:
  131. if (SIEGE_POSSIBLE)
  132. {
  133. $type = SIEGE;
  134. $what = "Erobern von ";
  135. $commander_needed = true;
  136. $setup_time = SETUP_SIEGE;
  137. $allflag = 1;
  138. }
  139. else
  140. $send_err = "Erobern ist in dieser Welt nicht m&ouml;glich.";
  141. break;
  142. case ATTACK:
  143. $type = ATTACK;
  144. $what = "Angriff auf ";
  145. $commander_needed = true;
  146. $setup_time = SETUP_ATTACK;
  147. $allflag = 1;
  148. break;
  149. case RAID:
  150. $type = RAID;
  151. $what = "Raubzug auf ";
  152. $commander_needed = true;
  153. $setup_time = SETUP_RAID;
  154. $allflag = 1;
  155. break;
  156. case SUPPORT:
  157. $type = SUPPORT;
  158. $what = "Unterst&uuml;tzung für ";
  159. $commander_needed = true;
  160. $setup_time = SETUP_SUPPORT;
  161. $cost_fac = 4;
  162. $allflag = 0;
  163. break;
  164. case SPY:
  165. $type = SPY;
  166. $what = "Aussp&auml;hen von ";
  167. $commander_needed = false;
  168. $setup_time = SETUP_SPY;
  169. $allflag = 1;
  170. if ((int)$u[$spy_byrace[$me->race]] == 0)
  171. $send_err = "Keine Sp&auml;her angewählt";
  172. break;
  173. case MOVE:
  174. $type = MOVE;
  175. $what = "Stationieren in ";
  176. $commander_needed = false;
  177. $setup_time = SETUP_MOVE;
  178. $cost_fac = 1;
  179. $allflag = 0;
  180. break;
  181. }
  182. if ($commander_needed)
  183. {
  184. if (!$commander_there)
  185. $send_err = "Ein Heerführer muss mitgehen!";
  186. }
  187. if (!isset ($send_err))
  188. {
  189. if (isset ($lastrow)) // Only true if we are talking about returning troops
  190. {
  191. $x = $lastrow['tox'];
  192. $y = $lastrow['toy'];
  193. $dist = sqrt (abs((($lastrow['fromx'] - $lastrow['tox']) * ($lastrow['fromx'] - $lastrow['tox'])) + (($lastrow['fromy'] - $lastrow['toy']) * ($lastrow['fromy'] - $lastrow['toy']))));
  194. // $dist = sqrt ( (($x - $me->activevillage->x) * ($x - $me->activevillage->x)) + (($y - $me->activevillage->y) * ($y - $me->activevillage->y)));
  195. $targetuid = $lastrow['touid'];
  196. $fromvill = $lastrow['tovid'];
  197. $targetvill = $lastrow['fromvid'];
  198. $targetvname = $lastrow['tovill'];
  199. $targetuid = $lastrow['touser'];
  200. $fromvname = $lastrow['fromvill'];
  201. $fromuid = $lastrow['uid'];
  202. $ownerusername = $lastrow['ownername'];
  203. $dur = (int)(($dist/$slowest_speed)*3600 / WORLD_UNIT_SPEED);
  204. if ($all_commanders_coming && !$all_gone)
  205. $send_err = 'Du kannst nicht alle Heerführer abziehen. <a href="javascript:history.back()">Probiere es hier noch einmal</a>';
  206. }
  207. else
  208. {
  209. if (isset ($x) && isset ($y))
  210. {
  211. if (!is_numeric ($x) && !is_numeric ($y))
  212. $send_err = "Koordinaten falsch!";
  213. else
  214. {
  215. // Alle Checks soweit erfolgreich, nun noch das Ziel untersuchen
  216. $query = "select A.name as vname, A.uid, A.outpost_of, A.villageid, B.user, B.aid, round(B.score/1000) as score, B.inactivity from village A, user B where A.uid=B.uid and A.x=".$x." and A.y=".$y;
  217. $res = mysql_query ($query);
  218. if ($row = mysql_fetch_array ($res))
  219. {
  220. // Ok, the village exists
  221. $targetuid = $row['uid'];
  222. $targetvill = $row['villageid'];
  223. $targetvname = $row['vname'];
  224. $targetuser = $row['user'];
  225. $targetuid = $row['uid'];
  226. if ($me->sat_by && $targetuid != $me->uid)
  227. {
  228. $send_err = "Als Sitter darfst du Einheiten nur innerhalb des Accounts verschicken. ";
  229. }
  230. else
  231. {
  232. $fromvill = $me->activevillageid;
  233. $fromuid = $me->uid;
  234. if (($type == SIEGE) && ($row['outpost_of'] == 0))
  235. $send_err = "Du kannst nur Aussenposten erobern!";
  236. else
  237. {
  238. if (($type == SIEGE) && ($me->activevillage->outpost_expansion == 0))
  239. $send_err = "Du musst erst dein Hauptgeb&auml;ude ausbauen, um einen Aussenposten erobern zu k&ouml;nnen!";
  240. else
  241. {
  242. // We don't allow attacks on our own villages!
  243. // FIXME This will be allowed later on to break sieges
  244. if ((($type == ATTACK) || ($type == RAID) || ($type == GROUPATTACK) || ($type == GROUPATTACK_PART) || ($type == SPY) || ($type == SIEGE)) && ($me->uid == $targetuid))
  245. {
  246. $send_err = "Du willst doch nicht etwa deine eigenen Leute angreifen??? Schande! ;)";
  247. }
  248. else
  249. {
  250. if (($type == MOVE) && (($me->uid != $targetuid) || ($row['outpost_of'])))
  251. $send_err = "Du kannst deine Truppen nur in deinen eigenen D&ouml;rfern (nicht Aussenposten) stationieren!";
  252. else
  253. {
  254. // Check that this guy isn't newbie protected
  255. if ((($type == ATTACK) || ($type == RAID) || ($type == GROUPATTACK) || ($type == GROUPATTACK_PART) || ($type == SPY) || ($type == SIEGE)) && ($row['score'] < round ($me->score/4)) && ($row['inactivity'] == 0) && (!in_array($row['aid'], $me->allywars)))
  256. {
  257. $send_err = "Dieser Spieler ist f&uuml;r dich im Noob-Schutz und kann nicht angegriffen werden, bis er st&auml;rker ist.";
  258. }
  259. else
  260. {
  261. // Check that I'm not newbie protected
  262. if ((($type == ATTACK) || ($type == RAID) || ($type == GROUPATTACK) || ($type == GROUPATTACK_PART)|| ($type == SPY) || ($type == SIEGE)) && ($row['score'] > $me->score*4) && (!$row['inactivity']) && (!in_array($row['aid'], $me->allywars)))
  263. {
  264. $send_err = "Du bist f&uuml;r diesen Spieler im Noob-Schutz und kannst ihn erst angreifen, wenn du st&auml;rker bist.";
  265. }
  266. else
  267. {
  268. mysql_free_result ($res);
  269. if ($type == MOVE)
  270. {
  271. // Now calculate food and population of destination village if this is a move action
  272. // First we run its event handler, so that we get a feeling for the number of units
  273. // in the recruit queue
  274. $me->villages[$targetvill]->update_to_latest ();
  275. $dummygui = new Gui;
  276. $eh = new EventHandler ($me, $dummygui, $user_row, $gamedb, $targetvill);
  277. if (!$eh->run())
  278. $me->log_debug ("Moving units: Restarting because of transaction fail!");
  279. $me->villages[$targetvill]->do_income (); // Update the income to this point in time
  280. $me->villages[$targetvill]->update_stamp_nounlock (); // And save it
  281. $me->save_score ();
  282. // Now sum up the population there
  283. $targetpop = $me->villages[$targetvill]->sum_population ($dummygui);
  284. if ($targetpop + $total_units > $me->villages[$targetvill]->ress_prod[5])
  285. $send_err = "Die Zielstadt kann nicht mehr als ".($me->villages[$targetvill]->ress_prod[5] - $targetpop)." Einheiten ern&auml;hren!";
  286. }
  287. $dist = sqrt ( (($x - $me->activevillage->x) * ($x - $me->activevillage->x)) + (($y - $me->activevillage->y) * ($y - $me->activevillage->y)));
  288. $dur = (int)(($dist/$slowest_speed)*3600 / WORLD_UNIT_SPEED);
  289. // Add setup time
  290. $dur += $setup_time;
  291. // For participation in group attacks we need some additional checks
  292. if ($type == GROUPATTACK_PART)
  293. {
  294. // First one: Are all needed variables set?
  295. if (isset ($_REQUEST['grattid']))
  296. {
  297. $groupattack_id = (int)$_REQUEST['grattid'];
  298. // Then we have to check if this group attack exists
  299. $query = "select A.grattid,A.targetvillage,time_to_sec(timediff(A.eta,now())) as dif,A.eta from groupatts A join groupatt_invites B on (A.grattid=".$groupattack_id." and B.grattid=".$groupattack_id." and B.uid=".$me->uid.")";
  300. if (!($res = mysql_query ($query)))
  301. {
  302. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten.";
  303. log_err ("Cannot read group attack info when sending troops. Query: ".$query.", ".mysql_error ());
  304. }
  305. else
  306. {
  307. if ($grouprow = mysql_fetch_array ($res))
  308. {
  309. // So it exists. Is there enough time to join yet?
  310. if ($grouprow['dif'] < $dur)
  311. $send_err = "Deine Truppen sind nicht schnell genug, um an diesem Gruppenangriff teilzunehmen.";
  312. else
  313. {
  314. // Yes, there is enough time. We set the duration to the time of the final event
  315. $dur = $grouprow['dif'];
  316. }
  317. }
  318. else
  319. $send_err = "Diese Einladung zum Gruppenangriff existiert nicht oder nicht mehr.";
  320. }
  321. }
  322. else
  323. $send_err = "Es ist ein interner Fehler aufgetreten.";
  324. }
  325. // Calculate how much this action will cost the player
  326. $gold_cost = ceil ($total_units / 50 * $cost_fac * ($dur / 3600) * WORLD_UNIT_SPEED);
  327. $gold_cost *= $me->hero->troop_send_costs;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. else
  337. $send_err = "Dort ist keine Stadt!";
  338. }
  339. }
  340. else
  341. {
  342. if (isset ($_REQUEST['dname']))
  343. {
  344. $send_err = "Stadtnamen-Suche noch nicht implementiert!";
  345. }
  346. else
  347. $send_err = "Bitte Ziel eingeben!";
  348. }
  349. }
  350. if (!isset ($send_err))
  351. {
  352. if ($stage == 1)
  353. {
  354. // Display all info again
  355. $info_str = "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"><table class=\"border\">"; //<tr id=\"rundrum\"><th colspan=\"4\">";
  356. // $info_str .= $what.$targetvname.", Spieler ".$targetuser."</th></tr>";
  357. $info_str .= "<tr><th>&nbsp;</th><th id=rundrum>Einheit</th><th id=rundrum>Geschwindigkeit</th><th id=rundrum>Anzahl</th></tr>\n";
  358. if ($type == SPY)
  359. {
  360. $id = $spy_byrace[$me->race];
  361. $info_str .= "<tr id=\"rundrum\"><td id=rundrum><a href=\"hilfe.php?up=".$id."\" title=\"Hilfe\"><img src=\"gfx/u/".$id.".gif\"></a></td><td id=rundrum>".$unitmap[$id]."</td>";
  362. $info_str .= "<td id=rundrum>".(int) ($unit_speed[$id] * WORLD_UNIT_SPEED)."</td><td id=rundrum>".$u[$id]."<input type=\"hidden\" name=\"u[".$id."]\" value=\"".$u[$id]."\"></td></tr>";
  363. }
  364. else
  365. {
  366. $capacity = 0;
  367. foreach ($u as $id=>$amount)
  368. {
  369. // if ($type == SUPPORT || $type == RETURNING || $type == MOVE || $id != $spy_byrace[$me->race])
  370. {
  371. if ((int)$amount > 0)
  372. {
  373. if ($id == 1000)
  374. {
  375. if (isset ($_REQUEST['c']))
  376. $info_str .= "<tr id=\"rundrum\"><td id=rundrum><img src=\"gfx/u/1000.gif\"></td><td id=rundrum>".$ownerusername."</td>";
  377. else
  378. $info_str .= "<tr id=\"rundrum\"><td id=rundrum><img src=\"gfx/u/1000.gif\"></td><td id=rundrum>".$me->user."</td>";
  379. $info_str .= "<td id=rundrum>".(int) ($unit_speed[$id] * WORLD_UNIT_SPEED)."</td><td id=rundrum>1<input type=\"hidden\" name=\"u[".$id."]\" value=\"".min (1, $u[$id])."\"></td></tr>";
  380. $hero_cometh = true; // Better set it twice eh :)
  381. $capacity += 50;
  382. }
  383. else
  384. {
  385. $info_str .= "<tr id=\"rundrum\"><td id=rundrum><a href=\"hilfe.php?up=".$id."\" title=\"Hilfe\"><img src=\"gfx/u/".$id.".gif\"></a></td><td id=rundrum>".$unitmap[$id]."</td>";
  386. $info_str .= "<td id=rundrum>".(int) ($unit_speed[$id] * WORLD_UNIT_SPEED)."<input type=\"hidden\" name=\"u[".$id."]\" value=\"".(int)$u[$id]."\"></td><td id=rundrum>".(int)$amount."</td></tr>";
  387. $capacity += $capacitymap[$id] * (int)$amount;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. if ($commander_needed)
  394. $info_str .= "<tr><td colspan=\"4\"><b>Gesamttragekapazit&auml;t: ".($capacity)."</td></tr>";
  395. $info_str .= "</table><br>";
  396. if ($gold_cost > $me->activevillage->ress[4])
  397. {
  398. $info_str .= '<p>Diese Aktion w&uuml;rde dich <span id="bug">'.(int)$gold_cost.'</span>&nbsp;<img src="styles/'.$_SESSION['theme'].'/gold.gif" alt="Gold" width="18" title="Gold"> kosten, so viel hast du nicht. Probiers doch mal mit weniger Truppen oder l&ouml;se den Bausparvertrag auf.</p>';
  399. $info_str .= "<input type=\"button\" value=\"Verdammt, zur&uuml;ck\" onclick=\"javascript:history.back()\">\n";
  400. }
  401. else
  402. {
  403. $time_arrival = date ("H:i:s", time () + $dur);
  404. $info_str .= '<span class="info">Es wird '.external_calcdur ($dur).' dauern, bis die Einheiten ankommen, das wäre um <span id="itimer1">'.$time_arrival.'</span>. Die Aktion wird dich '.(int)$gold_cost.'&nbsp;<img src="styles/'.$_SESSION['theme'].'/gold.gif" width="18" alt="Gold" title="Gold" height="16"> kosten.</span></p>';
  405. if (($type == GROUPATTACK) || ($type == GROUPATTACK_PART))
  406. $info_str .= "<p><b>Gruppenangriffe k&ouml;nnen nicht abgebrochen werden.</b></p>";
  407. $info_str .= "Sollen deine Truppen wirklich losgesendet werden?<br><br>";
  408. $info_str .= "<input type=\"hidden\" name=\"f\" value=\"3\">";
  409. if (isset ($lastrow))
  410. $info_str .= "<input type=\"hidden\" name=\"c\" value=\"".$troopid."\">";
  411. else
  412. $info_str .= "<input type=\"hidden\" name=\"x\" value=\"".$x."\"><input type=\"hidden\" name=\"y\" value=\"".$y."\">\n";
  413. if (isset ($groupattack_id))
  414. $info_str .= "<input type=\"hidden\" name=\"grattid\" value=\"".$groupattack_id."\">";
  415. $info_str .= "<input type=\"hidden\" name=\"stage\" value=\"2\"><input type=\"hidden\" name=\"sendtroops[".$type."]\" value=\"".$_REQUEST['sendtroops']."\">";
  416. $info_str .= "<input type=\"submit\" name=\"send\" value=\"Absenden!\">&nbsp;";
  417. $info_str .= "<input type=\"button\" value=\"Ne, Zur&uuml;ck\" onclick=\"javascript:history.back()\">\n";
  418. $info_str .= "</form>\n";
  419. }
  420. }
  421. if ($stage == 2)
  422. {
  423. if (!$me->activevillage->can_spend (0, 0, 0, $gold_cost))
  424. // if ($gold_cost > $me->activevillage->ress[4])
  425. $send_err = "Nicht gen&uuml;gend Gold vorhanden.";
  426. else
  427. {
  428. // log_debug ("all_gone ".$all_gone.", type ".$type.", troopid ".$troopid);
  429. if ($all_gone && $type == RETURNING && isset ($troopid))
  430. {
  431. $all_gone_done = true;
  432. $query = "update armies set flag=".$type.", location=".$targetvill." where troopid=".$troopid;
  433. if (!mysql_query ($query))
  434. {
  435. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  436. log_err ("Cannot update army when sending troops home. Query: ".$query.", error".mysql_error());
  437. }
  438. }
  439. else
  440. {
  441. // When moving troops, the rules are a bit different. To correctly calculate food in the
  442. // destination village, we swap location and villageid, i.e. the troops are already fed by
  443. // the destination village
  444. if ($type == MOVE)
  445. $query = "insert into armies (uid, villageid, flag, location) values (".$fromuid.", ".$targetvill.", ".$type.", ".$fromvill.")";
  446. else
  447. $query = "insert into armies (uid, villageid, flag, location) values (".$fromuid.", ".$fromvill.", ".$type.", ".$targetvill.")";
  448. if (!mysql_query ($query))
  449. {
  450. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  451. log_err ("Cannot insert new army when sending troops. Query: ".$query.", error".mysql_error());
  452. }
  453. else
  454. {
  455. $troopid = mysql_insert_id ();
  456. $query = "insert into troops values ";
  457. foreach ($u as $idx => $this_unit)
  458. {
  459. if ($this_unit > 0)
  460. {
  461. if (!isset ($query_suffix))
  462. $query_suffix = "(".$troopid.", ".(int)$idx.", ".(int)$this_unit.")";
  463. else
  464. $query_suffix .= ",(".$troopid.", ".(int)$idx.", ".(int)$this_unit.")";
  465. }
  466. }
  467. if (!mysql_query ($query.$query_suffix))
  468. {
  469. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  470. log_err ("Cannot insert new troops when sending troops. Query: ".$query.$query_suffix.", error".$mysql_error());
  471. }
  472. }
  473. }
  474. }
  475. if (!isset ($send_err))
  476. {
  477. if (isset ($hero_cometh) && ($hero_cometh == true))
  478. {
  479. if ($type == RETURNING)
  480. $me->hero->location = $fromvill;
  481. else
  482. $me->hero->location = $targetvill;
  483. $me->hero->troopid = $troopid;
  484. $me->hero->needs_updating = true;
  485. log_debug ("Setting hero location to ".$targetvill." and troopid to ".$troopid);
  486. }
  487. if ($type == GROUPATTACK)
  488. {
  489. // First acquire a group attack id
  490. $query = "insert into groupatts values (NULL, ".$targetvill.", addtime(now(), sec_to_time(".$dur.")))";
  491. if (!mysql_query ($query))
  492. {
  493. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  494. log_err ("Cannot insert groupatts entry. Query: ".$query.", ".mysql_error ());
  495. }
  496. else
  497. $groupattack_id = mysql_insert_id ();
  498. }
  499. if (!isset ($send_err) && (($type == GROUPATTACK) || ($type == GROUPATTACK_PART)))
  500. {
  501. // A group attack first needs an entry in groupattacks - surprise
  502. $query = "insert into groupatt_members (grattid, troopid,type,uid,from_village,home_duration) values (".$groupattack_id.", ".$troopid.", ".$type.", ".$fromuid.", ".$fromvill.", ".$dur.")";
  503. if (!mysql_query ($query))
  504. {
  505. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  506. log_err ("Cannot insert groupatts entry. Query: ".$query.", ".mysql_error ());
  507. }
  508. }
  509. else
  510. $groupattack_id = 0;
  511. if ($groupattack_id && ($type == GROUPATTACK_PART))
  512. {
  513. $query = "delete from groupatt_invites where uid=".$me->uid." and grattid=".$groupattack_id;
  514. if (!mysql_query ($query))
  515. log_err ("Cannot delete group attack invitation after sending troops. Query: ".$query.", ".mysql_error ());
  516. }
  517. if (!isset ($send_err))
  518. {
  519. // This is stupid, but we definitely want to avoid adding another row to the DB. So
  520. // we abuse the score (!) column for storing the grattid
  521. $query = "insert into event (uid1, village1, uid2, village2, timestamp, type, param, param2, flag, score) values (".$fromuid.", ".$fromvill.", ".$targetuid.", ".$targetvill.", addtime(now(), sec_to_time(".$dur.")), ".$type.", ".$troopid.", ".$dur.", ".$allflag.", ".$groupattack_id.")";
  522. if (!@mysql_query ($query))
  523. {
  524. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  525. log_err ("Cannot insert troop send event: ".$query.", error: ".mysql_error ());
  526. }
  527. else
  528. {
  529. $redirect_page="einheiten.php?f=2";
  530. $eid = mysql_insert_id ();
  531. $query = "insert into eventlog (eid, uid1, village1, uid2, village2, fired, happened, type, param, param2, state) values (".$eid.", ".$fromuid.", ".$fromvill.", ".$targetuid.", ".$targetvill.", NULL, addtime(now(), sec_to_time(".$dur.")), ".$type.", ".$troopid.", ".$dur.", 0)";
  532. if (!@mysql_query ($query))
  533. log_err ("Cannot log event ".$query.", ".mysql_error());
  534. }
  535. if (!isset ($send_err) && !isset ($all_gone_done))
  536. {
  537. // Now subtract these units from the troop at home
  538. unset ($query);
  539. foreach ($u as $unit_id => $amount)
  540. {
  541. if ($amount > 0)
  542. {
  543. if (!isset ($query))
  544. $query = "(".$home_troopid.", ".(int)$unit_id.", ".($units_here[$unit_id] - $amount).")";
  545. else
  546. $query .= ", (".$home_troopid.", ".(int)$unit_id.", ".($units_here[$unit_id] - $amount).")";
  547. }
  548. }
  549. $query = "replace into troops values ".$query;
  550. if (!(mysql_query ($query)))
  551. {
  552. unset ($redirect_page);
  553. $send_err = "Leider ist ein Datenbank-Fehler aufgetreten. :(";
  554. log_err ("Cannot subtract troops at home after sending troops. Query: ".$query.", ".mysql_error());
  555. }
  556. //$me->activevillage->ress[4] -= $gold_cost;
  557. $me->activevillage->spend (0, 0, 0, $gold_cost);
  558. // Adding a siege event is different than other attack types
  559. // It sets flags in the target village
  560. if ($type == SIEGE)
  561. {
  562. $query = "update village set out_expansion=out_expansion-1 where villageid=".$me->activevillageid;
  563. if (!mysql_query ($query))
  564. {
  565. log_err ("Cannot reduce outpost expansion slots when building. Query: ".$query.", ".mysql_error ());
  566. }
  567. $me->activevillage->outpost_expansion--;
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. else
  577. $send_err = "Es wurden nicht genügend Einheiten ausgewählt!";
  578. }
  579. if (isset ($redirect_page))
  580. echo '<script>window.location = "einheiten.php?f=2"; </script>';
  581. require_once ("einheiten-schicken.php");
  582. ?>