Olympiad.java 48 KB

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