2
0

Olympiad.java 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  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, -3028, 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. if (g == null)
  651. continue;
  652. for(L2PcInstance player : g.getPlayers())
  653. {
  654. if(player.getObjectId() == noble.getObjectId())
  655. {
  656. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_FOR_ALL_CLASSES_WAITING_TO_PARTICIPATE_IN_THE_GAME);
  657. noble.sendPacket(sm);
  658. return false;
  659. }
  660. }
  661. }
  662. if (classBased && getNoblePoints(noble.getObjectId()) < 3)
  663. {
  664. noble.sendMessage("Cant register when you have less than 3 points");
  665. return false;
  666. }
  667. if (!classBased && getNoblePoints(noble.getObjectId()) < 5)
  668. {
  669. noble.sendMessage("Cant register when you have less than 5 points");
  670. return false;
  671. }
  672. if (classBased)
  673. {
  674. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  675. {
  676. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  677. classed.add(noble);
  678. _classBasedRegisters.remove(noble.getClassId().getId());
  679. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  680. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  681. noble.sendPacket(sm);
  682. }
  683. else
  684. {
  685. L2FastList<L2PcInstance> classed = new L2FastList<L2PcInstance>();
  686. classed.add(noble);
  687. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  688. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  689. noble.sendPacket(sm);
  690. }
  691. }
  692. else
  693. {
  694. _nonClassBasedRegisters.add(noble);
  695. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  696. noble.sendPacket(sm);
  697. }
  698. return true;
  699. }
  700. public boolean isRegistered(L2PcInstance noble)
  701. {
  702. if(_nonClassBasedRegisters == null) return false;
  703. if(_classBasedRegisters == null) return false;
  704. if (!_nonClassBasedRegisters.contains(noble))
  705. {
  706. if (!_classBasedRegisters.containsKey(noble.getClassId().getId()))
  707. {
  708. return false;
  709. }
  710. else
  711. {
  712. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  713. if (!classed.contains(noble))
  714. {
  715. return false;
  716. }
  717. }
  718. }
  719. return true;
  720. }
  721. public boolean unRegisterNoble(L2PcInstance noble)
  722. {
  723. SystemMessage sm;
  724. /*
  725. if (_compStarted)
  726. {
  727. noble.sendMessage("Cant Unregister whilst competition is under way");
  728. return false;
  729. }
  730. */
  731. if (!_inCompPeriod)
  732. {
  733. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  734. noble.sendPacket(sm);
  735. return false;
  736. }
  737. if (!noble.isNoble())
  738. {
  739. sm = new SystemMessage(SystemMessageId.ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  740. noble.sendPacket(sm);
  741. return false;
  742. }
  743. if (!isRegistered(noble))
  744. {
  745. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  746. noble.sendPacket(sm);
  747. return false;
  748. }
  749. if (_nonClassBasedRegisters.contains(noble))
  750. _nonClassBasedRegisters.remove(noble);
  751. else
  752. {
  753. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  754. classed.remove(noble);
  755. _classBasedRegisters.remove(noble.getClassId().getId());
  756. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  757. }
  758. for(L2OlympiadGame game: _manager.getOlympiadGames().values())
  759. {
  760. if (game == null)
  761. continue;
  762. if(game._playerOne.getObjectId() == noble.getObjectId() || game._playerTwo.getObjectId() == noble.getObjectId())
  763. {
  764. noble.sendMessage("Cant Unregister whilst you are already selected for a game");
  765. return false;
  766. }
  767. }
  768. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  769. noble.sendPacket(sm);
  770. return true;
  771. }
  772. public void removeDisconnectedCompetitor(L2PcInstance player)
  773. {
  774. if (_manager == null || (_manager.getOlympiadInstance(player.getOlympiadGameId()) == null)) return;
  775. _manager.getOlympiadInstance(player.getOlympiadGameId()).handleDisconnect(player);
  776. }
  777. public void notifyCompetitorDamage(int objId, int damage, int gameId)
  778. {
  779. if (_manager.getOlympiadGames().get(gameId) == null) return;
  780. if (_manager.getOlympiadGames().get(gameId)._playerOneID == objId)
  781. _manager.getOlympiadGames().get(gameId)._damageP1 += damage;
  782. else _manager.getOlympiadGames().get(gameId)._damageP2 += damage;
  783. }
  784. private void updateCompStatus()
  785. {
  786. //_compStarted = false;
  787. synchronized(this)
  788. {
  789. long milliToStart = getMillisToCompBegin();
  790. double numSecs = (milliToStart / 1000) % 60;
  791. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  792. int numMins = (int) Math.floor(countDown % 60);
  793. countDown = (countDown - numMins) / 60;
  794. int numHours = (int) Math.floor(countDown % 24);
  795. int numDays = (int) Math.floor((countDown - numHours) / 24);
  796. _log.info("Olympiad System: Competition Period Starts in "
  797. + numDays + " days, " + numHours
  798. + " hours and " + numMins + " mins.");
  799. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  800. }
  801. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  802. public void run()
  803. {
  804. if (isOlympiadEnd())
  805. return;
  806. _inCompPeriod = true;
  807. OlympiadManager om = new OlympiadManager();
  808. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  809. _log.info("Olympiad System: Olympiad Game Started");
  810. Thread olyCycle = new Thread(om);
  811. olyCycle.start();
  812. //_scheduledManagerTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(om, INITIAL_WAIT, BATTLE_WAIT);
  813. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  814. public void run()
  815. {
  816. if (isOlympiadEnd())
  817. return;
  818. //_scheduledManagerTask.cancel(true);
  819. _inCompPeriod = false;
  820. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  821. _log.info("Olympiad System: Olympiad Game Ended");
  822. try {
  823. while(_battleStarted){
  824. try{
  825. //wait 1 minutes for end of pendings games
  826. Thread.sleep(60000);
  827. }catch (InterruptedException e){}
  828. }
  829. save();
  830. }
  831. catch (Exception e) {
  832. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  833. }
  834. init();
  835. }
  836. }, getMillisToCompEnd());
  837. }
  838. }, getMillisToCompBegin());
  839. }
  840. private long getMillisToOlympiadEnd()
  841. {
  842. //if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  843. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  844. //return 10L;
  845. }
  846. public void manualSelectHeroes()
  847. {
  848. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  849. sm.addNumber(_currentCycle);
  850. Announcements.getInstance().announceToAll(sm);
  851. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  852. _isOlympiadEnd = true;
  853. if (_scheduledManagerTask != null)
  854. _scheduledManagerTask.cancel(true);
  855. if (_scheduledWeeklyTask != null)
  856. _scheduledWeeklyTask.cancel(true);
  857. if(_scheduledOlympiadEnd != null)
  858. _scheduledOlympiadEnd.cancel(true);
  859. Calendar validationEnd = Calendar.getInstance();
  860. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  861. saveNobleData();
  862. _period = 1;
  863. sortHerosToBe();
  864. giveHeroBonus();
  865. Hero.getInstance().computeNewHeroes(_heroesToBe);
  866. try {
  867. save();
  868. }
  869. catch (Exception e) {
  870. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  871. }
  872. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  873. public void run()
  874. {
  875. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  876. _period = 0;
  877. _currentCycle++;
  878. deleteNobles();
  879. setNewOlympiadEnd();
  880. init();
  881. }
  882. }, getMillisToValidationEnd());
  883. }
  884. protected long getMillisToValidationEnd()
  885. {
  886. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  887. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  888. return 10L;
  889. }
  890. public boolean isOlympiadEnd()
  891. {
  892. return _isOlympiadEnd;
  893. }
  894. protected void setNewOlympiadEnd()
  895. {
  896. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  897. sm.addNumber(_currentCycle);
  898. Announcements.getInstance().announceToAll(sm);
  899. Calendar currentTime = Calendar.getInstance();
  900. currentTime.add(Calendar.MONTH, 1);
  901. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  902. currentTime.set(Calendar.AM_PM, Calendar.AM);
  903. currentTime.set(Calendar.HOUR, 12);
  904. currentTime.set(Calendar.MINUTE, 0);
  905. currentTime.set(Calendar.SECOND, 0);
  906. _olympiadEnd = currentTime.getTimeInMillis();
  907. Calendar nextChange = Calendar.getInstance();
  908. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  909. _isOlympiadEnd = false;
  910. }
  911. public boolean inCompPeriod()
  912. {
  913. return _inCompPeriod;
  914. }
  915. private long getMillisToCompBegin()
  916. {
  917. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() &&
  918. _compEnd > Calendar.getInstance().getTimeInMillis())
  919. return 10L;
  920. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  921. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  922. return setNewCompBegin();
  923. }
  924. private long setNewCompBegin()
  925. {
  926. _compStart = Calendar.getInstance();
  927. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  928. _compStart.set(Calendar.MINUTE, COMP_MIN);
  929. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  930. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  931. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  932. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  933. }
  934. protected long getMillisToCompEnd()
  935. {
  936. //if (_compEnd > Calendar.getInstance().getTimeInMillis())
  937. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  938. //return 10L;
  939. }
  940. private long getMillisToWeekChange()
  941. {
  942. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  943. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  944. return 10L;
  945. }
  946. private void scheduleWeeklyChange()
  947. {
  948. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  949. public void run()
  950. {
  951. addWeeklyPoints();
  952. _log.info("Olympiad System: Added weekly points to nobles");
  953. Calendar nextChange = Calendar.getInstance();
  954. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  955. }
  956. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  957. }
  958. protected synchronized void addWeeklyPoints()
  959. {
  960. if (_period == 1)
  961. return;
  962. for (Integer nobleId : _nobles.keySet())
  963. {
  964. StatsSet nobleInfo = _nobles.get(nobleId);
  965. int currentPoints = nobleInfo.getInteger(POINTS);
  966. currentPoints += WEEKLY_POINTS;
  967. nobleInfo.set(POINTS, currentPoints);
  968. _nobles.remove(nobleId);
  969. _nobles.put(nobleId, nobleInfo);
  970. }
  971. }
  972. public String[] getMatchList()
  973. {
  974. return (_manager == null)? null : _manager.getAllTitles();
  975. }
  976. // returns the players for the given olympiad game Id
  977. public L2PcInstance[] getPlayers(int Id)
  978. {
  979. if (_manager == null || (_manager.getOlympiadInstance(Id) == null))
  980. {
  981. return null;
  982. }
  983. L2PcInstance[] players = _manager.getOlympiadInstance(Id).getPlayers();
  984. return players;
  985. }
  986. public int getCurrentCycle()
  987. {
  988. return _currentCycle;
  989. }
  990. public void addSpectator(int id, L2PcInstance spectator)
  991. {
  992. for(L2PcInstance player : _nonClassBasedRegisters)
  993. {
  994. if(spectator.getObjectId() == player.getObjectId())
  995. {
  996. spectator.sendMessage("You are already registered for a competition");
  997. return;
  998. }
  999. }
  1000. for(L2FastList<L2PcInstance> list : _classBasedRegisters.values())
  1001. {
  1002. for(L2PcInstance player : list)
  1003. {
  1004. if(spectator.getObjectId() == player.getObjectId())
  1005. {
  1006. spectator.sendMessage("You are already registered for a competition");
  1007. return;
  1008. }
  1009. }
  1010. }
  1011. if(spectator.getOlympiadGameId() != -1)
  1012. {
  1013. spectator.sendMessage("You are already registered for a competition");
  1014. return;
  1015. }
  1016. if (_manager == null || (_manager.getOlympiadInstance(id) == null))
  1017. {
  1018. spectator.sendPacket(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS));
  1019. return;
  1020. }
  1021. L2PcInstance[] players = _manager.getOlympiadInstance(id).getPlayers();
  1022. if (players == null) return;
  1023. spectator.enterOlympiadObserverMode(STADIUMS[id].getCoordinates()[0], STADIUMS[id].getCoordinates()[1], STADIUMS[id].getCoordinates()[2], id);
  1024. _manager.getOlympiadInstance(id).addSpectator(spectator);
  1025. }
  1026. public void UnSpawnBuffer(L2Spawn spawn)
  1027. {
  1028. spawn.getLastSpawn().decayMe();
  1029. }
  1030. public void removeSpectator(int id, L2PcInstance spectator)
  1031. {
  1032. if (_manager == null || (_manager.getOlympiadInstance(id) == null)) return;
  1033. _manager.getOlympiadInstance(id).removeSpectator(spectator);
  1034. }
  1035. public L2FastList<L2PcInstance> getSpectators(int id)
  1036. {
  1037. if (_manager == null || _manager.getOlympiadInstance(id) == null)
  1038. return null;
  1039. return _manager.getOlympiadInstance(id).getSpectators();
  1040. }
  1041. public Map<Integer, L2OlympiadGame> getOlympiadGames()
  1042. {
  1043. return _manager.getOlympiadGames();
  1044. }
  1045. public boolean playerInStadia(L2PcInstance player)
  1046. {
  1047. return (OlympiadStadiaManager.getInstance().getStadium(player) != null);
  1048. }
  1049. public int[] getWaitingList()
  1050. {
  1051. int[] array = new int[2];
  1052. if (!inCompPeriod())
  1053. return null;
  1054. int classCount = 0;
  1055. if (_classBasedRegisters.size() != 0)
  1056. for (L2FastList<L2PcInstance> classed : _classBasedRegisters.values())
  1057. {
  1058. classCount += classed.size();
  1059. }
  1060. array[0] = classCount;
  1061. array[1] = _nonClassBasedRegisters.size();
  1062. return array;
  1063. }
  1064. protected synchronized void saveNobleData()
  1065. {
  1066. Connection con = null;
  1067. if (_nobles == null)
  1068. return;
  1069. try
  1070. {
  1071. con = L2DatabaseFactory.getInstance().getConnection();
  1072. PreparedStatement statement;
  1073. for (Integer nobleId : _nobles.keySet())
  1074. {
  1075. StatsSet nobleInfo = _nobles.get(nobleId);
  1076. if (nobleInfo == null)
  1077. continue;
  1078. int charId = nobleId;
  1079. int classId = nobleInfo.getInteger(CLASS_ID);
  1080. String charName = nobleInfo.getString(CHAR_NAME);
  1081. int points = nobleInfo.getInteger(POINTS);
  1082. int compDone = nobleInfo.getInteger(COMP_DONE);
  1083. boolean toSave = nobleInfo.getBool("to_save");
  1084. if (toSave)
  1085. {
  1086. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  1087. statement.setInt(1, charId);
  1088. statement.setInt(2, classId);
  1089. statement.setString(3, charName);
  1090. statement.setInt(4, points);
  1091. statement.setInt(5, compDone);
  1092. statement.execute();
  1093. statement.close();
  1094. nobleInfo.set("to_save", false);
  1095. _nobles.remove(nobleId);
  1096. _nobles.put(nobleId, nobleInfo);
  1097. }
  1098. else
  1099. {
  1100. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  1101. statement.setInt(1, points);
  1102. statement.setInt(2, compDone);
  1103. statement.setInt(3, charId);
  1104. statement.execute();
  1105. statement.close();
  1106. }
  1107. }
  1108. }
  1109. catch(SQLException e) {_log.warning("Olympiad System: Couldnt save nobles info in db");}
  1110. finally
  1111. {
  1112. try{con.close();}catch(Exception e){e.printStackTrace();}
  1113. }
  1114. }
  1115. protected void sortHerosToBe()
  1116. {
  1117. if (_period != 1) return;
  1118. _heroesToBe = new L2FastList<StatsSet>();
  1119. Connection con = null;
  1120. try
  1121. {
  1122. con = L2DatabaseFactory.getInstance().getConnection();
  1123. PreparedStatement statement;
  1124. ResultSet rset;
  1125. StatsSet hero;
  1126. for (int i = 0; i < HERO_IDS.length; i++)
  1127. {
  1128. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  1129. statement.setInt(1, HERO_IDS[i]);
  1130. rset = statement.executeQuery();
  1131. if (rset.next())
  1132. {
  1133. hero = new StatsSet();
  1134. hero.set(CLASS_ID, HERO_IDS[i]);
  1135. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1136. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  1137. _heroesToBe.add(hero);
  1138. }
  1139. statement.close();
  1140. rset.close();
  1141. }
  1142. }
  1143. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  1144. finally
  1145. {
  1146. try{con.close();}catch(Exception e){e.printStackTrace();}
  1147. }
  1148. }
  1149. public L2FastList<String> getClassLeaderBoard(int classId)
  1150. {
  1151. //if (_period != 1) return;
  1152. L2FastList<String> names = new L2FastList<String>();
  1153. Connection con = null;
  1154. try
  1155. {
  1156. con = L2DatabaseFactory.getInstance().getConnection();
  1157. PreparedStatement statement;
  1158. ResultSet rset;
  1159. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  1160. statement.setInt(1, classId);
  1161. rset = statement.executeQuery();
  1162. while (rset.next())
  1163. {
  1164. names.add(rset.getString(CHAR_NAME));
  1165. }
  1166. statement.close();
  1167. rset.close();
  1168. return names;
  1169. }
  1170. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  1171. finally
  1172. {
  1173. try{con.close();}catch(Exception e){e.printStackTrace();}
  1174. }
  1175. return names;
  1176. }
  1177. protected void giveHeroBonus()
  1178. {
  1179. if (_heroesToBe.size() == 0)
  1180. return;
  1181. for (StatsSet hero : _heroesToBe)
  1182. {
  1183. int charId = hero.getInteger(CHAR_ID);
  1184. StatsSet noble = _nobles.get(charId);
  1185. int currentPoints = noble.getInteger(POINTS);
  1186. currentPoints += Config.ALT_OLY_HERO_POINTS;
  1187. noble.set(POINTS, currentPoints);
  1188. _nobles.remove(charId);
  1189. _nobles.put(charId, noble);
  1190. }
  1191. }
  1192. public int getNoblessePasses(int objId)
  1193. {
  1194. if (_period != 1 || _nobles.size() == 0)
  1195. return 0;
  1196. StatsSet noble = _nobles.get(objId);
  1197. if (noble == null)
  1198. return 0;
  1199. int points = noble.getInteger(POINTS);
  1200. if (points <= Config.ALT_OLY_MIN_POINT_FOR_EXCH)
  1201. return 0;
  1202. noble.set(POINTS, 0);
  1203. _nobles.remove(objId);
  1204. _nobles.put(objId, noble);
  1205. points *= Config.ALT_OLY_GP_PER_POINT;
  1206. return points;
  1207. }
  1208. public boolean isRegisteredInComp(L2PcInstance player)
  1209. {
  1210. boolean result = false;
  1211. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(player))
  1212. result = true;
  1213. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(player.getClassId().getId()))
  1214. {
  1215. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  1216. if (classed.contains(player))
  1217. result = true;
  1218. }
  1219. if (!_inCompPeriod){}
  1220. else {
  1221. for(L2OlympiadGame game: _manager.getOlympiadGames().values())
  1222. {
  1223. if(game._playerOne.getObjectId() == player.getObjectId() || game._playerTwo.getObjectId() == player.getObjectId())
  1224. {
  1225. result = true;
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. return result;
  1231. }
  1232. public int getNoblePoints(int objId)
  1233. {
  1234. if (_nobles.size() == 0)
  1235. return 0;
  1236. StatsSet noble = _nobles.get(objId);
  1237. if (noble == null)
  1238. return 0;
  1239. int points = noble.getInteger(POINTS);
  1240. return points;
  1241. }
  1242. public int getCompetitionDone(int objId)
  1243. {
  1244. if (_nobles.size() == 0)
  1245. return 0;
  1246. StatsSet noble = _nobles.get(objId);
  1247. if (noble == null)
  1248. return 0;
  1249. int points = noble.getInteger(COMP_DONE);
  1250. return points;
  1251. }
  1252. protected void deleteNobles()
  1253. {
  1254. Connection con = null;
  1255. try
  1256. {
  1257. con = L2DatabaseFactory.getInstance().getConnection();
  1258. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  1259. statement.execute();
  1260. statement.close();
  1261. }
  1262. catch(SQLException e){_log.warning("Olympiad System: Couldnt delete nobles from db");}
  1263. finally
  1264. {
  1265. try{con.close();}catch(Exception e){e.printStackTrace();}
  1266. }
  1267. _nobles.clear();
  1268. }
  1269. public void save() throws IOException
  1270. {
  1271. saveNobleData();
  1272. Properties OlympiadProperties = new Properties();
  1273. FileOutputStream fos = new FileOutputStream(new File(Config.DATAPACK_ROOT, OLYMPIAD_DATA_FILE));
  1274. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  1275. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  1276. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  1277. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  1278. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  1279. OlympiadProperties.store(fos, "Olympiad Properties");
  1280. fos.close();
  1281. }
  1282. private class OlympiadManager implements Runnable
  1283. {
  1284. private Map<Integer, L2OlympiadGame> _olympiadInstances;
  1285. public OlympiadManager()
  1286. {
  1287. _olympiadInstances = new FastMap<Integer, L2OlympiadGame>();
  1288. _manager = this;
  1289. }
  1290. public synchronized void run()
  1291. {
  1292. _cycleTerminated = false;
  1293. if (isOlympiadEnd())
  1294. {
  1295. _scheduledManagerTask.cancel(true);
  1296. _cycleTerminated = true;
  1297. return;
  1298. }
  1299. Map<Integer, OlympiadGameTask> _gamesQueue = new FastMap<Integer, OlympiadGameTask>();
  1300. while(inCompPeriod())
  1301. {
  1302. if (_nobles.size() == 0)
  1303. {
  1304. try{
  1305. wait(60000);
  1306. }catch(InterruptedException ex){}
  1307. continue;
  1308. }
  1309. //_compStarted = true;
  1310. int classBasedPgCount = 0;
  1311. for(L2FastList<L2PcInstance> classList : _classBasedRegisters.values())
  1312. classBasedPgCount += classList.size();
  1313. while((_gamesQueue.size() > 0 || classBasedPgCount >= Config.ALT_OLY_CLASSED || _nonClassBasedRegisters.size() >= Config.ALT_OLY_NONCLASSED) && inCompPeriod())
  1314. {
  1315. //first cycle do nothing
  1316. int _gamesQueueSize = 0;
  1317. _gamesQueueSize = _gamesQueue.size();
  1318. for(int i=0; i<_gamesQueueSize;i++)
  1319. {
  1320. if(_gamesQueue.get(i) == null || _gamesQueue.get(i).isTerminated() || _gamesQueue.get(i)._game == null)
  1321. {
  1322. if(_gamesQueue.containsKey(i)) {
  1323. //removes terminated games from the queue
  1324. try {
  1325. _olympiadInstances.remove(i);
  1326. _gamesQueue.remove(i);
  1327. STADIUMS[i].setStadiaFree();
  1328. } catch(Exception e) {e.printStackTrace();}
  1329. } else {
  1330. _gamesQueueSize = _gamesQueueSize+1;
  1331. }
  1332. }else if(_gamesQueue.get(i) != null && !_gamesQueue.get(i).isStarted())
  1333. {
  1334. //start new games
  1335. Thread T = new Thread(_gamesQueue.get(i));
  1336. T.start();
  1337. }
  1338. }
  1339. //set up the games queue
  1340. for(int i=0; i<STADIUMS.length; i++)
  1341. {
  1342. if(!existNextOpponents(_nonClassBasedRegisters) && !existNextOpponents(getRandomClassList(_classBasedRegisters)))
  1343. {
  1344. break;
  1345. }
  1346. if(STADIUMS[i].isFreeToUse())
  1347. {
  1348. if (existNextOpponents(_nonClassBasedRegisters))
  1349. {
  1350. try{
  1351. _olympiadInstances.put(i, new L2OlympiadGame(i, COMP_TYPE.NON_CLASSED, nextOpponents(_nonClassBasedRegisters), STADIUMS[i].getCoordinates()));
  1352. _gamesQueue.put(i,new OlympiadGameTask(_olympiadInstances.get(i)));
  1353. STADIUMS[i].setStadiaBusy();
  1354. }catch(Exception ex)
  1355. {
  1356. if(_olympiadInstances.get(i) != null){
  1357. for(L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  1358. {
  1359. player.sendMessage("Your olympiad registration was canceled due to an error");
  1360. player.setIsInOlympiadMode(false);
  1361. player.setIsOlympiadStart(false);
  1362. player.setOlympiadSide(-1);
  1363. player.setOlympiadGameId(-1);
  1364. }
  1365. _olympiadInstances.remove(i);
  1366. }
  1367. if(_gamesQueue.get(i)!= null)
  1368. _gamesQueue.remove(i);
  1369. STADIUMS[i].setStadiaFree();
  1370. //try to reuse this stadia next time
  1371. i--;
  1372. }
  1373. }
  1374. else if (existNextOpponents(getRandomClassList(_classBasedRegisters)))
  1375. {
  1376. try{
  1377. _olympiadInstances.put(i, new L2OlympiadGame(i, COMP_TYPE.CLASSED, nextOpponents(getRandomClassList(_classBasedRegisters)), STADIUMS[i].getCoordinates()));
  1378. _gamesQueue.put(i,new OlympiadGameTask(_olympiadInstances.get(i)));
  1379. STADIUMS[i].setStadiaBusy();
  1380. }catch(Exception ex)
  1381. {
  1382. if(_olympiadInstances.get(i) != null){
  1383. for(L2PcInstance player : _olympiadInstances.get(i).getPlayers())
  1384. {
  1385. player.sendMessage("Your olympiad registration was canceled due to an error");
  1386. player.setIsInOlympiadMode(false);
  1387. player.setIsOlympiadStart(false);
  1388. player.setOlympiadSide(-1);
  1389. player.setOlympiadGameId(-1);
  1390. }
  1391. _olympiadInstances.remove(i);
  1392. }
  1393. if(_gamesQueue.get(i)!= null)
  1394. _gamesQueue.remove(i);
  1395. STADIUMS[i].setStadiaFree();
  1396. //try to reuse this stadia next time
  1397. i--;
  1398. }
  1399. }
  1400. }
  1401. }
  1402. //wait 30 sec for !stress the server
  1403. try{
  1404. wait(30000);
  1405. }catch (InterruptedException e){}
  1406. }
  1407. //wait 30 sec for !stress the server
  1408. try{
  1409. wait(30000);
  1410. }catch (InterruptedException e){}
  1411. }
  1412. //when comp time finish wait for all games terminated before execute the cleanup code
  1413. boolean allGamesTerminated = false;
  1414. //wait for all games terminated
  1415. while(!allGamesTerminated)
  1416. {
  1417. try{
  1418. wait(30000);
  1419. }catch (InterruptedException e){}
  1420. if(_gamesQueue.size() == 0)
  1421. {
  1422. allGamesTerminated = true;
  1423. }else{
  1424. for(OlympiadGameTask game : _gamesQueue.values())
  1425. {
  1426. allGamesTerminated = allGamesTerminated || game.isTerminated();
  1427. }
  1428. }
  1429. }
  1430. _cycleTerminated = true;
  1431. //when all games terminated clear all
  1432. _gamesQueue.clear();
  1433. /*_classBasedParticipants.clear();
  1434. _nonClassBasedParticipants.clear();*/
  1435. //Wait 20 seconds
  1436. _olympiadInstances.clear();
  1437. _classBasedRegisters.clear();
  1438. _nonClassBasedRegisters.clear();
  1439. _battleStarted = false;
  1440. //_compStarted = false;
  1441. }
  1442. protected L2OlympiadGame getOlympiadInstance(int index)
  1443. {
  1444. if (_olympiadInstances != null && _olympiadInstances.size() > 0)
  1445. {
  1446. return _olympiadInstances.get(index);
  1447. }
  1448. return null;
  1449. }
  1450. protected Map<Integer, L2OlympiadGame> getOlympiadGames()
  1451. {
  1452. return (_olympiadInstances == null)? null : _olympiadInstances;
  1453. }
  1454. private L2FastList<L2PcInstance> getRandomClassList(Map<Integer, L2FastList<L2PcInstance>> list)
  1455. {
  1456. if(list.size() == 0)
  1457. return null;
  1458. Map<Integer, L2FastList<L2PcInstance>> tmp = new FastMap<Integer, L2FastList<L2PcInstance>>();
  1459. int tmpIndex = 0;
  1460. for(L2FastList<L2PcInstance> l : list.values())
  1461. {
  1462. tmp.put(tmpIndex, l);
  1463. tmpIndex ++;
  1464. }
  1465. L2FastList<L2PcInstance> rndList = new L2FastList<L2PcInstance>();
  1466. int classIndex = 0;
  1467. if(tmp.size() == 1)
  1468. classIndex = 0;
  1469. else
  1470. classIndex = Rnd.nextInt(tmp.size());
  1471. rndList = tmp.get(classIndex);
  1472. return rndList;
  1473. }
  1474. private L2FastList<L2PcInstance> nextOpponents(L2FastList<L2PcInstance> list)
  1475. {
  1476. L2FastList<L2PcInstance> opponents = new L2FastList<L2PcInstance>();
  1477. if (list.size() == 0)
  1478. return opponents;
  1479. int loopCount = (list.size() / 2);
  1480. int first;
  1481. int second;
  1482. if (loopCount < 1)
  1483. return opponents;
  1484. first = Rnd.nextInt(list.size());
  1485. opponents.add(list.get(first));
  1486. list.remove(first);
  1487. second = Rnd.nextInt(list.size());
  1488. opponents.add(list.get(second));
  1489. list.remove(second);
  1490. return opponents;
  1491. }
  1492. private boolean existNextOpponents(L2FastList<L2PcInstance> list)
  1493. {
  1494. if(list == null)
  1495. return false;
  1496. if (list.size() == 0)
  1497. return false;
  1498. int loopCount = list.size() >> 1;
  1499. if (loopCount < 1)
  1500. return false;
  1501. else
  1502. return true;
  1503. }
  1504. protected String[] getAllTitles()
  1505. {
  1506. /*if(!_compStarted)
  1507. return null;*/
  1508. String[] msg = new String[_olympiadInstances.size()];
  1509. int count = 0;
  1510. int match = 1;
  1511. int showbattle = 0;
  1512. for (L2OlympiadGame instance : _olympiadInstances.values())
  1513. {
  1514. if (instance._gamestarted == true) { showbattle = 1; } else { showbattle = 0; }
  1515. msg[count] = "<"+showbattle+"><"+instance._stadiumID+"> In Progress " + instance.getTitle();
  1516. count++;
  1517. match++;
  1518. }
  1519. return msg;
  1520. }
  1521. }
  1522. private class L2OlympiadGame
  1523. {
  1524. protected COMP_TYPE _type;
  1525. public boolean _aborted;
  1526. public boolean _gamestarted;
  1527. public boolean _playerOneDisconnected;
  1528. public boolean _playerTwoDisconnected;
  1529. public String _playerOneName;
  1530. public String _playerTwoName;
  1531. public int _playerOneID = 0;
  1532. public int _playerTwoID = 0;
  1533. public static final int OLY_BUFFER = 36402;
  1534. public int _damageP1 = 0;
  1535. public int _damageP2 = 0;
  1536. public L2PcInstance _playerOne;
  1537. public L2PcInstance _playerTwo;
  1538. public L2Spawn _spawnOne;
  1539. public L2Spawn _spawnTwo;
  1540. private L2FastList<L2PcInstance> _players;
  1541. private int[] _stadiumPort;
  1542. private int x1, y1, z1, x2, y2, z2;
  1543. public int _stadiumID;
  1544. public L2FastList<L2PcInstance> _spectators;
  1545. private SystemMessage _sm;
  1546. private SystemMessage _sm2;
  1547. private SystemMessage _sm3;
  1548. protected L2OlympiadGame(int id, COMP_TYPE type, L2FastList<L2PcInstance> list, int[] stadiumPort)
  1549. {
  1550. _aborted = false;
  1551. _gamestarted = false;
  1552. _stadiumID = id;
  1553. _playerOneDisconnected = false;
  1554. _playerTwoDisconnected = false;
  1555. _type = type;
  1556. _stadiumPort = stadiumPort;
  1557. _spectators = new L2FastList<L2PcInstance>();
  1558. if (list != null)
  1559. {
  1560. _players = list;
  1561. _playerOne = list.get(0);
  1562. _playerTwo = list.get(1);
  1563. try {
  1564. _playerOneName = _playerOne.getName();
  1565. _playerTwoName = _playerTwo.getName();
  1566. _playerOne.setOlympiadGameId(id);
  1567. _playerTwo.setOlympiadGameId(id);
  1568. _playerOneID = _playerOne.getObjectId();
  1569. _playerTwoID = _playerTwo.getObjectId();
  1570. }
  1571. catch (Exception e) {
  1572. _aborted = true;
  1573. clearPlayers();
  1574. }
  1575. if (Config.DEBUG)
  1576. _log.info("Olympiad System: Game - " + id + ": " + _playerOne.getName() + " Vs " + _playerTwo.getName());
  1577. }
  1578. else {
  1579. _aborted = true;
  1580. clearPlayers();
  1581. return;
  1582. }
  1583. }
  1584. public boolean isAborted()
  1585. {
  1586. return _aborted;
  1587. }
  1588. protected void clearPlayers()
  1589. {
  1590. _playerOne = null;
  1591. _playerTwo = null;
  1592. _players = null;
  1593. _playerOneName = "";
  1594. _playerTwoName = "";
  1595. _playerOneID = 0;
  1596. _playerTwoID = 0;
  1597. }
  1598. protected void handleDisconnect(L2PcInstance player)
  1599. {
  1600. if (player == _playerOne)
  1601. _playerOneDisconnected = true;
  1602. else if (player == _playerTwo)
  1603. _playerTwoDisconnected = true;
  1604. }
  1605. public void openDoors()
  1606. {
  1607. _doorTable = DoorTable.getInstance();
  1608. try {
  1609. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[0]).openMe();
  1610. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[1]).openMe();
  1611. //_log.info("open doors"+STADIUMS[_stadiumID].getDoorID()[0]+"<>"+STADIUMS[_stadiumID].getDoorID()[1]);
  1612. } catch (Exception e) {}
  1613. }
  1614. public void closeDoors()
  1615. {
  1616. _doorTable = DoorTable.getInstance();
  1617. try {
  1618. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[0]).closeMe();
  1619. _doorTable.getDoor(STADIUMS[_stadiumID].getDoorID()[1]).closeMe();
  1620. //_log.info("close doors"+STADIUMS[_stadiumID].getDoorID()[0]+"<>"+STADIUMS[_stadiumID].getDoorID()[1]);
  1621. } catch (Exception e) {}
  1622. }
  1623. public L2Spawn SpawnBuffer(int xPos, int yPos, int zPos, int npcId)
  1624. {
  1625. L2NpcTemplate template;
  1626. template = NpcTable.getInstance().getTemplate(npcId);
  1627. try {
  1628. L2Spawn spawn = new L2Spawn(template);
  1629. spawn.setLocx(xPos);
  1630. spawn.setLocy(yPos);
  1631. spawn.setLocz(zPos);
  1632. spawn.setAmount(1);
  1633. spawn.setHeading(0);
  1634. spawn.setRespawnDelay(1);
  1635. SpawnTable.getInstance().addNewSpawn(spawn, false);
  1636. spawn.init();
  1637. return spawn;
  1638. } catch (Exception e) { return null; }
  1639. }
  1640. protected void removals()
  1641. {
  1642. if (_aborted) return;
  1643. if (_playerOne == null || _playerTwo == null) return;
  1644. if (_playerOneDisconnected || _playerTwoDisconnected) return;
  1645. for (L2PcInstance player : _players)
  1646. {
  1647. try{
  1648. //Remove Clan Skills
  1649. if (player.getClan() != null)
  1650. {
  1651. for(L2Skill skill: player.getClan().getAllSkills())
  1652. player.removeSkill(skill,false);
  1653. }
  1654. //Abort casting if player casting
  1655. if (player.isCastingNow())
  1656. {
  1657. player.abortCast();
  1658. }
  1659. //Remove Hero Skills
  1660. if (player.isHero())
  1661. {
  1662. for(L2Skill skill: HeroSkillTable.getHeroSkills())
  1663. player.removeSkill(skill,false);
  1664. }
  1665. // Heal Player fully
  1666. player.setCurrentCp(player.getMaxCp());
  1667. player.setCurrentHp(player.getMaxHp());
  1668. player.setCurrentMp(player.getMaxMp());
  1669. //Remove Buffs
  1670. player.stopAllEffects();
  1671. //Remove Summon's Buffs
  1672. if (player.getPet() != null)
  1673. {
  1674. L2Summon summon = player.getPet();
  1675. summon.stopAllEffects();
  1676. if (summon instanceof L2PetInstance)
  1677. summon.unSummon(player);
  1678. }
  1679. /*if (player.getCubics() != null)
  1680. {
  1681. for(L2CubicInstance cubic : player.getCubics().values())
  1682. {
  1683. cubic.stopAction();
  1684. player.delCubic(cubic.getId());
  1685. }
  1686. player.getCubics().clear();
  1687. }*/
  1688. //Remove player from his party
  1689. if (player.getParty() != null)
  1690. {
  1691. L2Party party = player.getParty();
  1692. party.removePartyMember(player);
  1693. }
  1694. //Remove Hero Weapons
  1695. // check to prevent the using of weapon/shield on strider/wyvern
  1696. L2ItemInstance wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  1697. if (wpn == null) wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LRHAND);
  1698. if (wpn != null && (wpn.isHeroItem() || wpn.isOlyRestrictedItem()))
  1699. {
  1700. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(wpn.getItem().getBodyPart());
  1701. InventoryUpdate iu = new InventoryUpdate();
  1702. for (int i = 0; i < unequiped.length; i++)
  1703. iu.addModifiedItem(unequiped[i]);
  1704. player.sendPacket(iu);
  1705. player.abortAttack();
  1706. player.broadcastUserInfo();
  1707. // this can be 0 if the user pressed the right mouse button twice very fast
  1708. if (unequiped.length > 0)
  1709. {
  1710. if (unequiped[0].isWear())
  1711. return;
  1712. SystemMessage sm = null;
  1713. if (unequiped[0].getEnchantLevel() > 0){
  1714. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  1715. sm.addNumber(unequiped[0].getEnchantLevel());
  1716. sm.addItemName(unequiped[0]);
  1717. }else{
  1718. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  1719. sm.addItemName(unequiped[0]);
  1720. }
  1721. player.sendPacket(sm);
  1722. }
  1723. }
  1724. //Remove shot automation
  1725. Map<Integer, Integer> activeSoulShots = player.getAutoSoulShot();
  1726. for (int itemId : activeSoulShots.values())
  1727. {
  1728. player.removeAutoSoulShot(itemId);
  1729. ExAutoSoulShot atk = new ExAutoSoulShot(itemId, 0);
  1730. player.sendPacket(atk);
  1731. }
  1732. player.sendSkillList();
  1733. }
  1734. catch (Exception e) {}
  1735. }
  1736. }
  1737. protected boolean portPlayersToArena()
  1738. {
  1739. boolean _playerOneCrash = (_playerOne == null || _playerOneDisconnected);
  1740. boolean _playerTwoCrash = (_playerTwo == null || _playerTwoDisconnected);
  1741. if (_playerOneCrash || _playerTwoCrash || _aborted)
  1742. {
  1743. _playerOne=null;
  1744. _playerTwo=null;
  1745. _aborted = true;
  1746. return false;
  1747. }
  1748. try {
  1749. x1 = _playerOne.getX();
  1750. y1 = _playerOne.getY();
  1751. z1 = _playerOne.getZ();
  1752. x2 = _playerTwo.getX();
  1753. y2 = _playerTwo.getY();
  1754. z2 = _playerTwo.getZ();
  1755. if (_playerOne.isSitting())
  1756. _playerOne.standUp();
  1757. if (_playerTwo.isSitting())
  1758. _playerTwo.standUp();
  1759. _playerOne.setTarget(null);
  1760. _playerTwo.setTarget(null);
  1761. _playerOne.teleToLocation(_stadiumPort[0]+1200, _stadiumPort[1], _stadiumPort[2], false);
  1762. _playerTwo.teleToLocation(_stadiumPort[0]-1200, _stadiumPort[1], _stadiumPort[2], false);
  1763. _playerOne.sendPacket(new ExOlympiadMode(2));
  1764. _playerTwo.sendPacket(new ExOlympiadMode(2));
  1765. _spawnOne = SpawnBuffer(_stadiumPort[0]+1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  1766. _spawnTwo = SpawnBuffer(_stadiumPort[0]-1100, _stadiumPort[1], _stadiumPort[2], OLY_BUFFER);
  1767. _playerOne.setIsInOlympiadMode(true);
  1768. _playerOne.setIsOlympiadStart(false);
  1769. _playerOne.setOlympiadSide(1);
  1770. _playerOne.olyBuff = 5;
  1771. _playerTwo.setIsInOlympiadMode(true);
  1772. _playerTwo.setIsOlympiadStart(false);
  1773. _playerTwo.setOlympiadSide(2);
  1774. _playerTwo.olyBuff = 5;
  1775. _gamestarted = true;
  1776. } catch (NullPointerException e)
  1777. {
  1778. return false;
  1779. }
  1780. return true;
  1781. }
  1782. protected void sendMessageToPlayers(boolean toBattleBegin, int nsecond)
  1783. {
  1784. if(!toBattleBegin)
  1785. _sm = new SystemMessage(SystemMessageId.YOU_WILL_ENTER_THE_OLYMPIAD_STADIUM_IN_S1_SECOND_S);
  1786. else
  1787. _sm = new SystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  1788. _sm.addNumber(nsecond);
  1789. try {
  1790. for (L2PcInstance player : _players)
  1791. player.sendPacket(_sm);
  1792. } catch (Exception e) {}
  1793. }
  1794. protected void portPlayersBack()
  1795. {
  1796. if (_playerOne != null)
  1797. _playerOne.teleToLocation(x1, y1, z1, true);
  1798. if (_playerTwo != null)
  1799. _playerTwo.teleToLocation(x2, y2, z2, true);
  1800. }
  1801. protected void PlayersStatusBack()
  1802. {
  1803. for (L2PcInstance player : _players)
  1804. {
  1805. try {
  1806. if(player.isDead() == true) { player.setIsDead(false); }
  1807. player.getStatus().startHpMpRegeneration();
  1808. player.setCurrentCp(player.getMaxCp());
  1809. player.setCurrentHp(player.getMaxHp());
  1810. player.setCurrentMp(player.getMaxMp());
  1811. player.setIsInOlympiadMode(false);
  1812. player.setIsOlympiadStart(false);
  1813. player.setOlympiadSide(-1);
  1814. player.setOlympiadGameId(-1);
  1815. player.sendPacket(new ExOlympiadMode(0));
  1816. //Add Clan Skills
  1817. if (player.getClan() != null)
  1818. {
  1819. for(L2Skill skill: player.getClan().getAllSkills())
  1820. {
  1821. if ( skill.getMinPledgeClass() <= player.getPledgeClass() )
  1822. player.addSkill(skill,false);
  1823. }
  1824. }
  1825. //Add Hero Skills
  1826. if (player.isHero())
  1827. {
  1828. for(L2Skill skill: HeroSkillTable.getHeroSkills())
  1829. player.addSkill(skill,false);
  1830. }
  1831. player.sendSkillList();
  1832. } catch (Exception e) {}
  1833. }
  1834. }
  1835. protected boolean haveWinner()
  1836. {
  1837. boolean retval = false;
  1838. if (_aborted || _playerOne == null || _playerTwo == null)
  1839. {
  1840. return true;
  1841. }
  1842. double playerOneHp = 0;
  1843. try{
  1844. if (_playerOne != null && _playerOne.getOlympiadGameId() != -1)
  1845. {
  1846. playerOneHp = _playerOne.getCurrentHp();
  1847. }
  1848. }catch (Exception e){
  1849. playerOneHp = 0;
  1850. }
  1851. double playerTwoHp = 0;
  1852. try{
  1853. if (_playerTwo != null && _playerTwo.getOlympiadGameId()!=-1)
  1854. {
  1855. playerTwoHp = _playerTwo.getCurrentHp();
  1856. }
  1857. }catch (Exception e){
  1858. playerTwoHp = 0;
  1859. }
  1860. if (playerTwoHp==0 || playerOneHp==0)
  1861. {
  1862. return true;
  1863. }
  1864. return retval;
  1865. }
  1866. protected void validateWinner()
  1867. {
  1868. if (_aborted || _playerOne == null || _playerTwo == null || _playerOneDisconnected || _playerTwoDisconnected)
  1869. {
  1870. return;
  1871. }
  1872. StatsSet playerOneStat;
  1873. StatsSet playerTwoStat;
  1874. playerOneStat = _nobles.get(_playerOneID);
  1875. playerTwoStat = _nobles.get(_playerTwoID);
  1876. int _div;
  1877. int _gpreward;
  1878. int playerOnePlayed = playerOneStat.getInteger(COMP_DONE);
  1879. int playerTwoPlayed = playerTwoStat.getInteger(COMP_DONE);
  1880. int playerOnePoints = playerOneStat.getInteger(POINTS);
  1881. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  1882. double playerOneHp = 0;
  1883. try
  1884. {
  1885. if (_playerOne != null && !_playerOneDisconnected)
  1886. {
  1887. if (!_playerOne.isDead())
  1888. {
  1889. playerOneHp = _playerOne.getCurrentHp()+_playerOne.getCurrentCp();
  1890. }
  1891. }
  1892. }
  1893. catch (Exception e)
  1894. {
  1895. playerOneHp = 0;
  1896. }
  1897. double playerTwoHp = 0;
  1898. try
  1899. {
  1900. if (_playerTwo != null && !_playerTwoDisconnected)
  1901. {
  1902. if (!_playerTwo.isDead())
  1903. {
  1904. playerTwoHp = _playerTwo.getCurrentHp()+_playerTwo.getCurrentCp();
  1905. }
  1906. }
  1907. }
  1908. catch (Exception e)
  1909. {
  1910. playerTwoHp = 0;
  1911. }
  1912. _sm = new SystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  1913. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  1914. _sm3 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  1915. String result = "";
  1916. // if players crashed, search if they've relogged
  1917. _playerOne = L2World.getInstance().getPlayer(_playerOneName);
  1918. _players.set(0, _playerOne);
  1919. _playerTwo = L2World.getInstance().getPlayer(_playerTwoName);
  1920. _players.set(1, _playerTwo);
  1921. switch(_type){
  1922. case NON_CLASSED: _div=5; _gpreward=Config.ALT_OLY_NONCLASSED_RITEM_C; break;
  1923. default: _div=3; _gpreward=Config.ALT_OLY_CLASSED_RITEM_C; break;
  1924. }
  1925. if (_playerOne == null && _playerTwo == null)
  1926. {
  1927. result=" tie";
  1928. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  1929. broadcastMessage(_sm, true);
  1930. }
  1931. else if (_playerTwo == null || _playerTwo.isOnline() == 0 || (playerTwoHp == 0 && playerOneHp != 0) || (_damageP1 > _damageP2 && playerTwoHp != 0 && playerOneHp != 0))
  1932. {
  1933. int pointDiff;
  1934. pointDiff = playerTwoPoints / _div;
  1935. playerOneStat.set(POINTS, playerOnePoints + pointDiff);
  1936. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  1937. _sm.addString(_playerOneName);
  1938. broadcastMessage(_sm, true);
  1939. _sm2.addString(_playerOneName);
  1940. _sm2.addNumber(pointDiff);
  1941. broadcastMessage(_sm2, true);
  1942. _sm3.addString(_playerTwoName);
  1943. _sm3.addNumber(pointDiff);
  1944. broadcastMessage(_sm3, true);
  1945. try {
  1946. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+_damageP1+"dmg vs "+_damageP2+"dmg) "+_playerOneName+" win "+pointDiff+" points";
  1947. L2ItemInstance item = _playerOne.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerOne, null);
  1948. InventoryUpdate iu = new InventoryUpdate();
  1949. iu.addModifiedItem(item);
  1950. _playerOne.sendPacket(iu);
  1951. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1952. sm.addItemName(item);
  1953. sm.addNumber(_gpreward);
  1954. _playerOne.sendPacket(sm);
  1955. } catch (Exception e) { }
  1956. }
  1957. else if (_playerOne == null || _playerOne.isOnline() == 0 || (playerOneHp == 0 && playerTwoHp != 0) || (_damageP2 > _damageP1 && playerOneHp != 0 && playerTwoHp != 0))
  1958. {
  1959. int pointDiff;
  1960. pointDiff = playerOnePoints / _div;
  1961. playerTwoStat.set(POINTS, playerTwoPoints + pointDiff);
  1962. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  1963. _sm.addString(_playerTwoName);
  1964. broadcastMessage(_sm, true);
  1965. _sm2.addString(_playerTwoName);
  1966. _sm2.addNumber(pointDiff);
  1967. broadcastMessage(_sm2, true);
  1968. _sm3.addString(_playerOneName);
  1969. _sm3.addNumber(pointDiff);
  1970. broadcastMessage(_sm3, true);
  1971. try {
  1972. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+_damageP1+"dmg vs "+_damageP2+"dmg) "+_playerTwoName+" win "+pointDiff+" points";
  1973. L2ItemInstance item = _playerTwo.getInventory().addItem("Olympiad", Config.ALT_OLY_BATTLE_REWARD_ITEM, _gpreward, _playerTwo, null);
  1974. InventoryUpdate iu = new InventoryUpdate();
  1975. iu.addModifiedItem(item);
  1976. _playerTwo.sendPacket(iu);
  1977. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1978. sm.addItemName(item);
  1979. sm.addNumber(_gpreward);
  1980. _playerTwo.sendPacket(sm);
  1981. } catch (Exception e) { }
  1982. }
  1983. else
  1984. {
  1985. result=" tie";
  1986. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  1987. broadcastMessage(_sm, true);
  1988. }
  1989. if (Config.DEBUG)
  1990. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... "+result);
  1991. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  1992. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  1993. _nobles.remove(_playerOneID);
  1994. _nobles.remove(_playerTwoID);
  1995. _nobles.put(_playerOneID, playerOneStat);
  1996. _nobles.put(_playerTwoID, playerTwoStat);
  1997. for (int i=40;i>10;i-=10)
  1998. {
  1999. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  2000. _sm.addNumber(i);
  2001. broadcastMessage(_sm, false);
  2002. try{ Thread.sleep(10000); }catch (InterruptedException e){}
  2003. if (i==20) {
  2004. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  2005. _sm.addNumber(10);
  2006. broadcastMessage(_sm, false);
  2007. try{ Thread.sleep(5000); }catch (InterruptedException e){}
  2008. }
  2009. }
  2010. for (int i=5;i>0;i--)
  2011. {
  2012. _sm = new SystemMessage(SystemMessageId.YOU_WILL_BE_MOVED_TO_TOWN_IN_S1_SECONDS);
  2013. _sm.addNumber(i);
  2014. broadcastMessage(_sm, false);
  2015. try{ Thread.sleep(1000); }catch (InterruptedException e){}
  2016. }
  2017. }
  2018. protected void additions()
  2019. {
  2020. for (L2PcInstance player : _players)
  2021. {
  2022. try {
  2023. //Set HP/CP/MP to Max
  2024. player.setCurrentCp(player.getMaxCp());
  2025. player.setCurrentHp(player.getMaxHp());
  2026. player.setCurrentMp(player.getMaxMp());
  2027. } catch (Exception e) { }
  2028. finally
  2029. {
  2030. _damageP1 = 0;
  2031. _damageP2 = 0;
  2032. }
  2033. }
  2034. }
  2035. protected boolean makePlayersVisible()
  2036. {
  2037. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  2038. try {
  2039. for (L2PcInstance player : _players)
  2040. {
  2041. player.getAppearance().setVisible();
  2042. player.broadcastUserInfo();
  2043. player.sendPacket(_sm);
  2044. if (player.getPet() != null)
  2045. player.getPet().updateAbnormalEffect();
  2046. }
  2047. } catch (NullPointerException e) { _aborted = true; return false; }
  2048. return true;
  2049. }
  2050. protected boolean makeCompetitionStart()
  2051. {
  2052. if (_aborted) return false;
  2053. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  2054. broadcastMessage(_sm, true);
  2055. try {
  2056. for (L2PcInstance player : _players)
  2057. {
  2058. player.setIsOlympiadStart(true);
  2059. }
  2060. } catch (Exception e) {
  2061. _aborted = true;
  2062. return false;
  2063. }
  2064. return true;
  2065. }
  2066. protected String getTitle()
  2067. {
  2068. String msg = "";
  2069. msg+= _playerOneName + " : " + _playerTwoName;
  2070. return msg;
  2071. }
  2072. protected L2PcInstance[] getPlayers()
  2073. {
  2074. L2PcInstance[] players = new L2PcInstance[2];
  2075. if (_playerOne == null || _playerTwo == null) return null;
  2076. players[0] = _playerOne;
  2077. players[1] = _playerTwo;
  2078. return players;
  2079. }
  2080. protected L2FastList<L2PcInstance> getSpectators()
  2081. {
  2082. return _spectators;
  2083. }
  2084. protected void addSpectator(L2PcInstance spec)
  2085. {
  2086. _spectators.add(spec);
  2087. }
  2088. protected void removeSpectator(L2PcInstance spec)
  2089. {
  2090. if (_spectators != null && _spectators.contains(spec))
  2091. _spectators.remove(spec);
  2092. }
  2093. protected void clearSpectators()
  2094. {
  2095. if (_spectators != null)
  2096. {
  2097. for (L2PcInstance pc : _spectators)
  2098. {
  2099. try {
  2100. if(!pc.inObserverMode()) continue;
  2101. pc.leaveOlympiadObserverMode();
  2102. } catch (NullPointerException e) {}
  2103. }
  2104. _spectators.clear();
  2105. }
  2106. }
  2107. private void broadcastMessage(SystemMessage sm, boolean toAll)
  2108. {
  2109. try {
  2110. _playerOne.sendPacket(sm);
  2111. _playerTwo.sendPacket(sm);
  2112. } catch (Exception e) {}
  2113. if (toAll && _spectators != null)
  2114. {
  2115. for (L2PcInstance spec : _spectators)
  2116. {
  2117. try { spec.sendPacket(sm); } catch (NullPointerException e) {}
  2118. }
  2119. }
  2120. }
  2121. }
  2122. }