PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/war.php

https://bitbucket.org/obsidian/selador
PHP | 455 lines | 381 code | 26 blank | 48 comment | 85 complexity | cb9e662ac26ddf3d73bf7548aeecf89c MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. /* war.php - Front-end code for the wars page
  3. *
  4. * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
  5. *
  6. * This file is part of Selador, a browser-based fantasy strategy game
  7. *
  8. * This program is distributed under the terms of the GNU Affero General Public License.
  9. *
  10. *
  11. * Selador is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * any later version.
  15. *
  16. * Selador is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with Selador. If not, see <http://www.gnu.org/licenses/>.
  23. **/
  24. $artefact = "F&auml;hnchen"; // werden wir sicherlich noch umbenennen
  25. require_once("php/base/user.inc.php");
  26. require_once("php/base.php");
  27. require_once("php/event.php");
  28. require_once("headers.php");
  29. /* $me->event_handler (true, $me->activevillageid, $gui);
  30. $me->activevillage->do_income (); */
  31. $me->activevillage->update_stamp ();
  32. require_once ("styles/php/base_design-".$_SESSION['theme'].".php");
  33. echo '<div id="contentbox">';
  34. require_once("blocked.php");
  35. $perms = $me->get_ally_perms ();
  36. if (!isset($_REQUEST['f']))
  37. $f = 0;
  38. else
  39. $f = (int)$_REQUEST['f'];
  40. function add_allies_msg ($aid1, $aid2)
  41. {
  42. $query = "update user set allymsgs=allymsgs+1 where aid in (".$aid1.", ".$aid2.")";
  43. log_debug ("Pimping ally msgs: ".$query);
  44. if (!mysql_query ($query))
  45. log_err ("Cannot update ally msg counter. Query: ".$query.", ".mysql_error ());
  46. else
  47. {
  48. if (!mysql_affected_rows ())
  49. log_err ("After updating ally msg counter no rows where affected. Query: ".$query);
  50. }
  51. }
  52. function save_ally_news ($aid1, $aid2, $type)
  53. {
  54. global $me;
  55. /* type-werte:
  56. * 20 : aid1 hat abgesagt
  57. * 21: aid1 hat aufgegeben
  58. * 22: aid1 hat die fähnchen geklaut
  59. * 23: zeit ist um
  60. jeweils gegenseite: + 4
  61. */
  62. $query = "insert into allyactions (aid1,aid2,uid1,uid2,stamp,type) values (".$aid1.", ".$aid2.", ".$me->uid.", NULL, now(), ".$type."), (".$aid2.", ".$aid1.", NULL, ".$me->uid.", now(), ".($type+4) .")";
  63. if (!mysql_query($query))
  64. {
  65. log_err("war.php: could not insert allynews when cancelling war: ".$query);
  66. }
  67. log_debug ("Cancel war: ". $query);
  68. }
  69. ?>
  70. <form action="war.php" method="POST" name="war">
  71. <div id="reiter">
  72. <table width="98%">
  73. <tr>
  74. <th><a href="war.php?f=3">Wer gegen wen</a></th>
  75. <th><a href="war.php?f=0">Aktuelle Kriege</a></th>
  76. <th><a href="war.php?f=1">Abgeschlossene Kriege</a></th>
  77. </tr>
  78. </table>
  79. </div><br><br>
  80. <div id="onecolumn">
  81. <?php
  82. if ($me->sat_by && $f != 3)
  83. echo "Als Sitter hast du nicht das Rechte, die Kriege der Allianz zu sehen. ";
  84. else
  85. {
  86. ?>
  87. <table width="98%">
  88. <tr>
  89. <?php if ($f == 0)
  90. echo '<h3>Aktuelle Kriege der eigenen Allianz</h3>
  91. ';
  92. else
  93. {
  94. if ($f == 3)
  95. echo '<h3>Wer gegen wen in '.WORLDTITLE.'</h3>';
  96. else
  97. echo '<h3>Abgeschlossene Kriege der eigenen Allianz</h3>
  98. ';
  99. }
  100. ?>
  101. </tr>
  102. <?php
  103. require_once("php/base/Flag.php");
  104. require_once("php/base/Krieg.php");
  105. require_once("php/war_functions.php");
  106. //erst mal alle buttons abfragen, die so gedrueckt worden sein koennten
  107. //zunaechst: moechte jemand einen krieg absagen?
  108. if (isset($_POST['cancel_war']) && ($perms & 16))
  109. {
  110. $tmp = explode ("_",$_POST['cancel_really']);
  111. $aid1 = (int)$tmp[0];
  112. $aid2 = (int)$tmp[1];
  113. if (count($tmp)== 2)
  114. {
  115. if ($perms & 16)
  116. {
  117. if ($aid1 == $me->aid)
  118. {
  119. $query = "update wars set over=30, causer=".$me->aid.", end_of_war=now() where aid1=".$aid1." and aid2=".$aid2;
  120. if (!mysql_query($query))
  121. {
  122. echo '<span id="bug">Problem beim Zur&uuml;ckziehen der Kriegserkl&auml;rung..!</span>';
  123. log_err("war.php: could not cancel war: ".$query);
  124. }
  125. else
  126. {
  127. // Add ally reload diplomacy event
  128. $query = "update user set changed=changed,flags=flags|4 where aid in (".$aid1.",".$aid2.")";
  129. if (!$gamedb->query ($query))
  130. log_err ("Cannot add ally reload diplomacy flags after war abort. Query: ".$query.", ".$gamedb->error ());
  131. save_ally_news($aid1, $aid2, 20);
  132. add_allies_msg ($aid1, $aid2);
  133. }
  134. }
  135. }
  136. }
  137. else
  138. echo '<span id="bug">Du musst das H&auml;kchen setzen, damit etwas passiert!</span>';
  139. }
  140. //moechte jemand ein faehnchen setzen?
  141. if (isset($_POST['place']) && ($perms & 16))
  142. {
  143. $to_delete_tmp = $_POST['setFlags'];
  144. $to_delete = explode("_",$to_delete_tmp);
  145. if (count($to_delete) == 2)
  146. {
  147. $aid1 = $to_delete[0];
  148. $aid2 = $to_delete[1];
  149. $player = $_POST['allyMembers'];
  150. foreach ($_POST['village'] as $village_key => $village)
  151. {
  152. if (strlen($village) > 0)
  153. {
  154. if (isset ($player[$village_key]))
  155. {
  156. $village = mysql_real_escape_string($village);
  157. $query = "select A.uid, A.villageid, A.outpost_of, A.x, A.y, A.name, B.aid,B.user from village as A, user as B where A.uid=".$player[$village_key]." and A.name='".$village."' AND A.uid=B.uid";
  158. if (!$res=mysql_query($query))
  159. echo "<span id=\"bug\">Fehler beim Auslesen der Dorfnamen...</span>";
  160. else
  161. {
  162. $i = 0;
  163. $all_villages = array();
  164. while ($row = mysql_fetch_array($res))
  165. {
  166. if (($row['aid']) != $me->aid)
  167. {
  168. echo "<span id=\"bug\">Dieser Spieler geh&ouml;rt nicht zu deiner Allianz!</span><br>";
  169. break;
  170. }
  171. $all_villages[] = $row;
  172. $i++;
  173. }
  174. if ($i == 0)
  175. echo "<span id=\"bug\">Kein Dorf mit diesem Namen zu diesem Spieler gefunden! </span><br>";
  176. if ($i > 1)
  177. {
  178. echo "<span id=\"bug\">Es wurden mehrere passende D&ouml;rfer gefunden, bitte w&auml;hle eines aus der Liste aus.</span><br>";
  179. echo "<table><tr><td>";
  180. foreach ($all_villages as $value=>$current_village)
  181. {
  182. if ($current_village['outpost_of'] == 0)
  183. echo "<input type=\"radio\" name=\"place_choice\" value=\"".$current_village['villageid']."_".$aid1."_".$aid2."\"> ".$current_village['user'] .": ".$current_village['name']." (".$current_village['x']." | ".$current_village['y'].")<br>";
  184. }
  185. echo "<input type=\"submit\" name=\"placeIt\" value=\"Mach es so\"></td></tr></table>";
  186. }
  187. if ($i == 1)
  188. {
  189. if ($all_villages[0]['outpost_of'] != 0)
  190. echo '<span id="bug">Du darfst in Au&szlig;enposten keine F&auml;hnchen platzieren, nur in St&auml;dten.</span>';
  191. else
  192. {
  193. //nun muss noch ueberprueft werden, ob in dieser Stadt schon ein fähnchen steht
  194. $query = "select A.villageid, B.over from war_flags as A, wars as B where A.aid1=B.aid1 AND A.aid2=B.aid2 AND A.villageid=".(int)$all_villages[0]['villageid']." AND B.over=0 AND A.aid1=".$aid1." AND A.aid2=".$aid2 ; //FIXME: insegsamt nur ein fähnchen pro dorf oder pro krieg? zur zeit:pro krieg
  195. if(!$res = mysql_query($query))
  196. echo '<span id="bug">Something strange happened here! </span>'.$query;
  197. else
  198. {
  199. while ($row = mysql_fetch_array($res))
  200. {
  201. $in_village = true;
  202. }
  203. if ($in_village == true)
  204. echo '<span id="bug">In diesem Dorf steht schon ein F&auml;hnchen</span>';
  205. else
  206. {
  207. $query = "insert into war_flags (aid1,aid2, villageid,current_villageid) values (".$aid1.",".$aid2.",".(int)$all_villages[0]['villageid'].",".(int)$all_villages[0]['villageid'].")";
  208. if (!mysql_query($query))
  209. echo "<span id=\"bug\">Platzieren fehlgeschlagen".$query."</span>";
  210. else
  211. echo "<span id=\"plus\">Erfolgreich platziert</span><br>";
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. else
  222. echo '<span id="bug">Du musst das H&auml;kchen setzen, damit etwas passiert!</span>';
  223. }
  224. //wurde eben aus einer auswahl an dörfern eines ausgewählt?
  225. if (isset($_POST['placeIt']) && ($perms & 16))
  226. {
  227. $tmp = explode("_", $_POST['place_choice']);
  228. if (count($tmp)==3)
  229. {
  230. $villageid=(int)$tmp[0];
  231. $aid1=(int)$tmp[1];
  232. $aid2 = (int)$tmp[2];
  233. $query = "select A.uid, A.outpost_of, B.aid from village as A, user as B where villageid=".$villageid. " and A.uid=B.uid ";
  234. if (!$res = mysql_query($query))
  235. echo "<span id=\"bug\">Fehler beim &Uuml;berpr&uuml;fen der Dorfdaten!</span>".$query."<br>";
  236. else
  237. {
  238. while ($row = mysql_fetch_array($res))
  239. {
  240. if (($row['aid']) != $me->aid)
  241. echo "<span id=\"bug\">Diese Stadt geh&ouml;rt nicht zu deiner Ally!</span>";
  242. else
  243. {
  244. $query = "select A.villageid, B.over from war_flags as A, wars as B where A.aid1=B.aid1 AND A.aid2=B.aid2 AND A.villageid=".$villageid." AND B.over=0 AND A.aid1=".$aid1." AND A.aid2=".$aid2 ; //FIXME: insegsamt nur ein fähnchen pro dorf oder pro krieg? Im Moment: pro Krieg
  245. if(!$res = mysql_query($query))
  246. echo '<span id="bug">Something strange happened here! </span>'.$query;
  247. else
  248. {
  249. while ($row = mysql_fetch_array($res))
  250. {
  251. $in_village = true;
  252. }
  253. if ($in_village == true)
  254. echo '<span id="bug">In diesem Dorf steht schon ein F&auml;hnchen</span>';
  255. else
  256. {
  257. if ($row['outpost_of'] != 0)
  258. echo '<span id="bug">Du darfst in Au&szlig;enposten keine F&auml;hnchen platzieren, nur in St&auml;dten.</span>';
  259. else
  260. {
  261. $query = "insert into war_flags (aid1, aid2, villageid,current_villageid) values (".$aid1.", ".$aid2.", ". $villageid.", ".$villageid.")";
  262. if (!mysql_query($query))
  263. echo "<span id=\"bug\">Versuch fehlgeschlagen!</span><br>";
  264. else
  265. echo "<span id=\"plus\">Erfolgreich platziert</span><br>";
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. else
  274. echo 'span id="bug">Du musst das H&auml;kchen setzen, damit etwas passiert!</span>';
  275. }
  276. //will eine der Parteien aufgeben?
  277. if (isset($_POST['assign']) && ($perms & 16))
  278. {
  279. $tmp = explode("_",$_POST['assign_what']);
  280. if (count ($tmp) == 2)
  281. {
  282. if ($me->aid == $tmp[0])
  283. $other_aid = (int)$tmp[1];
  284. else
  285. $other_aid = (int)$tmp[0];
  286. $query = "update wars set over=20, causer='".$me->aid."', end_of_war=now() where aid1=".(int)$tmp[0]." and aid2=".(int)$tmp[1];
  287. if (!mysql_query($query))
  288. {
  289. log_err("war.php: could not assing war: ".$query);
  290. echo "<span id=\"bug\">Aufgeben fehlgeschlagen!</span><br>";
  291. }
  292. else
  293. {
  294. $query = "select flag_score1, flag_score2 from wars where aid1=".(int)$tmp[0]." and aid2=".(int)$tmp[1];
  295. if (!$res = mysql_query($query))
  296. log_err("war.php. couldnt check worth of flags: ".$query);
  297. else
  298. {
  299. while ($row = mysql_fetch_array($res))
  300. {
  301. if ($other_aid == $tmp[0])
  302. // wenn die andere allianz aid1 ist, interessieren wir uns nur fuer flag score 2,
  303. //da die eigene allianz beim aufgeben keine punkte bekommt
  304. {
  305. $flag_score = $row['flag_score2'];
  306. $score_string = "score1";
  307. $not_score_string = "score2";
  308. }
  309. else
  310. //die andere allianz ist aid2. sie bekommt die flaggenpunkte gutgeschrieben.
  311. //die allianz, die aufgegeben hat, bekommt 0 Punkte - egal, wieviele Punkte sie schon gemacht hatte
  312. {
  313. $flag_score = $row['flag_score1'];
  314. $score_string = "score2";
  315. $not_score_string = "score1";
  316. }
  317. }
  318. $query = "update war_stats_tmp set ".$score_string."=".$score_string."+(".$flag_score."*3), ".$not_score_string."=0 where aid1=".(int)$tmp[0]." and aid2=".(int)$tmp[1];
  319. if (!mysql_query($query))
  320. log_err("war.php: Could not update war_stats: ". $query);
  321. // Add ally reload diplomacy event
  322. $query = "update user set changed=changed,flags=flags|4 where aid in (".$me->aid.",".$other_aid.")";
  323. if (!$gamedb->query ($query))
  324. log_err ("Cannot add ally reload diplomacy flags after diplomacy addition. Query: ".$query.", ".$gamedb->error ());
  325. calcStats($tmp[0],$tmp[1]);
  326. }
  327. save_ally_news($me->aid, $other_aid, 21);
  328. add_allies_msg ( $tmp[0], $tmp[1]);
  329. //abandonStats ($tmp[0], $tmp[1], $other_aid);
  330. }
  331. }
  332. else
  333. echo '<span id="bug">Du musst das H&auml;kchen setzen, damit etwas passiert!</span>';
  334. }
  335. if ($f != 3)
  336. {
  337. if ($f == 0)
  338. $constraint = "=";
  339. else
  340. $constraint = ">=";
  341. //zunächst werden einmal alle aktuelle kriege aus der datenbank geholt
  342. $query = "select A.*, date_format(A.begin, \"%d.%c.%Y - %H:%i\") as startdate, time_to_sec(timediff(now(), A.begin)) as remainingTime,B.tag as tag1, C.tag as tag2, D.score1,D.score2 from wars A left join allies B on (A.aid1=B.aid) left join allies C on (A.aid2=C.aid) join war_stats_tmp D on (A.aid1=D.aid1 AND A.aid2=D.aid2) where (A.aid1=".$me->aid. " or A.aid2=".$me->aid.") AND over".$constraint.$f;
  343. if (!$res=mysql_query($query))
  344. echo "<span id=\"bug\">Datenbankfehler beim Auslesen der aktuellen Kriege!</span>";
  345. else
  346. {
  347. while ($row = mysql_fetch_array($res))
  348. {
  349. $aid1 = $row['aid1'];
  350. $aid2 = $row['aid2'];
  351. $tag1 = $row['tag1'];
  352. $tag2 = $row['tag2'];
  353. $begin = $row['begin'];
  354. $remainingTime = $row['remainingTime'];
  355. $over = $row['over'];
  356. $causer = $row['causer'];
  357. $flag_score1 = $row['flag_score1'];
  358. $flag_score2 = $row['flag_score2'];
  359. $score1 = $row['score1'];
  360. $score2 = $row['score2'];
  361. //hat die eigene allianz den krieg angezettelt?
  362. if (($row['aid1']) == $me->aid)
  363. $beginner = true;
  364. else
  365. $beginner = false;
  366. $niceRemainingTime = $row['startdate'];
  367. $all_wars[] = new Krieg($aid1, $aid2, $beginner, $tag1, $tag2, $begin, $remainingTime,$over, $causer, $niceRemainingTime, $flag_score1, $flag_score2, $me->aid, $score1, $score2);
  368. }
  369. if (!isset($all_wars))
  370. {
  371. if ($f == 0)
  372. $output = "Deine Allianz befindet sich derzeit nicht im Krieg";
  373. else
  374. $output = "Deine Allianz hat in den letzten " . WAR_COOLDOWN . " Tagen keine Kriege beendet";
  375. echo "<span id=\"bug\">".$output."</span>";
  376. }
  377. else
  378. {
  379. //jetzt suchen wir uns zu jedem der Kriege die passenden Fähnchen raus
  380. //wir iterieren über alle Kriegobjekte
  381. $count = 1;
  382. foreach ($all_wars as $current_war=>$obj)
  383. {
  384. $obj->setLimit();
  385. $obj->getFlags($me->aid);
  386. $obj->setTitle();
  387. $obj->setLine();
  388. $obj->setBegin();
  389. $obj->setLine();
  390. $gui->displayedtimers++;
  391. $obj->setLimitInfo($perms, $me->aid, $gui->displayedtimers);
  392. $obj->setLine();
  393. $obj->setOwnFlagInfo($perms, $me->aid);
  394. $obj->setLine();
  395. $obj->setForeignFlagInfo($me->aid);
  396. $obj->setLine();
  397. $obj->setScoreForm();
  398. $obj->setLine();
  399. $obj->setAssignmentForm($perms,$me->aid);
  400. $obj->setLine();
  401. $obj->setWarState();
  402. echo "</table>";
  403. $count ++;
  404. }
  405. }
  406. }
  407. }
  408. else // alle Kriege dieser Welt anzeigen
  409. {
  410. $query = "select A.*, date_format(A.begin, \"%d.%c.%Y - %H:%i\") as startdate, time_to_sec(timediff(now(), A.begin)) as remainingTime,B.tag as tag1, C.tag as tag2, D.score1,D.score2 from wars A join allies B on (A.aid1=B.aid) join allies C on (A.aid2=C.aid) join war_stats_tmp D on (A.aid1=D.aid1 AND A.aid2=D.aid2) AND over = 0";
  411. if ($res = mysql_query($query))
  412. {
  413. while ($row = mysql_fetch_array($res))
  414. {
  415. $display = true;
  416. echo '<img src="gfx/axe2.gif" height="20px">';
  417. echo '<b><a href="allianz.php?a='.$row['aid1'].'">'.$row['tag1'].'</a> vs
  418. <a href="allianz.php?a='.$row['aid2'].'">'.$row['tag2'].'</a></b>
  419. (Zwischenstand: '.$row['score1'].' Punkte / '.$row['score2'].' Punkte)<br><br>';
  420. }
  421. }
  422. if (!$display)
  423. echo '<span id="bug">Zur Zeit finden in dieser Welt keine Kriege statt.</span>';
  424. }
  425. }
  426. echo '</table>';
  427. echo '</div></div>';
  428. require_once("footer.php");
  429. ?>