AbstractOlympiadGame.java 16 KB

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