Olympiad.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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.model.olympiad;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.InputStream;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.util.Calendar;
  27. import java.util.Map;
  28. import java.util.Properties;
  29. import java.util.concurrent.ScheduledFuture;
  30. import java.util.logging.Level;
  31. import java.util.logging.LogRecord;
  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.Announcements;
  37. import net.sf.l2j.gameserver.ThreadPoolManager;
  38. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  40. import net.sf.l2j.gameserver.model.entity.Hero;
  41. import net.sf.l2j.gameserver.model.entity.TvTEvent;
  42. import net.sf.l2j.gameserver.network.SystemMessageId;
  43. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  44. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  45. import net.sf.l2j.gameserver.templates.StatsSet;
  46. import net.sf.l2j.util.L2FastList;
  47. public class Olympiad
  48. {
  49. protected static final Logger _log = Logger.getLogger(Olympiad.class.getName());
  50. protected static final Logger _logResults = Logger.getLogger("olympiad");
  51. private static Map<Integer, StatsSet> _nobles;
  52. protected static L2FastList<StatsSet> _heroesToBe;
  53. private static L2FastList<L2PcInstance> _nonClassBasedRegisters;
  54. private static Map<Integer, L2FastList<L2PcInstance>> _classBasedRegisters;
  55. private static Map<Integer, Integer> _noblesRank;
  56. private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties";
  57. public static final String OLYMPIAD_HTML_PATH = "data/html/olympiad/";
  58. private static final String OLYMPIAD_LOAD_DATA = "SELECT current_cycle, period, olympiad_end, validation_end, "
  59. + "next_weekly_change FROM olympiad_data WHERE id = 0";
  60. private static final String OLYMPIAD_SAVE_DATA = "INSERT INTO olympiad_data (id, current_cycle, "
  61. + "period, olympiad_end, validation_end, next_weekly_change) VALUES (0,?,?,?,?,?) "
  62. + "ON DUPLICATE KEY UPDATE current_cycle=?, period=?, olympiad_end=?, "
  63. + "validation_end=?, next_weekly_change=?";
  64. private static final String OLYMPIAD_LOAD_NOBLES = "SELECT olympiad_nobles.charId, olympiad_nobles.class_id, "
  65. + "characters.char_name, olympiad_nobles.olympiad_points, olympiad_nobles.competitions_done, "
  66. + "olympiad_nobles.competitions_won, olympiad_nobles.competitions_lost, olympiad_nobles.competitions_drawn "
  67. + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId";
  68. private static final String OLYMPIAD_SAVE_NOBLES = "INSERT INTO olympiad_nobles "
  69. + "(`charId`,`class_id`,`olympiad_points`,`competitions_done`,`competitions_won`,`competitions_lost`,"
  70. + "`competitions_drawn`) VALUES (?,?,?,?,?,?,?)";
  71. private static final String OLYMPIAD_UPDATE_NOBLES = "UPDATE olympiad_nobles SET "
  72. + "olympiad_points = ?, competitions_done = ?, competitions_won = ?, competitions_lost = ?, competitions_drawn = ? WHERE charId = ?";
  73. private static final String OLYMPIAD_GET_HEROS = "SELECT olympiad_nobles.charId, characters.char_name "
  74. + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId "
  75. + "AND olympiad_nobles.class_id = ? AND olympiad_nobles.competitions_done >= 9 "
  76. + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC";
  77. private static final String GET_ALL_CLASSIFIED_NOBLESS = "SELECT charId from olympiad_nobles_eom "
  78. + "WHERE competitions_done >= 9 ORDER BY olympiad_points DESC, competitions_done DESC";
  79. private static final String GET_EACH_CLASS_LEADER = "SELECT characters.char_name from olympiad_nobles_eom, characters "
  80. + "WHERE characters.charId = olympiad_nobles_eom.charId AND olympiad_nobles_eom.class_id = ? "
  81. + "AND olympiad_nobles_eom.competitions_done >= 9 "
  82. + "ORDER BY olympiad_nobles_eom.olympiad_points DESC, olympiad_nobles_eom.competitions_done DESC LIMIT 10";
  83. private static final String GET_EACH_CLASS_LEADER_CURRENT = "SELECT characters.char_name from olympiad_nobles, characters "
  84. + "WHERE characters.charId = olympiad_nobles.charId AND olympiad_nobles.class_id = ? "
  85. + "AND olympiad_nobles.competitions_done >= 9 "
  86. + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC LIMIT 10";
  87. private static final String OLYMPIAD_DELETE_ALL = "TRUNCATE olympiad_nobles";
  88. private static final String OLYMPIAD_MONTH_CLEAR = "TRUNCATE olympiad_nobles_eom";
  89. private static final String OLYMPIAD_MONTH_CREATE = "INSERT INTO olympiad_nobles_eom SELECT * FROM olympiad_nobles";
  90. private static final int[] HERO_IDS = { 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
  91. 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 131, 132, 133, 134 };
  92. private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
  93. private static final int COMP_MIN = Config.ALT_OLY_MIN; // 00 mins
  94. private static final long COMP_PERIOD = Config.ALT_OLY_CPERIOD; // 6 hours
  95. protected static final long WEEKLY_PERIOD = Config.ALT_OLY_WPERIOD; // 1 week
  96. protected static final long VALIDATION_PERIOD = Config.ALT_OLY_VPERIOD; // 24 hours
  97. private static final int DEFAULT_POINTS = 18;
  98. protected static final int WEEKLY_POINTS = 3;
  99. public static final String CHAR_ID = "charId";
  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. public static final String COMP_WON = "competitions_won";
  105. public static final String COMP_LOST = "competitions_lost";
  106. public static final String COMP_DRAWN = "competitions_drawn";
  107. protected long _olympiadEnd;
  108. protected long _validationEnd;
  109. /**
  110. * The current period of the olympiad.<br>
  111. * <b>0 -</b> Competition period<br>
  112. * <b>1 -</b> Validation Period
  113. */
  114. protected int _period;
  115. protected long _nextWeeklyChange;
  116. protected int _currentCycle;
  117. private long _compEnd;
  118. private Calendar _compStart;
  119. protected static boolean _inCompPeriod;
  120. protected static boolean _compStarted = false;
  121. protected ScheduledFuture<?> _scheduledCompStart;
  122. protected ScheduledFuture<?> _scheduledCompEnd;
  123. protected ScheduledFuture<?> _scheduledOlympiadEnd;
  124. protected ScheduledFuture<?> _scheduledWeeklyTask;
  125. protected ScheduledFuture<?> _scheduledValdationTask;
  126. protected static enum COMP_TYPE
  127. {
  128. CLASSED,
  129. NON_CLASSED
  130. }
  131. public static Olympiad getInstance()
  132. {
  133. return SingletonHolder._instance;
  134. }
  135. private Olympiad()
  136. {
  137. load();
  138. if (_period == 0)
  139. init();
  140. }
  141. public static Integer getStadiumCount()
  142. {
  143. return OlympiadManager.STADIUMS.length;
  144. }
  145. private void load()
  146. {
  147. _nobles = new FastMap<Integer, StatsSet>();
  148. Connection con = null;
  149. boolean loaded = false;
  150. try
  151. {
  152. con = L2DatabaseFactory.getInstance().getConnection();
  153. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_DATA);
  154. ResultSet rset = statement.executeQuery();
  155. while (rset.next())
  156. {
  157. _currentCycle = rset.getInt("current_cycle");
  158. _period = rset.getInt("period");
  159. _olympiadEnd = rset.getLong("olympiad_end");
  160. _validationEnd = rset.getLong("validation_end");
  161. _nextWeeklyChange = rset.getLong("next_weekly_change");
  162. loaded = true;
  163. }
  164. rset.close();
  165. statement.close();
  166. }
  167. catch (Exception e)
  168. {
  169. _log.log(Level.WARNING, "Olympiad System: Error loading olympiad data from database: ", e);
  170. }
  171. finally
  172. {
  173. try
  174. {
  175. con.close();
  176. }
  177. catch (Exception e)
  178. {
  179. e.printStackTrace();
  180. }
  181. }
  182. if (!loaded)
  183. {
  184. _log.log(Level.INFO, "Olympiad System: failed to load data from database, trying to load from file.");
  185. Properties OlympiadProperties = new Properties();
  186. InputStream is = null;
  187. try
  188. {
  189. is = new FileInputStream(new File("./" + OLYMPIAD_DATA_FILE));
  190. OlympiadProperties.load(is);
  191. }
  192. catch (Exception e)
  193. {
  194. _log.log(Level.SEVERE, "Olympiad System: Error loading olympiad properties: ", e);
  195. return;
  196. }
  197. finally
  198. {
  199. try
  200. {
  201. is.close();
  202. }
  203. catch (Exception e)
  204. {
  205. e.printStackTrace();
  206. }
  207. }
  208. _currentCycle = Integer.parseInt(OlympiadProperties.getProperty("CurrentCycle", "1"));
  209. _period = Integer.parseInt(OlympiadProperties.getProperty("Period", "0"));
  210. _olympiadEnd = Long.parseLong(OlympiadProperties.getProperty("OlympiadEnd", "0"));
  211. _validationEnd = Long.parseLong(OlympiadProperties.getProperty("ValidationEnd", "0"));
  212. _nextWeeklyChange = Long.parseLong(OlympiadProperties.getProperty("NextWeeklyChange", "0"));
  213. }
  214. switch (_period)
  215. {
  216. case 0:
  217. if (_olympiadEnd == 0 || _olympiadEnd < Calendar.getInstance().getTimeInMillis())
  218. setNewOlympiadEnd();
  219. else
  220. scheduleWeeklyChange();
  221. break;
  222. case 1:
  223. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  224. {
  225. loadNoblesRank();
  226. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  227. }
  228. else
  229. {
  230. _currentCycle++;
  231. _period = 0;
  232. deleteNobles();
  233. setNewOlympiadEnd();
  234. }
  235. break;
  236. default:
  237. _log.warning("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  238. return;
  239. }
  240. try
  241. {
  242. con = L2DatabaseFactory.getInstance().getConnection();
  243. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
  244. ResultSet rset = statement.executeQuery();
  245. while (rset.next())
  246. {
  247. StatsSet statData = new StatsSet();
  248. int charId = rset.getInt(CHAR_ID);
  249. statData.set(CLASS_ID, rset.getInt(CLASS_ID));
  250. statData.set(CHAR_NAME, rset.getString(CHAR_NAME));
  251. statData.set(POINTS, rset.getInt(POINTS));
  252. statData.set(COMP_DONE, rset.getInt(COMP_DONE));
  253. statData.set(COMP_WON, rset.getInt(COMP_WON));
  254. statData.set(COMP_LOST, rset.getInt(COMP_LOST));
  255. statData.set(COMP_DRAWN, rset.getInt(COMP_DRAWN));
  256. statData.set("to_save", false);
  257. _nobles.put(charId, statData);
  258. }
  259. rset.close();
  260. statement.close();
  261. }
  262. catch (Exception e)
  263. {
  264. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database: ", e);
  265. }
  266. finally
  267. {
  268. try
  269. {
  270. con.close();
  271. }
  272. catch (Exception e)
  273. {
  274. e.printStackTrace();
  275. }
  276. }
  277. synchronized (this)
  278. {
  279. _log.info("Olympiad System: Loading Olympiad System....");
  280. if (_period == 0)
  281. _log.info("Olympiad System: Currently in Olympiad Period");
  282. else
  283. _log.info("Olympiad System: Currently in Validation Period");
  284. long milliToEnd;
  285. if (_period == 0)
  286. milliToEnd = getMillisToOlympiadEnd();
  287. else
  288. milliToEnd = getMillisToValidationEnd();
  289. _log.info("Olympiad System: " + Math.round(milliToEnd / 60000) + " minutes until period ends");
  290. if (_period == 0)
  291. {
  292. milliToEnd = getMillisToWeekChange();
  293. _log.info("Olympiad System: Next weekly change is in " + Math.round(milliToEnd / 60000) + " minutes");
  294. }
  295. }
  296. _log.info("Olympiad System: Loaded " + _nobles.size() + " Nobles");
  297. }
  298. public void loadNoblesRank()
  299. {
  300. _noblesRank = new FastMap<Integer, Integer>();
  301. Map<Integer, Integer> tmpPlace = new FastMap<Integer, Integer>();
  302. Connection con = null;
  303. try
  304. {
  305. con = L2DatabaseFactory.getInstance().getConnection();
  306. PreparedStatement statement = con.prepareStatement(GET_ALL_CLASSIFIED_NOBLESS);
  307. ResultSet rset = statement.executeQuery();
  308. int place = 1;
  309. while (rset.next())
  310. {
  311. tmpPlace.put(rset.getInt(CHAR_ID), place++);
  312. }
  313. rset.close();
  314. statement.close();
  315. }
  316. catch (Exception e)
  317. {
  318. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database for Ranking: ", e);
  319. }
  320. finally
  321. {
  322. try
  323. {
  324. con.close();
  325. }
  326. catch (Exception e)
  327. {
  328. }
  329. }
  330. int rank1 = (int) Math.round(tmpPlace.size() * 0.01);
  331. int rank2 = (int) Math.round(tmpPlace.size() * 0.10);
  332. int rank3 = (int) Math.round(tmpPlace.size() * 0.25);
  333. int rank4 = (int) Math.round(tmpPlace.size() * 0.50);
  334. if (rank1 == 0)
  335. {
  336. rank1 = 1;
  337. rank2++;
  338. rank3++;
  339. rank4++;
  340. }
  341. for (int charId : tmpPlace.keySet())
  342. {
  343. if (tmpPlace.get(charId) <= rank1)
  344. _noblesRank.put(charId, 1);
  345. else if (tmpPlace.get(charId) <= rank2)
  346. _noblesRank.put(charId, 2);
  347. else if (tmpPlace.get(charId) <= rank3)
  348. _noblesRank.put(charId, 3);
  349. else if (tmpPlace.get(charId) <= rank4)
  350. _noblesRank.put(charId, 4);
  351. else
  352. _noblesRank.put(charId, 5);
  353. }
  354. }
  355. protected void init()
  356. {
  357. if (_period == 1)
  358. return;
  359. _nonClassBasedRegisters = new L2FastList<L2PcInstance>();
  360. _classBasedRegisters = new FastMap<Integer, L2FastList<L2PcInstance>>();
  361. _compStart = Calendar.getInstance();
  362. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  363. _compStart.set(Calendar.MINUTE, COMP_MIN);
  364. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  365. if (_scheduledOlympiadEnd != null)
  366. _scheduledOlympiadEnd.cancel(true);
  367. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), getMillisToOlympiadEnd());
  368. updateCompStatus();
  369. }
  370. protected class OlympiadEndTask implements Runnable
  371. {
  372. public void run()
  373. {
  374. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  375. sm.addNumber(_currentCycle);
  376. Announcements.getInstance().announceToAll(sm);
  377. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  378. if (_scheduledWeeklyTask != null)
  379. _scheduledWeeklyTask.cancel(true);
  380. saveNobleData();
  381. _period = 1;
  382. sortHerosToBe();
  383. Hero.getInstance().computeNewHeroes(_heroesToBe);
  384. saveOlympiadStatus();
  385. updateMonthlyData();
  386. Calendar validationEnd = Calendar.getInstance();
  387. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  388. loadNoblesRank();
  389. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  390. }
  391. }
  392. protected class ValidationEndTask implements Runnable
  393. {
  394. public void run()
  395. {
  396. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  397. _period = 0;
  398. _currentCycle++;
  399. deleteNobles();
  400. setNewOlympiadEnd();
  401. init();
  402. }
  403. }
  404. public boolean registerNoble(L2PcInstance noble, boolean classBased)
  405. {
  406. SystemMessage sm;
  407. /*
  408. * if (_compStarted) {
  409. * noble.sendMessage("Cant Register whilst competition is under way");
  410. * return false; }
  411. */
  412. if (!_inCompPeriod)
  413. {
  414. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  415. noble.sendPacket(sm);
  416. return false;
  417. }
  418. if (!noble.isNoble())
  419. {
  420. sm = new SystemMessage(SystemMessageId.C1_DOES_NOT_MEET_REQUIREMENTS_ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  421. sm.addPcName(noble);
  422. noble.sendPacket(sm);
  423. return false;
  424. }
  425. /** Begin Olympiad Restrictions */
  426. if (noble.getBaseClass() != noble.getClassId().getId())
  427. {
  428. sm = new SystemMessage(SystemMessageId.C1_CANT_JOIN_THE_OLYMPIAD_WITH_A_SUB_CLASS_CHARACTER);
  429. sm.addPcName(noble);
  430. noble.sendPacket(sm);
  431. return false;
  432. }
  433. if (noble.isCursedWeaponEquipped())
  434. {
  435. sm = new SystemMessage(SystemMessageId.C1_CANNOT_JOIN_OLYMPIAD_POSSESSING_S2);
  436. sm.addPcName(noble);
  437. sm.addItemName(noble.getCursedWeaponEquippedId());
  438. noble.sendPacket(sm);
  439. return false;
  440. }
  441. if (noble.getInventoryLimit() * 0.8 <= noble.getInventory().getSize())
  442. {
  443. sm = new SystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_INVENTORY_SLOT_EXCEEDS_80_PERCENT);
  444. sm.addPcName(noble);
  445. noble.sendPacket(sm);
  446. return false;
  447. }
  448. if (getMillisToCompEnd() < 600000)
  449. {
  450. sm = new SystemMessage(SystemMessageId.GAME_REQUEST_CANNOT_BE_MADE);
  451. noble.sendPacket(sm);
  452. return false;
  453. }
  454. if (TvTEvent.isPlayerParticipant(noble.getObjectId()))
  455. {
  456. noble.sendMessage("You can't join olympiad while participating on TvT Event.");
  457. return false;
  458. }
  459. /** End Olympiad Restrictions */
  460. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  461. {
  462. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  463. for (L2PcInstance participant : classed)
  464. {
  465. if (participant.getObjectId() == noble.getObjectId())
  466. {
  467. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_CLASS_MATCH_WAITING_LIST);
  468. sm.addPcName(noble);
  469. noble.sendPacket(sm);
  470. return false;
  471. }
  472. }
  473. }
  474. if (isRegisteredInComp(noble))
  475. {
  476. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_NON_CLASS_LIMITED_MATCH_WAITING_LIST);
  477. sm.addPcName(noble);
  478. noble.sendPacket(sm);
  479. return false;
  480. }
  481. if (!_nobles.containsKey(noble.getObjectId()))
  482. {
  483. StatsSet statDat = new StatsSet();
  484. statDat.set(CLASS_ID, noble.getClassId().getId());
  485. statDat.set(CHAR_NAME, noble.getName());
  486. statDat.set(POINTS, DEFAULT_POINTS);
  487. statDat.set(COMP_DONE, 0);
  488. statDat.set(COMP_WON, 0);
  489. statDat.set(COMP_LOST, 0);
  490. statDat.set(COMP_DRAWN, 0);
  491. statDat.set("to_save", true);
  492. _nobles.put(noble.getObjectId(), statDat);
  493. }
  494. if (classBased && getNoblePoints(noble.getObjectId()) < 3)
  495. {
  496. noble.sendMessage("Cant register when you have less than 3 points");
  497. return false;
  498. }
  499. if (!classBased && getNoblePoints(noble.getObjectId()) < 5)
  500. {
  501. noble.sendMessage("Cant register when you have less than 5 points");
  502. return false;
  503. }
  504. if (classBased)
  505. {
  506. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  507. {
  508. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  509. classed.add(noble);
  510. _classBasedRegisters.remove(noble.getClassId().getId());
  511. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  512. }
  513. else
  514. {
  515. L2FastList<L2PcInstance> classed = new L2FastList<L2PcInstance>();
  516. classed.add(noble);
  517. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  518. }
  519. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  520. noble.sendPacket(sm);
  521. }
  522. else
  523. {
  524. _nonClassBasedRegisters.add(noble);
  525. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  526. noble.sendPacket(sm);
  527. }
  528. return true;
  529. }
  530. protected static int getNobleCount()
  531. {
  532. return _nobles.size();
  533. }
  534. protected static StatsSet getNobleStats(int playerId)
  535. {
  536. return _nobles.get(playerId);
  537. }
  538. protected static synchronized void updateNobleStats(int playerId, StatsSet stats)
  539. {
  540. _nobles.remove(playerId);
  541. _nobles.put(playerId, stats);
  542. }
  543. protected static L2FastList<L2PcInstance> getRegisteredNonClassBased()
  544. {
  545. return _nonClassBasedRegisters;
  546. }
  547. protected static Map<Integer, L2FastList<L2PcInstance>> getRegisteredClassBased()
  548. {
  549. return _classBasedRegisters;
  550. }
  551. protected static L2FastList<Integer> hasEnoughRegisteredClassed()
  552. {
  553. L2FastList<Integer> result = new L2FastList<Integer>();
  554. for (Integer classList : getRegisteredClassBased().keySet())
  555. {
  556. if (getRegisteredClassBased().get(classList).size() >= Config.ALT_OLY_CLASSED)
  557. {
  558. result.add(classList);
  559. }
  560. }
  561. if (!result.isEmpty())
  562. {
  563. return result;
  564. }
  565. return null;
  566. }
  567. protected static boolean hasEnoughRegisteredNonClassed()
  568. {
  569. return Olympiad.getRegisteredNonClassBased().size() >= Config.ALT_OLY_NONCLASSED;
  570. }
  571. protected static void clearRegistered()
  572. {
  573. _nonClassBasedRegisters.clear();
  574. _classBasedRegisters.clear();
  575. }
  576. public boolean isRegistered(L2PcInstance noble)
  577. {
  578. boolean result = false;
  579. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(noble))
  580. result = true;
  581. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(noble.getClassId().getId()))
  582. {
  583. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  584. if (classed != null && classed.contains(noble))
  585. result = true;
  586. }
  587. return result;
  588. }
  589. public boolean unRegisterNoble(L2PcInstance noble)
  590. {
  591. SystemMessage sm;
  592. /*
  593. * if (_compStarted) {
  594. * noble.sendMessage("Cant Unregister whilst competition is under way");
  595. * return false; }
  596. */
  597. if (!_inCompPeriod)
  598. {
  599. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  600. noble.sendPacket(sm);
  601. return false;
  602. }
  603. if (!noble.isNoble())
  604. {
  605. sm = new SystemMessage(SystemMessageId.C1_DOES_NOT_MEET_REQUIREMENTS_ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  606. sm.addString(noble.getName());
  607. noble.sendPacket(sm);
  608. return false;
  609. }
  610. if (!isRegistered(noble))
  611. {
  612. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  613. noble.sendPacket(sm);
  614. return false;
  615. }
  616. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  617. {
  618. if (game == null)
  619. continue;
  620. if (game._playerOneID == noble.getObjectId() || game._playerTwoID == noble.getObjectId())
  621. {
  622. noble.sendMessage("Can't deregister whilst you are already selected for a game");
  623. return false;
  624. }
  625. }
  626. if (_nonClassBasedRegisters.contains(noble))
  627. _nonClassBasedRegisters.remove(noble);
  628. else
  629. {
  630. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  631. classed.remove(noble);
  632. _classBasedRegisters.remove(noble.getClassId().getId());
  633. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  634. }
  635. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  636. noble.sendPacket(sm);
  637. return true;
  638. }
  639. public void removeDisconnectedCompetitor(L2PcInstance player)
  640. {
  641. if (OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()) != null)
  642. OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()).handleDisconnect(player);
  643. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  644. if (_nonClassBasedRegisters.contains(player))
  645. _nonClassBasedRegisters.remove(player);
  646. else if (classed != null && classed.contains(player))
  647. {
  648. classed.remove(player);
  649. _classBasedRegisters.remove(player.getClassId().getId());
  650. _classBasedRegisters.put(player.getClassId().getId(), classed);
  651. }
  652. }
  653. public void notifyCompetitorDamage(L2PcInstance player, int damage, int gameId)
  654. {
  655. if (OlympiadManager.getInstance().getOlympiadGames().get(gameId) != null)
  656. OlympiadManager.getInstance().getOlympiadGames().get(gameId).addDamage(player, damage);
  657. }
  658. private void updateCompStatus()
  659. {
  660. // _compStarted = false;
  661. synchronized (this)
  662. {
  663. long milliToStart = getMillisToCompBegin();
  664. double numSecs = (milliToStart / 1000) % 60;
  665. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  666. int numMins = (int) Math.floor(countDown % 60);
  667. countDown = (countDown - numMins) / 60;
  668. int numHours = (int) Math.floor(countDown % 24);
  669. int numDays = (int) Math.floor((countDown - numHours) / 24);
  670. _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins
  671. + " mins.");
  672. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  673. }
  674. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  675. public void run()
  676. {
  677. if (isOlympiadEnd())
  678. return;
  679. _inCompPeriod = true;
  680. OlympiadManager om = OlympiadManager.getInstance();
  681. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  682. _log.info("Olympiad System: Olympiad Game Started");
  683. _logResults.info("Result,Player1,Player2,Player1 HP,Player2 HP,Player1 Damage,Player2 Damage,Points,Classed");
  684. Thread olyCycle = new Thread(om);
  685. olyCycle.start();
  686. long regEnd = getMillisToCompEnd() - 600000;
  687. if (regEnd > 0)
  688. {
  689. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  690. public void run()
  691. {
  692. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.OLYMPIAD_REGISTRATION_PERIOD_ENDED));
  693. }
  694. }, regEnd);
  695. }
  696. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  697. public void run()
  698. {
  699. if (isOlympiadEnd())
  700. return;
  701. _inCompPeriod = false;
  702. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  703. _log.info("Olympiad System: Olympiad Game Ended");
  704. while (OlympiadGame._battleStarted)
  705. {
  706. try
  707. {
  708. // wait 1 minutes for end of pendings games
  709. Thread.sleep(60000);
  710. }
  711. catch (InterruptedException e)
  712. {
  713. }
  714. }
  715. saveOlympiadStatus();
  716. init();
  717. }
  718. }, getMillisToCompEnd());
  719. }
  720. }, getMillisToCompBegin());
  721. }
  722. private long getMillisToOlympiadEnd()
  723. {
  724. // if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  725. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  726. // return 10L;
  727. }
  728. public void manualSelectHeroes()
  729. {
  730. if (_scheduledOlympiadEnd != null)
  731. _scheduledOlympiadEnd.cancel(true);
  732. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), 0);
  733. }
  734. protected long getMillisToValidationEnd()
  735. {
  736. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  737. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  738. return 10L;
  739. }
  740. public boolean isOlympiadEnd()
  741. {
  742. return (_period != 0);
  743. }
  744. protected void setNewOlympiadEnd()
  745. {
  746. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  747. sm.addNumber(_currentCycle);
  748. Announcements.getInstance().announceToAll(sm);
  749. Calendar currentTime = Calendar.getInstance();
  750. currentTime.add(Calendar.MONTH, 1);
  751. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  752. currentTime.set(Calendar.AM_PM, Calendar.AM);
  753. currentTime.set(Calendar.HOUR, 12);
  754. currentTime.set(Calendar.MINUTE, 0);
  755. currentTime.set(Calendar.SECOND, 0);
  756. _olympiadEnd = currentTime.getTimeInMillis();
  757. Calendar nextChange = Calendar.getInstance();
  758. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  759. scheduleWeeklyChange();
  760. }
  761. public boolean inCompPeriod()
  762. {
  763. return _inCompPeriod;
  764. }
  765. private long getMillisToCompBegin()
  766. {
  767. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() && _compEnd > Calendar.getInstance().getTimeInMillis())
  768. return 10L;
  769. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  770. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  771. return setNewCompBegin();
  772. }
  773. private long setNewCompBegin()
  774. {
  775. _compStart = Calendar.getInstance();
  776. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  777. _compStart.set(Calendar.MINUTE, COMP_MIN);
  778. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  779. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  780. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  781. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  782. }
  783. protected long getMillisToCompEnd()
  784. {
  785. // if (_compEnd > Calendar.getInstance().getTimeInMillis())
  786. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  787. // return 10L;
  788. }
  789. private long getMillisToWeekChange()
  790. {
  791. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  792. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  793. return 10L;
  794. }
  795. private void scheduleWeeklyChange()
  796. {
  797. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  798. public void run()
  799. {
  800. addWeeklyPoints();
  801. _log.info("Olympiad System: Added weekly points to nobles");
  802. Calendar nextChange = Calendar.getInstance();
  803. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  804. }
  805. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  806. }
  807. protected synchronized void addWeeklyPoints()
  808. {
  809. if (_period == 1)
  810. return;
  811. for (Integer nobleId : _nobles.keySet())
  812. {
  813. StatsSet nobleInfo = _nobles.get(nobleId);
  814. int currentPoints = nobleInfo.getInteger(POINTS);
  815. currentPoints += WEEKLY_POINTS;
  816. nobleInfo.set(POINTS, currentPoints);
  817. updateNobleStats(nobleId, nobleInfo);
  818. }
  819. }
  820. public FastMap<Integer, String> getMatchList()
  821. {
  822. return OlympiadManager.getInstance().getAllTitles();
  823. }
  824. // returns the players for the given olympiad game Id
  825. public L2PcInstance[] getPlayers(int Id)
  826. {
  827. if (OlympiadManager.getInstance().getOlympiadGame(Id) == null)
  828. return null;
  829. else
  830. return OlympiadManager.getInstance().getOlympiadGame(Id).getPlayers();
  831. }
  832. public int getCurrentCycle()
  833. {
  834. return _currentCycle;
  835. }
  836. public static void addSpectator(int id, L2PcInstance spectator, boolean storeCoords)
  837. {
  838. if (getInstance().isRegisteredInComp(spectator))
  839. {
  840. spectator.sendPacket(new SystemMessage(SystemMessageId.WHILE_YOU_ARE_ON_THE_WAITING_LIST_YOU_ARE_NOT_ALLOWED_TO_WATCH_THE_GAME));
  841. return;
  842. }
  843. if (!TvTEvent.isInactive() && TvTEvent.isPlayerParticipant(spectator.getObjectId()))
  844. {
  845. spectator.sendMessage("You can not observe games while registered for TvT");
  846. return;
  847. }
  848. OlympiadManager.STADIUMS[id].addSpectator(id, spectator, storeCoords);
  849. }
  850. public static int getSpectatorArena(L2PcInstance player)
  851. {
  852. for (int i = 0; i < OlympiadManager.STADIUMS.length; i++)
  853. {
  854. if (OlympiadManager.STADIUMS[i].getSpectators().contains(player))
  855. return i;
  856. }
  857. return -1;
  858. }
  859. public static void removeSpectator(int id, L2PcInstance spectator)
  860. {
  861. OlympiadManager.STADIUMS[id].removeSpectator(spectator);
  862. }
  863. public L2FastList<L2PcInstance> getSpectators(int id)
  864. {
  865. if (OlympiadManager.getInstance().getOlympiadGame(id) == null)
  866. return null;
  867. return OlympiadManager.STADIUMS[id].getSpectators();
  868. }
  869. public Map<Integer, OlympiadGame> getOlympiadGames()
  870. {
  871. return OlympiadManager.getInstance().getOlympiadGames();
  872. }
  873. public boolean playerInStadia(L2PcInstance player)
  874. {
  875. return (ZoneManager.getInstance().getOlympiadStadium(player) != null);
  876. }
  877. public int[] getWaitingList()
  878. {
  879. int[] array = new int[2];
  880. if (!inCompPeriod())
  881. return null;
  882. int classCount = 0;
  883. if (_classBasedRegisters.size() != 0)
  884. for (L2FastList<L2PcInstance> classed : _classBasedRegisters.values())
  885. {
  886. classCount += classed.size();
  887. }
  888. array[0] = classCount;
  889. array[1] = _nonClassBasedRegisters.size();
  890. return array;
  891. }
  892. /**
  893. * Save noblesse data to database
  894. */
  895. protected synchronized void saveNobleData()
  896. {
  897. if (_nobles == null || _nobles.isEmpty())
  898. return;
  899. Connection con = null;
  900. try
  901. {
  902. con = L2DatabaseFactory.getInstance().getConnection();
  903. PreparedStatement statement;
  904. for (Integer nobleId : _nobles.keySet())
  905. {
  906. StatsSet nobleInfo = _nobles.get(nobleId);
  907. if (nobleInfo == null)
  908. continue;
  909. int charId = nobleId;
  910. int classId = nobleInfo.getInteger(CLASS_ID);
  911. int points = nobleInfo.getInteger(POINTS);
  912. int compDone = nobleInfo.getInteger(COMP_DONE);
  913. int compWon = nobleInfo.getInteger(COMP_WON);
  914. int compLost = nobleInfo.getInteger(COMP_LOST);
  915. int compDrawn = nobleInfo.getInteger(COMP_DRAWN);
  916. boolean toSave = nobleInfo.getBool("to_save");
  917. if (toSave)
  918. {
  919. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  920. statement.setInt(1, charId);
  921. statement.setInt(2, classId);
  922. statement.setInt(3, points);
  923. statement.setInt(4, compDone);
  924. statement.setInt(5, compWon);
  925. statement.setInt(6, compLost);
  926. statement.setInt(7, compDrawn);
  927. nobleInfo.set("to_save", false);
  928. updateNobleStats(nobleId, nobleInfo);
  929. }
  930. else
  931. {
  932. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  933. statement.setInt(1, points);
  934. statement.setInt(2, compDone);
  935. statement.setInt(3, compWon);
  936. statement.setInt(4, compLost);
  937. statement.setInt(5, compDrawn);
  938. statement.setInt(6, charId);
  939. }
  940. statement.execute();
  941. statement.close();
  942. }
  943. }
  944. catch (SQLException e)
  945. {
  946. _log.log(Level.SEVERE, "Olympiad System: Failed to save noblesse data to database: ", e);
  947. }
  948. finally
  949. {
  950. try
  951. {
  952. con.close();
  953. }
  954. catch (Exception e)
  955. {
  956. e.printStackTrace();
  957. }
  958. }
  959. }
  960. /**
  961. * Save olympiad.properties file with current olympiad status and update noblesse table in database
  962. */
  963. public void saveOlympiadStatus()
  964. {
  965. saveNobleData();
  966. Connection con = null;
  967. try
  968. {
  969. con = L2DatabaseFactory.getInstance().getConnection();
  970. PreparedStatement statement = con.prepareStatement(OLYMPIAD_SAVE_DATA);
  971. statement.setInt(1, _currentCycle);
  972. statement.setInt(2, _period);
  973. statement.setLong(3, _olympiadEnd);
  974. statement.setLong(4, _validationEnd);
  975. statement.setLong(5, _nextWeeklyChange);
  976. statement.setInt(6, _currentCycle);
  977. statement.setInt(7, _period);
  978. statement.setLong(8, _olympiadEnd);
  979. statement.setLong(9, _validationEnd);
  980. statement.setLong(10, _nextWeeklyChange);
  981. statement.execute();
  982. statement.close();
  983. }
  984. catch (SQLException e)
  985. {
  986. _log.log(Level.SEVERE, "Olympiad System: Failed to save olympiad data to database: ", e);
  987. }
  988. finally
  989. {
  990. try
  991. {
  992. con.close();
  993. }
  994. catch (Exception e)
  995. {
  996. e.printStackTrace();
  997. }
  998. }
  999. /* Properties OlympiadProperties = new Properties();
  1000. FileOutputStream fos = null;
  1001. try
  1002. {
  1003. fos = new FileOutputStream(new File("./" + OLYMPIAD_DATA_FILE));
  1004. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  1005. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  1006. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  1007. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  1008. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  1009. OlympiadProperties.store(fos, "Olympiad Properties");
  1010. }
  1011. catch (Exception e)
  1012. {
  1013. _log.log(Level.WARNING, "Olympiad System: Unable to save olympiad properties to file: ", e);
  1014. }
  1015. finally
  1016. {
  1017. try
  1018. {
  1019. fos.close();
  1020. }
  1021. catch (Exception e)
  1022. {
  1023. }
  1024. }*/
  1025. }
  1026. protected void updateMonthlyData()
  1027. {
  1028. Connection con = null;
  1029. try
  1030. {
  1031. con = L2DatabaseFactory.getInstance().getConnection();
  1032. PreparedStatement statement;
  1033. statement = con.prepareStatement(OLYMPIAD_MONTH_CLEAR);
  1034. statement.execute();
  1035. statement.close();
  1036. statement = con.prepareStatement(OLYMPIAD_MONTH_CREATE);
  1037. statement.execute();
  1038. statement.close();
  1039. }
  1040. catch (SQLException e)
  1041. {
  1042. _log.log(Level.SEVERE, "Olympiad System: Failed to update monthly noblese data: ", e);
  1043. }
  1044. finally
  1045. {
  1046. try
  1047. {
  1048. con.close();
  1049. }
  1050. catch (Exception e)
  1051. {
  1052. }
  1053. }
  1054. }
  1055. protected void sortHerosToBe()
  1056. {
  1057. if (_period != 1)
  1058. return;
  1059. LogRecord record;
  1060. if (_nobles != null)
  1061. {
  1062. _logResults.info("Noble,charid,classid,compDone,points");
  1063. for (Integer nobleId : _nobles.keySet())
  1064. {
  1065. StatsSet nobleInfo = _nobles.get(nobleId);
  1066. if (nobleInfo == null)
  1067. continue;
  1068. int charId = nobleId;
  1069. int classId = nobleInfo.getInteger(CLASS_ID);
  1070. String charName = nobleInfo.getString(CHAR_NAME);
  1071. int points = nobleInfo.getInteger(POINTS);
  1072. int compDone = nobleInfo.getInteger(COMP_DONE);
  1073. record = new LogRecord(Level.INFO, charName);
  1074. record.setParameters(new Object[]{charId, classId, compDone, points});
  1075. _logResults.log(record);
  1076. }
  1077. }
  1078. _heroesToBe = new L2FastList<StatsSet>();
  1079. Connection con = null;
  1080. try
  1081. {
  1082. con = L2DatabaseFactory.getInstance().getConnection();
  1083. PreparedStatement statement;
  1084. ResultSet rset;
  1085. StatsSet hero;
  1086. L2FastList<StatsSet> soulHounds = new L2FastList<StatsSet>();
  1087. for (int i = 0; i < HERO_IDS.length; i++)
  1088. {
  1089. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  1090. statement.setInt(1, HERO_IDS[i]);
  1091. rset = statement.executeQuery();
  1092. if (rset.next())
  1093. {
  1094. hero = new StatsSet();
  1095. hero.set(CLASS_ID, HERO_IDS[i]);
  1096. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1097. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  1098. if (HERO_IDS[i] == 132 || HERO_IDS[i] == 133) // Male & Female Soulhounds rank as one hero class
  1099. {
  1100. hero = _nobles.get(hero.getInteger(CHAR_ID));
  1101. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1102. soulHounds.add(hero);
  1103. }
  1104. else
  1105. {
  1106. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1107. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1108. _logResults.log(record);
  1109. _heroesToBe.add(hero);
  1110. }
  1111. }
  1112. statement.close();
  1113. rset.close();
  1114. }
  1115. switch (soulHounds.size())
  1116. {
  1117. case 0:
  1118. {
  1119. break;
  1120. }
  1121. case 1:
  1122. {
  1123. hero = new StatsSet();
  1124. StatsSet winner = soulHounds.get(0);
  1125. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1126. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1127. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1128. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1129. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1130. _logResults.log(record);
  1131. _heroesToBe.add(hero);
  1132. break;
  1133. }
  1134. case 2:
  1135. {
  1136. hero = new StatsSet();
  1137. StatsSet winner;
  1138. StatsSet hero1 = soulHounds.get(0);
  1139. StatsSet hero2 = soulHounds.get(1);
  1140. int hero1Points = hero1.getInteger(POINTS);
  1141. int hero2Points = hero2.getInteger(POINTS);
  1142. int hero1Comps = hero1.getInteger(COMP_DONE);
  1143. int hero2Comps = hero2.getInteger(COMP_DONE);
  1144. if (hero1Points > hero2Points)
  1145. winner = hero1;
  1146. else if (hero2Points > hero1Points)
  1147. winner = hero2;
  1148. else
  1149. {
  1150. if (hero1Comps > hero2Comps)
  1151. winner = hero1;
  1152. else
  1153. winner = hero2;
  1154. }
  1155. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1156. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1157. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1158. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1159. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1160. _logResults.log(record);
  1161. _heroesToBe.add(hero);
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. catch (SQLException e)
  1167. {
  1168. _log.warning("Olympiad System: Couldnt load heros from DB");
  1169. }
  1170. finally
  1171. {
  1172. try
  1173. {
  1174. con.close();
  1175. }
  1176. catch (Exception e)
  1177. {
  1178. e.printStackTrace();
  1179. }
  1180. }
  1181. }
  1182. public L2FastList<String> getClassLeaderBoard(int classId)
  1183. {
  1184. // if (_period != 1) return;
  1185. L2FastList<String> names = new L2FastList<String>();
  1186. Connection con = null;
  1187. try
  1188. {
  1189. con = L2DatabaseFactory.getInstance().getConnection();
  1190. PreparedStatement statement;
  1191. ResultSet rset;
  1192. if (Config.ALT_OLY_SHOW_MONTHLY_WINNERS)
  1193. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  1194. else
  1195. statement = con.prepareStatement(GET_EACH_CLASS_LEADER_CURRENT);
  1196. statement.setInt(1, classId);
  1197. rset = statement.executeQuery();
  1198. while (rset.next())
  1199. {
  1200. names.add(rset.getString(CHAR_NAME));
  1201. }
  1202. if (classId == 132) // Male & Female SoulHounds are ranked together
  1203. {
  1204. statement.setInt(1, 133);
  1205. rset = statement.executeQuery();
  1206. while (rset.next())
  1207. {
  1208. names.add(rset.getString(CHAR_NAME));
  1209. }
  1210. }
  1211. statement.close();
  1212. rset.close();
  1213. return names;
  1214. }
  1215. catch (SQLException e)
  1216. {
  1217. _log.warning("Olympiad System: Couldnt load olympiad leaders from DB");
  1218. }
  1219. finally
  1220. {
  1221. try
  1222. {
  1223. con.close();
  1224. }
  1225. catch (Exception e)
  1226. {
  1227. e.printStackTrace();
  1228. }
  1229. }
  1230. return names;
  1231. }
  1232. public int getNoblessePasses(L2PcInstance player, boolean clear)
  1233. {
  1234. if (_period != 1 || _noblesRank.isEmpty())
  1235. return 0;
  1236. int objId = player.getObjectId();
  1237. if (!_noblesRank.containsKey(objId))
  1238. return 0;
  1239. StatsSet noble = _nobles.get(objId);
  1240. if (noble.getInteger(POINTS) == 0)
  1241. return 0;
  1242. int rank = _noblesRank.get(objId);
  1243. int points = (player.isHero() ? Config.ALT_OLY_HERO_POINTS : 0);
  1244. switch (rank)
  1245. {
  1246. case 1:
  1247. points += Config.ALT_OLY_RANK1_POINTS;
  1248. break;
  1249. case 2:
  1250. points += Config.ALT_OLY_RANK2_POINTS;
  1251. break;
  1252. case 3:
  1253. points += Config.ALT_OLY_RANK3_POINTS;
  1254. break;
  1255. case 4:
  1256. points += Config.ALT_OLY_RANK4_POINTS;
  1257. break;
  1258. default:
  1259. points += Config.ALT_OLY_RANK5_POINTS;
  1260. }
  1261. if (clear)
  1262. {
  1263. noble.set(POINTS, 0);
  1264. updateNobleStats(objId, noble);
  1265. }
  1266. points *= Config.ALT_OLY_GP_PER_POINT;
  1267. return points;
  1268. }
  1269. public boolean isRegisteredInComp(L2PcInstance player)
  1270. {
  1271. boolean result = isRegistered(player);
  1272. if (_inCompPeriod)
  1273. {
  1274. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  1275. {
  1276. if ((game._playerOneID == player.getObjectId()) || (game._playerTwoID == player.getObjectId()))
  1277. {
  1278. result = true;
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. return result;
  1284. }
  1285. public int getNoblePoints(int objId)
  1286. {
  1287. if (_nobles.isEmpty())
  1288. return 0;
  1289. StatsSet noble = _nobles.get(objId);
  1290. if (noble == null)
  1291. return 0;
  1292. int points = noble.getInteger(POINTS);
  1293. return points;
  1294. }
  1295. public int getLastNobleOlympiadPoints(int objId)
  1296. {
  1297. int result = 0;
  1298. Connection con = null;
  1299. try
  1300. {
  1301. con = L2DatabaseFactory.getInstance().getConnection();
  1302. PreparedStatement statement;
  1303. statement = con.prepareStatement("SELECT olympiad_points FROM olympiad_nobles_eom WHERE charId = ?");
  1304. statement.setInt(1, objId);
  1305. ResultSet rs = statement.executeQuery();
  1306. if (rs.first())
  1307. result = rs.getInt(1);
  1308. rs.close();
  1309. statement.close();
  1310. }
  1311. catch (Exception e)
  1312. {
  1313. _log.log(Level.WARNING, "Could not load last olympiad points:", e);
  1314. }
  1315. finally
  1316. {
  1317. try
  1318. {
  1319. con.close();
  1320. }
  1321. catch (Exception e){}
  1322. }
  1323. return result;
  1324. }
  1325. public int getCompetitionDone(int objId)
  1326. {
  1327. if (_nobles.isEmpty())
  1328. return 0;
  1329. StatsSet noble = _nobles.get(objId);
  1330. if (noble == null)
  1331. return 0;
  1332. int points = noble.getInteger(COMP_DONE);
  1333. return points;
  1334. }
  1335. public int getCompetitionWon(int objId)
  1336. {
  1337. if (_nobles.isEmpty())
  1338. return 0;
  1339. StatsSet noble = _nobles.get(objId);
  1340. if (noble == null)
  1341. return 0;
  1342. int points = noble.getInteger(COMP_WON);
  1343. return points;
  1344. }
  1345. public int getCompetitionLost(int objId)
  1346. {
  1347. if (_nobles.isEmpty())
  1348. return 0;
  1349. StatsSet noble = _nobles.get(objId);
  1350. if (noble == null)
  1351. return 0;
  1352. int points = noble.getInteger(COMP_LOST);
  1353. return points;
  1354. }
  1355. protected void deleteNobles()
  1356. {
  1357. Connection con = null;
  1358. try
  1359. {
  1360. con = L2DatabaseFactory.getInstance().getConnection();
  1361. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  1362. statement.execute();
  1363. statement.close();
  1364. }
  1365. catch (SQLException e)
  1366. {
  1367. _log.warning("Olympiad System: Couldnt delete nobles from DB");
  1368. }
  1369. finally
  1370. {
  1371. try
  1372. {
  1373. con.close();
  1374. }
  1375. catch (Exception e)
  1376. {
  1377. e.printStackTrace();
  1378. }
  1379. }
  1380. _nobles.clear();
  1381. }
  1382. public static void sendMatchList(L2PcInstance player)
  1383. {
  1384. NpcHtmlMessage message = new NpcHtmlMessage(0);
  1385. message.setFile(Olympiad.OLYMPIAD_HTML_PATH + "olympiad_observe2.htm");
  1386. FastMap<Integer, String> matches = getInstance().getMatchList();
  1387. for (int i = 0; i < Olympiad.getStadiumCount(); i++)
  1388. {
  1389. int arenaId = i + 1;
  1390. String state = "Initial State";
  1391. String players = "&nbsp;";
  1392. if (matches.containsKey(i))
  1393. {
  1394. if (OlympiadGame._gameIsStarted)
  1395. state = "Playing";
  1396. else
  1397. state = "Standby";
  1398. players = matches.get(i);
  1399. }
  1400. message.replace("%state"+ arenaId +"%", state);
  1401. message.replace("%players"+ arenaId +"%", players);
  1402. }
  1403. player.sendPacket(message);
  1404. }
  1405. public static void bypassChangeArena(String command, L2PcInstance player)
  1406. {
  1407. if (!player.inObserverMode())
  1408. return;
  1409. String[] commands = command.split(" ");
  1410. int id = Integer.parseInt(commands[1]);
  1411. int arena = getSpectatorArena(player);
  1412. if (arena >= 0)
  1413. Olympiad.removeSpectator(arena, player);
  1414. else
  1415. return;
  1416. Olympiad.addSpectator(id, player, false);
  1417. }
  1418. @SuppressWarnings("synthetic-access")
  1419. private static class SingletonHolder
  1420. {
  1421. protected static final Olympiad _instance = new Olympiad();
  1422. }
  1423. }