FlagWar.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (C) 2004-2013 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package conquerablehalls.flagwar;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.Announcements;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.ai.CtrlIntention;
  32. import com.l2jserver.gameserver.ai.L2SpecialSiegeGuardAI;
  33. import com.l2jserver.gameserver.cache.HtmCache;
  34. import com.l2jserver.gameserver.datatables.ClanTable;
  35. import com.l2jserver.gameserver.datatables.NpcTable;
  36. import com.l2jserver.gameserver.model.L2Clan;
  37. import com.l2jserver.gameserver.model.L2ClanMember;
  38. import com.l2jserver.gameserver.model.L2SiegeClan;
  39. import com.l2jserver.gameserver.model.L2SiegeClan.SiegeClanType;
  40. import com.l2jserver.gameserver.model.L2Spawn;
  41. import com.l2jserver.gameserver.model.L2World;
  42. import com.l2jserver.gameserver.model.Location;
  43. import com.l2jserver.gameserver.model.TeleportWhereType;
  44. import com.l2jserver.gameserver.model.actor.L2Npc;
  45. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  46. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  47. import com.l2jserver.gameserver.model.entity.Siegable;
  48. import com.l2jserver.gameserver.model.entity.clanhall.ClanHallSiegeEngine;
  49. import com.l2jserver.gameserver.model.entity.clanhall.SiegeStatus;
  50. import com.l2jserver.gameserver.model.zone.type.L2ResidenceHallTeleportZone;
  51. import com.l2jserver.gameserver.network.SystemMessageId;
  52. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  53. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  54. /**
  55. * @author BiggBoss
  56. */
  57. public abstract class FlagWar extends ClanHallSiegeEngine
  58. {
  59. protected static String qn;
  60. private static final String SQL_LOAD_ATTACKERS = "SELECT * FROM siegable_hall_flagwar_attackers WHERE hall_id = ?";
  61. private static final String SQL_SAVE_ATTACKER = "INSERT INTO siegable_hall_flagwar_attackers_members VALUES (?,?,?)";
  62. private static final String SQL_LOAD_MEMEBERS = "SELECT object_id FROM siegable_hall_flagwar_attackers_members WHERE clan_id = ?";
  63. private static final String SQL_SAVE_CLAN = "INSERT INTO siegable_hall_flagwar_attackers VALUES(?,?,?,?)";
  64. private static final String SQL_SAVE_NPC = "UPDATE siegable_hall_flagwar_attackers SET npc = ? WHERE clan_id = ?";
  65. private static final String SQL_CLEAR_CLAN = "DELETE FROM siegable_hall_flagwar_attackers WHERE hall_id = ?";
  66. private static final String SQL_CLEAR_CLAN_ATTACKERS = "DELETE FROM siegable_hall_flagwar_attackers_members WHERE hall_id = ?";
  67. protected static int ROYAL_FLAG;
  68. protected static int FLAG_RED;
  69. protected static int FLAG_YELLOW;
  70. protected static int FLAG_GREEN;
  71. protected static int FLAG_BLUE;
  72. protected static int FLAG_PURPLE;
  73. protected static int ALLY_1;
  74. protected static int ALLY_2;
  75. protected static int ALLY_3;
  76. protected static int ALLY_4;
  77. protected static int ALLY_5;
  78. protected static int TELEPORT_1;
  79. protected static int MESSENGER;
  80. protected static int[] OUTTER_DOORS_TO_OPEN = new int[2];
  81. protected static int[] INNER_DOORS_TO_OPEN = new int[2];
  82. protected static Location[] FLAG_COORDS = new Location[7];
  83. protected static L2ResidenceHallTeleportZone[] TELE_ZONES = new L2ResidenceHallTeleportZone[6];
  84. protected static int QUEST_REWARD;
  85. protected static Location CENTER;
  86. protected Map<Integer, ClanData> _data = new HashMap<>(6);
  87. protected L2Clan _winner;
  88. private boolean _firstPhase;
  89. public FlagWar(int questId, String name, String descr, int hallId)
  90. {
  91. super(questId, name, descr, hallId);
  92. addStartNpc(MESSENGER);
  93. addFirstTalkId(MESSENGER);
  94. addTalkId(MESSENGER);
  95. for (int i = 0; i < 6; i++)
  96. {
  97. addFirstTalkId(TELEPORT_1 + i);
  98. }
  99. addKillId(ALLY_1);
  100. addKillId(ALLY_2);
  101. addKillId(ALLY_3);
  102. addKillId(ALLY_4);
  103. addKillId(ALLY_5);
  104. addSpawnId(ALLY_1);
  105. addSpawnId(ALLY_2);
  106. addSpawnId(ALLY_3);
  107. addSpawnId(ALLY_4);
  108. addSpawnId(ALLY_5);
  109. // If siege ends w/ more than 1 flag alive, winner is old owner
  110. _winner = ClanTable.getInstance().getClan(_hall.getOwnerId());
  111. }
  112. @Override
  113. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  114. {
  115. String html = null;
  116. /*
  117. * if(player.getQuestState(qn) == null) newQuestState(player);
  118. */
  119. if (npc.getId() == MESSENGER)
  120. {
  121. if (!checkIsAttacker(player.getClan()))
  122. {
  123. L2Clan clan = ClanTable.getInstance().getClan(_hall.getOwnerId());
  124. String content = HtmCache.getInstance().getHtm(null, "data/scripts/conquerablehalls/flagwar/" + qn + "/messenger_initial.htm");
  125. content = content.replaceAll("%clanName%", clan == null ? "no owner" : clan.getName());
  126. content = content.replaceAll("%objectId%", String.valueOf(npc.getObjectId()));
  127. html = content;
  128. }
  129. else
  130. {
  131. html = "messenger_initial.htm";
  132. }
  133. }
  134. else
  135. {
  136. int index = npc.getId() - TELEPORT_1;
  137. if ((index == 0) && _firstPhase)
  138. {
  139. html = "teleporter_notyet.htm";
  140. }
  141. else
  142. {
  143. TELE_ZONES[index].checkTeleporTask();
  144. html = "teleporter.htm";
  145. }
  146. }
  147. return html;
  148. }
  149. @Override
  150. public synchronized String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  151. {
  152. String html = event;
  153. L2Clan clan = player.getClan();
  154. if (event.startsWith("register_clan")) // Register the clan for the siege
  155. {
  156. if (!_hall.isRegistering())
  157. {
  158. if (_hall.isInSiege())
  159. {
  160. html = "messenger_registrationpassed.htm";
  161. }
  162. else
  163. {
  164. sendRegistrationPageDate(player);
  165. return null;
  166. }
  167. }
  168. else if ((clan == null) || !player.isClanLeader())
  169. {
  170. html = "messenger_notclannotleader.htm";
  171. }
  172. else if (getAttackers().size() >= 5)
  173. {
  174. html = "messenger_attackersqueuefull.htm";
  175. }
  176. else if (checkIsAttacker(clan))
  177. {
  178. html = "messenger_clanalreadyregistered.htm";
  179. }
  180. else if (_hall.getOwnerId() == clan.getId())
  181. {
  182. html = "messenger_curownermessage.htm";
  183. }
  184. else
  185. {
  186. String[] arg = event.split(" ");
  187. if (arg.length >= 2)
  188. {
  189. // Register passing the quest
  190. if (arg[1].equals("wQuest"))
  191. {
  192. if (player.destroyItemByItemId(_hall.getName() + " Siege", QUEST_REWARD, 1, npc, false)) // Quest passed
  193. {
  194. registerClan(clan);
  195. html = getFlagHtml(_data.get(clan.getId()).flag);
  196. }
  197. else
  198. {
  199. html = "messenger_noquest.htm";
  200. }
  201. }
  202. // Register paying the fee
  203. else if (arg[1].equals("wFee") && canPayRegistration())
  204. {
  205. if (player.reduceAdena(qn + " Siege", 200000, npc, false)) // Fee payed
  206. {
  207. registerClan(clan);
  208. html = getFlagHtml(_data.get(clan.getId()).flag);
  209. }
  210. else
  211. {
  212. html = "messenger_nomoney.htm";
  213. }
  214. }
  215. }
  216. }
  217. }
  218. // Select the flag to defend
  219. else if (event.startsWith("select_clan_npc"))
  220. {
  221. if (!player.isClanLeader())
  222. {
  223. html = "messenger_onlyleaderselectally.htm";
  224. }
  225. else if (!_data.containsKey(clan.getId()))
  226. {
  227. html = "messenger_clannotregistered.htm";
  228. }
  229. else
  230. {
  231. String[] var = event.split(" ");
  232. if (var.length >= 2)
  233. {
  234. int id = 0;
  235. try
  236. {
  237. id = Integer.parseInt(var[1]);
  238. }
  239. catch (Exception e)
  240. {
  241. _log.warning(qn + "->select_clan_npc->Wrong mahum warrior id: " + var[1]);
  242. }
  243. if ((id > 0) && ((html = getAllyHtml(id)) != null))
  244. {
  245. _data.get(clan.getId()).npc = id;
  246. saveNpc(id, clan.getId());
  247. }
  248. }
  249. else
  250. {
  251. _log.warning(qn + " Siege: Not enough parameters to save clan npc for clan: " + clan.getName());
  252. }
  253. }
  254. }
  255. // View (and change ? ) the current selected mahum warrior
  256. else if (event.startsWith("view_clan_npc"))
  257. {
  258. ClanData cd = null;
  259. if (clan == null)
  260. {
  261. html = "messenger_clannotregistered.htm";
  262. }
  263. else if ((cd = _data.get(clan.getId())) == null)
  264. {
  265. html = "messenger_notclannotleader.htm";
  266. }
  267. else if (cd.npc == 0)
  268. {
  269. html = "messenger_leaderdidnotchooseyet.htm";
  270. }
  271. else
  272. {
  273. html = getAllyHtml(cd.npc);
  274. }
  275. }
  276. // Register a clan member for the fight
  277. else if (event.equals("register_member"))
  278. {
  279. if (clan == null)
  280. {
  281. html = "messenger_clannotregistered.htm";
  282. }
  283. else if (!_hall.isRegistering())
  284. {
  285. html = "messenger_registrationpassed.htm";
  286. }
  287. else if (!_data.containsKey(clan.getId()))
  288. {
  289. html = "messenger_notclannotleader.htm";
  290. }
  291. else if (_data.get(clan.getId()).players.size() >= 18)
  292. {
  293. html = "messenger_clanqueuefull.htm";
  294. }
  295. else
  296. {
  297. ClanData data = _data.get(clan.getId());
  298. data.players.add(player.getObjectId());
  299. saveMember(clan.getId(), player.getObjectId());
  300. if (data.npc == 0)
  301. {
  302. html = "messenger_leaderdidnotchooseyet.htm";
  303. }
  304. else
  305. {
  306. html = "messenger_clanregistered.htm";
  307. }
  308. }
  309. }
  310. // Show cur attacker list
  311. else if (event.equals("view_attacker_list"))
  312. {
  313. if (_hall.isRegistering())
  314. {
  315. sendRegistrationPageDate(player);
  316. }
  317. else
  318. {
  319. html = HtmCache.getInstance().getHtm(null, "data/scripts/conquerablehalls/flagwar/" + qn + "/messenger_registeredclans.htm");
  320. int i = 0;
  321. for (Entry<Integer, ClanData> clanData : _data.entrySet())
  322. {
  323. L2Clan attacker = ClanTable.getInstance().getClan(clanData.getKey());
  324. if (attacker == null)
  325. {
  326. continue;
  327. }
  328. html = html.replaceAll("%clan" + i + "%", clan.getName());
  329. html = html.replaceAll("%clanMem" + i + "%", String.valueOf(clanData.getValue().players.size()));
  330. i++;
  331. }
  332. if (_data.size() < 5)
  333. {
  334. for (int c = _data.size(); c < 5; c++)
  335. {
  336. html = html.replaceAll("%clan" + c + "%", "Empty pos. ");
  337. html = html.replaceAll("%clanMem" + c + "%", "Empty pos. ");
  338. }
  339. }
  340. }
  341. }
  342. return html;
  343. }
  344. @Override
  345. public synchronized String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  346. {
  347. if (_hall.isInSiege())
  348. {
  349. final int npcId = npc.getId();
  350. for (int keys : _data.keySet())
  351. {
  352. if (_data.get(keys).npc == npcId)
  353. {
  354. removeParticipant(keys, true);
  355. }
  356. }
  357. synchronized (this)
  358. {
  359. // TODO: Zoey76: previous bad implementation.
  360. // Converting map.keySet() to List and map.values() to List doesn't ensure that
  361. // first element in the key's List correspond to the first element in the values' List
  362. // That's the reason that values aren't copied to a List, instead using _data.get(clanIds.get(0))
  363. final List<Integer> clanIds = new ArrayList<>(_data.keySet());
  364. if (_firstPhase)
  365. {
  366. // Siege ends if just 1 flag is alive
  367. // Hall was free before battle or owner didn't set the ally npc
  368. if (((clanIds.size() == 1) && (_hall.getOwnerId() <= 0)) || (_data.get(clanIds.get(0)).npc == 0))
  369. {
  370. _missionAccomplished = true;
  371. // _winner = ClanTable.getInstance().getClan(_data.keySet()[0]);
  372. // removeParticipant(_data.keySet()[0], false);
  373. cancelSiegeTask();
  374. endSiege();
  375. }
  376. else if ((_data.size() == 2) && (_hall.getOwnerId() > 0)) // Hall has defender (owner)
  377. {
  378. cancelSiegeTask(); // No time limit now
  379. _firstPhase = false;
  380. _hall.getSiegeZone().setIsActive(false);
  381. for (int doorId : INNER_DOORS_TO_OPEN)
  382. {
  383. _hall.openCloseDoor(doorId, true);
  384. }
  385. for (ClanData data : _data.values())
  386. {
  387. doUnSpawns(data);
  388. }
  389. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  390. {
  391. @Override
  392. public void run()
  393. {
  394. for (int doorId : INNER_DOORS_TO_OPEN)
  395. {
  396. _hall.openCloseDoor(doorId, false);
  397. }
  398. for (Entry<Integer, ClanData> e : _data.entrySet())
  399. {
  400. doSpawns(e.getKey(), e.getValue());
  401. }
  402. _hall.getSiegeZone().setIsActive(true);
  403. }
  404. }, 300000);
  405. }
  406. }
  407. else
  408. {
  409. _missionAccomplished = true;
  410. _winner = ClanTable.getInstance().getClan(clanIds.get(0));
  411. removeParticipant(clanIds.get(0), false);
  412. endSiege();
  413. }
  414. }
  415. }
  416. return null;
  417. }
  418. @Override
  419. public String onSpawn(L2Npc npc)
  420. {
  421. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, CENTER);
  422. return null;
  423. }
  424. @Override
  425. public L2Clan getWinner()
  426. {
  427. return _winner;
  428. }
  429. @Override
  430. public void prepareOwner()
  431. {
  432. if (_hall.getOwnerId() > 0)
  433. {
  434. registerClan(ClanTable.getInstance().getClan(_hall.getOwnerId()));
  435. }
  436. _hall.banishForeigners();
  437. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.REGISTRATION_TERM_FOR_S1_ENDED);
  438. msg.addString(getName());
  439. Announcements.getInstance().announceToAll(msg);
  440. _hall.updateSiegeStatus(SiegeStatus.WAITING_BATTLE);
  441. _siegeTask = ThreadPoolManager.getInstance().scheduleGeneral(new SiegeStarts(), 3600000);
  442. }
  443. @Override
  444. public void startSiege()
  445. {
  446. if (getAttackers().size() < 2)
  447. {
  448. onSiegeEnds();
  449. getAttackers().clear();
  450. _hall.updateNextSiege();
  451. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.SIEGE_OF_S1_HAS_BEEN_CANCELED_DUE_TO_LACK_OF_INTEREST);
  452. sm.addString(_hall.getName());
  453. Announcements.getInstance().announceToAll(sm);
  454. return;
  455. }
  456. // Open doors for challengers
  457. for (int door : OUTTER_DOORS_TO_OPEN)
  458. {
  459. _hall.openCloseDoor(door, true);
  460. }
  461. // Teleport owner inside
  462. if (_hall.getOwnerId() > 0)
  463. {
  464. L2Clan owner = ClanTable.getInstance().getClan(_hall.getOwnerId());
  465. final Location loc = _hall.getZone().getSpawns().get(0); // Owner restart point
  466. for (L2ClanMember pc : owner.getMembers())
  467. {
  468. if (pc != null)
  469. {
  470. final L2PcInstance player = pc.getPlayerInstance();
  471. if ((player != null) && player.isOnline())
  472. {
  473. player.teleToLocation(loc, false);
  474. }
  475. }
  476. }
  477. }
  478. // Schedule open doors closement and siege start in 2 minutes
  479. ThreadPoolManager.getInstance().scheduleGeneral(new CloseOutterDoorsTask(FlagWar.super), 300000);
  480. }
  481. /**
  482. * Runnable class to schedule doors closing and siege start.
  483. * @author Zoey76
  484. */
  485. protected class CloseOutterDoorsTask implements Runnable
  486. {
  487. private final Siegable _siegable;
  488. protected CloseOutterDoorsTask(Siegable clanHallSiege)
  489. {
  490. _siegable = clanHallSiege;
  491. }
  492. @Override
  493. public void run()
  494. {
  495. for (int door : OUTTER_DOORS_TO_OPEN)
  496. {
  497. _hall.openCloseDoor(door, false);
  498. }
  499. _hall.getZone().banishNonSiegeParticipants();
  500. _siegable.startSiege();
  501. }
  502. }
  503. @Override
  504. public void onSiegeStarts()
  505. {
  506. for (Entry<Integer, ClanData> clan : _data.entrySet())
  507. {
  508. // Spawns challengers flags and npcs
  509. try
  510. {
  511. ClanData data = clan.getValue();
  512. doSpawns(clan.getKey(), data);
  513. fillPlayerList(data);
  514. }
  515. catch (Exception e)
  516. {
  517. endSiege();
  518. _log.warning(qn + ": Problems in siege initialization!");
  519. e.printStackTrace();
  520. }
  521. }
  522. }
  523. @Override
  524. public void endSiege()
  525. {
  526. if (_hall.getOwnerId() > 0)
  527. {
  528. L2Clan clan = ClanTable.getInstance().getClan(_hall.getOwnerId());
  529. clan.setHideoutId(0);
  530. _hall.free();
  531. }
  532. super.endSiege();
  533. }
  534. @Override
  535. public void onSiegeEnds()
  536. {
  537. if (_data.size() > 0)
  538. {
  539. for (int clanId : _data.keySet())
  540. {
  541. if (_hall.getOwnerId() == clanId)
  542. {
  543. removeParticipant(clanId, false);
  544. }
  545. else
  546. {
  547. removeParticipant(clanId, true);
  548. }
  549. }
  550. }
  551. clearTables();
  552. }
  553. @Override
  554. public final Location getInnerSpawnLoc(final L2PcInstance player)
  555. {
  556. Location loc = null;
  557. if (player.getClanId() == _hall.getOwnerId())
  558. {
  559. loc = _hall.getZone().getSpawns().get(0);
  560. }
  561. else
  562. {
  563. ClanData cd = _data.get(player.getClanId());
  564. if (cd != null)
  565. {
  566. int index = cd.flag - FLAG_RED;
  567. if ((index >= 0) && (index <= 4))
  568. {
  569. loc = _hall.getZone().getChallengerSpawns().get(index);
  570. }
  571. else
  572. {
  573. throw new ArrayIndexOutOfBoundsException();
  574. }
  575. }
  576. }
  577. return loc;
  578. }
  579. @Override
  580. public final boolean canPlantFlag()
  581. {
  582. return false;
  583. }
  584. @Override
  585. public final boolean doorIsAutoAttackable()
  586. {
  587. return false;
  588. }
  589. void doSpawns(int clanId, ClanData data)
  590. {
  591. try
  592. {
  593. L2NpcTemplate mahumTemplate = NpcTable.getInstance().getTemplate(data.npc);
  594. L2NpcTemplate flagTemplate = NpcTable.getInstance().getTemplate(data.flag);
  595. if (flagTemplate == null)
  596. {
  597. _log.warning(qn + ": Flag L2NpcTemplate[" + data.flag + "] does not exist!");
  598. throw new NullPointerException();
  599. }
  600. else if (mahumTemplate == null)
  601. {
  602. _log.warning(qn + ": Ally L2NpcTemplate[" + data.npc + "] does not exist!");
  603. throw new NullPointerException();
  604. }
  605. int index = 0;
  606. if (_firstPhase)
  607. {
  608. index = data.flag - FLAG_RED;
  609. }
  610. else
  611. {
  612. index = clanId == _hall.getOwnerId() ? 5 : 6;
  613. }
  614. Location loc = FLAG_COORDS[index];
  615. data.flagInstance = new L2Spawn(flagTemplate);
  616. data.flagInstance.setLocation(loc);
  617. data.flagInstance.setRespawnDelay(10000);
  618. data.flagInstance.setAmount(1);
  619. data.flagInstance.init();
  620. data.warrior = new L2Spawn(mahumTemplate);
  621. data.warrior.setLocation(loc);
  622. data.warrior.setRespawnDelay(10000);
  623. data.warrior.setAmount(1);
  624. data.warrior.init();
  625. ((L2SpecialSiegeGuardAI) data.warrior.getLastSpawn().getAI()).getAlly().addAll(data.players);
  626. }
  627. catch (Exception e)
  628. {
  629. _log.warning(qn + ": Couldnt make clan spawns: " + e.getMessage());
  630. e.printStackTrace();
  631. }
  632. }
  633. private void fillPlayerList(ClanData data)
  634. {
  635. for (int objId : data.players)
  636. {
  637. L2PcInstance plr = L2World.getInstance().getPlayer(objId);
  638. if (plr != null)
  639. {
  640. data.playersInstance.add(plr);
  641. }
  642. }
  643. }
  644. private void registerClan(L2Clan clan)
  645. {
  646. final int clanId = clan.getId();
  647. L2SiegeClan sc = new L2SiegeClan(clanId, SiegeClanType.ATTACKER);
  648. getAttackers().put(clanId, sc);
  649. ClanData data = new ClanData();
  650. data.flag = ROYAL_FLAG + _data.size();
  651. data.players.add(clan.getLeaderId());
  652. _data.put(clanId, data);
  653. saveClan(clanId, data.flag);
  654. saveMember(clanId, clan.getLeaderId());
  655. }
  656. private final void doUnSpawns(ClanData data)
  657. {
  658. if (data.flagInstance != null)
  659. {
  660. data.flagInstance.stopRespawn();
  661. data.flagInstance.getLastSpawn().deleteMe();
  662. }
  663. if (data.warrior != null)
  664. {
  665. data.warrior.stopRespawn();
  666. data.warrior.getLastSpawn().deleteMe();
  667. }
  668. }
  669. private final void removeParticipant(int clanId, boolean teleport)
  670. {
  671. ClanData dat = _data.remove(clanId);
  672. if (dat != null)
  673. {
  674. // Destroy clan flag
  675. if (dat.flagInstance != null)
  676. {
  677. dat.flagInstance.stopRespawn();
  678. if (dat.flagInstance.getLastSpawn() != null)
  679. {
  680. dat.flagInstance.getLastSpawn().deleteMe();
  681. }
  682. }
  683. if (dat.warrior != null)
  684. {
  685. // Destroy clan warrior
  686. dat.warrior.stopRespawn();
  687. if (dat.warrior.getLastSpawn() != null)
  688. {
  689. dat.warrior.getLastSpawn().deleteMe();
  690. }
  691. }
  692. dat.players.clear();
  693. if (teleport)
  694. {
  695. // Teleport players outside
  696. for (L2PcInstance pc : dat.playersInstance)
  697. {
  698. if (pc != null)
  699. {
  700. pc.teleToLocation(TeleportWhereType.TOWN);
  701. }
  702. }
  703. }
  704. dat.playersInstance.clear();
  705. }
  706. }
  707. public boolean canPayRegistration()
  708. {
  709. return true;
  710. }
  711. private void sendRegistrationPageDate(L2PcInstance player)
  712. {
  713. final NpcHtmlMessage msg = new NpcHtmlMessage();
  714. msg.setFile(null, "data/scripts/conquerablehalls/flagwar/" + qn + "/siege_date.htm");
  715. msg.replace("%nextSiege%", _hall.getSiegeDate().getTime().toString());
  716. player.sendPacket(msg);
  717. }
  718. public abstract String getFlagHtml(int flag);
  719. public abstract String getAllyHtml(int ally);
  720. @Override
  721. public final void loadAttackers()
  722. {
  723. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  724. {
  725. PreparedStatement statement = con.prepareStatement(SQL_LOAD_ATTACKERS);
  726. statement.setInt(1, _hall.getId());
  727. ResultSet rset = statement.executeQuery();
  728. while (rset.next())
  729. {
  730. final int clanId = rset.getInt("clan_id");
  731. if (ClanTable.getInstance().getClan(clanId) == null)
  732. {
  733. _log.warning(qn + ": Loaded an unexistent clan as attacker! Clan Id: " + clanId);
  734. continue;
  735. }
  736. ClanData data = new ClanData();
  737. data.flag = rset.getInt("flag");
  738. data.npc = rset.getInt("npc");
  739. _data.put(clanId, data);
  740. loadAttackerMembers(clanId);
  741. }
  742. rset.close();
  743. statement.close();
  744. }
  745. catch (Exception e)
  746. {
  747. _log.warning(qn + ".loadAttackers()->" + e.getMessage());
  748. e.printStackTrace();
  749. }
  750. }
  751. private final void loadAttackerMembers(int clanId)
  752. {
  753. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  754. {
  755. ArrayList<Integer> listInstance = _data.get(clanId).players;
  756. if (listInstance == null)
  757. {
  758. _log.warning(qn + ": Tried to load unregistered clan: " + clanId + "[clan Id]");
  759. return;
  760. }
  761. PreparedStatement statement = con.prepareStatement(SQL_LOAD_MEMEBERS);
  762. statement.setInt(1, clanId);
  763. ResultSet rset = statement.executeQuery();
  764. while (rset.next())
  765. {
  766. listInstance.add(rset.getInt("object_id"));
  767. }
  768. rset.close();
  769. statement.close();
  770. }
  771. catch (Exception e)
  772. {
  773. _log.warning(qn + ".loadAttackerMembers()->" + e.getMessage());
  774. e.printStackTrace();
  775. }
  776. }
  777. private final void saveClan(int clanId, int flag)
  778. {
  779. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  780. {
  781. PreparedStatement statement = con.prepareStatement(SQL_SAVE_CLAN);
  782. statement.setInt(1, _hall.getId());
  783. statement.setInt(2, flag);
  784. statement.setInt(3, 0);
  785. statement.setInt(4, clanId);
  786. statement.execute();
  787. statement.close();
  788. }
  789. catch (Exception e)
  790. {
  791. _log.warning(qn + ".saveClan()->" + e.getMessage());
  792. e.printStackTrace();
  793. }
  794. }
  795. private final void saveNpc(int npc, int clanId)
  796. {
  797. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  798. {
  799. PreparedStatement statement = con.prepareStatement(SQL_SAVE_NPC);
  800. statement.setInt(1, npc);
  801. statement.setInt(2, clanId);
  802. statement.execute();
  803. statement.close();
  804. }
  805. catch (Exception e)
  806. {
  807. _log.warning(qn + ".saveNpc()->" + e.getMessage());
  808. e.printStackTrace();
  809. }
  810. }
  811. private final void saveMember(int clanId, int objectId)
  812. {
  813. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  814. {
  815. PreparedStatement statement = con.prepareStatement(SQL_SAVE_ATTACKER);
  816. statement.setInt(1, _hall.getId());
  817. statement.setInt(2, clanId);
  818. statement.setInt(3, objectId);
  819. statement.execute();
  820. statement.close();
  821. }
  822. catch (Exception e)
  823. {
  824. _log.warning(qn + ".saveMember()->" + e.getMessage());
  825. e.printStackTrace();
  826. }
  827. }
  828. private void clearTables()
  829. {
  830. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  831. {
  832. PreparedStatement stat1 = con.prepareStatement(SQL_CLEAR_CLAN);
  833. stat1.setInt(1, _hall.getId());
  834. stat1.execute();
  835. stat1.close();
  836. PreparedStatement stat2 = con.prepareStatement(SQL_CLEAR_CLAN_ATTACKERS);
  837. stat2.setInt(1, _hall.getId());
  838. stat2.execute();
  839. stat2.close();
  840. }
  841. catch (Exception e)
  842. {
  843. _log.warning(qn + ".clearTables()->" + e.getMessage());
  844. }
  845. }
  846. class ClanData
  847. {
  848. int flag = 0;
  849. int npc = 0;
  850. ArrayList<Integer> players = new ArrayList<>(18);
  851. ArrayList<L2PcInstance> playersInstance = new ArrayList<>(18);
  852. L2Spawn warrior = null;
  853. L2Spawn flagInstance = null;
  854. }
  855. }