FlagWar.java 24 KB

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