Olympiad.java 81 KB

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