FlagWar.java 23 KB

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