Olympiad.java 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  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. /**
  16. * @author godson
  17. */
  18. package net.sf.l2j.gameserver;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.util.Calendar;
  29. import java.util.Map;
  30. import java.util.Properties;
  31. import java.util.concurrent.ScheduledFuture;
  32. import java.util.logging.Logger;
  33. import javolution.util.FastMap;
  34. import net.sf.l2j.Config;
  35. import net.sf.l2j.L2DatabaseFactory;
  36. import net.sf.l2j.gameserver.datatables.DoorTable;
  37. import net.sf.l2j.gameserver.datatables.HeroSkillTable;
  38. import net.sf.l2j.gameserver.datatables.NpcTable;
  39. import net.sf.l2j.gameserver.datatables.SpawnTable;
  40. import net.sf.l2j.gameserver.instancemanager.OlympiadStadiaManager;
  41. import net.sf.l2j.gameserver.model.Inventory;
  42. import net.sf.l2j.gameserver.model.L2ItemInstance;
  43. import net.sf.l2j.gameserver.model.L2Party;
  44. import net.sf.l2j.gameserver.model.L2Skill;
  45. import net.sf.l2j.gameserver.model.L2Spawn;
  46. import net.sf.l2j.gameserver.model.L2Summon;
  47. import net.sf.l2j.gameserver.model.L2World;
  48. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  49. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  50. import net.sf.l2j.gameserver.model.entity.Hero;
  51. import net.sf.l2j.gameserver.network.SystemMessageId;
  52. import net.sf.l2j.gameserver.network.serverpackets.ExAutoSoulShot;
  53. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadMode;
  54. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadUserInfoSpectator;
  55. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  56. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  57. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  58. import net.sf.l2j.gameserver.templates.StatsSet;
  59. import net.sf.l2j.util.L2FastList;
  60. import net.sf.l2j.util.Rnd;
  61. public class Olympiad
  62. {
  63. /**
  64. *
  65. * @author ascharot
  66. *
  67. */
  68. private class OlympiadGameTask implements Runnable
  69. {
  70. public L2OlympiadGame _game = null;
  71. private SystemMessage _sm;
  72. private SystemMessage _sm2;
  73. private boolean _terminated = false;
  74. public boolean isTerminated()
  75. {
  76. return _terminated;
  77. }
  78. private boolean _started = false;
  79. public boolean isStarted()
  80. {
  81. return _started;
  82. }
  83. public OlympiadGameTask(L2OlympiadGame game)
  84. {
  85. _game = game;
  86. }
  87. protected boolean checkBattleStatus()
  88. {
  89. boolean _pOneCrash = (_game._playerOne == null || _game._playerOneDisconnected);
  90. boolean _pTwoCrash = (_game._playerTwo == null || _game._playerTwoDisconnected);
  91. if(_pOneCrash || _pTwoCrash || _game._aborted) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. protected boolean checkStatus()
  97. {
  98. boolean _pOneCrash = (_game._playerOne == null || _game._playerOneDisconnected);
  99. boolean _pTwoCrash = (_game._playerTwo == null || _game._playerTwoDisconnected);
  100. StatsSet playerOneStat;
  101. StatsSet playerTwoStat;
  102. playerOneStat = _nobles.get(_game._playerOneID);
  103. playerTwoStat = _nobles.get(_game._playerTwoID);
  104. int playerOnePlayed = playerOneStat.getInteger(COMP_DONE);
  105. int playerTwoPlayed = playerTwoStat.getInteger(COMP_DONE);
  106. if(_pOneCrash || _pTwoCrash || _game._aborted) {
  107. if (_pOneCrash && !_pTwoCrash){
  108. try{
  109. int playerOnePoints = playerOneStat.getInteger(POINTS);
  110. int transferPoints = playerOnePoints / 5;
  111. playerOneStat.set(POINTS, playerOnePoints - transferPoints);
  112. if (Config.DEBUG)
  113. _log.info("Olympia Result: "+_game._playerOneName+" vs "+_game._playerTwoName+" ... "+_game._playerOneName+" lost "+transferPoints+" points for crash");
  114. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  115. playerTwoStat.set(POINTS, playerTwoPoints + transferPoints);
  116. if (Config.DEBUG)
  117. _log.info("Olympia Result: "+_game._playerOneName+" vs "+_game._playerTwoName+" ... "+_game._playerTwoName+" Win "+transferPoints+" points");
  118. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  119. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  120. _sm.addString(_game._playerTwoName);
  121. broadcastMessage(_sm, true);
  122. _sm2.addString(_game._playerTwoName);
  123. _sm2.addNumber(transferPoints);
  124. broadcastMessage(_sm2, true);
  125. } catch(Exception e){e.printStackTrace();}
  126. }
  127. if (_pTwoCrash && !_pOneCrash){
  128. try{
  129. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  130. int transferPoints = playerTwoPoints / 5;
  131. playerTwoStat.set(POINTS, playerTwoPoints - transferPoints);
  132. if (Config.DEBUG)
  133. _log.info("Olympia Result: "+_game._playerTwoName+" vs "+_game._playerOneName+" ... "+_game._playerTwoName+" lost "+transferPoints+" points for crash");
  134. int playerOnePoints = playerOneStat.getInteger(POINTS);
  135. playerOneStat.set(POINTS, playerOnePoints + transferPoints);
  136. if (Config.DEBUG)
  137. _log.info("Olympia Result: "+_game._playerTwoName+" vs "+_game._playerOneName+" ... "+_game._playerOneName+" Win "+transferPoints+" points");
  138. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  139. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  140. _sm.addString(_game._playerOneName);
  141. broadcastMessage(_sm, true);
  142. _sm2.addString(_game._playerOneName);
  143. _sm2.addNumber(transferPoints);
  144. broadcastMessage(_sm2, true);
  145. } catch(Exception e){e.printStackTrace();}
  146. }
  147. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  148. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  149. _terminated = true;
  150. _game._gamestarted = false;
  151. _game.closeDoors();
  152. _game.PlayersStatusBack();
  153. try{
  154. _game.portPlayersBack();
  155. }catch(Exception e){e.printStackTrace();}
  156. _game = null;
  157. return false;
  158. }
  159. return true;
  160. }
  161. public void run()
  162. {
  163. _started = true;
  164. if(_game != null)
  165. {
  166. if(_game._playerOne != null && _game._playerTwo != null)
  167. {
  168. //Waiting for teleport to arena
  169. for (int i=120;i>10;i-=5)
  170. {
  171. switch(i) {
  172. case 120:
  173. case 60:
  174. case 30:
  175. case 15:
  176. _game.sendMessageToPlayers(false,i);
  177. break;
  178. }
  179. try{ Thread.sleep(5000); }catch (InterruptedException e){}
  180. }
  181. for (int i=5;i>0;i--)
  182. {
  183. _game.sendMessageToPlayers(false,i);
  184. try{ Thread.sleep(1000); }catch (InterruptedException e){}
  185. }
  186. //Checking for openents and teleporting to arena
  187. if(!checkStatus()) { return; }
  188. _game.closeDoors();
  189. _game.removals();
  190. _game.portPlayersToArena();
  191. try{ Thread.sleep(5000); }catch (InterruptedException e){}
  192. synchronized(this){
  193. if(!_battleStarted)
  194. _battleStarted = true;
  195. }
  196. for (int i=60;i>10;i-=10)
  197. {
  198. _game.sendMessageToPlayers(true,i);
  199. try{ Thread.sleep(10000); }catch (InterruptedException e){}
  200. if (i==20) {
  201. _game.additions();
  202. _game.openDoors();
  203. _game.sendMessageToPlayers(true,10);
  204. try{ Thread.sleep(5000); }catch (InterruptedException e){}
  205. }
  206. }
  207. for (int i=5;i>0;i--)
  208. {
  209. _game.sendMessageToPlayers(true,i);
  210. try{ Thread.sleep(1000); }catch (InterruptedException e){}
  211. }
  212. if(!checkStatus()) { return; }
  213. UnSpawnBuffer(_game._spawnOne);
  214. UnSpawnBuffer(_game._spawnTwo);
  215. _game._playerOne.sendPacket(new ExOlympiadUserInfoSpectator(_game._playerTwo, 1));
  216. _game._playerTwo.sendPacket(new ExOlympiadUserInfoSpectator(_game._playerOne, 1));
  217. if (_game._spectators != null)
  218. {
  219. for (L2PcInstance spec : _game.getSpectators())
  220. {
  221. try {
  222. spec.sendPacket(new ExOlympiadUserInfoSpectator(_game._playerTwo, 2));
  223. spec.sendPacket(new ExOlympiadUserInfoSpectator(_game._playerOne, 1));
  224. } catch (NullPointerException e) {}
  225. }
  226. }
  227. _game.makeCompetitionStart();
  228. //Wait 3 mins (Battle)
  229. for(int i=0; i<BATTLE_PERIOD; i += 10000)
  230. {
  231. try{
  232. Thread.sleep(10000);
  233. //If game haveWinner thean stop waiting battle_period and validate winner
  234. if(_game.haveWinner() || !checkBattleStatus())
  235. break;
  236. } catch (InterruptedException e){}
  237. }
  238. if(!checkStatus()) { return; }
  239. _terminated = true;
  240. _game._gamestarted = false;
  241. try{
  242. _game.validateWinner();
  243. _game.PlayersStatusBack();
  244. _game.removals();
  245. _game.portPlayersBack();
  246. }catch(Exception e){e.printStackTrace();}
  247. _game = null;
  248. }
  249. }
  250. }
  251. private void broadcastMessage(SystemMessage sm, boolean toAll)
  252. {
  253. try {
  254. _game._playerOne.sendPacket(sm);
  255. _game._playerTwo.sendPacket(sm);
  256. } catch (Exception e) {}
  257. if (toAll && _game._spectators != null)
  258. {
  259. for (L2PcInstance spec : _game._spectators)
  260. {
  261. try { spec.sendPacket(sm); } catch (NullPointerException e) {}
  262. }
  263. }
  264. }
  265. }
  266. protected static final Logger _log = Logger.getLogger(Olympiad.class.getName());
  267. private static Olympiad _instance;
  268. protected static Map<Integer, StatsSet> _nobles;
  269. protected static L2FastList<StatsSet> _heroesToBe;
  270. protected static L2FastList<L2PcInstance> _nonClassBasedRegisters;
  271. protected static Map<Integer, L2FastList<L2PcInstance>> _classBasedRegisters;
  272. private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties";
  273. public static final String OLYMPIAD_HTML_FILE = "data/html/olympiad/";
  274. private static final String OLYMPIAD_LOAD_NOBLES = "SELECT * FROM olympiad_nobles";
  275. private static final String OLYMPIAD_SAVE_NOBLES = "INSERT INTO olympiad_nobles " +
  276. "VALUES (?,?,?,?,?)";
  277. private static final String OLYMPIAD_UPDATE_NOBLES = "UPDATE olympiad_nobles SET " +
  278. "olympiad_points = ?, competitions_done = ? WHERE charId = ?";
  279. private static final String OLYMPIAD_GET_HEROS = "SELECT charId, char_name FROM " +
  280. "olympiad_nobles WHERE class_id = ? AND competitions_done >= 9 ORDER BY " +
  281. "olympiad_points DESC, competitions_done DESC";
  282. private static final String GET_EACH_CLASS_LEADER = "SELECT char_name FROM " +
  283. "olympiad_nobles WHERE class_id = ? ORDER BY olympiad_points DESC, " +
  284. "competitions_done DESC";
  285. private static final String OLYMPIAD_DELETE_ALL = "DELETE FROM olympiad_nobles";
  286. private static final int[] HERO_IDS = {88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,
  287. 106,107,108,109,110,111,112,113,114,115,116,117,118,131,132,133,134};
  288. private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
  289. private static final int COMP_MIN = Config.ALT_OLY_MIN; // 00 mins
  290. private static final long COMP_PERIOD = Config.ALT_OLY_CPERIOD; // 6 hours
  291. protected static final long BATTLE_PERIOD = Config.ALT_OLY_BATTLE; // 6 mins
  292. protected static final long BATTLE_WAIT = Config.ALT_OLY_BWAIT; // 10mins
  293. protected static final long INITIAL_WAIT = Config.ALT_OLY_IWAIT; // 5mins
  294. protected static final long WEEKLY_PERIOD = Config.ALT_OLY_WPERIOD; // 1 week
  295. protected static final long VALIDATION_PERIOD = Config.ALT_OLY_VPERIOD; // 24 hours
  296. /* FOR TESTING
  297. private static final int COMP_START = 8; // 1PM - 2PM
  298. private static final int COMP_MIN = 15; // 20mins
  299. private static final long COMP_PERIOD = 7200000; // 2hours
  300. private static final long BATTLE_PERIOD = 180000; // 3mins
  301. private static final long BATTLE_WAIT = 600000; // 10mins
  302. private static final long INITIAL_WAIT = 300000; // 5mins
  303. private static final long WEEKLY_PERIOD = 7200000; // 2 hours
  304. private static final long VALIDATION_PERIOD = 3600000; // 1 hour */
  305. private static final int DEFAULT_POINTS = 18;
  306. protected static final int WEEKLY_POINTS = 3;
  307. public static final String CHAR_ID = "charId";
  308. public static final String CLASS_ID = "class_id";
  309. public static final String CHAR_NAME = "char_name";
  310. public static final String POINTS = "olympiad_points";
  311. public static final String COMP_DONE = "competitions_done";
  312. public static DoorTable _doorTable;
  313. protected long _olympiadEnd;
  314. protected long _validationEnd;
  315. protected int _period;
  316. protected long _nextWeeklyChange;
  317. protected int _currentCycle;
  318. private long _compEnd;
  319. private Calendar _compStart;
  320. protected static boolean _inCompPeriod;
  321. protected static boolean _isOlympiadEnd;
  322. protected static boolean _compStarted = false;
  323. protected static boolean _battleStarted;
  324. protected static boolean _cycleTerminated;
  325. protected ScheduledFuture<?> _scheduledCompStart;
  326. protected ScheduledFuture<?> _scheduledCompEnd;
  327. protected ScheduledFuture<?> _scheduledOlympiadEnd;
  328. protected ScheduledFuture<?> _scheduledManagerTask;
  329. protected ScheduledFuture<?> _scheduledWeeklyTask;
  330. protected ScheduledFuture<?> _scheduledValdationTask;
  331. public static class Stadia
  332. {
  333. private boolean _freeToUse = true;
  334. public boolean isFreeToUse()
  335. {
  336. return _freeToUse;
  337. }
  338. public void setStadiaBusy()
  339. {
  340. _freeToUse = false;
  341. }
  342. public void setStadiaFree()
  343. {
  344. _freeToUse = true;
  345. }
  346. private int[] _coords = new int[3];
  347. private int[] _doors = new int[2];
  348. public int[] getCoordinates()
  349. {
  350. return _coords;
  351. }
  352. public int[] getDoorID()
  353. {
  354. return _doors;
  355. }
  356. public Stadia(int[] coords, int[] doors)
  357. {
  358. _coords = coords;
  359. _doors = doors;
  360. }
  361. public Stadia(int x, int y, int z, int d1, int d2)
  362. {
  363. _coords[0] = x;
  364. _coords[1] = y;
  365. _coords[2] = z;
  366. _doors[0] = d1;
  367. _doors[1] = d2;
  368. }
  369. }
  370. protected static final Stadia[] STADIUMS =
  371. {
  372. new Stadia(-20838, -21075, -3000, 19170001, 19170002),
  373. new Stadia(-88000, -252637, -3331, 17100001, 17100002),
  374. new Stadia(-83760, -252637, -3331, 17100003, 17100004),
  375. new Stadia(-79600, -252637, -3331, 17100005, 17100006),
  376. new Stadia(-75648, -252637, -3331, 17100007, 17100008),
  377. new Stadia(-88000, -249762, -3331, 17100009, 17100010),
  378. new Stadia(-83760, -249762, -3331, 17100011, 17100012),
  379. new Stadia(-79600, -249762, -3331, 17100013, 17100014),
  380. new Stadia(-75648, -249762, -3331, 17100015, 17100016),
  381. new Stadia(-88000, -246951, -3331, 17100017, 17100018),
  382. new Stadia(-83760, -246951, -3331, 17100019, 17100020),
  383. new Stadia(-79600, -246951, -3331, 17100021, 17100022),
  384. new Stadia(-75648, -246951, -3331, 17100023, 17100024),
  385. new Stadia(-88000, -244290, -3331, 17100025, 17100026),
  386. new Stadia(-83760, -244290, -3331, 17100027, 17100028),
  387. new Stadia(-79600, -244290, -3331, 17100029, 17100030),
  388. new Stadia(-75648, -244290, -3331, 17100031, 17100032),
  389. new Stadia(-88000, -241490, -3331, 17100033, 17100034),
  390. new Stadia(-83760, -241490, -3331, 17100035, 17100036),
  391. new Stadia(-79600, -241490, -3331, 17100037, 17100038),
  392. new Stadia(-75648, -241490, -3331, 17100039, 17100040),
  393. new Stadia(-88000, -238825, -3331, 17100041, 17100042),
  394. new Stadia(-83760, -238825, -3331, 17100043, 17100044)
  395. };
  396. private static enum COMP_TYPE
  397. {
  398. CLASSED,
  399. NON_CLASSED
  400. }
  401. protected static OlympiadManager _manager;
  402. public static Olympiad getInstance()
  403. {
  404. if (_instance == null)
  405. _instance = new Olympiad();
  406. return _instance;
  407. }
  408. public Olympiad()
  409. {
  410. try
  411. {
  412. load();
  413. }
  414. catch(IOException e)
  415. {
  416. e.printStackTrace();
  417. }
  418. catch(SQLException s)
  419. {
  420. s.printStackTrace();
  421. }
  422. if (_period == 0) init();
  423. }
  424. private void load() throws IOException, SQLException
  425. {
  426. _nobles = new FastMap<Integer, StatsSet>();
  427. Properties OlympiadProperties = new Properties();
  428. InputStream is = new FileInputStream(new File("./" + OLYMPIAD_DATA_FILE));
  429. OlympiadProperties.load(is);
  430. is.close();
  431. _currentCycle = Integer.parseInt(OlympiadProperties.getProperty("CurrentCycle", "1"));
  432. _period = Integer.parseInt(OlympiadProperties.getProperty("Period", "0"));
  433. _olympiadEnd = Long.parseLong(OlympiadProperties.getProperty("OlympiadEnd", "0"));
  434. _validationEnd = Long.parseLong(OlympiadProperties.getProperty("ValdationEnd", "0"));
  435. _nextWeeklyChange = Long.parseLong(OlympiadProperties.getProperty("NextWeeklyChange", "0"));
  436. switch(_period)
  437. {
  438. case 0:
  439. if (_olympiadEnd == 0 || _olympiadEnd < Calendar.getInstance().getTimeInMillis())
  440. setNewOlympiadEnd();
  441. else
  442. _isOlympiadEnd = false;
  443. break;
  444. case 1:
  445. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  446. {
  447. _isOlympiadEnd = true;
  448. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  449. public void run()
  450. {
  451. _period = 0;
  452. _currentCycle++;
  453. deleteNobles();
  454. setNewOlympiadEnd();
  455. init();
  456. }
  457. }, getMillisToValidationEnd());
  458. }
  459. else
  460. {
  461. _currentCycle++;
  462. _period = 0;
  463. deleteNobles();
  464. setNewOlympiadEnd();
  465. }
  466. break;
  467. default:
  468. _log.warning("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  469. return;
  470. }
  471. try
  472. {
  473. Connection con = L2DatabaseFactory.getInstance().getConnection();
  474. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
  475. ResultSet rset = statement.executeQuery();
  476. while(rset.next())
  477. {
  478. StatsSet statDat = new StatsSet();
  479. int charId = rset.getInt(CHAR_ID);
  480. statDat.set(CLASS_ID, rset.getInt(CLASS_ID));
  481. statDat.set(CHAR_NAME, rset.getString(CHAR_NAME));
  482. statDat.set(POINTS, rset.getInt(POINTS));
  483. statDat.set(COMP_DONE, rset.getInt(COMP_DONE));
  484. statDat.set("to_save", false);
  485. _nobles.put(charId, statDat);
  486. }
  487. rset.close();
  488. statement.close();
  489. con.close();
  490. }
  491. catch(Exception e)
  492. {
  493. e.printStackTrace();
  494. }
  495. synchronized(this)
  496. {
  497. _log.info("Olympiad System: Loading Olympiad System....");
  498. if (_period == 0)
  499. _log.info("Olympiad System: Currently in Olympiad Period");
  500. else
  501. _log.info("Olympiad System: Currently in Validation Period");
  502. _log.info("Olympiad System: Period Ends....");
  503. long milliToEnd;
  504. if (_period == 0)
  505. milliToEnd = getMillisToOlympiadEnd();
  506. else
  507. milliToEnd = getMillisToValidationEnd();
  508. double numSecs = (milliToEnd / 1000) % 60;
  509. double countDown = ((milliToEnd / 1000) - numSecs) / 60;
  510. int numMins = (int) Math.floor(countDown % 60);
  511. countDown = (countDown - numMins) / 60;
  512. int numHours = (int) Math.floor(countDown % 24);
  513. int numDays = (int) Math.floor((countDown - numHours) / 24);
  514. _log.info("Olympiad System: In " + numDays + " days, " + numHours
  515. + " hours and " + numMins + " mins.");
  516. if (_period == 0)
  517. {
  518. _log.info("Olympiad System: Next Weekly Change is in....");
  519. milliToEnd = getMillisToWeekChange();
  520. double numSecs2 = (milliToEnd / 1000) % 60;
  521. double countDown2 = ((milliToEnd / 1000) - numSecs2) / 60;
  522. int numMins2 = (int) Math.floor(countDown2 % 60);
  523. countDown2 = (countDown2 - numMins2) / 60;
  524. int numHours2 = (int) Math.floor(countDown2 % 24);
  525. int numDays2 = (int) Math.floor((countDown2 - numHours2) / 24);
  526. _log.info("Olympiad System: " + numDays2 + " days, " + numHours2
  527. + " hours and " + numMins2 + " mins.");
  528. }
  529. }
  530. _log.info("Olympiad System: Loaded " + _nobles.size() + " Nobles");
  531. }
  532. protected void init()
  533. {
  534. if (_period == 1)
  535. return;
  536. _nonClassBasedRegisters = new L2FastList<L2PcInstance>();
  537. _classBasedRegisters = new FastMap<Integer, L2FastList<L2PcInstance>>();
  538. _compStart = Calendar.getInstance();
  539. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  540. _compStart.set(Calendar.MINUTE, COMP_MIN);
  541. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  542. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  543. public void run()
  544. {
  545. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  546. sm.addNumber(_currentCycle);
  547. Announcements.getInstance().announceToAll(sm);
  548. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  549. _isOlympiadEnd = true;
  550. if (_scheduledManagerTask != null)
  551. _scheduledManagerTask.cancel(true);
  552. if (_scheduledWeeklyTask != null)
  553. _scheduledWeeklyTask.cancel(true);
  554. Calendar validationEnd = Calendar.getInstance();
  555. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  556. saveNobleData();
  557. _period = 1;
  558. sortHerosToBe();
  559. giveHeroBonus();
  560. Hero.getInstance().computeNewHeroes(_heroesToBe);
  561. try {
  562. save();
  563. }
  564. catch (Exception e) {
  565. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  566. }
  567. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  568. public void run()
  569. {
  570. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  571. _period = 0;
  572. _currentCycle++;
  573. deleteNobles();
  574. setNewOlympiadEnd();
  575. init();
  576. }
  577. }, getMillisToValidationEnd());
  578. }
  579. }, getMillisToOlympiadEnd());
  580. updateCompStatus();
  581. scheduleWeeklyChange();
  582. }
  583. public boolean registerNoble(L2PcInstance noble, boolean classBased)
  584. {
  585. SystemMessage sm;
  586. /*
  587. if (_compStarted)
  588. {
  589. noble.sendMessage("Cant Register whilst competition is under way");
  590. return false;
  591. }
  592. */
  593. if (!_inCompPeriod)
  594. {
  595. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  596. noble.sendPacket(sm);
  597. return false;
  598. }
  599. if (noble.isCursedWeaponEquipped())
  600. {
  601. noble.sendMessage("You can't participate to Olympiad while holding a cursed weapon.");
  602. return false;
  603. }
  604. if (!noble.isNoble())
  605. {
  606. sm = new SystemMessage(SystemMessageId.ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  607. noble.sendPacket(sm);
  608. return false;
  609. }
  610. if (noble.getBaseClass() != noble.getClassId().getId())
  611. {
  612. sm = new SystemMessage(SystemMessageId.YOU_CANT_JOIN_THE_OLYMPIAD_WITH_A_SUB_JOB_CHARACTER);
  613. noble.sendPacket(sm);
  614. return false;
  615. }
  616. if (!_nobles.containsKey(noble.getObjectId()))
  617. {
  618. StatsSet statDat = new StatsSet();
  619. statDat.set(CLASS_ID, noble.getClassId().getId());
  620. statDat.set(CHAR_NAME, noble.getName());
  621. statDat.set(POINTS, DEFAULT_POINTS);
  622. statDat.set(COMP_DONE, 0);
  623. statDat.set("to_save", true);
  624. _nobles.put(noble.getObjectId(), statDat);
  625. }
  626. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  627. {
  628. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  629. for (L2PcInstance partecipant: classed)
  630. {
  631. if (partecipant.getObjectId()==noble.getObjectId())
  632. {
  633. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_TO_PARTICIPATE_IN_THE_GAME_FOR_YOUR_CLASS);
  634. noble.sendPacket(sm);
  635. return false;
  636. }
  637. }
  638. }
  639. for (L2PcInstance partecipant: _nonClassBasedRegisters)
  640. {
  641. if (partecipant.getObjectId()==noble.getObjectId())
  642. {
  643. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_FOR_ALL_CLASSES_WAITING_TO_PARTICIPATE_IN_THE_GAME);
  644. noble.sendPacket(sm);
  645. return false;
  646. }
  647. }
  648. for (L2OlympiadGame g : _manager.getOlympiadGames().values())
  649. {
  650. for(L2PcInstance player : g.getPlayers())
  651. {
  652. if(player.getObjectId() == noble.getObjectId())
  653. {
  654. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_FOR_ALL_CLASSES_WAITING_TO_PARTICIPATE_IN_THE_GAME);
  655. noble.sendPacket(sm);
  656. return false;
  657. }
  658. }
  659. }
  660. if (classBased && getNoblePoints(noble.getObjectId()) < 3)
  661. {
  662. noble.sendMessage("Cant register when you have less than 3 points");
  663. return false;
  664. }
  665. if (!classBased && getNoblePoints(noble.getObjectId()) < 5)
  666. {
  667. noble.sendMessage("Cant register when you have less than 5 points");
  668. return false;
  669. }
  670. if (classBased)
  671. {
  672. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  673. {
  674. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  675. classed.add(noble);
  676. _classBasedRegisters.remove(noble.getClassId().getId());
  677. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  678. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  679. noble.sendPacket(sm);
  680. }
  681. else
  682. {
  683. L2FastList<L2PcInstance> classed = new L2FastList<L2PcInstance>();
  684. classed.add(noble);
  685. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  686. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  687. noble.sendPacket(sm);
  688. }
  689. }
  690. else
  691. {
  692. _nonClassBasedRegisters.add(noble);
  693. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  694. noble.sendPacket(sm);
  695. }
  696. return true;
  697. }
  698. public boolean isRegistered(L2PcInstance noble)
  699. {
  700. if(_nonClassBasedRegisters == null) return false;
  701. if(_classBasedRegisters == null) return false;
  702. if (!_nonClassBasedRegisters.contains(noble))
  703. {
  704. if (!_classBasedRegisters.containsKey(noble.getClassId().getId()))
  705. {
  706. return false;
  707. }
  708. else
  709. {
  710. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  711. if (!classed.contains(noble))
  712. {
  713. return false;
  714. }
  715. }
  716. }
  717. return true;
  718. }
  719. public boolean unRegisterNoble(L2PcInstance noble)
  720. {
  721. SystemMessage sm;
  722. /*
  723. if (_compStarted)
  724. {
  725. noble.sendMessage("Cant Unregister whilst competition is under way");
  726. return false;
  727. }
  728. */
  729. if (!_inCompPeriod)
  730. {
  731. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  732. noble.sendPacket(sm);
  733. return false;
  734. }
  735. if (!noble.isNoble())
  736. {
  737. sm = new SystemMessage(SystemMessageId.ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  738. noble.sendPacket(sm);
  739. return false;
  740. }
  741. if (!isRegistered(noble))
  742. {
  743. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  744. noble.sendPacket(sm);
  745. return false;
  746. }
  747. if (_nonClassBasedRegisters.contains(noble))
  748. _nonClassBasedRegisters.remove(noble);
  749. else
  750. {
  751. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  752. classed.remove(noble);
  753. _classBasedRegisters.remove(noble.getClassId().getId());
  754. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  755. }
  756. for(L2OlympiadGame game: _manager.getOlympiadGames().values())
  757. {
  758. if(game._playerOne.getObjectId() == noble.getObjectId() || game._playerTwo.getObjectId() == noble.getObjectId())
  759. {
  760. noble.sendMessage("Cant Unregister whilst you are already selected for a game");
  761. return false;
  762. }
  763. }
  764. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  765. noble.sendPacket(sm);
  766. return true;
  767. }
  768. public void removeDisconnectedCompetitor(L2PcInstance player)
  769. {
  770. if (_manager == null || (_manager.getOlympiadInstance(player.getOlympiadGameId()) == null)) return;
  771. _manager.getOlympiadInstance(player.getOlympiadGameId()).handleDisconnect(player);
  772. }
  773. public void notifyCompetitorDamage(int objId, int damage, int gameId)
  774. {
  775. if (_manager.getOlympiadGames().get(gameId) == null) return;
  776. if (_manager.getOlympiadGames().get(gameId)._playerOneID == objId)
  777. _manager.getOlympiadGames().get(gameId)._damageP1 += damage;
  778. else _manager.getOlympiadGames().get(gameId)._damageP2 += damage;
  779. }
  780. private void updateCompStatus()
  781. {
  782. //_compStarted = false;
  783. synchronized(this)
  784. {
  785. long milliToStart = getMillisToCompBegin();
  786. double numSecs = (milliToStart / 1000) % 60;
  787. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  788. int numMins = (int) Math.floor(countDown % 60);
  789. countDown = (countDown - numMins) / 60;
  790. int numHours = (int) Math.floor(countDown % 24);
  791. int numDays = (int) Math.floor((countDown - numHours) / 24);
  792. _log.info("Olympiad System: Competition Period Starts in "
  793. + numDays + " days, " + numHours
  794. + " hours and " + numMins + " mins.");
  795. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  796. }
  797. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  798. public void run()
  799. {
  800. if (isOlympiadEnd())
  801. return;
  802. _inCompPeriod = true;
  803. OlympiadManager om = new OlympiadManager();
  804. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  805. _log.info("Olympiad System: Olympiad Game Started");
  806. Thread olyCycle = new Thread(om);
  807. olyCycle.start();
  808. //_scheduledManagerTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(om, INITIAL_WAIT, BATTLE_WAIT);
  809. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  810. public void run()
  811. {
  812. if (isOlympiadEnd())
  813. return;
  814. //_scheduledManagerTask.cancel(true);
  815. _inCompPeriod = false;
  816. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  817. _log.info("Olympiad System: Olympiad Game Ended");
  818. try {
  819. while(_battleStarted){
  820. try{
  821. //wait 1 minutes for end of pendings games
  822. Thread.sleep(60000);
  823. }catch (InterruptedException e){}
  824. }
  825. save();
  826. }
  827. catch (Exception e) {
  828. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  829. }
  830. init();
  831. }
  832. }, getMillisToCompEnd());
  833. }
  834. }, getMillisToCompBegin());
  835. }
  836. private long getMillisToOlympiadEnd()
  837. {
  838. //if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  839. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  840. //return 10L;
  841. }
  842. public void manualSelectHeroes()
  843. {
  844. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  845. sm.addNumber(_currentCycle);
  846. Announcements.getInstance().announceToAll(sm);
  847. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  848. _isOlympiadEnd = true;
  849. if (_scheduledManagerTask != null)
  850. _scheduledManagerTask.cancel(true);
  851. if (_scheduledWeeklyTask != null)
  852. _scheduledWeeklyTask.cancel(true);
  853. if(_scheduledOlympiadEnd != null)
  854. _scheduledOlympiadEnd.cancel(true);
  855. Calendar validationEnd = Calendar.getInstance();
  856. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  857. saveNobleData();
  858. _period = 1;
  859. sortHerosToBe();
  860. giveHeroBonus();
  861. Hero.getInstance().computeNewHeroes(_heroesToBe);
  862. try {
  863. save();
  864. }
  865. catch (Exception e) {
  866. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  867. }
  868. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  869. public void run()
  870. {
  871. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  872. _period = 0;
  873. _currentCycle++;
  874. deleteNobles();
  875. setNewOlympiadEnd();
  876. init();
  877. }
  878. }, getMillisToValidationEnd());
  879. }
  880. protected long getMillisToValidationEnd()
  881. {
  882. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  883. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  884. return 10L;
  885. }
  886. public boolean isOlympiadEnd()
  887. {
  888. return _isOlympiadEnd;
  889. }
  890. protected void setNewOlympiadEnd()
  891. {
  892. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  893. sm.addNumber(_currentCycle);
  894. Announcements.getInstance().announceToAll(sm);
  895. Calendar currentTime = Calendar.getInstance();
  896. currentTime.add(Calendar.MONTH, 1);
  897. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  898. currentTime.set(Calendar.AM_PM, Calendar.AM);
  899. currentTime.set(Calendar.HOUR, 12);
  900. currentTime.set(Calendar.MINUTE, 0);
  901. currentTime.set(Calendar.SECOND, 0);
  902. _olympiadEnd = currentTime.getTimeInMillis();
  903. Calendar nextChange = Calendar.getInstance();
  904. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  905. _isOlympiadEnd = false;
  906. }
  907. public boolean inCompPeriod()
  908. {
  909. return _inCompPeriod;
  910. }
  911. private long getMillisToCompBegin()
  912. {
  913. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() &&
  914. _compEnd > Calendar.getInstance().getTimeInMillis())
  915. return 10L;
  916. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  917. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  918. return setNewCompBegin();
  919. }
  920. private long setNewCompBegin()
  921. {
  922. _compStart = Calendar.getInstance();
  923. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  924. _compStart.set(Calendar.MINUTE, COMP_MIN);
  925. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  926. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  927. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  928. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  929. }
  930. protected long getMillisToCompEnd()
  931. {
  932. //if (_compEnd > Calendar.getInstance().getTimeInMillis())
  933. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  934. //return 10L;
  935. }
  936. private long getMillisToWeekChange()
  937. {
  938. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  939. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  940. return 10L;
  941. }
  942. private void scheduleWeeklyChange()
  943. {
  944. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  945. public void run()
  946. {
  947. addWeeklyPoints();
  948. _log.info("Olympiad System: Added weekly points to nobles");
  949. Calendar nextChange = Calendar.getInstance();
  950. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  951. }
  952. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  953. }
  954. protected synchronized void addWeeklyPoints()
  955. {
  956. if (_period == 1)
  957. return;
  958. for (Integer nobleId : _nobles.keySet())
  959. {
  960. StatsSet nobleInfo = _nobles.get(nobleId);
  961. int currentPoints = nobleInfo.getInteger(POINTS);
  962. currentPoints += WEEKLY_POINTS;
  963. nobleInfo.set(POINTS, currentPoints);
  964. _nobles.remove(nobleId);
  965. _nobles.put(nobleId, nobleInfo);
  966. }
  967. }
  968. public String[] getMatchList()
  969. {
  970. return (_manager == null)? null : _manager.getAllTitles();
  971. }
  972. // returns the players for the given olympiad game Id
  973. public L2PcInstance[] getPlayers(int Id)
  974. {
  975. if (_manager == null || (_manager.getOlympiadInstance(Id) == null))
  976. {
  977. return null;
  978. }
  979. L2PcInstance[] players = _manager.getOlympiadInstance(Id).getPlayers();
  980. return players;
  981. }
  982. public int getCurrentCycle()
  983. {
  984. return _currentCycle;
  985. }
  986. public void addSpectator(int id, L2PcInstance spectator)
  987. {
  988. for(L2PcInstance player : _nonClassBasedRegisters)
  989. {
  990. if(spectator.getObjectId() == player.getObjectId())
  991. {
  992. spectator.sendMessage("You are already registered for a competition");
  993. return;
  994. }
  995. }
  996. for(L2FastList<L2PcInstance> list : _classBasedRegisters.values())
  997. {
  998. for(L2PcInstance player : list)
  999. {
  1000. if(spectator.getObjectId() == player.getObjectId())
  1001. {
  1002. spectator.sendMessage("You are already registered for a competition");
  1003. return;
  1004. }
  1005. }
  1006. }
  1007. if(spectator.getOlympiadGameId() != -1)
  1008. {
  1009. spectator.sendMessage("You are already registered for a competition");
  1010. return;
  1011. }
  1012. if (_manager == null || (_manager.getOlympiadInstance(id) == null))
  1013. {
  1014. spectator.sendPacket(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS));
  1015. return;
  1016. }
  1017. L2PcInstance[] players = _manager.getOlympiadInstance(id).getPlayers();
  1018. if (players == null) return;
  1019. spectator.enterOlympiadObserverMode(STADIUMS[id].getCoordinates()[0], STADIUMS[id].getCoordinates()[1], STADIUMS[id].getCoordinates()[2], id);
  1020. _manager.getOlympiadInstance(id).addSpectator(spectator);
  1021. }
  1022. public void UnSpawnBuffer(L2Spawn spawn)
  1023. {
  1024. spawn.getLastSpawn().decayMe();
  1025. }
  1026. public void removeSpectator(int id, L2PcInstance spectator)
  1027. {
  1028. if (_manager == null || (_manager.getOlympiadInstance(id) == null)) return;
  1029. _manager.getOlympiadInstance(id).removeSpectator(spectator);
  1030. }
  1031. public L2FastList<L2PcInstance> getSpectators(int id)
  1032. {
  1033. if (_manager == null || _manager.getOlympiadInstance(id) == null)
  1034. return null;
  1035. return _manager.getOlympiadInstance(id).getSpectators();
  1036. }
  1037. public Map<Integer, L2OlympiadGame> getOlympiadGames()
  1038. {
  1039. return _manager.getOlympiadGames();
  1040. }
  1041. public boolean playerInStadia(L2PcInstance player)
  1042. {
  1043. return (OlympiadStadiaManager.getInstance().getStadium(player) != null);
  1044. }
  1045. public int[] getWaitingList()
  1046. {
  1047. int[] array = new int[2];
  1048. if (!inCompPeriod())
  1049. return null;
  1050. int classCount = 0;
  1051. if (_classBasedRegisters.size() != 0)
  1052. for (L2FastList<L2PcInstance> classed : _classBasedRegisters.values())
  1053. {
  1054. classCount += classed.size();
  1055. }
  1056. array[0] = classCount;
  1057. array[1] = _nonClassBasedRegisters.size();
  1058. return array;
  1059. }
  1060. protected synchronized void saveNobleData()
  1061. {
  1062. Connection con = null;
  1063. if (_nobles == null)
  1064. return;
  1065. try
  1066. {
  1067. con = L2DatabaseFactory.getInstance().getConnection();
  1068. PreparedStatement statement;
  1069. for (Integer nobleId : _nobles.keySet())
  1070. {
  1071. StatsSet nobleInfo = _nobles.get(nobleId);
  1072. int charId = nobleId;
  1073. int classId = nobleInfo.getInteger(CLASS_ID);
  1074. String charName = nobleInfo.getString(CHAR_NAME);
  1075. int points = nobleInfo.getInteger(POINTS);
  1076. int compDone = nobleInfo.getInteger(COMP_DONE);
  1077. boolean toSave = nobleInfo.getBool("to_save");
  1078. if (toSave)
  1079. {
  1080. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  1081. statement.setInt(1, charId);
  1082. statement.setInt(2, classId);
  1083. statement.setString(3, charName);
  1084. statement.setInt(4, points);
  1085. statement.setInt(5, compDone);
  1086. statement.execute();
  1087. statement.close();
  1088. nobleInfo.set("to_save", false);
  1089. _nobles.remove(nobleId);
  1090. _nobles.put(nobleId, nobleInfo);
  1091. }
  1092. else
  1093. {
  1094. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  1095. statement.setInt(1, points);
  1096. statement.setInt(2, compDone);
  1097. statement.setInt(3, charId);
  1098. statement.execute();
  1099. statement.close();
  1100. }
  1101. }
  1102. }
  1103. catch(SQLException e) {_log.warning("Olympiad System: Couldnt save nobles info in db");}
  1104. finally
  1105. {
  1106. try{con.close();}catch(Exception e){e.printStackTrace();}
  1107. }
  1108. }
  1109. protected void sortHerosToBe()
  1110. {
  1111. if (_period != 1) return;
  1112. _heroesToBe = new L2FastList<StatsSet>();
  1113. Connection con = null;
  1114. try
  1115. {
  1116. con = L2DatabaseFactory.getInstance().getConnection();
  1117. PreparedStatement statement;
  1118. ResultSet rset;
  1119. StatsSet hero;
  1120. for (int i = 0; i < HERO_IDS.length; i++)
  1121. {
  1122. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  1123. statement.setInt(1, HERO_IDS[i]);
  1124. rset = statement.executeQuery();
  1125. if (rset.next())
  1126. {
  1127. hero = new StatsSet();
  1128. hero.set(CLASS_ID, HERO_IDS[i]);
  1129. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1130. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  1131. _heroesToBe.add(hero);
  1132. }
  1133. statement.close();
  1134. rset.close();
  1135. }
  1136. }
  1137. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  1138. finally
  1139. {
  1140. try{con.close();}catch(Exception e){e.printStackTrace();}
  1141. }
  1142. }
  1143. public L2FastList<String> getClassLeaderBoard(int classId)
  1144. {
  1145. //if (_period != 1) return;
  1146. L2FastList<String> names = new L2FastList<String>();
  1147. Connection con = null;
  1148. try
  1149. {
  1150. con = L2DatabaseFactory.getInstance().getConnection();
  1151. PreparedStatement statement;
  1152. ResultSet rset;
  1153. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  1154. statement.setInt(1, classId);
  1155. rset = statement.executeQuery();
  1156. while (rset.next())
  1157. {
  1158. names.add(rset.getString(CHAR_NAME));
  1159. }
  1160. statement.close();
  1161. rset.close();
  1162. return names;
  1163. }
  1164. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  1165. finally
  1166. {
  1167. try{con.close();}catch(Exception e){e.printStackTrace();}
  1168. }
  1169. return names;
  1170. }
  1171. protected void giveHeroBonus()
  1172. {
  1173. if (_heroesToBe.size() == 0)
  1174. return;
  1175. for (StatsSet hero : _heroesToBe)
  1176. {
  1177. int charId = hero.getInteger(CHAR_ID);
  1178. StatsSet noble = _nobles.get(charId);
  1179. int currentPoints = noble.getInteger(POINTS);
  1180. currentPoints += Config.ALT_OLY_HERO_POINTS;
  1181. noble.set(POINTS, currentPoints);
  1182. _nobles.remove(charId);
  1183. _nobles.put(charId, noble);
  1184. }
  1185. }
  1186. public int getNoblessePasses(int objId)
  1187. {
  1188. if (_period != 1 || _nobles.size() == 0)
  1189. return 0;
  1190. StatsSet noble = _nobles.get(objId);
  1191. if (noble == null)
  1192. return 0;
  1193. int points = noble.getInteger(POINTS);
  1194. if (points <= Config.ALT_OLY_MIN_POINT_FOR_EXCH)
  1195. return 0;
  1196. noble.set(POINTS, 0);
  1197. _nobles.remove(objId);
  1198. _nobles.put(objId, noble);
  1199. points *= Config.ALT_OLY_GP_PER_POINT;
  1200. return points;
  1201. }
  1202. public boolean isRegisteredInComp(L2PcInstance player)
  1203. {
  1204. boolean result = false;
  1205. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(player))
  1206. result = true;
  1207. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(player.getClassId().getId()))
  1208. {
  1209. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  1210. if (classed.contains(player))
  1211. result = true;
  1212. }
  1213. if (!_inCompPeriod){}
  1214. else {
  1215. for(L2OlympiadGame game: _manager.getOlympiadGames().values())
  1216. {
  1217. if(game._playerOne.getObjectId() == player.getObjectId() || game._playerTwo.getObjectId() == player.getObjectId())
  1218. {
  1219. result = true;
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. return result;
  1225. }
  1226. public int getNoblePoints(int objId)
  1227. {
  1228. if (_nobles.size() == 0)
  1229. return 0;
  1230. StatsSet noble = _nobles.get(objId);
  1231. if (noble == null)
  1232. return 0;
  1233. int points = noble.getInteger(POINTS);
  1234. return points;
  1235. }
  1236. public int getCompetitionDone(int objId)
  1237. {
  1238. if (_nobles.size() == 0)
  1239. return 0;
  1240. StatsSet noble = _nobles.get(objId);
  1241. if (noble == null)
  1242. return 0;
  1243. int points = noble.getInteger(COMP_DONE);
  1244. return points;
  1245. }
  1246. protected void deleteNobles()
  1247. {
  1248. Connection con = null;
  1249. try
  1250. {
  1251. con = L2DatabaseFactory.getInstance().getConnection();
  1252. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  1253. statement.execute();
  1254. statement.close();
  1255. }
  1256. catch(SQLException e){_log.warning("Olympiad System: Couldnt delete nobles from db");}
  1257. finally
  1258. {
  1259. try{con.close();}catch(Exception e){e.printStackTrace();}
  1260. }
  1261. _nobles.clear();
  1262. }
  1263. public void save() throws IOException
  1264. {
  1265. saveNobleData();
  1266. Properties OlympiadProperties = new Properties();
  1267. FileOutputStream fos = new FileOutputStream(new File(Config.DATAPACK_ROOT, OLYMPIAD_DATA_FILE));
  1268. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  1269. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  1270. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  1271. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  1272. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  1273. OlympiadProperties.store(fos, "Olympiad Properties");
  1274. fos.close();
  1275. }
  1276. private class OlympiadManager implements Runnable
  1277. {
  1278. private Map<Integer, L2OlympiadGame> _olympiadInstances;
  1279. public OlympiadManager()
  1280. {
  1281. _olympiadInstances = new FastMap<Integer, L2OlympiadGame>();
  1282. _manager = this;
  1283. }
  1284. public synchronized void run()
  1285. {
  1286. _cycleTerminated = false;
  1287. if (isOlympiadEnd())
  1288. {
  1289. _scheduledManagerTask.cancel(true);
  1290. _cycleTerminated = true;
  1291. return;
  1292. }
  1293. Map<Integer, OlympiadGameTask> _gamesQueue = new FastMap<Integer, OlympiadGameTask>();
  1294. while(inCompPeriod())
  1295. {
  1296. if (_nobles.size() == 0)
  1297. {
  1298. try{
  1299. wait(60000);
  1300. }catch(InterruptedException ex){}
  1301. continue;
  1302. }
  1303. //_compStarted = true;
  1304. int classBasedPgCount = 0;
  1305. for(L2FastList<L2PcInstance> classList : _classBasedRegisters.values())
  1306. classBasedPgCount += classList.size();
  1307. while((_gamesQueue.size() > 0 || classBasedPgCount >= Config.ALT_OLY_CLASSED || _nonClassBasedRegisters.size() >= Config.ALT_OLY_NONCLASSED) && inCompPeriod())
  1308. {
  1309. //first cycle do nothing
  1310. int _gamesQueueSize = 0;
  1311. _gamesQueueSize = _gamesQueue.size();
  1312. for(int i=0; i<_gamesQueueSize;i++)
  1313. {
  1314. if(_gamesQueue.get(i) == null || _gamesQueue.get(i).isTerminated() || _gamesQueue.get(i)._game == null)
  1315. {
  1316. if(_gamesQueue.containsKey(i)) {
  1317. //removes terminated games from the queue
  1318. try {
  1319. _olympiadInstances.remove(i);
  1320. _gamesQueue.remove(i);
  1321. STADIUMS[i].setStadiaFree();
  1322. } catch(Exception e) {e.printStackTrace();}
  1323. } else {
  1324. _gamesQueueSize = _gamesQueueSize+1;
  1325. }
  1326. }else if(_gamesQueue.get(i) != null && !_gamesQueue.get(i).isStarted())
  1327. {
  1328. //start new games
  1329. Thread T = new Thread(_gamesQueue.get(i));
  1330. T.start();
  1331. }
  1332. }
  1333. //set up the games queue
  1334. for(int i=0; i<STADIUMS.length; i++)
  1335. {
  1336. if(!existNextOpponents(_nonClassBasedRegisters) && !existNextOpponents(getRandomClassList(_classBasedRegisters)))
  1337. {
  1338. break;
  1339. }
  1340. if(STADIUMS[i].isFreeToUse())
  1341. {
  1342. if (existNextOpponents(_nonClassBasedRegisters))
  1343. {
  1344. try{
  1345. _olympiadInstances.put(i, new L2OlympiadGame(i, COMP_TYPE.NON_CLASSED, nextOpponents(_nonClassBasedRegisters), STADIUMS[i].getCoordinates()));
  1346. _gamesQueue.put(i,new OlympiadGameTask(_olympiadInstances.get(i)));
  1347. STADIUMS[i].setStadiaBusy();
  1348. }catch(Exception ex)
  1349. {
  1350. if(_olympiadInstances.get(i) != null){
  1351. for(L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  1352. {
  1353. player.sendMessage("Your olympiad registration was canceled due to an error");
  1354. player.setIsInOlympiadMode(false);
  1355. player.setIsOlympiadStart(false);
  1356. player.setOlympiadSide(-1);
  1357. player.setOlympiadGameId(-1);
  1358. }
  1359. _olympiadInstances.remove(i);
  1360. }
  1361. if(_gamesQueue.get(i)!= null)
  1362. _gamesQueue.remove(i);
  1363. STADIUMS[i].setStadiaFree();
  1364. //try to reuse this stadia next time
  1365. i--;
  1366. }
  1367. }
  1368. else if (existNextOpponents(getRandomClassList(_classBasedRegisters)))
  1369. {
  1370. try{
  1371. _olympiadInstances.put(i, new L2OlympiadGame(i, COMP_TYPE.CLASSED, nextOpponents(getRandomClassList(_classBasedRegisters)), STADIUMS[i].getCoordinates()));
  1372. _gamesQueue.put(i,new OlympiadGameTask(_olympiadInstances.get(i)));
  1373. STADIUMS[i].setStadiaBusy();
  1374. }catch(Exception ex)
  1375. {
  1376. if(_olympiadInstances.get(i) != null){
  1377. for(L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  1378. {
  1379. player.sendMessage("Your olympiad registration was canceled due to an error");
  1380. player.setIsInOlympiadMode(false);
  1381. player.setIsOlympiadStart(false);
  1382. player.setOlympiadSide(-1);
  1383. player.setOlympiadGameId(-1);
  1384. }
  1385. _olympiadInstances.remove(i);
  1386. }
  1387. if(_gamesQueue.get(i)!= null)
  1388. _gamesQueue.remove(i);
  1389. STADIUMS[i].setStadiaFree();
  1390. //try to reuse this stadia next time
  1391. i--;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. //wait 30 sec for !stress the server
  1397. try{
  1398. wait(30000);
  1399. }catch (InterruptedException e){}
  1400. }
  1401. //wait 30 sec for !stress the server
  1402. try{
  1403. wait(30000);
  1404. }catch (InterruptedException e){}
  1405. }
  1406. //when comp time finish wait for all games terminated before execute the cleanup code
  1407. boolean allGamesTerminated = false;
  1408. //wait for all games terminated
  1409. while(!allGamesTerminated)
  1410. {
  1411. try{
  1412. wait(30000);
  1413. }catch (InterruptedException e){}
  1414. if(_gamesQueue.size() == 0)
  1415. {
  1416. allGamesTerminated = true;
  1417. }else{
  1418. for(OlympiadGameTask game : _gamesQueue.values())
  1419. {
  1420. allGamesTerminated = allGamesTerminated || game.isTerminated();
  1421. }
  1422. }
  1423. }
  1424. _cycleTerminated = true;
  1425. //when all games terminated clear all
  1426. _gamesQueue.clear();
  1427. /*_classBasedParticipants.clear();
  1428. _nonClassBasedParticipants.clear();*/
  1429. //Wait 20 seconds
  1430. _olympiadInstances.clear();
  1431. _classBasedRegisters.clear();
  1432. _nonClassBasedRegisters.clear();
  1433. _battleStarted = false;
  1434. //_compStarted = false;
  1435. }
  1436. protected L2OlympiadGame getOlympiadInstance(int index)
  1437. {
  1438. if (_olympiadInstances != null && _olympiadInstances.size() > 0)
  1439. {
  1440. return _olympiadInstances.get(index);
  1441. }
  1442. return null;
  1443. }
  1444. protected Map<Integer, L2OlympiadGame> getOlympiadGames()
  1445. {
  1446. return (_olympiadInstances == null)? null : _olympiadInstances;
  1447. }
  1448. private L2FastList<L2PcInstance> getRandomClassList(Map<Integer, L2FastList<L2PcInstance>> list)
  1449. {
  1450. if(list.size() == 0)
  1451. return null;
  1452. Map<Integer, L2FastList<L2PcInstance>> tmp = new FastMap<Integer, L2FastList<L2PcInstance>>();
  1453. int tmpIndex = 0;
  1454. for(L2FastList<L2PcInstance> l : list.values())
  1455. {
  1456. tmp.put(tmpIndex, l);
  1457. tmpIndex ++;
  1458. }
  1459. L2FastList<L2PcInstance> rndList = new L2FastList<L2PcInstance>();
  1460. int classIndex = 0;
  1461. if(tmp.size() == 1)
  1462. classIndex = 0;
  1463. else
  1464. classIndex = Rnd.nextInt(tmp.size());
  1465. rndList = tmp.get(classIndex);
  1466. return rndList;
  1467. }
  1468. private L2FastList<L2PcInstance> nextOpponents(L2FastList<L2PcInstance> list)
  1469. {
  1470. L2FastList<L2PcInstance> opponents = new L2FastList<L2PcInstance>();
  1471. if (list.size() == 0)
  1472. return opponents;
  1473. int loopCount = (list.size() / 2);
  1474. int first;
  1475. int second;
  1476. if (loopCount < 1)
  1477. return opponents;
  1478. first = Rnd.nextInt(list.size());
  1479. opponents.add(list.get(first));
  1480. list.remove(first);
  1481. second = Rnd.nextInt(list.size());
  1482. opponents.add(list.get(second));
  1483. list.remove(second);
  1484. return opponents;
  1485. }
  1486. private boolean existNextOpponents(L2FastList<L2PcInstance> list)
  1487. {
  1488. if(list == null)
  1489. return false;
  1490. if (list.size() == 0)
  1491. return false;
  1492. int loopCount = list.size() >> 1;
  1493. if (loopCount < 1)
  1494. return false;
  1495. else
  1496. return true;
  1497. }
  1498. protected String[] getAllTitles()
  1499. {
  1500. /*if(!_compStarted)
  1501. return null;*/
  1502. String[] msg = new String[_olympiadInstances.size()];
  1503. int count = 0;
  1504. int match = 1;
  1505. int showbattle = 0;
  1506. for (L2OlympiadGame instance : _olympiadInstances.values())
  1507. {
  1508. if (instance._gamestarted == true) { showbattle = 1; } else { showbattle = 0; }
  1509. msg[count] = "<"+showbattle+"><"+instance._stadiumID+"> In Progress " + instance.getTitle();
  1510. count++;
  1511. match++;
  1512. }
  1513. return msg;
  1514. }
  1515. }
  1516. private class L2OlympiadGame
  1517. {
  1518. protected COMP_TYPE _type;
  1519. public boolean _aborted;
  1520. public boolean _gamestarted;
  1521. public boolean _playerOneDisconnected;
  1522. public boolean _playerTwoDisconnected;
  1523. public String _playerOneName;
  1524. public String _playerTwoName;
  1525. public int _playerOneID = 0;
  1526. public int _playerTwoID = 0;
  1527. public static final int OLY_BUFFER = 36402;
  1528. public int _damageP1 = 0;
  1529. public int _damageP2 = 0;
  1530. public L2PcInstance _playerOne;
  1531. public L2PcInstance _playerTwo;
  1532. public L2Spawn _spawnOne;
  1533. public L2Spawn _spawnTwo;
  1534. private L2FastList<L2PcInstance> _players;
  1535. private int[] _stadiumPort;
  1536. private int x1, y1, z1, x2, y2, z2;
  1537. public int _stadiumID;
  1538. public L2FastList<L2PcInstance> _spectators;
  1539. private SystemMessage _sm;
  1540. private SystemMessage _sm2;
  1541. private SystemMessage _sm3;
  1542. protected L2OlympiadGame(int id, COMP_TYPE type, L2FastList<L2PcInstance> list, int[] stadiumPort)
  1543. {
  1544. _aborted = false;
  1545. _gamestarted = false;
  1546. _stadiumID = id;
  1547. _playerOneDisconnected = false;
  1548. _playerTwoDisconnected = false;
  1549. _type = type;
  1550. _stadiumPort = stadiumPort;
  1551. _spectators = new L2FastList<L2PcInstance>();
  1552. if (list != null)
  1553. {
  1554. _players = list;
  1555. _playerOne = list.get(0);
  1556. _playerTwo = list.get(1);
  1557. try {
  1558. _playerOneName = _playerOne.getName();
  1559. _playerTwoName = _playerTwo.getName();
  1560. _playerOne.setOlympiadGameId(id);
  1561. _playerTwo.setOlympiadGameId(id);
  1562. _playerOneID = _playerOne.getObjectId();
  1563. _playerTwoID = _playerTwo.getObjectId();
  1564. }
  1565. catch (Exception e) {
  1566. _aborted = true;
  1567. clearPlayers();
  1568. }
  1569. if (Config.DEBUG)
  1570. _log.info("Olympiad System: Game - " + id + ": " + _playerOne.getName() + " Vs " + _playerTwo.getName());
  1571. }
  1572. else {
  1573. _aborted = true;
  1574. clearPlayers();
  1575. return;
  1576. }
  1577. }
  1578. public boolean isAborted()
  1579. {
  1580. return _aborted;
  1581. }
  1582. protected void clearPlayers()
  1583. {
  1584. _playerOne = null;
  1585. _playerTwo = null;
  1586. _players = null;
  1587. _playerOneName = "";
  1588. _playerTwoName = "";
  1589. _playerOneID = 0;
  1590. _playerTwoID = 0;
  1591. }
  1592. protected void handleDisconnect(L2PcInstance player)
  1593. {
  1594. if (player == _playerOne)
  1595. _playerOneDisconnected = true;
  1596. else if (player == _playerTwo)
  1597. _playerTwoDisconnected = true;
  1598. }
  1599. public void openDoors()
  1600. {
  1601. _doorTable = DoorTable.getInstance();
  1602. try {
  1603. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[0]).openMe();
  1604. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[1]).openMe();
  1605. //_log.info("open doors"+STADIUMS[_stadiumID].getDoorID()[0]+"<>"+STADIUMS[_stadiumID].getDoorID()[1]);
  1606. } catch (Exception e) {}
  1607. }
  1608. public void closeDoors()
  1609. {
  1610. _doorTable = DoorTable.getInstance();
  1611. try {
  1612. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[0]).closeMe();
  1613. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[1]).closeMe();
  1614. //_log.info("close doors"+STADIUMS[_stadiumID].getDoorID()[0]+"<>"+STADIUMS[_stadiumID].getDoorID()[1]);
  1615. } catch (Exception e) {}
  1616. }
  1617. public L2Spawn SpawnBuffer(int xPos, int yPos, int zPos, int npcId)
  1618. {
  1619. L2NpcTemplate template;
  1620. template = NpcTable.getInstance().getTemplate(npcId);
  1621. try {
  1622. L2Spawn spawn = new L2Spawn(template);
  1623. spawn.setLocx(xPos);
  1624. spawn.setLocy(yPos);
  1625. spawn.setLocz(zPos);
  1626. spawn.setAmount(1);
  1627. spawn.setHeading(0);
  1628. spawn.setRespawnDelay(1);
  1629. SpawnTable.getInstance().addNewSpawn(spawn, false);
  1630. spawn.init();
  1631. return spawn;
  1632. } catch (Exception e) { return null; }
  1633. }
  1634. protected void removals()
  1635. {
  1636. if (_aborted) return;
  1637. if (_playerOne == null || _playerTwo == null) return;
  1638. if (_playerOneDisconnected || _playerTwoDisconnected) return;
  1639. for (L2PcInstance player : _players)
  1640. {
  1641. try{
  1642. //Remove Clan Skills
  1643. if (player.getClan() != null)
  1644. {
  1645. for(L2Skill skill: player.getClan().getAllSkills())
  1646. player.removeSkill(skill,false);
  1647. }
  1648. //Abort casting if player casting
  1649. if (player.isCastingNow())
  1650. {
  1651. player.abortCast();
  1652. }
  1653. //Remove Hero Skills
  1654. if (player.isHero())
  1655. {
  1656. for(L2Skill skill: HeroSkillTable.getHeroSkills())
  1657. player.removeSkill(skill,false);
  1658. }
  1659. // Heal Player fully
  1660. player.setCurrentCp(player.getMaxCp());
  1661. player.setCurrentHp(player.getMaxHp());
  1662. player.setCurrentMp(player.getMaxMp());
  1663. //Remove Buffs
  1664. player.stopAllEffects();
  1665. //Remove Summon's Buffs
  1666. if (player.getPet() != null)
  1667. {
  1668. L2Summon summon = player.getPet();
  1669. summon.stopAllEffects();
  1670. if (summon instanceof L2PetInstance)
  1671. summon.unSummon(player);
  1672. }
  1673. /*if (player.getCubics() != null)
  1674. {
  1675. for(L2CubicInstance cubic : player.getCubics().values())
  1676. {
  1677. cubic.stopAction();
  1678. player.delCubic(cubic.getId());
  1679. }
  1680. player.getCubics().clear();
  1681. }*/
  1682. //Remove player from his party
  1683. if (player.getParty() != null)
  1684. {
  1685. L2Party party = player.getParty();
  1686. party.removePartyMember(player);
  1687. }
  1688. //Remove Hero Weapons
  1689. // check to prevent the using of weapon/shield on strider/wyvern
  1690. L2ItemInstance wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  1691. if (wpn == null) wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LRHAND);
  1692. if (wpn != null && (wpn.isHeroItem() || wpn.isOlyRestrictedItem()))
  1693. {
  1694. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(wpn.getItem().getBodyPart());
  1695. InventoryUpdate iu = new InventoryUpdate();
  1696. for (int i = 0; i < unequiped.length; i++)
  1697. iu.addModifiedItem(unequiped[i]);
  1698. player.sendPacket(iu);
  1699. player.abortAttack();
  1700. player.broadcastUserInfo();
  1701. // this can be 0 if the user pressed the right mouse button twice very fast
  1702. if (unequiped.length > 0)
  1703. {
  1704. if (unequiped[0].isWear())
  1705. return;
  1706. SystemMessage sm = null;
  1707. if (unequiped[0].getEnchantLevel() > 0){
  1708. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  1709. sm.addNumber(unequiped[0].getEnchantLevel());
  1710. sm.addItemName(unequiped[0]);
  1711. }else{
  1712. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  1713. sm.addItemName(unequiped[0]);
  1714. }
  1715. player.sendPacket(sm);
  1716. }
  1717. }
  1718. //Remove shot automation
  1719. Map<Integer, Integer> activeSoulShots = player.getAutoSoulShot();
  1720. for (int itemId : activeSoulShots.values())
  1721. {
  1722. player.removeAutoSoulShot(itemId);
  1723. ExAutoSoulShot atk = new ExAutoSoulShot(itemId, 0);
  1724. player.sendPacket(atk);
  1725. }
  1726. player.sendSkillList();
  1727. }
  1728. catch (Exception e) {}
  1729. }
  1730. }
  1731. protected boolean portPlayersToArena()
  1732. {
  1733. boolean _playerOneCrash = (_playerOne == null || _playerOneDisconnected);
  1734. boolean _playerTwoCrash = (_playerTwo == null || _playerTwoDisconnected);
  1735. if (_playerOneCrash || _playerTwoCrash || _aborted)
  1736. {
  1737. _playerOne=null;
  1738. _playerTwo=null;
  1739. _aborted = true;
  1740. return false;
  1741. }
  1742. try {
  1743. x1 = _playerOne.getX();
  1744. y1 = _playerOne.getY();
  1745. z1 = _playerOne.getZ();
  1746. x2 = _playerTwo.getX();
  1747. y2 = _playerTwo.getY();
  1748. z2 = _playerTwo.getZ();
  1749. if (_playerOne.isSitting())
  1750. _playerOne.standUp();
  1751. if (_playerTwo.isSitting())
  1752. _playerTwo.standUp();
  1753. _playerOne.setTarget(null);
  1754. _playerTwo.setTarget(null);
  1755. _playerOne.teleToLocation(_stadiumPort[0]+1200, _stadiumPort[1], _stadiumPort[2], false);
  1756. _playerTwo.teleToLocation(_stadiumPort[0]-1200, _stadiumPort[1], _stadiumPort[2], false);
  1757. _playerOne.sendPacket(new ExOlympiadMode(2));
  1758. _playerTwo.sendPacket(new ExOlympiadMode(2));
  1759. _spawnOne = SpawnBuffer(_stadiumPort[0]+1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  1760. _spawnTwo = SpawnBuffer(_stadiumPort[0]-1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  1761. _playerOne.setIsInOlympiadMode(true);
  1762. _playerOne.setIsOlympiadStart(false);
  1763. _playerOne.setOlympiadSide(1);
  1764. _playerOne.olyBuff = 5;
  1765. _playerTwo.setIsInOlympiadMode(true);
  1766. _playerTwo.setIsOlympiadStart(false);
  1767. _playerTwo.setOlympiadSide(2);
  1768. _playerTwo.olyBuff = 5;
  1769. _gamestarted = true;
  1770. } catch (NullPointerException e)
  1771. {
  1772. return false;
  1773. }
  1774. return true;
  1775. }
  1776. protected void sendMessageToPlayers(boolean toBattleBegin, int nsecond)
  1777. {
  1778. if(!toBattleBegin)
  1779. _sm = new SystemMessage(SystemMessageId.YOU_WILL_ENTER_THE_OLYMPIAD_STADIUM_IN_S1_SECOND_S);
  1780. else
  1781. _sm = new SystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  1782. _sm.addNumber(nsecond);
  1783. try {
  1784. for (L2PcInstance player : _players)
  1785. player.sendPacket(_sm);
  1786. } catch (Exception e) {}
  1787. }
  1788. protected void portPlayersBack()
  1789. {
  1790. _playerOne.teleToLocation(x1, y1, z1, true);
  1791. _playerTwo.teleToLocation(x2, y2, z2, true);
  1792. }
  1793. protected void PlayersStatusBack()
  1794. {
  1795. for (L2PcInstance player : _players)
  1796. {
  1797. try {
  1798. if(player.isDead() == true) { player.setIsDead(false); }
  1799. player.getStatus().startHpMpRegeneration();
  1800. player.setCurrentCp(player.getMaxCp());
  1801. player.setCurrentHp(player.getMaxHp());
  1802. player.setCurrentMp(player.getMaxMp());
  1803. player.setIsInOlympiadMode(false);
  1804. player.setIsOlympiadStart(false);
  1805. player.setOlympiadSide(-1);
  1806. player.setOlympiadGameId(-1);
  1807. player.sendPacket(new ExOlympiadMode(0));
  1808. //Add Clan Skills
  1809. if (player.getClan() != null)
  1810. {
  1811. for(L2Skill skill: player.getClan().getAllSkills())
  1812. {
  1813. if ( skill.getMinPledgeClass() <= player.getPledgeClass() )
  1814. player.addSkill(skill,false);
  1815. }
  1816. }
  1817. //Add Hero Skills
  1818. if (player.isHero())
  1819. {
  1820. for(L2Skill skill: HeroSkillTable.getHeroSkills())
  1821. player.addSkill(skill,false);
  1822. }
  1823. player.sendSkillList();
  1824. } catch (Exception e) {}
  1825. }
  1826. }
  1827. protected boolean haveWinner()
  1828. {
  1829. boolean retval = false;
  1830. if (_aborted || _playerOne == null || _playerTwo == null)
  1831. {
  1832. return true;
  1833. }
  1834. double playerOneHp = 0;
  1835. try{
  1836. if (_playerOne != null && _playerOne.getOlympiadGameId() != -1)
  1837. {
  1838. playerOneHp = _playerOne.getCurrentHp();
  1839. }
  1840. }catch (Exception e){
  1841. playerOneHp = 0;
  1842. }
  1843. double playerTwoHp = 0;
  1844. try{
  1845. if (_playerTwo != null && _playerTwo.getOlympiadGameId()!=-1)
  1846. {
  1847. playerTwoHp = _playerTwo.getCurrentHp();
  1848. }
  1849. }catch (Exception e){
  1850. playerTwoHp = 0;
  1851. }
  1852. if (playerTwoHp==0 || playerOneHp==0)
  1853. {
  1854. return true;
  1855. }
  1856. return retval;
  1857. }
  1858. protected void validateWinner()
  1859. {
  1860. if (_aborted || _playerOne == null || _playerTwo == null || _playerOneDisconnected || _playerTwoDisconnected)
  1861. {
  1862. return;
  1863. }
  1864. StatsSet playerOneStat;
  1865. StatsSet playerTwoStat;
  1866. playerOneStat = _nobles.get(_playerOneID);
  1867. playerTwoStat = _nobles.get(_playerTwoID);
  1868. int _div;
  1869. int _gpreward;
  1870. int playerOnePlayed = playerOneStat.getInteger(COMP_DONE);
  1871. int playerTwoPlayed = playerTwoStat.getInteger(COMP_DONE);
  1872. int playerOnePoints = playerOneStat.getInteger(POINTS);
  1873. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  1874. double playerOneHp = 0;
  1875. try
  1876. {
  1877. if (_playerOne != null && !_playerOneDisconnected)
  1878. {
  1879. if (!_playerOne.isDead())
  1880. {
  1881. playerOneHp = _playerOne.getCurrentHp()+_playerOne.getCurrentCp();
  1882. }
  1883. }
  1884. }
  1885. catch (Exception e)
  1886. {
  1887. playerOneHp = 0;
  1888. }
  1889. double playerTwoHp = 0;
  1890. try
  1891. {
  1892. if (_playerTwo != null && !_playerTwoDisconnected)
  1893. {
  1894. if (!_playerTwo.isDead())
  1895. {
  1896. playerTwoHp = _playerTwo.getCurrentHp()+_playerTwo.getCurrentCp();
  1897. }
  1898. }
  1899. }
  1900. catch (Exception e)
  1901. {
  1902. playerTwoHp = 0;
  1903. }
  1904. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  1905. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  1906. _sm3 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  1907. String result = "";
  1908. // if players crashed, search if they've relogged
  1909. _playerOne = L2World.getInstance().getPlayer(_playerOneName);
  1910. _players.set(0, _playerOne);
  1911. _playerTwo = L2World.getInstance().getPlayer(_playerTwoName);
  1912. _players.set(1, _playerTwo);
  1913. switch(_type){
  1914. case NON_CLASSED: _div=5; _gpreward=Config.ALT_OLY_NONCLASSED_RITEM_C; break;
  1915. default: _div=3; _gpreward=Config.ALT_OLY_CLASSED_RITEM_C; break;
  1916. }
  1917. if (_playerOne == null && _playerTwo == null)
  1918. {
  1919. result=" tie";
  1920. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  1921. broadcastMessage(_sm, true);
  1922. }
  1923. else if (_playerTwo == null || _playerTwo.isOnline() == 0 || (playerTwoHp == 0 && playerOneHp != 0) || (_damageP1 > _damageP2 && playerTwoHp != 0 && playerOneHp != 0))
  1924. {
  1925. int pointDiff;
  1926. pointDiff = playerTwoPoints / _div;
  1927. playerOneStat.set(POINTS, playerOnePoints + pointDiff);
  1928. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  1929. _sm.addString(_playerOneName);
  1930. broadcastMessage(_sm, true);
  1931. _sm2.addString(_playerOneName);
  1932. _sm2.addNumber(pointDiff);
  1933. broadcastMessage(_sm2, true);
  1934. _sm3.addString(_playerTwoName);
  1935. _sm3.addNumber(pointDiff);
  1936. broadcastMessage(_sm3, true);
  1937. try {
  1938. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+_damageP1+"dmg vs "+_damageP2+"dmg) "+_playerOneName+" win "+pointDiff+" points";
  1939. L2ItemInstance item = _playerOne.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerOne, null);
  1940. InventoryUpdate iu = new InventoryUpdate();
  1941. iu.addModifiedItem(item);
  1942. _playerOne.sendPacket(iu);
  1943. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1944. sm.addItemName(item);
  1945. sm.addNumber(_gpreward);
  1946. _playerOne.sendPacket(sm);
  1947. } catch (Exception e) { }
  1948. }
  1949. else if (_playerOne == null || _playerOne.isOnline() == 0 || (playerOneHp == 0 && playerTwoHp != 0) || (_damageP2 > _damageP1 && playerOneHp != 0 && playerTwoHp != 0))
  1950. {
  1951. int pointDiff;
  1952. pointDiff = playerOnePoints / _div;
  1953. playerTwoStat.set(POINTS, playerTwoPoints + pointDiff);
  1954. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  1955. _sm.addString(_playerTwoName);
  1956. broadcastMessage(_sm, true);
  1957. _sm2.addString(_playerTwoName);
  1958. _sm2.addNumber(pointDiff);
  1959. broadcastMessage(_sm2, true);
  1960. _sm3.addString(_playerOneName);
  1961. _sm3.addNumber(pointDiff);
  1962. broadcastMessage(_sm3, true);
  1963. try {
  1964. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+_damageP1+"dmg vs "+_damageP2+"dmg) "+_playerTwoName+" win "+pointDiff+" points";
  1965. L2ItemInstance item = _playerTwo.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerTwo, null);
  1966. InventoryUpdate iu = new InventoryUpdate();
  1967. iu.addModifiedItem(item);
  1968. _playerTwo.sendPacket(iu);
  1969. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1970. sm.addItemName(item);
  1971. sm.addNumber(_gpreward);
  1972. _playerTwo.sendPacket(sm);
  1973. } catch (Exception e) { }
  1974. }
  1975. else
  1976. {
  1977. result=" tie";
  1978. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  1979. broadcastMessage(_sm, true);
  1980. }
  1981. if (Config.DEBUG)
  1982. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... "+result);
  1983. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  1984. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  1985. _nobles.remove(_playerOneID);
  1986. _nobles.remove(_playerTwoID);
  1987. _nobles.put(_playerOneID, playerOneStat);
  1988. _nobles.put(_playerTwoID, playerTwoStat);
  1989. for (int i=40;i>10;i-=10)
  1990. {
  1991. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  1992. _sm.addNumber(i);
  1993. broadcastMessage(_sm, false);
  1994. try{ Thread.sleep(10000); }catch (InterruptedException e){}
  1995. if (i==20) {
  1996. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  1997. _sm.addNumber(10);
  1998. broadcastMessage(_sm, false);
  1999. try{ Thread.sleep(5000); }catch (InterruptedException e){}
  2000. }
  2001. }
  2002. for (int i=5;i>0;i--)
  2003. {
  2004. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  2005. _sm.addNumber(i);
  2006. broadcastMessage(_sm, false);
  2007. try{ Thread.sleep(1000); }catch (InterruptedException e){}
  2008. }
  2009. }
  2010. protected void additions()
  2011. {
  2012. for (L2PcInstance player : _players)
  2013. {
  2014. try {
  2015. //Set HP/CP/MP to Max
  2016. player.setCurrentCp(player.getMaxCp());
  2017. player.setCurrentHp(player.getMaxHp());
  2018. player.setCurrentMp(player.getMaxMp());
  2019. } catch (Exception e) { }
  2020. finally
  2021. {
  2022. _damageP1 = 0;
  2023. _damageP2 = 0;
  2024. }
  2025. }
  2026. }
  2027. protected boolean makePlayersVisible()
  2028. {
  2029. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  2030. try {
  2031. for (L2PcInstance player : _players)
  2032. {
  2033. player.getAppearance().setVisible();
  2034. player.broadcastUserInfo();
  2035. player.sendPacket(_sm);
  2036. if (player.getPet() != null)
  2037. player.getPet().updateAbnormalEffect();
  2038. }
  2039. } catch (NullPointerException e) { _aborted = true; return false; }
  2040. return true;
  2041. }
  2042. protected boolean makeCompetitionStart()
  2043. {
  2044. if (_aborted) return false;
  2045. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  2046. broadcastMessage(_sm, true);
  2047. try {
  2048. for (L2PcInstance player : _players)
  2049. {
  2050. player.setIsOlympiadStart(true);
  2051. }
  2052. } catch (Exception e) {
  2053. _aborted = true;
  2054. return false;
  2055. }
  2056. return true;
  2057. }
  2058. protected String getTitle()
  2059. {
  2060. String msg = "";
  2061. msg+= _playerOneName + " : " + _playerTwoName;
  2062. return msg;
  2063. }
  2064. protected L2PcInstance[] getPlayers()
  2065. {
  2066. L2PcInstance[] players = new L2PcInstance[2];
  2067. if (_playerOne == null || _playerTwo == null) return null;
  2068. players[0] = _playerOne;
  2069. players[1] = _playerTwo;
  2070. return players;
  2071. }
  2072. protected L2FastList<L2PcInstance> getSpectators()
  2073. {
  2074. return _spectators;
  2075. }
  2076. protected void addSpectator(L2PcInstance spec)
  2077. {
  2078. _spectators.add(spec);
  2079. }
  2080. protected void removeSpectator(L2PcInstance spec)
  2081. {
  2082. if (_spectators != null && _spectators.contains(spec))
  2083. _spectators.remove(spec);
  2084. }
  2085. protected void clearSpectators()
  2086. {
  2087. if (_spectators != null)
  2088. {
  2089. for (L2PcInstance pc : _spectators)
  2090. {
  2091. try {
  2092. if(!pc.inObserverMode()) continue;
  2093. pc.leaveOlympiadObserverMode();
  2094. } catch (NullPointerException e) {}
  2095. }
  2096. _spectators.clear();
  2097. }
  2098. }
  2099. private void broadcastMessage(SystemMessage sm, boolean toAll)
  2100. {
  2101. try {
  2102. _playerOne.sendPacket(sm);
  2103. _playerTwo.sendPacket(sm);
  2104. } catch (Exception e) {}
  2105. if (toAll && _spectators != null)
  2106. {
  2107. for (L2PcInstance spec : _spectators)
  2108. {
  2109. try { spec.sendPacket(sm); } catch (NullPointerException e) {}
  2110. }
  2111. }
  2112. }
  2113. }
  2114. }