AbstractOlympiadGame.java 15 KB

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