AbstractOlympiadGame.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model.olympiad;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.ai.CtrlIntention;
  25. import com.l2jserver.gameserver.datatables.SkillTreesData;
  26. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  27. import com.l2jserver.gameserver.instancemanager.CastleManager;
  28. import com.l2jserver.gameserver.instancemanager.FortManager;
  29. import com.l2jserver.gameserver.instancemanager.QuestManager;
  30. import com.l2jserver.gameserver.model.L2Party;
  31. import com.l2jserver.gameserver.model.L2Party.messageType;
  32. import com.l2jserver.gameserver.model.Location;
  33. import com.l2jserver.gameserver.model.actor.L2Character;
  34. import com.l2jserver.gameserver.model.actor.L2Summon;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.entity.TvTEvent;
  37. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  38. import com.l2jserver.gameserver.model.quest.Quest;
  39. import com.l2jserver.gameserver.model.skills.L2Skill;
  40. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  41. import com.l2jserver.gameserver.network.SystemMessageId;
  42. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadMode;
  43. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  44. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  45. import com.l2jserver.gameserver.network.serverpackets.SkillCoolTime;
  46. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  47. /**
  48. * @author godson, GodKratos, Pere, DS
  49. */
  50. public abstract class AbstractOlympiadGame
  51. {
  52. protected static final Logger _log = Logger.getLogger(AbstractOlympiadGame.class.getName());
  53. protected static final Logger _logResults = Logger.getLogger("olympiad");
  54. protected static final String POINTS = "olympiad_points";
  55. protected static final String COMP_DONE = "competitions_done";
  56. protected static final String COMP_WON = "competitions_won";
  57. protected static final String COMP_LOST = "competitions_lost";
  58. protected static final String COMP_DRAWN = "competitions_drawn";
  59. protected static final String COMP_DONE_WEEK = "competitions_done_week";
  60. protected static final String COMP_DONE_WEEK_CLASSED = "competitions_done_week_classed";
  61. protected static final String COMP_DONE_WEEK_NON_CLASSED = "competitions_done_week_non_classed";
  62. protected static final String COMP_DONE_WEEK_TEAM = "competitions_done_week_team";
  63. protected long _startTime = 0;
  64. protected boolean _aborted = false;
  65. protected final int _stadiumID;
  66. protected AbstractOlympiadGame(int id)
  67. {
  68. _stadiumID = id;
  69. }
  70. public final boolean isAborted()
  71. {
  72. return _aborted;
  73. }
  74. public final int getStadiumId()
  75. {
  76. return _stadiumID;
  77. }
  78. protected boolean makeCompetitionStart()
  79. {
  80. _startTime = System.currentTimeMillis();
  81. return !_aborted;
  82. }
  83. protected final void addPointsToParticipant(Participant par, int points)
  84. {
  85. par.updateStat(POINTS, points);
  86. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  87. sm.addString(par.getName());
  88. sm.addNumber(points);
  89. broadcastPacket(sm);
  90. for (Quest quest : QuestManager.getInstance().getAllManagedScripts())
  91. {
  92. if ((quest != null) && quest.isOlympiadUse())
  93. {
  94. quest.notifyOlympiadWin(par.getPlayer(), getType());
  95. }
  96. }
  97. }
  98. protected final void removePointsFromParticipant(Participant par, int points)
  99. {
  100. par.updateStat(POINTS, -points);
  101. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_LOST_S2_OLYMPIAD_POINTS);
  102. sm.addString(par.getName());
  103. sm.addNumber(points);
  104. broadcastPacket(sm);
  105. for (Quest quest : QuestManager.getInstance().getAllManagedScripts())
  106. {
  107. if ((quest != null) && quest.isOlympiadUse())
  108. {
  109. quest.notifyOlympiadLose(par.getPlayer(), getType());
  110. }
  111. }
  112. }
  113. /**
  114. * Function return null if player passed all checks or SystemMessage with reason for broadcast to opponent(s).
  115. * @param player
  116. * @return
  117. */
  118. protected static SystemMessage checkDefaulted(L2PcInstance player)
  119. {
  120. if ((player == null) || !player.isOnline())
  121. {
  122. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_ENDS_THE_GAME);
  123. }
  124. if ((player.getClient() == null) || player.getClient().isDetached())
  125. {
  126. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_ENDS_THE_GAME);
  127. }
  128. // safety precautions
  129. if (player.inObserverMode() || TvTEvent.isPlayerParticipant(player.getObjectId()))
  130. {
  131. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME);
  132. }
  133. SystemMessage sm;
  134. if (player.isDead())
  135. {
  136. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_OLYMPIAD_WHILE_DEAD);
  137. sm.addPcName(player);
  138. player.sendPacket(sm);
  139. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME);
  140. }
  141. if (player.isSubClassActive())
  142. {
  143. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_WHILE_CHANGED_TO_SUB_CLASS);
  144. sm.addPcName(player);
  145. player.sendPacket(sm);
  146. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME);
  147. }
  148. if (player.isCursedWeaponEquipped())
  149. {
  150. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_CANNOT_JOIN_OLYMPIAD_POSSESSING_S2);
  151. sm.addPcName(player);
  152. sm.addItemName(player.getCursedWeaponEquippedId());
  153. player.sendPacket(sm);
  154. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME);
  155. }
  156. if (!player.isInventoryUnder90(true))
  157. {
  158. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_INVENTORY_SLOT_EXCEEDS_80_PERCENT);
  159. sm.addPcName(player);
  160. player.sendPacket(sm);
  161. return SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_HAS_BEEN_CANCELLED_BECAUSE_THE_OTHER_PARTY_DOES_NOT_MEET_THE_REQUIREMENTS_FOR_JOINING_THE_GAME);
  162. }
  163. return null;
  164. }
  165. protected static final boolean portPlayerToArena(Participant par, Location loc, int id)
  166. {
  167. final L2PcInstance player = par.getPlayer();
  168. if ((player == null) || !player.isOnline())
  169. {
  170. return false;
  171. }
  172. try
  173. {
  174. player.setLastCords(player.getX(), player.getY(), player.getZ());
  175. if (player.isSitting())
  176. {
  177. player.standUp();
  178. }
  179. player.setTarget(null);
  180. player.setOlympiadGameId(id);
  181. player.setIsInOlympiadMode(true);
  182. player.setIsOlympiadStart(false);
  183. player.setOlympiadSide(par.getSide());
  184. player.olyBuff = 5;
  185. loc.setInstanceId(OlympiadGameManager.getInstance().getOlympiadTask(id).getZone().getInstanceId());
  186. player.teleToLocation(loc, false);
  187. player.sendPacket(new ExOlympiadMode(2));
  188. }
  189. catch (Exception e)
  190. {
  191. _log.log(Level.WARNING, e.getMessage(), e);
  192. return false;
  193. }
  194. return true;
  195. }
  196. protected static final void removals(L2PcInstance player, boolean removeParty)
  197. {
  198. try
  199. {
  200. if (player == null)
  201. {
  202. return;
  203. }
  204. // Remove Buffs
  205. player.stopAllEffectsExceptThoseThatLastThroughDeath();
  206. // Remove Clan Skills
  207. if (player.getClan() != null)
  208. {
  209. player.getClan().removeSkillEffects(player);
  210. if (player.getClan().getCastleId() > 0)
  211. {
  212. CastleManager.getInstance().getCastleByOwner(player.getClan()).removeResidentialSkills(player);
  213. }
  214. if (player.getClan().getFortId() > 0)
  215. {
  216. FortManager.getInstance().getFortByOwner(player.getClan()).removeResidentialSkills(player);
  217. }
  218. }
  219. // Abort casting if player casting
  220. player.abortAttack();
  221. player.abortCast();
  222. // Force the character to be visible
  223. player.getAppearance().setVisible();
  224. // Remove Hero Skills
  225. if (player.isHero())
  226. {
  227. for (L2Skill skill : SkillTreesData.getInstance().getHeroSkillTree().values())
  228. {
  229. player.removeSkill(skill, false);
  230. }
  231. }
  232. // Heal Player fully
  233. player.setCurrentCp(player.getMaxCp());
  234. player.setCurrentHp(player.getMaxHp());
  235. player.setCurrentMp(player.getMaxMp());
  236. // Remove Summon's Buffs
  237. if (player.hasSummon())
  238. {
  239. final L2Summon summon = player.getSummon();
  240. summon.stopAllEffectsExceptThoseThatLastThroughDeath();
  241. summon.abortAttack();
  242. summon.abortCast();
  243. if (summon.isPet())
  244. {
  245. summon.unSummon(player);
  246. }
  247. }
  248. // stop any cubic that has been given by other player.
  249. player.stopCubicsByOthers();
  250. // Remove player from his party
  251. if (removeParty)
  252. {
  253. final L2Party party = player.getParty();
  254. if (party != null)
  255. {
  256. party.removePartyMember(player, messageType.Expelled);
  257. }
  258. }
  259. // Remove Agathion
  260. if (player.getAgathionId() > 0)
  261. {
  262. player.setAgathionId(0);
  263. player.broadcastUserInfo();
  264. }
  265. player.checkItemRestriction();
  266. // Remove shot automation
  267. player.disableAutoShotsAll();
  268. // Discharge any active shots
  269. L2ItemInstance item = player.getActiveWeaponInstance();
  270. if (item != null)
  271. {
  272. item.unChargeAllShots();
  273. }
  274. // enable skills with cool time <= 15 minutes
  275. for (L2Skill skill : player.getAllSkills())
  276. {
  277. if (skill.getReuseDelay() <= 900000)
  278. {
  279. player.enableSkill(skill);
  280. }
  281. }
  282. player.sendSkillList();
  283. player.sendPacket(new SkillCoolTime(player));
  284. }
  285. catch (Exception e)
  286. {
  287. _log.log(Level.WARNING, e.getMessage(), e);
  288. }
  289. }
  290. protected static final void cleanEffects(L2PcInstance player)
  291. {
  292. try
  293. {
  294. // prevent players kill each other
  295. player.setIsOlympiadStart(false);
  296. player.setTarget(null);
  297. player.abortAttack();
  298. player.abortCast();
  299. player.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  300. if (player.isDead())
  301. {
  302. player.setIsDead(false);
  303. }
  304. player.stopAllEffectsExceptThoseThatLastThroughDeath();
  305. player.clearSouls();
  306. player.clearCharges();
  307. if (player.getAgathionId() > 0)
  308. {
  309. player.setAgathionId(0);
  310. }
  311. final L2Summon summon = player.getSummon();
  312. if ((summon != null) && !summon.isDead())
  313. {
  314. summon.setTarget(null);
  315. summon.abortAttack();
  316. summon.abortCast();
  317. summon.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  318. summon.stopAllEffectsExceptThoseThatLastThroughDeath();
  319. }
  320. player.setCurrentCp(player.getMaxCp());
  321. player.setCurrentHp(player.getMaxHp());
  322. player.setCurrentMp(player.getMaxMp());
  323. player.getStatus().startHpMpRegeneration();
  324. }
  325. catch (Exception e)
  326. {
  327. _log.log(Level.WARNING, e.getMessage(), e);
  328. }
  329. }
  330. protected static final void playerStatusBack(L2PcInstance player)
  331. {
  332. try
  333. {
  334. if (player.isTransformed())
  335. {
  336. player.untransform();
  337. }
  338. player.setIsInOlympiadMode(false);
  339. player.setIsOlympiadStart(false);
  340. player.setOlympiadSide(-1);
  341. player.setOlympiadGameId(-1);
  342. player.sendPacket(new ExOlympiadMode(0));
  343. // Add Clan Skills
  344. if (player.getClan() != null)
  345. {
  346. player.getClan().addSkillEffects(player);
  347. if (player.getClan().getCastleId() > 0)
  348. {
  349. CastleManager.getInstance().getCastleByOwner(player.getClan()).giveResidentialSkills(player);
  350. }
  351. if (player.getClan().getFortId() > 0)
  352. {
  353. FortManager.getInstance().getFortByOwner(player.getClan()).giveResidentialSkills(player);
  354. }
  355. }
  356. // Add Hero Skills
  357. if (player.isHero())
  358. {
  359. for (L2Skill skill : SkillTreesData.getInstance().getHeroSkillTree().values())
  360. {
  361. player.addSkill(skill, false);
  362. }
  363. }
  364. player.sendSkillList();
  365. // heal again after adding clan skills
  366. player.setCurrentCp(player.getMaxCp());
  367. player.setCurrentHp(player.getMaxHp());
  368. player.setCurrentMp(player.getMaxMp());
  369. player.getStatus().startHpMpRegeneration();
  370. if (Config.L2JMOD_DUALBOX_CHECK_MAX_OLYMPIAD_PARTICIPANTS_PER_IP > 0)
  371. {
  372. AntiFeedManager.getInstance().removePlayer(AntiFeedManager.OLYMPIAD_ID, player);
  373. }
  374. }
  375. catch (Exception e)
  376. {
  377. _log.log(Level.WARNING, "portPlayersToArena()", e);
  378. }
  379. }
  380. protected static final void portPlayerBack(L2PcInstance player)
  381. {
  382. if (player == null)
  383. {
  384. return;
  385. }
  386. if ((player.getLastX() == 0) && (player.getLastY() == 0))
  387. {
  388. return;
  389. }
  390. player.setInstanceId(0);
  391. player.teleToLocation(player.getLastX(), player.getLastY(), player.getLastZ());
  392. player.setLastCords(0, 0, 0);
  393. }
  394. public static final void rewardParticipant(L2PcInstance player, int[][] reward)
  395. {
  396. if ((player == null) || !player.isOnline() || (reward == null))
  397. {
  398. return;
  399. }
  400. try
  401. {
  402. SystemMessage sm;
  403. L2ItemInstance item;
  404. final InventoryUpdate iu = new InventoryUpdate();
  405. for (int[] it : reward)
  406. {
  407. if ((it == null) || (it.length != 2))
  408. {
  409. continue;
  410. }
  411. item = player.getInventory().addItem("Olympiad", it[0], it[1], player, null);
  412. if (item == null)
  413. {
  414. continue;
  415. }
  416. iu.addModifiedItem(item);
  417. sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  418. sm.addItemName(it[0]);
  419. sm.addNumber(it[1]);
  420. player.sendPacket(sm);
  421. }
  422. player.sendPacket(iu);
  423. }
  424. catch (Exception e)
  425. {
  426. _log.log(Level.WARNING, e.getMessage(), e);
  427. }
  428. }
  429. public abstract CompetitionType getType();
  430. public abstract String[] getPlayerNames();
  431. public abstract boolean containsParticipant(int playerId);
  432. public abstract void sendOlympiadInfo(L2Character player);
  433. public abstract void broadcastOlympiadInfo(L2OlympiadStadiumZone stadium);
  434. protected abstract void broadcastPacket(L2GameServerPacket packet);
  435. protected abstract boolean needBuffers();
  436. protected abstract boolean checkDefaulted();
  437. protected abstract void removals();
  438. protected abstract boolean portPlayersToArena(List<Location> spawns);
  439. protected abstract void cleanEffects();
  440. protected abstract void portPlayersBack();
  441. protected abstract void playersStatusBack();
  442. protected abstract void clearPlayers();
  443. protected abstract void handleDisconnect(L2PcInstance player);
  444. protected abstract void resetDamage();
  445. protected abstract void addDamage(L2PcInstance player, int damage);
  446. protected abstract boolean checkBattleStatus();
  447. protected abstract boolean haveWinner();
  448. protected abstract void validateWinner(L2OlympiadStadiumZone stadium);
  449. protected abstract int getDivider();
  450. protected abstract int[][] getReward();
  451. protected abstract String getWeeklyMatchType();
  452. }