FlagWar.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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.warn("{}: select_clan_npc->Wrong mahum warrior ID {}!", getName(), var[1], e);
  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.warn("{}: Siege: Not enough parameters to save clan npc for clan {}! ", getName(), 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.warn("{}: Problems in siege initialization!", getName(), e);
  508. }
  509. }
  510. }
  511. @Override
  512. public void endSiege()
  513. {
  514. if (_hall.getOwnerId() > 0)
  515. {
  516. L2Clan clan = ClanTable.getInstance().getClan(_hall.getOwnerId());
  517. clan.setHideoutId(0);
  518. _hall.free();
  519. }
  520. super.endSiege();
  521. }
  522. @Override
  523. public void onSiegeEnds()
  524. {
  525. if (_data.size() > 0)
  526. {
  527. for (int clanId : _data.keySet())
  528. {
  529. if (_hall.getOwnerId() == clanId)
  530. {
  531. removeParticipant(clanId, false);
  532. }
  533. else
  534. {
  535. removeParticipant(clanId, true);
  536. }
  537. }
  538. }
  539. clearTables();
  540. }
  541. @Override
  542. public final Location getInnerSpawnLoc(final L2PcInstance player)
  543. {
  544. Location loc = null;
  545. if (player.getClanId() == _hall.getOwnerId())
  546. {
  547. loc = _hall.getZone().getSpawns().get(0);
  548. }
  549. else
  550. {
  551. ClanData cd = _data.get(player.getClanId());
  552. if (cd != null)
  553. {
  554. int index = cd.flag - FLAG_RED;
  555. if ((index >= 0) && (index <= 4))
  556. {
  557. loc = _hall.getZone().getChallengerSpawns().get(index);
  558. }
  559. else
  560. {
  561. throw new ArrayIndexOutOfBoundsException();
  562. }
  563. }
  564. }
  565. return loc;
  566. }
  567. @Override
  568. public final boolean canPlantFlag()
  569. {
  570. return false;
  571. }
  572. @Override
  573. public final boolean doorIsAutoAttackable()
  574. {
  575. return false;
  576. }
  577. void doSpawns(int clanId, ClanData data)
  578. {
  579. try
  580. {
  581. int index = 0;
  582. if (_firstPhase)
  583. {
  584. index = data.flag - FLAG_RED;
  585. }
  586. else
  587. {
  588. index = clanId == _hall.getOwnerId() ? 5 : 6;
  589. }
  590. Location loc = FLAG_COORDS[index];
  591. data.flagInstance = new L2Spawn(data.flag);
  592. data.flagInstance.setLocation(loc);
  593. data.flagInstance.setRespawnDelay(10000);
  594. data.flagInstance.setAmount(1);
  595. data.flagInstance.init();
  596. data.warrior = new L2Spawn(data.npc);
  597. data.warrior.setLocation(loc);
  598. data.warrior.setRespawnDelay(10000);
  599. data.warrior.setAmount(1);
  600. data.warrior.init();
  601. ((L2SpecialSiegeGuardAI) data.warrior.getLastSpawn().getAI()).getAlly().addAll(data.players);
  602. }
  603. catch (Exception e)
  604. {
  605. _log.warn("{}: Could not make clan spawns!", getName(), e);
  606. }
  607. }
  608. private void fillPlayerList(ClanData data)
  609. {
  610. for (int objId : data.players)
  611. {
  612. L2PcInstance plr = L2World.getInstance().getPlayer(objId);
  613. if (plr != null)
  614. {
  615. data.playersInstance.add(plr);
  616. }
  617. }
  618. }
  619. private void registerClan(L2Clan clan)
  620. {
  621. final int clanId = clan.getId();
  622. L2SiegeClan sc = new L2SiegeClan(clanId, SiegeClanType.ATTACKER);
  623. getAttackers().put(clanId, sc);
  624. ClanData data = new ClanData();
  625. data.flag = ROYAL_FLAG + _data.size();
  626. data.players.add(clan.getLeaderId());
  627. _data.put(clanId, data);
  628. saveClan(clanId, data.flag);
  629. saveMember(clanId, clan.getLeaderId());
  630. }
  631. private final void doUnSpawns(ClanData data)
  632. {
  633. if (data.flagInstance != null)
  634. {
  635. data.flagInstance.stopRespawn();
  636. data.flagInstance.getLastSpawn().deleteMe();
  637. }
  638. if (data.warrior != null)
  639. {
  640. data.warrior.stopRespawn();
  641. data.warrior.getLastSpawn().deleteMe();
  642. }
  643. }
  644. private final void removeParticipant(int clanId, boolean teleport)
  645. {
  646. ClanData dat = _data.remove(clanId);
  647. if (dat != null)
  648. {
  649. // Destroy clan flag
  650. if (dat.flagInstance != null)
  651. {
  652. dat.flagInstance.stopRespawn();
  653. if (dat.flagInstance.getLastSpawn() != null)
  654. {
  655. dat.flagInstance.getLastSpawn().deleteMe();
  656. }
  657. }
  658. if (dat.warrior != null)
  659. {
  660. // Destroy clan warrior
  661. dat.warrior.stopRespawn();
  662. if (dat.warrior.getLastSpawn() != null)
  663. {
  664. dat.warrior.getLastSpawn().deleteMe();
  665. }
  666. }
  667. dat.players.clear();
  668. if (teleport)
  669. {
  670. // Teleport players outside
  671. for (L2PcInstance pc : dat.playersInstance)
  672. {
  673. if (pc != null)
  674. {
  675. pc.teleToLocation(TeleportWhereType.TOWN);
  676. }
  677. }
  678. }
  679. dat.playersInstance.clear();
  680. }
  681. }
  682. public boolean canPayRegistration()
  683. {
  684. return true;
  685. }
  686. private void sendRegistrationPageDate(L2PcInstance player)
  687. {
  688. final NpcHtmlMessage msg = new NpcHtmlMessage();
  689. msg.setHtml(getHtm(player.getHtmlPrefix(), "siege_date.htm"));
  690. msg.replace("%nextSiege%", _hall.getSiegeDate().getTime().toString());
  691. player.sendPacket(msg);
  692. }
  693. public abstract String getFlagHtml(int flag);
  694. public abstract String getAllyHtml(int ally);
  695. @Override
  696. public final void loadAttackers()
  697. {
  698. try (Connection con = ConnectionFactory.getInstance().getConnection();
  699. PreparedStatement ps = con.prepareStatement(SQL_LOAD_ATTACKERS))
  700. {
  701. ps.setInt(1, _hall.getId());
  702. try (ResultSet rset = ps.executeQuery())
  703. {
  704. while (rset.next())
  705. {
  706. final int clanId = rset.getInt("clan_id");
  707. if (ClanTable.getInstance().getClan(clanId) == null)
  708. {
  709. _log.warn("{}: Loaded an unexistent clan as attacker! Clan ID {}!", getName(), clanId);
  710. continue;
  711. }
  712. ClanData data = new ClanData();
  713. data.flag = rset.getInt("flag");
  714. data.npc = rset.getInt("npc");
  715. _data.put(clanId, data);
  716. loadAttackerMembers(clanId);
  717. }
  718. }
  719. }
  720. catch (Exception e)
  721. {
  722. _log.warn("Could not load attackers for {}!", getName(), e);
  723. }
  724. }
  725. private final void loadAttackerMembers(int clanId)
  726. {
  727. final List<Integer> listInstance = _data.get(clanId).players;
  728. if (listInstance == null)
  729. {
  730. _log.warn(getName() + ": Tried to load unregistered clan with ID " + clanId);
  731. return;
  732. }
  733. try (Connection con = ConnectionFactory.getInstance().getConnection();
  734. PreparedStatement ps = con.prepareStatement(SQL_LOAD_MEMEBERS))
  735. {
  736. ps.setInt(1, clanId);
  737. try (ResultSet rset = ps.executeQuery())
  738. {
  739. while (rset.next())
  740. {
  741. listInstance.add(rset.getInt("object_id"));
  742. }
  743. }
  744. }
  745. catch (Exception e)
  746. {
  747. _log.warn("{}: loadAttackerMembers", getName(), e);
  748. }
  749. }
  750. private final void saveClan(int clanId, int flag)
  751. {
  752. try (Connection con = ConnectionFactory.getInstance().getConnection();
  753. PreparedStatement ps = con.prepareStatement(SQL_SAVE_CLAN))
  754. {
  755. ps.setInt(1, _hall.getId());
  756. ps.setInt(2, flag);
  757. ps.setInt(3, 0);
  758. ps.setInt(4, clanId);
  759. ps.execute();
  760. }
  761. catch (Exception e)
  762. {
  763. _log.warn("{}: saveClan", getName(), e);
  764. }
  765. }
  766. private final void saveNpc(int npc, int clanId)
  767. {
  768. try (Connection con = ConnectionFactory.getInstance().getConnection();
  769. PreparedStatement ps = con.prepareStatement(SQL_SAVE_NPC))
  770. {
  771. ps.setInt(1, npc);
  772. ps.setInt(2, clanId);
  773. ps.execute();
  774. }
  775. catch (Exception e)
  776. {
  777. _log.warn("{}: saveNpc()", getName(), e);
  778. }
  779. }
  780. private final void saveMember(int clanId, int objectId)
  781. {
  782. try (Connection con = ConnectionFactory.getInstance().getConnection();
  783. PreparedStatement ps = con.prepareStatement(SQL_SAVE_ATTACKER))
  784. {
  785. ps.setInt(1, _hall.getId());
  786. ps.setInt(2, clanId);
  787. ps.setInt(3, objectId);
  788. ps.execute();
  789. }
  790. catch (Exception e)
  791. {
  792. _log.warn("{}: saveMember", getName(), e);
  793. }
  794. }
  795. private void clearTables()
  796. {
  797. try (Connection con = ConnectionFactory.getInstance().getConnection();
  798. PreparedStatement ps1 = con.prepareStatement(SQL_CLEAR_CLAN);
  799. PreparedStatement ps2 = con.prepareStatement(SQL_CLEAR_CLAN_ATTACKERS))
  800. {
  801. ps1.setInt(1, _hall.getId());
  802. ps1.execute();
  803. ps2.setInt(1, _hall.getId());
  804. ps2.execute();
  805. }
  806. catch (Exception e)
  807. {
  808. _log.warn("Unable to clear data tables for {}!", getName(), e);
  809. }
  810. }
  811. class ClanData
  812. {
  813. int flag = 0;
  814. int npc = 0;
  815. List<Integer> players = new ArrayList<>(18);
  816. List<L2PcInstance> playersInstance = new ArrayList<>(18);
  817. L2Spawn warrior = null;
  818. L2Spawn flagInstance = null;
  819. }
  820. }