FlagWar.java 24 KB

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