Olympiad.java 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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.List;
  30. import java.util.Map;
  31. import java.util.Properties;
  32. import java.util.concurrent.ScheduledFuture;
  33. import java.util.logging.Logger;
  34. import javolution.util.FastList;
  35. import javolution.util.FastMap;
  36. import net.sf.l2j.Config;
  37. import net.sf.l2j.L2DatabaseFactory;
  38. import net.sf.l2j.gameserver.datatables.HeroSkillTable;
  39. import net.sf.l2j.gameserver.datatables.SkillTable;
  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.L2Summon;
  46. import net.sf.l2j.gameserver.model.L2World;
  47. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  48. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  49. import net.sf.l2j.gameserver.model.entity.Hero;
  50. import net.sf.l2j.gameserver.network.SystemMessageId;
  51. import net.sf.l2j.gameserver.serverpackets.ExOlympiadUserInfoSpectator;
  52. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  53. import net.sf.l2j.gameserver.serverpackets.MagicSkillUse;
  54. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  55. import net.sf.l2j.gameserver.templates.StatsSet;
  56. import net.sf.l2j.util.Rnd;
  57. public class Olympiad
  58. {
  59. protected static final Logger _log = Logger.getLogger(Olympiad.class.getName());
  60. private static Olympiad _instance;
  61. protected static Map<Integer, StatsSet> _nobles;
  62. protected static List<StatsSet> _heroesToBe;
  63. protected static List<L2PcInstance> _nonClassBasedRegisters;
  64. protected static Map<Integer, List<L2PcInstance>> _classBasedRegisters;
  65. private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties";
  66. public static final String OLYMPIAD_HTML_FILE = "data/html/olympiad/";
  67. private static final String OLYMPIAD_LOAD_NOBLES = "SELECT * from olympiad_nobles";
  68. private static final String OLYMPIAD_SAVE_NOBLES = "INSERT INTO olympiad_nobles " +
  69. "values (?,?,?,?,?)";
  70. private static final String OLYMPIAD_UPDATE_NOBLES = "UPDATE olympiad_nobles set " +
  71. "olympiad_points = ?, competitions_done = ? where char_id = ?";
  72. private static final String OLYMPIAD_GET_HEROS = "SELECT char_id, char_name from " +
  73. "olympiad_nobles where class_id = ? and competitions_done >= 9 order by " +
  74. "olympiad_points desc, competitions_done desc";
  75. private static final String GET_EACH_CLASS_LEADER = "SELECT char_name from " +
  76. "olympiad_nobles where class_id = ? order by olympiad_points desc, " +
  77. "competitions_done desc";
  78. private static final String OLYMPIAD_DELETE_ALL = "DELETE from olympiad_nobles";
  79. private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
  80. private static final int COMP_MIN = Config.ALT_OLY_MIN; // 00 mins
  81. private static final long COMP_PERIOD = Config.ALT_OLY_CPERIOD; // 6 hours
  82. protected static final long BATTLE_PERIOD = Config.ALT_OLY_BATTLE; // 6 mins
  83. protected static final long BATTLE_WAIT = Config.ALT_OLY_BWAIT; // 10mins
  84. protected static final long INITIAL_WAIT = Config.ALT_OLY_IWAIT; // 5mins
  85. protected static final long WEEKLY_PERIOD = Config.ALT_OLY_WPERIOD; // 1 week
  86. protected static final long VALIDATION_PERIOD = Config.ALT_OLY_VPERIOD; // 24 hours
  87. /* FOR TESTING
  88. private static final int COMP_START = 8; // 1PM - 2PM
  89. private static final int COMP_MIN = 15; // 20mins
  90. private static final long COMP_PERIOD = 7200000; // 2hours
  91. private static final long BATTLE_PERIOD = 180000; // 3mins
  92. private static final long BATTLE_WAIT = 600000; // 10mins
  93. private static final long INITIAL_WAIT = 300000; // 5mins
  94. private static final long WEEKLY_PERIOD = 7200000; // 2 hours
  95. private static final long VALIDATION_PERIOD = 3600000; // 1 hour */
  96. private static final int COLLISIEUMS = 11; // 22 in all, 11 each for (Classed and NonClassed)
  97. private static final int DEFAULT_POINTS = 18;
  98. protected static final int WEEKLY_POINTS = 3;
  99. public static final String CHAR_ID = "char_id";
  100. public static final String CLASS_ID = "class_id";
  101. public static final String CHAR_NAME = "char_name";
  102. public static final String POINTS = "olympiad_points";
  103. public static final String COMP_DONE = "competitions_done";
  104. protected long _olympiadEnd;
  105. protected long _validationEnd;
  106. protected int _period;
  107. protected long _nextWeeklyChange;
  108. protected int _currentCycle;
  109. private long _compEnd;
  110. private Calendar _compStart;
  111. protected static boolean _inCompPeriod;
  112. protected static boolean _isOlympiadEnd;
  113. protected static boolean _compStarted;
  114. protected static boolean _battleStarted;
  115. protected ScheduledFuture<?> _scheduledCompStart;
  116. protected ScheduledFuture<?> _scheduledCompEnd;
  117. protected ScheduledFuture<?> _scheduledOlympiadEnd;
  118. protected ScheduledFuture<?> _scheduledManagerTask;
  119. protected ScheduledFuture<?> _scheduledWeeklyTask;
  120. protected ScheduledFuture<?> _scheduledValdationTask;
  121. protected static final int[][] STADIUMS =
  122. {
  123. {-20814, -21189, -3030},
  124. {-120324, -225077, -3331},
  125. {-102495, -209023, -3331},
  126. {-120156, -207378, -3331},
  127. {-87628, -225021, -3331},
  128. {-81705, -213209, -3331},
  129. {-87593, -207339, -3331},
  130. {-93709, -218304, -3331},
  131. {-77157, -218608, -3331},
  132. {-69682, -209027, -3331},
  133. {-76887, -201256, -3331},
  134. {-109985, -218701, -3331},
  135. {-126367, -218228, -3331},
  136. {-109629, -201292, -3331},
  137. {-87523, -240169, -3331},
  138. {-81748, -245950, -3331},
  139. {-77123, -251473, -3331},
  140. {-69778, -241801, -3331},
  141. {-76754, -234014, -3331},
  142. {-93742, -251032, -3331},
  143. {-87466, -257752, -3331},
  144. {-114413, -213241, -3331}
  145. };
  146. private static enum COMP_TYPE
  147. {
  148. CLASSED,
  149. NON_CLASSED
  150. }
  151. protected static OlympiadManager _manager;
  152. public static Olympiad getInstance()
  153. {
  154. if (_instance == null)
  155. _instance = new Olympiad();
  156. return _instance;
  157. }
  158. public Olympiad()
  159. {
  160. try
  161. {
  162. load();
  163. }
  164. catch(IOException e)
  165. {
  166. e.printStackTrace();
  167. }
  168. catch(SQLException s)
  169. {
  170. s.printStackTrace();
  171. }
  172. if (_period == 0) init();
  173. }
  174. private void load() throws IOException, SQLException
  175. {
  176. _nobles = new FastMap<Integer, StatsSet>();
  177. Properties OlympiadProperties = new Properties();
  178. InputStream is = new FileInputStream(new File("./" + OLYMPIAD_DATA_FILE));
  179. OlympiadProperties.load(is);
  180. is.close();
  181. _currentCycle = Integer.parseInt(OlympiadProperties.getProperty("CurrentCycle", "1"));
  182. _period = Integer.parseInt(OlympiadProperties.getProperty("Period", "0"));
  183. _olympiadEnd = Long.parseLong(OlympiadProperties.getProperty("OlympiadEnd", "0"));
  184. _validationEnd = Long.parseLong(OlympiadProperties.getProperty("ValdationEnd", "0"));
  185. _nextWeeklyChange = Long.parseLong(OlympiadProperties.getProperty("NextWeeklyChange", "0"));
  186. switch(_period)
  187. {
  188. case 0:
  189. if (_olympiadEnd == 0 || _olympiadEnd < Calendar.getInstance().getTimeInMillis())
  190. setNewOlympiadEnd();
  191. else
  192. _isOlympiadEnd = false;
  193. break;
  194. case 1:
  195. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  196. {
  197. _isOlympiadEnd = true;
  198. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  199. public void run()
  200. {
  201. _period = 0;
  202. _currentCycle++;
  203. deleteNobles();
  204. setNewOlympiadEnd();
  205. init();
  206. }
  207. }, getMillisToValidationEnd());
  208. }
  209. else
  210. {
  211. _currentCycle++;
  212. _period = 0;
  213. deleteNobles();
  214. setNewOlympiadEnd();
  215. }
  216. break;
  217. default:
  218. _log.warning("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  219. return;
  220. }
  221. try
  222. {
  223. Connection con = L2DatabaseFactory.getInstance().getConnection();
  224. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
  225. ResultSet rset = statement.executeQuery();
  226. while(rset.next())
  227. {
  228. StatsSet statDat = new StatsSet();
  229. int charId = rset.getInt(CHAR_ID);
  230. statDat.set(CLASS_ID, rset.getInt(CLASS_ID));
  231. statDat.set(CHAR_NAME, rset.getString(CHAR_NAME));
  232. statDat.set(POINTS, rset.getInt(POINTS));
  233. statDat.set(COMP_DONE, rset.getInt(COMP_DONE));
  234. statDat.set("to_save", false);
  235. _nobles.put(charId, statDat);
  236. }
  237. rset.close();
  238. statement.close();
  239. con.close();
  240. }
  241. catch(Exception e)
  242. {
  243. e.printStackTrace();
  244. }
  245. synchronized(this)
  246. {
  247. _log.info("Olympiad System: Loading Olympiad System....");
  248. if (_period == 0)
  249. _log.info("Olympiad System: Currently in Olympiad Period");
  250. else
  251. _log.info("Olympiad System: Currently in Validation Period");
  252. _log.info("Olympiad System: Period Ends....");
  253. long milliToEnd;
  254. if (_period == 0)
  255. milliToEnd = getMillisToOlympiadEnd();
  256. else
  257. milliToEnd = getMillisToValidationEnd();
  258. double numSecs = (milliToEnd / 1000) % 60;
  259. double countDown = ((milliToEnd / 1000) - numSecs) / 60;
  260. int numMins = (int) Math.floor(countDown % 60);
  261. countDown = (countDown - numMins) / 60;
  262. int numHours = (int) Math.floor(countDown % 24);
  263. int numDays = (int) Math.floor((countDown - numHours) / 24);
  264. _log.info("Olympiad System: In " + numDays + " days, " + numHours
  265. + " hours and " + numMins + " mins.");
  266. if (_period == 0)
  267. {
  268. _log.info("Olympiad System: Next Weekly Change is in....");
  269. milliToEnd = getMillisToWeekChange();
  270. double numSecs2 = (milliToEnd / 1000) % 60;
  271. double countDown2 = ((milliToEnd / 1000) - numSecs2) / 60;
  272. int numMins2 = (int) Math.floor(countDown2 % 60);
  273. countDown2 = (countDown2 - numMins2) / 60;
  274. int numHours2 = (int) Math.floor(countDown2 % 24);
  275. int numDays2 = (int) Math.floor((countDown2 - numHours2) / 24);
  276. _log.info("Olympiad System: " + numDays2 + " days, " + numHours2
  277. + " hours and " + numMins2 + " mins.");
  278. }
  279. }
  280. _log.info("Olympiad System: Loaded " + _nobles.size() + " Nobles");
  281. }
  282. protected void init()
  283. {
  284. if (_period == 1)
  285. return;
  286. _nonClassBasedRegisters = new FastList<L2PcInstance>();
  287. _classBasedRegisters = new FastMap<Integer, List<L2PcInstance>>();
  288. _compStart = Calendar.getInstance();
  289. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  290. _compStart.set(Calendar.MINUTE, COMP_MIN);
  291. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  292. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  293. public void run()
  294. {
  295. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  296. sm.addNumber(_currentCycle);
  297. Announcements.getInstance().announceToAll(sm);
  298. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  299. _isOlympiadEnd = true;
  300. if (_scheduledManagerTask != null)
  301. _scheduledManagerTask.cancel(true);
  302. if (_scheduledWeeklyTask != null)
  303. _scheduledWeeklyTask.cancel(true);
  304. Calendar validationEnd = Calendar.getInstance();
  305. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  306. saveNobleData();
  307. _period = 1;
  308. sortHerosToBe();
  309. giveHeroBonus();
  310. Hero.getInstance().computeNewHeroes(_heroesToBe);
  311. try {
  312. save();
  313. }
  314. catch (Exception e) {
  315. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  316. }
  317. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  318. public void run()
  319. {
  320. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  321. _period = 0;
  322. _currentCycle++;
  323. deleteNobles();
  324. setNewOlympiadEnd();
  325. init();
  326. }
  327. }, getMillisToValidationEnd());
  328. }
  329. }, getMillisToOlympiadEnd());
  330. updateCompStatus();
  331. scheduleWeeklyChange();
  332. }
  333. public boolean registerNoble(L2PcInstance noble, boolean classBased)
  334. {
  335. SystemMessage sm;
  336. if (_compStarted)
  337. {
  338. noble.sendMessage("Cant Register whilst competition is under way");
  339. return false;
  340. }
  341. if (!_inCompPeriod)
  342. {
  343. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  344. noble.sendPacket(sm);
  345. return false;
  346. }
  347. if (noble.isCursedWeaponEquiped())
  348. {
  349. noble.sendMessage("You can't participate to Olympiad while holding a cursed weapon.");
  350. return false;
  351. }
  352. if (!noble.isNoble())
  353. {
  354. sm = new SystemMessage(SystemMessageId.ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  355. noble.sendPacket(sm);
  356. return false;
  357. }
  358. if (noble.getBaseClass() != noble.getClassId().getId())
  359. {
  360. sm = new SystemMessage(SystemMessageId.YOU_CANT_JOIN_THE_OLYMPIAD_WITH_A_SUB_JOB_CHARACTER);
  361. noble.sendPacket(sm);
  362. return false;
  363. }
  364. if (!_nobles.containsKey(noble.getObjectId()))
  365. {
  366. StatsSet statDat = new StatsSet();
  367. statDat.set(CLASS_ID, noble.getClassId().getId());
  368. statDat.set(CHAR_NAME, noble.getName());
  369. statDat.set(POINTS, DEFAULT_POINTS);
  370. statDat.set(COMP_DONE, 0);
  371. statDat.set("to_save", true);
  372. _nobles.put(noble.getObjectId(), statDat);
  373. }
  374. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  375. {
  376. List<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  377. for (L2PcInstance partecipant: classed)
  378. {
  379. if (partecipant.getObjectId()==noble.getObjectId())
  380. {
  381. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_TO_PARTICIPATE_IN_THE_GAME_FOR_YOUR_CLASS);
  382. noble.sendPacket(sm);
  383. return false;
  384. }
  385. }
  386. }
  387. for (L2PcInstance partecipant: _nonClassBasedRegisters)
  388. {
  389. if (partecipant.getObjectId()==noble.getObjectId())
  390. {
  391. sm = new SystemMessage(SystemMessageId.YOU_ARE_ALREADY_ON_THE_WAITING_LIST_FOR_ALL_CLASSES_WAITING_TO_PARTICIPATE_IN_THE_GAME);
  392. noble.sendPacket(sm);
  393. return false;
  394. }
  395. }
  396. if (getNoblePoints(noble.getObjectId()) < 3)
  397. {
  398. noble.sendMessage("Cant register when you have less than 3 points");
  399. return false;
  400. }
  401. if (classBased)
  402. {
  403. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  404. {
  405. List<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  406. classed.add(noble);
  407. _classBasedRegisters.remove(noble.getClassId().getId());
  408. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  409. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  410. noble.sendPacket(sm);
  411. }
  412. else
  413. {
  414. List<L2PcInstance> classed = new FastList<L2PcInstance>();
  415. classed.add(noble);
  416. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  417. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  418. noble.sendPacket(sm);
  419. }
  420. }
  421. else
  422. {
  423. _nonClassBasedRegisters.add(noble);
  424. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  425. noble.sendPacket(sm);
  426. }
  427. return true;
  428. }
  429. public boolean isRegistered(L2PcInstance noble)
  430. {
  431. if(_nonClassBasedRegisters == null) return false;
  432. if(_classBasedRegisters == null) return false;
  433. if (!_nonClassBasedRegisters.contains(noble))
  434. {
  435. if (!_classBasedRegisters.containsKey(noble.getClassId().getId()))
  436. {
  437. return false;
  438. }
  439. else
  440. {
  441. List<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  442. if (!classed.contains(noble))
  443. {
  444. return false;
  445. }
  446. }
  447. }
  448. return true;
  449. }
  450. public boolean unRegisterNoble(L2PcInstance noble)
  451. {
  452. SystemMessage sm;
  453. if (_compStarted)
  454. {
  455. noble.sendMessage("Cant Unregister whilst competition is under way");
  456. return false;
  457. }
  458. if (!_inCompPeriod)
  459. {
  460. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  461. noble.sendPacket(sm);
  462. return false;
  463. }
  464. if (!noble.isNoble())
  465. {
  466. sm = new SystemMessage(SystemMessageId.ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  467. noble.sendPacket(sm);
  468. return false;
  469. }
  470. if (!isRegistered(noble))
  471. {
  472. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  473. noble.sendPacket(sm);
  474. return false;
  475. }
  476. if (_nonClassBasedRegisters.contains(noble))
  477. _nonClassBasedRegisters.remove(noble);
  478. else
  479. {
  480. List<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  481. classed.remove(noble);
  482. _classBasedRegisters.remove(noble.getClassId().getId());
  483. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  484. }
  485. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  486. noble.sendPacket(sm);
  487. return true;
  488. }
  489. private void updateCompStatus()
  490. {
  491. _compStarted = false;
  492. synchronized(this)
  493. {
  494. long milliToStart = getMillisToCompBegin();
  495. double numSecs = (milliToStart / 1000) % 60;
  496. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  497. int numMins = (int) Math.floor(countDown % 60);
  498. countDown = (countDown - numMins) / 60;
  499. int numHours = (int) Math.floor(countDown % 24);
  500. int numDays = (int) Math.floor((countDown - numHours) / 24);
  501. _log.info("Olympiad System: Competition Period Starts in "
  502. + numDays + " days, " + numHours
  503. + " hours and " + numMins + " mins.");
  504. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  505. }
  506. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  507. public void run()
  508. {
  509. if (isOlympiadEnd())
  510. return;
  511. _inCompPeriod = true;
  512. OlympiadManager om = new OlympiadManager();
  513. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  514. _log.info("Olympiad System: Olympiad Game Started");
  515. _scheduledManagerTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(om, INITIAL_WAIT, BATTLE_WAIT);
  516. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
  517. public void run()
  518. {
  519. if (isOlympiadEnd())
  520. return;
  521. _scheduledManagerTask.cancel(true);
  522. _inCompPeriod = false;
  523. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  524. _log.info("Olympiad System: Olympiad Game Ended");
  525. try {
  526. save();
  527. }
  528. catch (Exception e) {
  529. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  530. }
  531. init();
  532. }
  533. }, getMillisToCompEnd());
  534. }
  535. }, getMillisToCompBegin());
  536. }
  537. private long getMillisToOlympiadEnd()
  538. {
  539. //if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  540. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  541. //return 10L;
  542. }
  543. public void manualSelectHeroes()
  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. if(_scheduledOlympiadEnd != null)
  555. _scheduledOlympiadEnd.cancel(true);
  556. Calendar validationEnd = Calendar.getInstance();
  557. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  558. saveNobleData();
  559. _period = 1;
  560. sortHerosToBe();
  561. giveHeroBonus();
  562. Hero.getInstance().computeNewHeroes(_heroesToBe);
  563. try {
  564. save();
  565. }
  566. catch (Exception e) {
  567. _log.warning("Olympiad System: Failed to save Olympiad configuration: " + e);
  568. }
  569. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  570. public void run()
  571. {
  572. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  573. _period = 0;
  574. _currentCycle++;
  575. deleteNobles();
  576. setNewOlympiadEnd();
  577. init();
  578. }
  579. }, getMillisToValidationEnd());
  580. }
  581. protected long getMillisToValidationEnd()
  582. {
  583. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  584. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  585. return 10L;
  586. }
  587. public boolean isOlympiadEnd()
  588. {
  589. return _isOlympiadEnd;
  590. }
  591. protected void setNewOlympiadEnd()
  592. {
  593. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  594. sm.addNumber(_currentCycle);
  595. Announcements.getInstance().announceToAll(sm);
  596. Calendar currentTime = Calendar.getInstance();
  597. currentTime.add(Calendar.MONTH, 1);
  598. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  599. currentTime.set(Calendar.AM_PM, Calendar.AM);
  600. currentTime.set(Calendar.HOUR, 12);
  601. currentTime.set(Calendar.MINUTE, 0);
  602. currentTime.set(Calendar.SECOND, 0);
  603. _olympiadEnd = currentTime.getTimeInMillis();
  604. Calendar nextChange = Calendar.getInstance();
  605. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  606. _isOlympiadEnd = false;
  607. }
  608. public boolean inCompPeriod()
  609. {
  610. return _inCompPeriod;
  611. }
  612. private long getMillisToCompBegin()
  613. {
  614. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() &&
  615. _compEnd > Calendar.getInstance().getTimeInMillis())
  616. return 10L;
  617. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  618. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  619. return setNewCompBegin();
  620. }
  621. private long setNewCompBegin()
  622. {
  623. _compStart = Calendar.getInstance();
  624. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  625. _compStart.set(Calendar.MINUTE, COMP_MIN);
  626. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  627. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  628. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  629. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  630. }
  631. protected long getMillisToCompEnd()
  632. {
  633. //if (_compEnd > Calendar.getInstance().getTimeInMillis())
  634. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  635. //return 10L;
  636. }
  637. private long getMillisToWeekChange()
  638. {
  639. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  640. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  641. return 10L;
  642. }
  643. private void scheduleWeeklyChange()
  644. {
  645. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  646. public void run()
  647. {
  648. addWeeklyPoints();
  649. _log.info("Olympiad System: Added weekly points to nobles");
  650. Calendar nextChange = Calendar.getInstance();
  651. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  652. }
  653. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  654. }
  655. protected synchronized void addWeeklyPoints()
  656. {
  657. if (_period == 1)
  658. return;
  659. for (Integer nobleId : _nobles.keySet())
  660. {
  661. StatsSet nobleInfo = _nobles.get(nobleId);
  662. int currentPoints = nobleInfo.getInteger(POINTS);
  663. currentPoints += WEEKLY_POINTS;
  664. nobleInfo.set(POINTS, currentPoints);
  665. _nobles.remove(nobleId);
  666. _nobles.put(nobleId, nobleInfo);
  667. }
  668. }
  669. public String[] getMatchList()
  670. {
  671. return (_manager == null)? null : _manager.getAllTitles();
  672. }
  673. public int getCurrentCycle()
  674. {
  675. return _currentCycle;
  676. }
  677. public void addSpectator(int id, L2PcInstance spectator)
  678. {
  679. if (_manager == null || (_manager.getOlympiadInstance(id) == null) || !_battleStarted)
  680. {
  681. spectator.sendPacket(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS));
  682. return;
  683. }
  684. L2PcInstance[] players = _manager.getOlympiadInstance(id).getPlayers();
  685. if (players == null) return;
  686. spectator.enterOlympiadObserverMode(STADIUMS[id][0], STADIUMS[id][1], STADIUMS[id][2], id);
  687. _manager.getOlympiadInstance(id).addSpectator(spectator);
  688. spectator.sendPacket(new ExOlympiadUserInfoSpectator(players[0], 2));
  689. spectator.sendPacket(new ExOlympiadUserInfoSpectator(players[1], 1));
  690. }
  691. public void removeSpectator(int id, L2PcInstance spectator)
  692. {
  693. if (_manager == null || (_manager.getOlympiadInstance(id) == null)) return;
  694. _manager.getOlympiadInstance(id).removeSpectator(spectator);
  695. }
  696. public List<L2PcInstance> getSpectators(int id)
  697. {
  698. return _manager.getOlympiadInstance(id).getSpectators();
  699. }
  700. public Map<Integer, L2OlympiadGame> getOlympiadGames()
  701. {
  702. return _manager.getOlympiadGames();
  703. }
  704. public boolean playerInStadia(L2PcInstance player)
  705. {
  706. return (OlympiadStadiaManager.getInstance().getStadium(player) != null);
  707. }
  708. public int[] getWaitingList()
  709. {
  710. int[] array = new int[2];
  711. if (!inCompPeriod())
  712. return null;
  713. int classCount = 0;
  714. if (_classBasedRegisters.size() != 0)
  715. for (List<L2PcInstance> classed : _classBasedRegisters.values())
  716. {
  717. classCount += classed.size();
  718. }
  719. array[0] = classCount;
  720. array[1] = _nonClassBasedRegisters.size();
  721. return array;
  722. }
  723. protected synchronized void saveNobleData()
  724. {
  725. Connection con = null;
  726. if (_nobles == null)
  727. return;
  728. try
  729. {
  730. con = L2DatabaseFactory.getInstance().getConnection();
  731. PreparedStatement statement;
  732. for (Integer nobleId : _nobles.keySet())
  733. {
  734. StatsSet nobleInfo = _nobles.get(nobleId);
  735. int charId = nobleId;
  736. int classId = nobleInfo.getInteger(CLASS_ID);
  737. String charName = nobleInfo.getString(CHAR_NAME);
  738. int points = nobleInfo.getInteger(POINTS);
  739. int compDone = nobleInfo.getInteger(COMP_DONE);
  740. boolean toSave = nobleInfo.getBool("to_save");
  741. if (toSave)
  742. {
  743. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  744. statement.setInt(1, charId);
  745. statement.setInt(2, classId);
  746. statement.setString(3, charName);
  747. statement.setInt(4, points);
  748. statement.setInt(5, compDone);
  749. statement.execute();
  750. statement.close();
  751. nobleInfo.set("to_save", false);
  752. _nobles.remove(nobleId);
  753. _nobles.put(nobleId, nobleInfo);
  754. }
  755. else
  756. {
  757. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  758. statement.setInt(1, points);
  759. statement.setInt(2, compDone);
  760. statement.setInt(3, charId);
  761. statement.execute();
  762. statement.close();
  763. }
  764. }
  765. }
  766. catch(SQLException e) {_log.warning("Olympiad System: Couldnt save nobles info in db");}
  767. finally
  768. {
  769. try{con.close();}catch(Exception e){e.printStackTrace();}
  770. }
  771. }
  772. protected void sortHerosToBe()
  773. {
  774. if (_period != 1) return;
  775. _heroesToBe = new FastList<StatsSet>();
  776. Connection con = null;
  777. try
  778. {
  779. con = L2DatabaseFactory.getInstance().getConnection();
  780. PreparedStatement statement;
  781. ResultSet rset;
  782. StatsSet hero;
  783. for (int i = 88; i < 119; i++)
  784. {
  785. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  786. statement.setInt(1, i);
  787. rset = statement.executeQuery();
  788. if (rset.next())
  789. {
  790. hero = new StatsSet();
  791. hero.set(CLASS_ID, i);
  792. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  793. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  794. _heroesToBe.add(hero);
  795. }
  796. statement.close();
  797. rset.close();
  798. }
  799. }
  800. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  801. finally
  802. {
  803. try{con.close();}catch(Exception e){e.printStackTrace();}
  804. }
  805. }
  806. public List<String> getClassLeaderBoard(int classId)
  807. {
  808. //if (_period != 1) return;
  809. List<String> names = new FastList<String>();
  810. Connection con = null;
  811. try
  812. {
  813. con = L2DatabaseFactory.getInstance().getConnection();
  814. PreparedStatement statement;
  815. ResultSet rset;
  816. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  817. statement.setInt(1, classId);
  818. rset = statement.executeQuery();
  819. while (rset.next())
  820. {
  821. names.add(rset.getString(CHAR_NAME));
  822. }
  823. statement.close();
  824. rset.close();
  825. return names;
  826. }
  827. catch(SQLException e){_log.warning("Olympiad System: Couldnt heros from db");}
  828. finally
  829. {
  830. try{con.close();}catch(Exception e){e.printStackTrace();}
  831. }
  832. return names;
  833. }
  834. protected void giveHeroBonus()
  835. {
  836. if (_heroesToBe.size() == 0)
  837. return;
  838. for (StatsSet hero : _heroesToBe)
  839. {
  840. int charId = hero.getInteger(CHAR_ID);
  841. StatsSet noble = _nobles.get(charId);
  842. int currentPoints = noble.getInteger(POINTS);
  843. currentPoints += 300;
  844. noble.set(POINTS, currentPoints);
  845. _nobles.remove(charId);
  846. _nobles.put(charId, noble);
  847. }
  848. }
  849. public int getNoblessePasses(int objId)
  850. {
  851. if (_period != 1 || _nobles.size() == 0)
  852. return 0;
  853. StatsSet noble = _nobles.get(objId);
  854. if (noble == null)
  855. return 0;
  856. int points = noble.getInteger(POINTS);
  857. if (points <= 50)
  858. return 0;
  859. noble.set(POINTS, 0);
  860. _nobles.remove(objId);
  861. _nobles.put(objId, noble);
  862. points *= 1000;
  863. return points;
  864. }
  865. public boolean isRegisteredInComp(L2PcInstance player)
  866. {
  867. boolean result = false;
  868. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(player))
  869. result = true;
  870. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(player.getClassId().getId()))
  871. {
  872. List<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  873. if (classed.contains(player))
  874. result = true;
  875. }
  876. return result;
  877. }
  878. public int getNoblePoints(int objId)
  879. {
  880. if (_nobles.size() == 0)
  881. return 0;
  882. StatsSet noble = _nobles.get(objId);
  883. if (noble == null)
  884. return 0;
  885. int points = noble.getInteger(POINTS);
  886. return points;
  887. }
  888. public int getCompetitionDone(int objId)
  889. {
  890. if (_nobles.size() == 0)
  891. return 0;
  892. StatsSet noble = _nobles.get(objId);
  893. if (noble == null)
  894. return 0;
  895. int points = noble.getInteger(COMP_DONE);
  896. return points;
  897. }
  898. protected void deleteNobles()
  899. {
  900. Connection con = null;
  901. try
  902. {
  903. con = L2DatabaseFactory.getInstance().getConnection();
  904. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  905. statement.execute();
  906. statement.close();
  907. }
  908. catch(SQLException e){_log.warning("Olympiad System: Couldnt delete nobles from db");}
  909. finally
  910. {
  911. try{con.close();}catch(Exception e){e.printStackTrace();}
  912. }
  913. _nobles.clear();
  914. }
  915. public void save() throws IOException
  916. {
  917. saveNobleData();
  918. Properties OlympiadProperties = new Properties();
  919. FileOutputStream fos = new FileOutputStream(new File(Config.DATAPACK_ROOT, OLYMPIAD_DATA_FILE));
  920. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  921. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  922. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  923. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  924. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  925. OlympiadProperties.store(fos, "Olympiad Properties");
  926. fos.close();
  927. }
  928. private class OlympiadManager implements Runnable
  929. {
  930. private Map<Integer, L2OlympiadGame> _olympiadInstances;
  931. private Map<Integer, List<L2PcInstance>> _classBasedParticipants;
  932. private Map<Integer, List<L2PcInstance>> _nonClassBasedParticipants;
  933. public OlympiadManager()
  934. {
  935. _olympiadInstances = new FastMap<Integer, L2OlympiadGame>();
  936. _manager = this;
  937. }
  938. public synchronized void run()
  939. {
  940. if (isOlympiadEnd())
  941. {
  942. _scheduledManagerTask.cancel(true);
  943. return;
  944. }
  945. if (!inCompPeriod())
  946. return;
  947. //Announcements.getInstance().announceToAll("Comp Match Init");
  948. if (_nobles.size() == 0)
  949. return;
  950. _compStarted = true;
  951. try{
  952. sortClassBasedOpponents();
  953. _nonClassBasedParticipants = pickOpponents(_nonClassBasedRegisters);
  954. }catch(Exception e){e.printStackTrace();}
  955. int classIndex = 0;
  956. int nonClassIndex = 0;
  957. int index = 0;
  958. for (int i = 0; i < COLLISIEUMS; i++)
  959. {
  960. if (_classBasedParticipants.get(classIndex) != null)
  961. {
  962. _olympiadInstances.put(index, new L2OlympiadGame(index, COMP_TYPE.CLASSED, _classBasedParticipants.get(classIndex), STADIUMS[index]));
  963. index++;
  964. classIndex++;
  965. }
  966. if (_nonClassBasedParticipants.get(nonClassIndex) != null)
  967. {
  968. _olympiadInstances.put(index, new L2OlympiadGame(index, COMP_TYPE.NON_CLASSED, _nonClassBasedParticipants.get(nonClassIndex), STADIUMS[index]));
  969. nonClassIndex++;
  970. index++;
  971. }
  972. }
  973. if (_olympiadInstances.size() == 0)
  974. {
  975. _compStarted = false;
  976. return;
  977. }
  978. for (L2OlympiadGame instance : _olympiadInstances.values())
  979. instance.sendMessageToPlayers(false,30);
  980. //Wait 30 seconds
  981. try{
  982. wait(30000);
  983. }catch (InterruptedException e){}
  984. for (L2OlympiadGame instance : _olympiadInstances.values())
  985. instance.portPlayersToArena();
  986. //Wait 20 seconds
  987. try{
  988. wait(20000);
  989. }catch (InterruptedException e){}
  990. for (L2OlympiadGame instance : _olympiadInstances.values())
  991. instance.removals();
  992. _battleStarted = true;
  993. //Wait 1 min
  994. for (int i=60;i>10;i-=10)
  995. {
  996. for (L2OlympiadGame instance : _olympiadInstances.values())
  997. {
  998. instance.sendMessageToPlayers(true,i);
  999. if (i==20)
  1000. instance.additions();
  1001. }
  1002. try{
  1003. wait(10000);
  1004. }catch (InterruptedException e){}
  1005. }
  1006. for (L2OlympiadGame instance : _olympiadInstances.values())
  1007. {
  1008. instance.sendMessageToPlayers(true,10);
  1009. }
  1010. try{
  1011. wait(5000);
  1012. }catch (InterruptedException e){}
  1013. for (int i=5;i>0;i--)
  1014. {
  1015. for (L2OlympiadGame instance : _olympiadInstances.values())
  1016. {
  1017. instance.sendMessageToPlayers(true,i);
  1018. }
  1019. try{
  1020. wait(1000);
  1021. }catch (InterruptedException e){}
  1022. }
  1023. for (L2OlympiadGame instance : _olympiadInstances.values())
  1024. {
  1025. instance.makeCompetitionStart();
  1026. }
  1027. //Wait 6 mins (Battle)
  1028. try{
  1029. wait(BATTLE_PERIOD);
  1030. }catch (InterruptedException e){}
  1031. for (L2OlympiadGame instance : _olympiadInstances.values())
  1032. {
  1033. try{
  1034. instance.validateWinner();
  1035. }catch(Exception e){e.printStackTrace();}
  1036. }
  1037. //Wait 20 seconds
  1038. try{
  1039. wait(20000);
  1040. }catch (InterruptedException e){}
  1041. for (L2OlympiadGame instance : _olympiadInstances.values())
  1042. {
  1043. instance.portPlayersBack();
  1044. instance.clearSpectators();
  1045. }
  1046. //Wait 20 seconds
  1047. try{
  1048. wait(20000);
  1049. }catch (InterruptedException e){}
  1050. _classBasedParticipants.clear();
  1051. _nonClassBasedParticipants.clear();
  1052. _olympiadInstances.clear();
  1053. _classBasedRegisters.clear();
  1054. _nonClassBasedRegisters.clear();
  1055. _battleStarted = false;
  1056. _compStarted = false;
  1057. }
  1058. protected L2OlympiadGame getOlympiadInstance(int index)
  1059. {
  1060. if (_olympiadInstances != null || _compStarted)
  1061. {
  1062. return _olympiadInstances.get(index);
  1063. }
  1064. return null;
  1065. }
  1066. private void sortClassBasedOpponents()
  1067. {
  1068. Map<Integer, List<L2PcInstance>> result = new FastMap<Integer, List<L2PcInstance>>();
  1069. _classBasedParticipants = new FastMap<Integer, List<L2PcInstance>>();
  1070. int count = 0;
  1071. if (_classBasedRegisters.size() == 0) return;
  1072. for (List<L2PcInstance> classed : _classBasedRegisters.values())
  1073. {
  1074. if (classed.size() == 0) continue;
  1075. try{
  1076. result = pickOpponents(classed);
  1077. }catch(Exception e){e.printStackTrace();}
  1078. if (result.size() == 0)
  1079. continue;
  1080. for (List<L2PcInstance> list : result.values())
  1081. {
  1082. if (count == 10) break;
  1083. _classBasedParticipants.put(count, list);
  1084. count++;
  1085. }
  1086. if (count == 10) break;
  1087. }
  1088. }
  1089. protected Map<Integer, L2OlympiadGame> getOlympiadGames()
  1090. {
  1091. return (_olympiadInstances == null)? null : _olympiadInstances;
  1092. }
  1093. private Map<Integer, List<L2PcInstance>> pickOpponents(List<L2PcInstance> list) throws Exception
  1094. {
  1095. Map<Integer, List<L2PcInstance>> result =
  1096. new FastMap<Integer, List<L2PcInstance>>();
  1097. if (list.size() == 0)
  1098. return result;
  1099. int loopCount = (list.size() / 2);
  1100. int first;
  1101. int second;
  1102. if (loopCount < 1)
  1103. return result;
  1104. int count = 0;
  1105. for (int i = 0; i < loopCount; i++)
  1106. {
  1107. count++;
  1108. List<L2PcInstance> opponents = new FastList<L2PcInstance>();
  1109. first = Rnd.nextInt(list.size());
  1110. opponents.add(list.get(first));
  1111. list.remove(first);
  1112. second = Rnd.nextInt(list.size());
  1113. opponents.add(list.get(second));
  1114. list.remove(second);
  1115. result.put(i, opponents);
  1116. if (count == 11)
  1117. break;
  1118. }
  1119. return result;
  1120. }
  1121. protected String[] getAllTitles()
  1122. {
  1123. if(!_compStarted)
  1124. return null;
  1125. if(_olympiadInstances.size() == 0)
  1126. return null;
  1127. String[] msg = new String[_olympiadInstances.size()];
  1128. int count = 0;
  1129. int match = 1;
  1130. for (L2OlympiadGame instance : _olympiadInstances.values())
  1131. {
  1132. msg[count] = match + "_In Progress_" + instance.getTitle();
  1133. count++;
  1134. match++;
  1135. }
  1136. return msg;
  1137. }
  1138. }
  1139. private class L2OlympiadGame
  1140. {
  1141. protected COMP_TYPE _type;
  1142. private boolean _aborted;
  1143. private String _playerOneName;
  1144. private String _playerTwoName;
  1145. private int _playerOneID = 0;
  1146. private int _playerTwoID = 0;
  1147. private L2PcInstance _playerOne;
  1148. private L2PcInstance _playerTwo;
  1149. private List<L2PcInstance> _players;
  1150. private int[] _playerOneLocation;
  1151. private int[] _playerTwoLocation;
  1152. private int[] _stadiumPort;
  1153. private List<L2PcInstance> _spectators;
  1154. private SystemMessage _sm;
  1155. private SystemMessage _sm2;
  1156. private SystemMessage _sm3;
  1157. protected L2OlympiadGame(int id, COMP_TYPE type, List<L2PcInstance> list, int[] stadiumPort)
  1158. {
  1159. _aborted = false;
  1160. _type = type;
  1161. _stadiumPort = stadiumPort;
  1162. _spectators = new FastList<L2PcInstance>();
  1163. if (list != null)
  1164. {
  1165. _players = list;
  1166. _playerOne = list.get(0);
  1167. _playerTwo = list.get(1);
  1168. try {
  1169. _playerOneName = _playerOne.getName();
  1170. _playerTwoName = _playerTwo.getName();
  1171. _playerOne.setOlympiadGameId(id);
  1172. _playerTwo.setOlympiadGameId(id);
  1173. _playerOneID = _playerOne.getObjectId();
  1174. _playerTwoID = _playerTwo.getObjectId();
  1175. }
  1176. catch (Exception e) {
  1177. _aborted = true;
  1178. clearPlayers();
  1179. }
  1180. _log.info("Olympiad System: Game - " + id + ": "
  1181. + _playerOne.getName() + " Vs " + _playerTwo.getName());
  1182. }
  1183. else {
  1184. _aborted = true;
  1185. clearPlayers();
  1186. return;
  1187. }
  1188. }
  1189. protected void removals()
  1190. {
  1191. if (_aborted) return;
  1192. if (_playerOne == null || _playerTwo == null) return;
  1193. for (L2PcInstance player : _players)
  1194. {
  1195. try{
  1196. //Remove Clan Skills
  1197. if (player.getClan() != null)
  1198. {
  1199. for(L2Skill skill: player.getClan().getAllSkills())
  1200. player.removeSkill(skill,false);
  1201. }
  1202. //Abort casting if player casting
  1203. if (player.isCastingNow())
  1204. {
  1205. player.abortCast();
  1206. }
  1207. //Remove Hero Skills
  1208. if (player.isHero())
  1209. {
  1210. for(L2Skill skill: HeroSkillTable.GetHeroSkills())
  1211. player.removeSkill(skill,false);
  1212. }
  1213. //Remove Buffs
  1214. player.stopAllEffects();
  1215. //Remove Summon's Buffs
  1216. if (player.getPet() != null)
  1217. {
  1218. L2Summon summon = player.getPet();
  1219. summon.stopAllEffects();
  1220. if (summon instanceof L2PetInstance)
  1221. summon.unSummon(player);
  1222. }
  1223. /*if (player.getCubics() != null)
  1224. {
  1225. for(L2CubicInstance cubic : player.getCubics().values())
  1226. {
  1227. cubic.stopAction();
  1228. player.delCubic(cubic.getId());
  1229. }
  1230. player.getCubics().clear();
  1231. }*/
  1232. //Remove player from his party
  1233. if (player.getParty() != null)
  1234. {
  1235. L2Party party = player.getParty();
  1236. party.removePartyMember(player);
  1237. }
  1238. //Remove Hero Weapons
  1239. // check to prevent the using of weapon/shield on strider/wyvern
  1240. L2ItemInstance wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  1241. if (wpn == null) wpn = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LRHAND);
  1242. if (wpn != null && wpn.isHeroItem())
  1243. {
  1244. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(wpn.getItem().getBodyPart());
  1245. InventoryUpdate iu = new InventoryUpdate();
  1246. for (int i = 0; i < unequiped.length; i++)
  1247. iu.addModifiedItem(unequiped[i]);
  1248. player.sendPacket(iu);
  1249. player.abortAttack();
  1250. player.broadcastUserInfo();
  1251. // this can be 0 if the user pressed the right mousebutton twice very fast
  1252. if (unequiped.length > 0)
  1253. {
  1254. if (unequiped[0].isWear())
  1255. return;
  1256. SystemMessage sm = null;
  1257. if (unequiped[0].getEnchantLevel() > 0){
  1258. sm = new SystemMessage(SystemMessageId.EQUIPMENT_S1_S2_REMOVED);
  1259. sm.addNumber(unequiped[0].getEnchantLevel());
  1260. sm.addItemName(unequiped[0].getItemId());
  1261. }else{
  1262. sm = new SystemMessage(SystemMessageId.S1_DISARMED);
  1263. sm.addItemName(unequiped[0].getItemId());
  1264. }
  1265. player.sendPacket(sm);
  1266. }
  1267. }
  1268. //Remove ss/sps/bsps automation
  1269. Map<Integer, Integer> activeSoulShots = player.getAutoSoulShot();
  1270. for (int itemId : activeSoulShots.values())
  1271. player.removeAutoSoulShot(itemId);
  1272. player.sendSkillList();
  1273. } catch (Exception e) {}
  1274. }
  1275. _sm = new SystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  1276. _sm.addNumber(120);
  1277. broadcastMessage(_sm, false);
  1278. }
  1279. protected void clearPlayers()
  1280. {
  1281. _playerOne = null;
  1282. _playerTwo = null;
  1283. _players = null;
  1284. _playerOneName = "";
  1285. _playerTwoName = "";
  1286. _playerOneID = 0;
  1287. _playerTwoID = 0;
  1288. }
  1289. protected boolean portPlayersToArena()
  1290. {
  1291. _playerOneLocation = new int[3];
  1292. _playerTwoLocation = new int[3];
  1293. boolean _playerOneCrash = true;
  1294. boolean _playerTwoCrash = true;
  1295. try {
  1296. if (_playerOne != null && _playerOne.getOlympiadGameId() != -1)
  1297. _playerOneCrash=false;
  1298. } catch (Exception e) {}
  1299. try {
  1300. if (_playerTwo != null && _playerTwo.getOlympiadGameId() != -1)
  1301. _playerTwoCrash=false;
  1302. } catch (Exception e){ }
  1303. if (_playerOneCrash){
  1304. StatsSet playerOneStat;
  1305. playerOneStat = _nobles.get(_playerOneID);
  1306. int playerOnePoints = playerOneStat.getInteger(POINTS);
  1307. playerOneStat.set(POINTS, playerOnePoints - (playerOnePoints / 5));
  1308. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... "+_playerOneName+" lost "+(playerOnePoints - (playerOnePoints / 5))+" points for crash before teleport to arena");
  1309. }
  1310. if (_playerTwoCrash){
  1311. StatsSet playerTwoStat;
  1312. playerTwoStat = _nobles.get(_playerTwoID);
  1313. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  1314. playerTwoStat.set(POINTS, playerTwoPoints - (playerTwoPoints / 5));
  1315. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... "+_playerTwoName+" lost "+(playerTwoPoints - (playerTwoPoints / 5))+" points for crash before teleport to arena");
  1316. }
  1317. if (_playerOneCrash || _playerTwoCrash){
  1318. _playerOne=null;
  1319. _playerTwo=null;
  1320. _aborted = true;
  1321. return false;
  1322. }
  1323. try {
  1324. _playerOneLocation[0] = _playerOne.getX();
  1325. _playerOneLocation[1] = _playerOne.getY();
  1326. _playerOneLocation[2] = _playerOne.getZ();
  1327. _playerTwoLocation[0] = _playerTwo.getX();
  1328. _playerTwoLocation[1] = _playerTwo.getY();
  1329. _playerTwoLocation[2] = _playerTwo.getZ();
  1330. //_playerOne.getAppearance().setInvisible();
  1331. //_playerOne.broadcastUserInfo();
  1332. //_playerOne.decayMe();
  1333. //_playerOne.spawnMe();
  1334. //_playerTwo.getAppearance().setInvisible();
  1335. //_playerTwo.broadcastUserInfo();
  1336. //_playerTwo.decayMe();
  1337. //_playerTwo.spawnMe();
  1338. if (_playerOne.isSitting())
  1339. _playerOne.standUp();
  1340. if (_playerTwo.isSitting())
  1341. _playerTwo.standUp();
  1342. _playerOne.setTarget(null);
  1343. _playerTwo.setTarget(null);
  1344. _playerOne.teleToLocation(_stadiumPort[0], _stadiumPort[1], _stadiumPort[2], true);
  1345. _playerTwo.teleToLocation(_stadiumPort[0], _stadiumPort[1], _stadiumPort[2], true);
  1346. _playerOne.setIsInOlympiadMode(true);
  1347. _playerOne.setIsOlympiadStart(false);
  1348. _playerTwo.setIsInOlympiadMode(true);
  1349. _playerTwo.setIsOlympiadStart(false);
  1350. } catch (Exception e) {
  1351. return false;
  1352. }
  1353. return true;
  1354. }
  1355. protected void sendMessageToPlayers(boolean toBattleBegin, int nsecond)
  1356. {
  1357. if(!toBattleBegin)
  1358. _sm = new SystemMessage(SystemMessageId.YOU_WILL_ENTER_THE_OLYMPIAD_STADIUM_IN_S1_SECOND_S);
  1359. else
  1360. _sm = new SystemMessage(SystemMessageId.THE_GAME_WILL_START_IN_S1_SECOND_S);
  1361. _sm.addNumber(nsecond);
  1362. try {
  1363. for (L2PcInstance player : _players)
  1364. player.sendPacket(_sm);
  1365. } catch (Exception e) {};
  1366. }
  1367. protected void portPlayersBack()
  1368. {
  1369. try {
  1370. _playerOne.teleToLocation(_playerOneLocation[0],
  1371. _playerOneLocation[1], _playerOneLocation[2], true);
  1372. }
  1373. catch (Exception e) {}
  1374. try {
  1375. _playerTwo.teleToLocation(_playerTwoLocation[0],
  1376. _playerTwoLocation[1], _playerTwoLocation[2], true);
  1377. }
  1378. catch (Exception e) {}
  1379. for (L2PcInstance player : _players)
  1380. {
  1381. try {
  1382. player.setIsInOlympiadMode(false);
  1383. player.setIsOlympiadStart(false);
  1384. player.setOlympiadSide(-1);
  1385. player.setOlympiadGameId(-1);
  1386. player.setCurrentCp(player.getMaxCp());
  1387. player.setCurrentHp(player.getMaxHp());
  1388. player.setCurrentMp(player.getMaxMp());
  1389. player.getStatus().startHpMpRegeneration();
  1390. //Add Clan Skills
  1391. if (player.getClan() != null)
  1392. {
  1393. for(L2Skill skill: player.getClan().getAllSkills())
  1394. {
  1395. if ( skill.getMinPledgeClass() <= player.getPledgeClass() )
  1396. player.addSkill(skill,false);
  1397. }
  1398. }
  1399. //Add Hero Skills
  1400. if (player.isHero())
  1401. {
  1402. for(L2Skill skill: HeroSkillTable.GetHeroSkills())
  1403. player.addSkill(skill,false);
  1404. }
  1405. player.sendSkillList();
  1406. } catch (Exception e) {}
  1407. }
  1408. }
  1409. protected void validateWinner()
  1410. {
  1411. if (_aborted || (_playerOne == null && _playerTwo == null))
  1412. {
  1413. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... aborted/tie due to crashes!");
  1414. return;
  1415. }
  1416. StatsSet playerOneStat;
  1417. StatsSet playerTwoStat;
  1418. playerOneStat = _nobles.get(_playerOneID);
  1419. playerTwoStat = _nobles.get(_playerTwoID);
  1420. int playerOnePlayed = playerOneStat.getInteger(COMP_DONE);
  1421. int playerTwoPlayed = playerTwoStat.getInteger(COMP_DONE);
  1422. int playerOnePoints = playerOneStat.getInteger(POINTS);
  1423. int playerTwoPoints = playerTwoStat.getInteger(POINTS);
  1424. double playerOneHp = 0;
  1425. double hpDiffOne = 9999999;
  1426. try{
  1427. if (_playerOne != null && _playerOne.getOlympiadGameId() != -1)
  1428. {
  1429. playerOneHp = _playerOne.getCurrentHp()+_playerOne.getCurrentCp();
  1430. hpDiffOne = (_playerOne.getMaxHp()+_playerOne.getMaxCp()) - playerOneHp;
  1431. }
  1432. }catch (Exception e){
  1433. playerOneHp = 0;
  1434. hpDiffOne = 9999999;
  1435. }
  1436. double playerTwoHp = 0;
  1437. double hpDiffTwo = 9999999;
  1438. try{
  1439. if (_playerTwo != null && _playerTwo.getOlympiadGameId()!=-1)
  1440. {
  1441. playerTwoHp = _playerTwo.getCurrentHp()+_playerTwo.getCurrentCp();
  1442. hpDiffTwo = (_playerTwo.getMaxHp()+_playerTwo.getMaxCp()) - playerTwoHp;
  1443. }
  1444. }catch (Exception e){
  1445. playerTwoHp = 0;
  1446. hpDiffTwo = 9999999;
  1447. }
  1448. _sm = new SystemMessage(SystemMessageId.S1_HAS_WON_THE_GAME);
  1449. _sm2 = new SystemMessage(SystemMessageId.S1_HAS_GAINED_S2_OLYMPIAD_POINTS);
  1450. _sm3 = new SystemMessage(SystemMessageId.S1_HAS_LOST_S2_OLYMPIAD_POINTS);
  1451. String result = "";
  1452. if (playerTwoHp==0 || hpDiffOne < hpDiffTwo)
  1453. {
  1454. int pointDiff;
  1455. pointDiff = (playerTwoPoints / 3);
  1456. playerOneStat.set(POINTS, playerOnePoints + pointDiff);
  1457. playerTwoStat.set(POINTS, playerTwoPoints - pointDiff);
  1458. _sm.addString(_playerOneName);
  1459. broadcastMessage(_sm, true);
  1460. _sm2.addString(_playerOneName);
  1461. _sm2.addNumber(pointDiff);
  1462. broadcastMessage(_sm2, true);
  1463. _sm3.addString(_playerTwoName);
  1464. _sm3.addNumber(pointDiff);
  1465. broadcastMessage(_sm3, true);
  1466. _playerOne = L2World.getInstance().getPlayer(_playerOneName); // why this...
  1467. try {
  1468. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+hpDiffOne+" vs "+hpDiffTwo+") "+_playerOneName+" win "+pointDiff+" points";
  1469. L2ItemInstance item = _playerOne.getInventory().addItem("Olympiad", 6651, 30, _playerOne, null);
  1470. InventoryUpdate iu = new InventoryUpdate();
  1471. iu.addModifiedItem(item);
  1472. _playerOne.sendPacket(iu);
  1473. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1474. sm.addItemName(item.getItemId());
  1475. sm.addNumber(30);
  1476. _playerOne.sendPacket(sm);
  1477. } catch (Exception e) { }
  1478. }
  1479. else if (playerOneHp==0 || hpDiffOne > hpDiffTwo)
  1480. {
  1481. int pointDiff;
  1482. pointDiff = (playerOnePoints / 3);
  1483. playerTwoStat.set(POINTS, playerTwoPoints + pointDiff);
  1484. playerOneStat.set(POINTS, playerOnePoints - pointDiff);
  1485. _sm.addString(_playerTwo.getName());
  1486. broadcastMessage(_sm, true);
  1487. _sm2.addString(_playerTwo.getName());
  1488. _sm2.addNumber(pointDiff);
  1489. broadcastMessage(_sm2, true);
  1490. _sm3.addString(_playerOne.getName());
  1491. _sm3.addNumber(pointDiff);
  1492. broadcastMessage(_sm3, true);
  1493. _playerTwo = L2World.getInstance().getPlayer(_playerTwoName); // why this...
  1494. try {
  1495. result=" ("+playerOneHp+"hp vs "+playerTwoHp+"hp - "+hpDiffOne+" vs "+hpDiffTwo+") "+_playerTwoName+" win "+pointDiff+" points";
  1496. L2ItemInstance item = _playerTwo.getInventory().addItem("Olympiad", 6651, 30, _playerTwo, null);
  1497. InventoryUpdate iu = new InventoryUpdate();
  1498. iu.addModifiedItem(item);
  1499. _playerTwo.sendPacket(iu);
  1500. SystemMessage sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  1501. sm.addItemName(item.getItemId());
  1502. sm.addNumber(30);
  1503. _playerTwo.sendPacket(sm);
  1504. } catch (Exception e) { }
  1505. }
  1506. else
  1507. {
  1508. result=" tie";
  1509. _sm = new SystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  1510. broadcastMessage(_sm, true);
  1511. }
  1512. _log.info("Olympia Result: "+_playerOneName+" vs "+_playerTwoName+" ... "+result);
  1513. playerOneStat.set(COMP_DONE, playerOnePlayed + 1);
  1514. playerTwoStat.set(COMP_DONE, playerTwoPlayed + 1);
  1515. _nobles.remove(_playerOneID);
  1516. _nobles.remove(_playerTwoID);
  1517. _nobles.put(_playerOneID, playerOneStat);
  1518. _nobles.put(_playerTwoID, playerTwoStat);
  1519. _sm = new SystemMessage(SystemMessageId.YOU_WILL_GO_BACK_TO_THE_VILLAGE_IN_S1_SECOND_S);
  1520. _sm.addNumber(20);
  1521. broadcastMessage(_sm, true);
  1522. }
  1523. protected void additions()
  1524. {
  1525. for (L2PcInstance player : _players)
  1526. {
  1527. try {
  1528. //Set HP/CP/MP to Max
  1529. player.setCurrentCp(player.getMaxCp());
  1530. player.setCurrentHp(player.getMaxHp());
  1531. player.setCurrentMp(player.getMaxMp());
  1532. //Wind Walk Buff for Both
  1533. L2Skill skill;
  1534. SystemMessage sm;
  1535. skill = SkillTable.getInstance().getInfo(1204, 2);
  1536. skill.getEffects(player, player);
  1537. player.broadcastPacket(new MagicSkillUse(player, player, skill.getId(), 2, skill.getHitTime(), 0));
  1538. sm = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  1539. sm.addSkillName(1204);
  1540. player.sendPacket(sm);
  1541. if (!player.isMageClass())
  1542. {
  1543. //Haste Buff to Fighters
  1544. skill = SkillTable.getInstance().getInfo(1086, 1);
  1545. skill.getEffects(player, player);
  1546. player.broadcastPacket(new MagicSkillUse(player, player, skill.getId(), 1, skill.getHitTime(), 0));
  1547. sm = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  1548. sm.addSkillName(1086);
  1549. player.sendPacket(sm);
  1550. }else{
  1551. //Acumen Buff to Mages
  1552. skill = SkillTable.getInstance().getInfo(1085, 1);
  1553. skill.getEffects(player, player);
  1554. player.broadcastPacket(new MagicSkillUse(player, player, skill.getId(), 1, skill.getHitTime(), 0));
  1555. sm = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  1556. sm.addSkillName(1085);
  1557. player.sendPacket(sm);
  1558. }
  1559. } catch (Exception e) { }
  1560. }
  1561. }
  1562. protected boolean makePlayersVisible()
  1563. {
  1564. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  1565. try {
  1566. for (L2PcInstance player : _players)
  1567. {
  1568. player.getAppearance().setVisible();
  1569. player.broadcastUserInfo();
  1570. player.sendPacket(_sm);
  1571. if (player.getPet() != null)
  1572. player.getPet().updateAbnormalEffect();
  1573. }
  1574. } catch (NullPointerException e) { _aborted = true; return false; }
  1575. return true;
  1576. }
  1577. protected boolean makeCompetitionStart()
  1578. {
  1579. if (_playerOne == null || _playerTwo == null)
  1580. {
  1581. _aborted = true;
  1582. return false;
  1583. }
  1584. _sm = new SystemMessage(SystemMessageId.STARTS_THE_GAME);
  1585. try {
  1586. for (L2PcInstance player : _players)
  1587. {
  1588. player.setIsOlympiadStart(true);
  1589. player.sendPacket(_sm);
  1590. }
  1591. } catch (Exception e) {
  1592. _aborted = true;
  1593. return false;
  1594. }
  1595. return true;
  1596. }
  1597. protected String getTitle()
  1598. {
  1599. String msg = "";
  1600. msg+= _playerOneName + " : " + _playerTwoName;
  1601. return msg;
  1602. }
  1603. protected L2PcInstance[] getPlayers()
  1604. {
  1605. L2PcInstance[] players = new L2PcInstance[2];
  1606. if (_playerOne == null || _playerTwo == null) return null;
  1607. players[0] = _playerOne;
  1608. players[1] = _playerTwo;
  1609. return players;
  1610. }
  1611. protected List<L2PcInstance> getSpectators()
  1612. {
  1613. return _spectators;
  1614. }
  1615. protected void addSpectator(L2PcInstance spec)
  1616. {
  1617. _spectators.add(spec);
  1618. }
  1619. protected void removeSpectator(L2PcInstance spec)
  1620. {
  1621. if (_spectators != null && _spectators.contains(spec))
  1622. _spectators.remove(spec);
  1623. }
  1624. protected void clearSpectators()
  1625. {
  1626. if (_spectators != null)
  1627. {
  1628. for (L2PcInstance pc : _spectators)
  1629. {
  1630. try {
  1631. if(!pc.inObserverMode()) continue;
  1632. pc.leaveOlympiadObserverMode();
  1633. } catch (NullPointerException e) {}
  1634. }
  1635. _spectators.clear();
  1636. }
  1637. }
  1638. private void broadcastMessage(SystemMessage sm, boolean toAll)
  1639. {
  1640. try { _playerOne.sendPacket(sm); } catch (Exception e) {}
  1641. try { _playerTwo.sendPacket(sm); } catch (Exception e) {}
  1642. if (toAll && _spectators != null)
  1643. {
  1644. for (L2PcInstance spec : _spectators)
  1645. {
  1646. try { spec.sendPacket(sm); } catch (NullPointerException e) {}
  1647. }
  1648. }
  1649. }
  1650. }
  1651. }