FlagWar.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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(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(() ->
  385. {
  386. for (int doorId : INNER_DOORS_TO_OPEN)
  387. {
  388. _hall.openCloseDoor(doorId, false);
  389. }
  390. for (Entry<Integer, ClanData> e : _data.entrySet())
  391. {
  392. doSpawns(e.getKey(), e.getValue());
  393. }
  394. _hall.getSiegeZone().setIsActive(true);
  395. }, 300000);
  396. }
  397. }
  398. else
  399. {
  400. _missionAccomplished = true;
  401. _winner = ClanTable.getInstance().getClan(clanIds.get(0));
  402. removeParticipant(clanIds.get(0), false);
  403. endSiege();
  404. }
  405. }
  406. }
  407. return null;
  408. }
  409. @Override
  410. public String onSpawn(L2Npc npc)
  411. {
  412. npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, CENTER);
  413. return null;
  414. }
  415. @Override
  416. public L2Clan getWinner()
  417. {
  418. return _winner;
  419. }
  420. @Override
  421. public void prepareOwner()
  422. {
  423. if (_hall.getOwnerId() > 0)
  424. {
  425. registerClan(ClanTable.getInstance().getClan(_hall.getOwnerId()));
  426. }
  427. _hall.banishForeigners();
  428. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.REGISTRATION_TERM_FOR_S1_ENDED);
  429. msg.addString(getName());
  430. Announcements.getInstance().announceToAll(msg);
  431. _hall.updateSiegeStatus(SiegeStatus.WAITING_BATTLE);
  432. _siegeTask = ThreadPoolManager.getInstance().scheduleGeneral(new SiegeStarts(), 3600000);
  433. }
  434. @Override
  435. public void startSiege()
  436. {
  437. if (getAttackers().size() < 2)
  438. {
  439. onSiegeEnds();
  440. getAttackers().clear();
  441. _hall.updateNextSiege();
  442. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.SIEGE_OF_S1_HAS_BEEN_CANCELED_DUE_TO_LACK_OF_INTEREST);
  443. sm.addString(_hall.getName());
  444. Announcements.getInstance().announceToAll(sm);
  445. return;
  446. }
  447. // Open doors for challengers
  448. for (int door : OUTTER_DOORS_TO_OPEN)
  449. {
  450. _hall.openCloseDoor(door, true);
  451. }
  452. // Teleport owner inside
  453. if (_hall.getOwnerId() > 0)
  454. {
  455. L2Clan owner = ClanTable.getInstance().getClan(_hall.getOwnerId());
  456. final Location loc = _hall.getZone().getSpawns().get(0); // Owner restart point
  457. for (L2ClanMember pc : owner.getMembers())
  458. {
  459. if (pc != null)
  460. {
  461. final L2PcInstance player = pc.getPlayerInstance();
  462. if ((player != null) && player.isOnline())
  463. {
  464. player.teleToLocation(loc, false);
  465. }
  466. }
  467. }
  468. }
  469. // Schedule open doors closement and siege start in 2 minutes
  470. ThreadPoolManager.getInstance().scheduleGeneral(new CloseOutterDoorsTask(FlagWar.super), 300000);
  471. }
  472. /**
  473. * Runnable class to schedule doors closing and siege start.
  474. * @author Zoey76
  475. */
  476. protected class CloseOutterDoorsTask implements Runnable
  477. {
  478. private final Siegable _siegable;
  479. protected CloseOutterDoorsTask(Siegable clanHallSiege)
  480. {
  481. _siegable = clanHallSiege;
  482. }
  483. @Override
  484. public void run()
  485. {
  486. for (int door : OUTTER_DOORS_TO_OPEN)
  487. {
  488. _hall.openCloseDoor(door, false);
  489. }
  490. _hall.getZone().banishNonSiegeParticipants();
  491. _siegable.startSiege();
  492. }
  493. }
  494. @Override
  495. public void onSiegeStarts()
  496. {
  497. for (Entry<Integer, ClanData> clan : _data.entrySet())
  498. {
  499. // Spawns challengers flags and npcs
  500. try
  501. {
  502. ClanData data = clan.getValue();
  503. doSpawns(clan.getKey(), data);
  504. fillPlayerList(data);
  505. }
  506. catch (Exception e)
  507. {
  508. endSiege();
  509. _log.warning(getName() + ": Problems in siege initialization!");
  510. e.printStackTrace();
  511. }
  512. }
  513. }
  514. @Override
  515. public void endSiege()
  516. {
  517. if (_hall.getOwnerId() > 0)
  518. {
  519. L2Clan clan = ClanTable.getInstance().getClan(_hall.getOwnerId());
  520. clan.setHideoutId(0);
  521. _hall.free();
  522. }
  523. super.endSiege();
  524. }
  525. @Override
  526. public void onSiegeEnds()
  527. {
  528. if (_data.size() > 0)
  529. {
  530. for (int clanId : _data.keySet())
  531. {
  532. if (_hall.getOwnerId() == clanId)
  533. {
  534. removeParticipant(clanId, false);
  535. }
  536. else
  537. {
  538. removeParticipant(clanId, true);
  539. }
  540. }
  541. }
  542. clearTables();
  543. }
  544. @Override
  545. public final Location getInnerSpawnLoc(final L2PcInstance player)
  546. {
  547. Location loc = null;
  548. if (player.getClanId() == _hall.getOwnerId())
  549. {
  550. loc = _hall.getZone().getSpawns().get(0);
  551. }
  552. else
  553. {
  554. ClanData cd = _data.get(player.getClanId());
  555. if (cd != null)
  556. {
  557. int index = cd.flag - FLAG_RED;
  558. if ((index >= 0) && (index <= 4))
  559. {
  560. loc = _hall.getZone().getChallengerSpawns().get(index);
  561. }
  562. else
  563. {
  564. throw new ArrayIndexOutOfBoundsException();
  565. }
  566. }
  567. }
  568. return loc;
  569. }
  570. @Override
  571. public final boolean canPlantFlag()
  572. {
  573. return false;
  574. }
  575. @Override
  576. public final boolean doorIsAutoAttackable()
  577. {
  578. return false;
  579. }
  580. void doSpawns(int clanId, ClanData data)
  581. {
  582. try
  583. {
  584. L2NpcTemplate mahumTemplate = NpcData.getInstance().getTemplate(data.npc);
  585. L2NpcTemplate flagTemplate = NpcData.getInstance().getTemplate(data.flag);
  586. if (flagTemplate == null)
  587. {
  588. _log.warning(getName() + ": Flag L2NpcTemplate[" + data.flag + "] does not exist!");
  589. throw new NullPointerException();
  590. }
  591. else if (mahumTemplate == null)
  592. {
  593. _log.warning(getName() + ": Ally L2NpcTemplate[" + data.npc + "] does not exist!");
  594. throw new NullPointerException();
  595. }
  596. int index = 0;
  597. if (_firstPhase)
  598. {
  599. index = data.flag - FLAG_RED;
  600. }
  601. else
  602. {
  603. index = clanId == _hall.getOwnerId() ? 5 : 6;
  604. }
  605. Location loc = FLAG_COORDS[index];
  606. data.flagInstance = new L2Spawn(flagTemplate);
  607. data.flagInstance.setLocation(loc);
  608. data.flagInstance.setRespawnDelay(10000);
  609. data.flagInstance.setAmount(1);
  610. data.flagInstance.init();
  611. data.warrior = new L2Spawn(mahumTemplate);
  612. data.warrior.setLocation(loc);
  613. data.warrior.setRespawnDelay(10000);
  614. data.warrior.setAmount(1);
  615. data.warrior.init();
  616. ((L2SpecialSiegeGuardAI) data.warrior.getLastSpawn().getAI()).getAlly().addAll(data.players);
  617. }
  618. catch (Exception e)
  619. {
  620. _log.warning(getName() + ": Couldnt make clan spawns: " + e.getMessage());
  621. e.printStackTrace();
  622. }
  623. }
  624. private void fillPlayerList(ClanData data)
  625. {
  626. for (int objId : data.players)
  627. {
  628. L2PcInstance plr = L2World.getInstance().getPlayer(objId);
  629. if (plr != null)
  630. {
  631. data.playersInstance.add(plr);
  632. }
  633. }
  634. }
  635. private void registerClan(L2Clan clan)
  636. {
  637. final int clanId = clan.getId();
  638. L2SiegeClan sc = new L2SiegeClan(clanId, SiegeClanType.ATTACKER);
  639. getAttackers().put(clanId, sc);
  640. ClanData data = new ClanData();
  641. data.flag = ROYAL_FLAG + _data.size();
  642. data.players.add(clan.getLeaderId());
  643. _data.put(clanId, data);
  644. saveClan(clanId, data.flag);
  645. saveMember(clanId, clan.getLeaderId());
  646. }
  647. private final void doUnSpawns(ClanData data)
  648. {
  649. if (data.flagInstance != null)
  650. {
  651. data.flagInstance.stopRespawn();
  652. data.flagInstance.getLastSpawn().deleteMe();
  653. }
  654. if (data.warrior != null)
  655. {
  656. data.warrior.stopRespawn();
  657. data.warrior.getLastSpawn().deleteMe();
  658. }
  659. }
  660. private final void removeParticipant(int clanId, boolean teleport)
  661. {
  662. ClanData dat = _data.remove(clanId);
  663. if (dat != null)
  664. {
  665. // Destroy clan flag
  666. if (dat.flagInstance != null)
  667. {
  668. dat.flagInstance.stopRespawn();
  669. if (dat.flagInstance.getLastSpawn() != null)
  670. {
  671. dat.flagInstance.getLastSpawn().deleteMe();
  672. }
  673. }
  674. if (dat.warrior != null)
  675. {
  676. // Destroy clan warrior
  677. dat.warrior.stopRespawn();
  678. if (dat.warrior.getLastSpawn() != null)
  679. {
  680. dat.warrior.getLastSpawn().deleteMe();
  681. }
  682. }
  683. dat.players.clear();
  684. if (teleport)
  685. {
  686. // Teleport players outside
  687. for (L2PcInstance pc : dat.playersInstance)
  688. {
  689. if (pc != null)
  690. {
  691. pc.teleToLocation(TeleportWhereType.TOWN);
  692. }
  693. }
  694. }
  695. dat.playersInstance.clear();
  696. }
  697. }
  698. public boolean canPayRegistration()
  699. {
  700. return true;
  701. }
  702. private void sendRegistrationPageDate(L2PcInstance player)
  703. {
  704. final NpcHtmlMessage msg = new NpcHtmlMessage();
  705. msg.setHtml(getHtm(player.getHtmlPrefix(), "siege_date.htm"));
  706. msg.replace("%nextSiege%", _hall.getSiegeDate().getTime().toString());
  707. player.sendPacket(msg);
  708. }
  709. public abstract String getFlagHtml(int flag);
  710. public abstract String getAllyHtml(int ally);
  711. @Override
  712. public final void loadAttackers()
  713. {
  714. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  715. {
  716. PreparedStatement statement = con.prepareStatement(SQL_LOAD_ATTACKERS);
  717. statement.setInt(1, _hall.getId());
  718. ResultSet rset = statement.executeQuery();
  719. while (rset.next())
  720. {
  721. final int clanId = rset.getInt("clan_id");
  722. if (ClanTable.getInstance().getClan(clanId) == null)
  723. {
  724. _log.warning(getName() + ": Loaded an unexistent clan as attacker! Clan ID: " + clanId);
  725. continue;
  726. }
  727. ClanData data = new ClanData();
  728. data.flag = rset.getInt("flag");
  729. data.npc = rset.getInt("npc");
  730. _data.put(clanId, data);
  731. loadAttackerMembers(clanId);
  732. }
  733. rset.close();
  734. statement.close();
  735. }
  736. catch (Exception e)
  737. {
  738. _log.warning(getName() + ".loadAttackers()->" + e.getMessage());
  739. e.printStackTrace();
  740. }
  741. }
  742. private final void loadAttackerMembers(int clanId)
  743. {
  744. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  745. {
  746. ArrayList<Integer> listInstance = _data.get(clanId).players;
  747. if (listInstance == null)
  748. {
  749. _log.warning(getName() + ": Tried to load unregistered clan with ID " + clanId);
  750. return;
  751. }
  752. PreparedStatement statement = con.prepareStatement(SQL_LOAD_MEMEBERS);
  753. statement.setInt(1, clanId);
  754. ResultSet rset = statement.executeQuery();
  755. while (rset.next())
  756. {
  757. listInstance.add(rset.getInt("object_id"));
  758. }
  759. rset.close();
  760. statement.close();
  761. }
  762. catch (Exception e)
  763. {
  764. _log.warning(getName() + ".loadAttackerMembers()->" + e.getMessage());
  765. e.printStackTrace();
  766. }
  767. }
  768. private final void saveClan(int clanId, int flag)
  769. {
  770. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  771. {
  772. PreparedStatement statement = con.prepareStatement(SQL_SAVE_CLAN);
  773. statement.setInt(1, _hall.getId());
  774. statement.setInt(2, flag);
  775. statement.setInt(3, 0);
  776. statement.setInt(4, clanId);
  777. statement.execute();
  778. statement.close();
  779. }
  780. catch (Exception e)
  781. {
  782. _log.warning(getName() + ".saveClan()->" + e.getMessage());
  783. e.printStackTrace();
  784. }
  785. }
  786. private final void saveNpc(int npc, int clanId)
  787. {
  788. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  789. {
  790. PreparedStatement statement = con.prepareStatement(SQL_SAVE_NPC);
  791. statement.setInt(1, npc);
  792. statement.setInt(2, clanId);
  793. statement.execute();
  794. statement.close();
  795. }
  796. catch (Exception e)
  797. {
  798. _log.warning(getName() + ".saveNpc()->" + e.getMessage());
  799. e.printStackTrace();
  800. }
  801. }
  802. private final void saveMember(int clanId, int objectId)
  803. {
  804. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  805. {
  806. PreparedStatement statement = con.prepareStatement(SQL_SAVE_ATTACKER);
  807. statement.setInt(1, _hall.getId());
  808. statement.setInt(2, clanId);
  809. statement.setInt(3, objectId);
  810. statement.execute();
  811. statement.close();
  812. }
  813. catch (Exception e)
  814. {
  815. _log.warning(getName() + ".saveMember()->" + e.getMessage());
  816. e.printStackTrace();
  817. }
  818. }
  819. private void clearTables()
  820. {
  821. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  822. {
  823. PreparedStatement stat1 = con.prepareStatement(SQL_CLEAR_CLAN);
  824. stat1.setInt(1, _hall.getId());
  825. stat1.execute();
  826. stat1.close();
  827. PreparedStatement stat2 = con.prepareStatement(SQL_CLEAR_CLAN_ATTACKERS);
  828. stat2.setInt(1, _hall.getId());
  829. stat2.execute();
  830. stat2.close();
  831. }
  832. catch (Exception e)
  833. {
  834. _log.warning(getName() + ".clearTables()->" + e.getMessage());
  835. }
  836. }
  837. class ClanData
  838. {
  839. int flag = 0;
  840. int npc = 0;
  841. ArrayList<Integer> players = new ArrayList<>(18);
  842. ArrayList<L2PcInstance> playersInstance = new ArrayList<>(18);
  843. L2Spawn warrior = null;
  844. L2Spawn flagInstance = null;
  845. }
  846. }