Olympiad.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.olympiad;
  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.HashMap;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30. import java.util.Properties;
  31. import java.util.concurrent.ScheduledFuture;
  32. import java.util.logging.Level;
  33. import java.util.logging.LogRecord;
  34. import java.util.logging.Logger;
  35. import javolution.util.FastMap;
  36. import com.l2jserver.Config;
  37. import com.l2jserver.L2DatabaseFactory;
  38. import com.l2jserver.gameserver.Announcements;
  39. import com.l2jserver.gameserver.ThreadPoolManager;
  40. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  41. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  42. import com.l2jserver.gameserver.model.StatsSet;
  43. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  44. import com.l2jserver.gameserver.model.entity.Hero;
  45. import com.l2jserver.gameserver.network.SystemMessageId;
  46. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  47. import com.l2jserver.util.L2FastList;
  48. /**
  49. * @author godson
  50. */
  51. public class Olympiad
  52. {
  53. protected static final Logger _log = Logger.getLogger(Olympiad.class.getName());
  54. protected static final Logger _logResults = Logger.getLogger("olympiad");
  55. private static final Map<Integer, StatsSet> _nobles = new FastMap<>();
  56. protected static L2FastList<StatsSet> _heroesToBe;
  57. private static final Map<Integer, Integer> _noblesRank = new HashMap<>();
  58. public static final String OLYMPIAD_HTML_PATH = "data/html/olympiad/";
  59. private static final String OLYMPIAD_LOAD_DATA = "SELECT current_cycle, period, olympiad_end, validation_end, " + "next_weekly_change FROM olympiad_data WHERE id = 0";
  60. private static final String OLYMPIAD_SAVE_DATA = "INSERT INTO olympiad_data (id, current_cycle, " + "period, olympiad_end, validation_end, next_weekly_change) VALUES (0,?,?,?,?,?) " + "ON DUPLICATE KEY UPDATE current_cycle=?, period=?, olympiad_end=?, " + "validation_end=?, next_weekly_change=?";
  61. private static final String OLYMPIAD_LOAD_NOBLES = "SELECT olympiad_nobles.charId, olympiad_nobles.class_id, " + "characters.char_name, olympiad_nobles.olympiad_points, olympiad_nobles.competitions_done, " + "olympiad_nobles.competitions_won, olympiad_nobles.competitions_lost, olympiad_nobles.competitions_drawn, " + "olympiad_nobles.competitions_done_week, olympiad_nobles.competitions_done_week_classed, olympiad_nobles.competitions_done_week_non_classed, olympiad_nobles.competitions_done_week_team " + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId";
  62. private static final String OLYMPIAD_SAVE_NOBLES = "INSERT INTO olympiad_nobles " + "(`charId`,`class_id`,`olympiad_points`,`competitions_done`,`competitions_won`,`competitions_lost`," + "`competitions_drawn`, `competitions_done_week`, `competitions_done_week_classed`, `competitions_done_week_non_classed`, `competitions_done_week_team`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
  63. private static final String OLYMPIAD_UPDATE_NOBLES = "UPDATE olympiad_nobles SET " + "olympiad_points = ?, competitions_done = ?, competitions_won = ?, competitions_lost = ?, competitions_drawn = ?, competitions_done_week = ?, competitions_done_week_classed = ?, competitions_done_week_non_classed = ?, competitions_done_week_team = ? WHERE charId = ?";
  64. private static final String OLYMPIAD_GET_HEROS = "SELECT olympiad_nobles.charId, characters.char_name " + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId " + "AND olympiad_nobles.class_id = ? AND olympiad_nobles.competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " AND olympiad_nobles.competitions_won > 0 " + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC, olympiad_nobles.competitions_won DESC";
  65. private static final String GET_ALL_CLASSIFIED_NOBLESS = "SELECT charId from olympiad_nobles_eom " + "WHERE competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " ORDER BY olympiad_points DESC, competitions_done DESC, competitions_won DESC";
  66. private static final String GET_EACH_CLASS_LEADER = "SELECT characters.char_name from olympiad_nobles_eom, characters " + "WHERE characters.charId = olympiad_nobles_eom.charId AND olympiad_nobles_eom.class_id = ? " + "AND olympiad_nobles_eom.competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " " + "ORDER BY olympiad_nobles_eom.olympiad_points DESC, olympiad_nobles_eom.competitions_done DESC, olympiad_nobles_eom.competitions_won DESC LIMIT 10";
  67. private static final String GET_EACH_CLASS_LEADER_CURRENT = "SELECT characters.char_name from olympiad_nobles, characters " + "WHERE characters.charId = olympiad_nobles.charId AND olympiad_nobles.class_id = ? " + "AND olympiad_nobles.competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " " + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC, olympiad_nobles.competitions_won DESC LIMIT 10";
  68. private static final String GET_EACH_CLASS_LEADER_SOULHOUND = "SELECT characters.char_name from olympiad_nobles_eom, characters " + "WHERE characters.charId = olympiad_nobles_eom.charId AND (olympiad_nobles_eom.class_id = ? OR olympiad_nobles_eom.class_id = 133) " + "AND olympiad_nobles_eom.competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " " + "ORDER BY olympiad_nobles_eom.olympiad_points DESC, olympiad_nobles_eom.competitions_done DESC, olympiad_nobles_eom.competitions_won DESC LIMIT 10";
  69. private static final String GET_EACH_CLASS_LEADER_CURRENT_SOULHOUND = "SELECT characters.char_name from olympiad_nobles, characters " + "WHERE characters.charId = olympiad_nobles.charId AND (olympiad_nobles.class_id = ? OR olympiad_nobles.class_id = 133) " + "AND olympiad_nobles.competitions_done >= " + Config.ALT_OLY_MIN_MATCHES + " " + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC, olympiad_nobles.competitions_won DESC LIMIT 10";
  70. private static final String OLYMPIAD_DELETE_ALL = "TRUNCATE olympiad_nobles";
  71. private static final String OLYMPIAD_MONTH_CLEAR = "TRUNCATE olympiad_nobles_eom";
  72. private static final String OLYMPIAD_MONTH_CREATE = "INSERT INTO olympiad_nobles_eom SELECT charId, class_id, olympiad_points, competitions_done, competitions_won, competitions_lost, competitions_drawn FROM olympiad_nobles";
  73. private static final int[] HERO_IDS =
  74. {
  75. 88,
  76. 89,
  77. 90,
  78. 91,
  79. 92,
  80. 93,
  81. 94,
  82. 95,
  83. 96,
  84. 97,
  85. 98,
  86. 99,
  87. 100,
  88. 101,
  89. 102,
  90. 103,
  91. 104,
  92. 105,
  93. 106,
  94. 107,
  95. 108,
  96. 109,
  97. 110,
  98. 111,
  99. 112,
  100. 113,
  101. 114,
  102. 115,
  103. 116,
  104. 117,
  105. 118,
  106. 131,
  107. 132,
  108. 133,
  109. 134
  110. };
  111. private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
  112. private static final int COMP_MIN = Config.ALT_OLY_MIN; // 00 mins
  113. private static final long COMP_PERIOD = Config.ALT_OLY_CPERIOD; // 6 hours
  114. protected static final long WEEKLY_PERIOD = Config.ALT_OLY_WPERIOD; // 1 week
  115. protected static final long VALIDATION_PERIOD = Config.ALT_OLY_VPERIOD; // 24 hours
  116. protected static final int DEFAULT_POINTS = Config.ALT_OLY_START_POINTS;
  117. protected static final int WEEKLY_POINTS = Config.ALT_OLY_WEEKLY_POINTS;
  118. public static final String CHAR_ID = "charId";
  119. public static final String CLASS_ID = "class_id";
  120. public static final String CHAR_NAME = "char_name";
  121. public static final String POINTS = "olympiad_points";
  122. public static final String COMP_DONE = "competitions_done";
  123. public static final String COMP_WON = "competitions_won";
  124. public static final String COMP_LOST = "competitions_lost";
  125. public static final String COMP_DRAWN = "competitions_drawn";
  126. public static final String COMP_DONE_WEEK = "competitions_done_week";
  127. public static final String COMP_DONE_WEEK_CLASSED = "competitions_done_week_classed";
  128. public static final String COMP_DONE_WEEK_NON_CLASSED = "competitions_done_week_non_classed";
  129. public static final String COMP_DONE_WEEK_TEAM = "competitions_done_week_team";
  130. protected long _olympiadEnd;
  131. protected long _validationEnd;
  132. /**
  133. * The current period of the olympiad.<br>
  134. * <b>0 -</b> Competition period<br>
  135. * <b>1 -</b> Validation Period
  136. */
  137. protected int _period;
  138. protected long _nextWeeklyChange;
  139. protected int _currentCycle;
  140. private long _compEnd;
  141. private Calendar _compStart;
  142. protected static boolean _inCompPeriod;
  143. protected static boolean _compStarted = false;
  144. protected ScheduledFuture<?> _scheduledCompStart;
  145. protected ScheduledFuture<?> _scheduledCompEnd;
  146. protected ScheduledFuture<?> _scheduledOlympiadEnd;
  147. protected ScheduledFuture<?> _scheduledWeeklyTask;
  148. protected ScheduledFuture<?> _scheduledValdationTask;
  149. protected ScheduledFuture<?> _gameManager = null;
  150. protected ScheduledFuture<?> _gameAnnouncer = null;
  151. protected Olympiad()
  152. {
  153. load();
  154. AntiFeedManager.getInstance().registerEvent(AntiFeedManager.OLYMPIAD_ID);
  155. if (_period == 0)
  156. {
  157. init();
  158. }
  159. }
  160. private void load()
  161. {
  162. _nobles.clear();
  163. boolean loaded = false;
  164. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  165. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_DATA);
  166. ResultSet rset = statement.executeQuery())
  167. {
  168. while (rset.next())
  169. {
  170. _currentCycle = rset.getInt("current_cycle");
  171. _period = rset.getInt("period");
  172. _olympiadEnd = rset.getLong("olympiad_end");
  173. _validationEnd = rset.getLong("validation_end");
  174. _nextWeeklyChange = rset.getLong("next_weekly_change");
  175. loaded = true;
  176. }
  177. }
  178. catch (Exception e)
  179. {
  180. _log.log(Level.WARNING, "Olympiad System: Error loading olympiad data from database: ", e);
  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. try (InputStream is = new FileInputStream(Config.OLYMPIAD_CONFIG_FILE))
  187. {
  188. OlympiadProperties.load(is);
  189. }
  190. catch (Exception e)
  191. {
  192. _log.log(Level.SEVERE, "Olympiad System: Error loading olympiad properties: ", e);
  193. return;
  194. }
  195. _currentCycle = Integer.parseInt(OlympiadProperties.getProperty("CurrentCycle", "1"));
  196. _period = Integer.parseInt(OlympiadProperties.getProperty("Period", "0"));
  197. _olympiadEnd = Long.parseLong(OlympiadProperties.getProperty("OlympiadEnd", "0"));
  198. _validationEnd = Long.parseLong(OlympiadProperties.getProperty("ValidationEnd", "0"));
  199. _nextWeeklyChange = Long.parseLong(OlympiadProperties.getProperty("NextWeeklyChange", "0"));
  200. }
  201. switch (_period)
  202. {
  203. case 0:
  204. if ((_olympiadEnd == 0) || (_olympiadEnd < Calendar.getInstance().getTimeInMillis()))
  205. {
  206. setNewOlympiadEnd();
  207. }
  208. else
  209. {
  210. scheduleWeeklyChange();
  211. }
  212. break;
  213. case 1:
  214. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  215. {
  216. loadNoblesRank();
  217. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  218. }
  219. else
  220. {
  221. _currentCycle++;
  222. _period = 0;
  223. deleteNobles();
  224. setNewOlympiadEnd();
  225. }
  226. break;
  227. default:
  228. _log.warning("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  229. return;
  230. }
  231. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  232. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
  233. ResultSet rset = statement.executeQuery())
  234. {
  235. StatsSet statData;
  236. while (rset.next())
  237. {
  238. statData = new StatsSet();
  239. statData.set(CLASS_ID, rset.getInt(CLASS_ID));
  240. statData.set(CHAR_NAME, rset.getString(CHAR_NAME));
  241. statData.set(POINTS, rset.getInt(POINTS));
  242. statData.set(COMP_DONE, rset.getInt(COMP_DONE));
  243. statData.set(COMP_WON, rset.getInt(COMP_WON));
  244. statData.set(COMP_LOST, rset.getInt(COMP_LOST));
  245. statData.set(COMP_DRAWN, rset.getInt(COMP_DRAWN));
  246. statData.set(COMP_DONE_WEEK, rset.getInt(COMP_DONE_WEEK));
  247. statData.set(COMP_DONE_WEEK_CLASSED, rset.getInt(COMP_DONE_WEEK_CLASSED));
  248. statData.set(COMP_DONE_WEEK_NON_CLASSED, rset.getInt(COMP_DONE_WEEK_NON_CLASSED));
  249. statData.set(COMP_DONE_WEEK_TEAM, rset.getInt(COMP_DONE_WEEK_TEAM));
  250. statData.set("to_save", false);
  251. addNobleStats(rset.getInt(CHAR_ID), statData);
  252. }
  253. }
  254. catch (Exception e)
  255. {
  256. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database: ", e);
  257. }
  258. synchronized (this)
  259. {
  260. _log.info("Olympiad System: Loading Olympiad System....");
  261. if (_period == 0)
  262. {
  263. _log.info("Olympiad System: Currently in Olympiad Period");
  264. }
  265. else
  266. {
  267. _log.info("Olympiad System: Currently in Validation Period");
  268. }
  269. long milliToEnd;
  270. if (_period == 0)
  271. {
  272. milliToEnd = getMillisToOlympiadEnd();
  273. }
  274. else
  275. {
  276. milliToEnd = getMillisToValidationEnd();
  277. }
  278. _log.info("Olympiad System: " + (milliToEnd / 60000) + " minutes until period ends");
  279. if (_period == 0)
  280. {
  281. milliToEnd = getMillisToWeekChange();
  282. _log.info("Olympiad System: Next weekly change is in " + (milliToEnd / 60000) + " minutes");
  283. }
  284. }
  285. _log.info("Olympiad System: Loaded " + _nobles.size() + " Nobles");
  286. }
  287. public void loadNoblesRank()
  288. {
  289. _noblesRank.clear();
  290. Map<Integer, Integer> tmpPlace = new HashMap<>();
  291. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  292. PreparedStatement statement = con.prepareStatement(GET_ALL_CLASSIFIED_NOBLESS);
  293. ResultSet rset = statement.executeQuery())
  294. {
  295. int place = 1;
  296. while (rset.next())
  297. {
  298. tmpPlace.put(rset.getInt(CHAR_ID), place++);
  299. }
  300. }
  301. catch (Exception e)
  302. {
  303. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database for Ranking: ", e);
  304. }
  305. int rank1 = (int) Math.round(tmpPlace.size() * 0.01);
  306. int rank2 = (int) Math.round(tmpPlace.size() * 0.10);
  307. int rank3 = (int) Math.round(tmpPlace.size() * 0.25);
  308. int rank4 = (int) Math.round(tmpPlace.size() * 0.50);
  309. if (rank1 == 0)
  310. {
  311. rank1 = 1;
  312. rank2++;
  313. rank3++;
  314. rank4++;
  315. }
  316. for (Entry<Integer, Integer> chr : tmpPlace.entrySet())
  317. {
  318. if (chr.getValue() <= rank1)
  319. {
  320. _noblesRank.put(chr.getKey(), 1);
  321. }
  322. else if (tmpPlace.get(chr.getKey()) <= rank2)
  323. {
  324. _noblesRank.put(chr.getKey(), 2);
  325. }
  326. else if (tmpPlace.get(chr.getKey()) <= rank3)
  327. {
  328. _noblesRank.put(chr.getKey(), 3);
  329. }
  330. else if (tmpPlace.get(chr.getKey()) <= rank4)
  331. {
  332. _noblesRank.put(chr.getKey(), 4);
  333. }
  334. else
  335. {
  336. _noblesRank.put(chr.getKey(), 5);
  337. }
  338. }
  339. }
  340. protected void init()
  341. {
  342. if (_period == 1)
  343. {
  344. return;
  345. }
  346. _compStart = Calendar.getInstance();
  347. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  348. _compStart.set(Calendar.MINUTE, COMP_MIN);
  349. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  350. if (_scheduledOlympiadEnd != null)
  351. {
  352. _scheduledOlympiadEnd.cancel(true);
  353. }
  354. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), getMillisToOlympiadEnd());
  355. updateCompStatus();
  356. }
  357. protected class OlympiadEndTask implements Runnable
  358. {
  359. @Override
  360. public void run()
  361. {
  362. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  363. sm.addInt(_currentCycle);
  364. Announcements.getInstance().announceToAll(sm);
  365. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  366. if (_scheduledWeeklyTask != null)
  367. {
  368. _scheduledWeeklyTask.cancel(true);
  369. }
  370. saveNobleData();
  371. _period = 1;
  372. sortHerosToBe();
  373. Hero.getInstance().resetData();
  374. Hero.getInstance().computeNewHeroes(_heroesToBe);
  375. saveOlympiadStatus();
  376. updateMonthlyData();
  377. Calendar validationEnd = Calendar.getInstance();
  378. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  379. loadNoblesRank();
  380. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  381. }
  382. }
  383. protected class ValidationEndTask implements Runnable
  384. {
  385. @Override
  386. public void run()
  387. {
  388. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  389. _period = 0;
  390. _currentCycle++;
  391. deleteNobles();
  392. setNewOlympiadEnd();
  393. init();
  394. }
  395. }
  396. protected static int getNobleCount()
  397. {
  398. return _nobles.size();
  399. }
  400. protected static StatsSet getNobleStats(int playerId)
  401. {
  402. return _nobles.get(playerId);
  403. }
  404. private void updateCompStatus()
  405. {
  406. // _compStarted = false;
  407. synchronized (this)
  408. {
  409. long milliToStart = getMillisToCompBegin();
  410. double numSecs = (milliToStart / 1000) % 60;
  411. double countDown = ((milliToStart / 1000.) - numSecs) / 60;
  412. int numMins = (int) Math.floor(countDown % 60);
  413. countDown = (countDown - numMins) / 60;
  414. int numHours = (int) Math.floor(countDown % 24);
  415. int numDays = (int) Math.floor((countDown - numHours) / 24);
  416. _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  417. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  418. }
  419. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  420. {
  421. @Override
  422. public void run()
  423. {
  424. if (isOlympiadEnd())
  425. {
  426. return;
  427. }
  428. _inCompPeriod = true;
  429. Announcements.getInstance().announceToAll(SystemMessage.getSystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  430. _log.info("Olympiad System: Olympiad Game Started");
  431. _logResults.info("Result,Player1,Player2,Player1 HP,Player2 HP,Player1 Damage,Player2 Damage,Points,Classed");
  432. _gameManager = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(OlympiadGameManager.getInstance(), 30000, 30000);
  433. if (Config.ALT_OLY_ANNOUNCE_GAMES)
  434. {
  435. _gameAnnouncer = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new OlympiadAnnouncer(), 30000, 500);
  436. }
  437. long regEnd = getMillisToCompEnd() - 600000;
  438. if (regEnd > 0)
  439. {
  440. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  441. {
  442. @Override
  443. public void run()
  444. {
  445. Announcements.getInstance().announceToAll(SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_REGISTRATION_PERIOD_ENDED));
  446. }
  447. }, regEnd);
  448. }
  449. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  450. {
  451. @Override
  452. public void run()
  453. {
  454. if (isOlympiadEnd())
  455. {
  456. return;
  457. }
  458. _inCompPeriod = false;
  459. Announcements.getInstance().announceToAll(SystemMessage.getSystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  460. _log.info("Olympiad System: Olympiad Game Ended");
  461. while (OlympiadGameManager.getInstance().isBattleStarted()) // cleared in game manager
  462. {
  463. try
  464. {
  465. // wait 1 minutes for end of pendings games
  466. Thread.sleep(60000);
  467. }
  468. catch (InterruptedException e)
  469. {
  470. }
  471. }
  472. if (_gameManager != null)
  473. {
  474. _gameManager.cancel(false);
  475. _gameManager = null;
  476. }
  477. if (_gameAnnouncer != null)
  478. {
  479. _gameAnnouncer.cancel(false);
  480. _gameAnnouncer = null;
  481. }
  482. saveOlympiadStatus();
  483. init();
  484. }
  485. }, getMillisToCompEnd());
  486. }
  487. }, getMillisToCompBegin());
  488. }
  489. private long getMillisToOlympiadEnd()
  490. {
  491. // if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  492. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  493. // return 10L;
  494. }
  495. public void manualSelectHeroes()
  496. {
  497. if (_scheduledOlympiadEnd != null)
  498. {
  499. _scheduledOlympiadEnd.cancel(true);
  500. }
  501. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), 0);
  502. }
  503. protected long getMillisToValidationEnd()
  504. {
  505. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  506. {
  507. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  508. }
  509. return 10L;
  510. }
  511. public boolean isOlympiadEnd()
  512. {
  513. return (_period != 0);
  514. }
  515. protected void setNewOlympiadEnd()
  516. {
  517. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  518. sm.addInt(_currentCycle);
  519. Announcements.getInstance().announceToAll(sm);
  520. Calendar currentTime = Calendar.getInstance();
  521. currentTime.add(Calendar.MONTH, 1);
  522. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  523. currentTime.set(Calendar.AM_PM, Calendar.AM);
  524. currentTime.set(Calendar.HOUR, 12);
  525. currentTime.set(Calendar.MINUTE, 0);
  526. currentTime.set(Calendar.SECOND, 0);
  527. _olympiadEnd = currentTime.getTimeInMillis();
  528. Calendar nextChange = Calendar.getInstance();
  529. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  530. scheduleWeeklyChange();
  531. }
  532. public boolean inCompPeriod()
  533. {
  534. return _inCompPeriod;
  535. }
  536. private long getMillisToCompBegin()
  537. {
  538. if ((_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis()) && (_compEnd > Calendar.getInstance().getTimeInMillis()))
  539. {
  540. return 10L;
  541. }
  542. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  543. {
  544. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  545. }
  546. return setNewCompBegin();
  547. }
  548. private long setNewCompBegin()
  549. {
  550. _compStart = Calendar.getInstance();
  551. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  552. _compStart.set(Calendar.MINUTE, COMP_MIN);
  553. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  554. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  555. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  556. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  557. }
  558. protected long getMillisToCompEnd()
  559. {
  560. // if (_compEnd > Calendar.getInstance().getTimeInMillis())
  561. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  562. // return 10L;
  563. }
  564. private long getMillisToWeekChange()
  565. {
  566. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  567. {
  568. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  569. }
  570. return 10L;
  571. }
  572. private void scheduleWeeklyChange()
  573. {
  574. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable()
  575. {
  576. @Override
  577. public void run()
  578. {
  579. addWeeklyPoints();
  580. _log.info("Olympiad System: Added weekly points to nobles");
  581. resetWeeklyMatches();
  582. _log.info("Olympiad System: Reset weekly matches to nobles");
  583. Calendar nextChange = Calendar.getInstance();
  584. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  585. }
  586. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  587. }
  588. protected synchronized void addWeeklyPoints()
  589. {
  590. if (_period == 1)
  591. {
  592. return;
  593. }
  594. int currentPoints;
  595. for (StatsSet nobleInfo : _nobles.values())
  596. {
  597. currentPoints = nobleInfo.getInt(POINTS);
  598. currentPoints += WEEKLY_POINTS;
  599. nobleInfo.set(POINTS, currentPoints);
  600. }
  601. }
  602. /**
  603. * Resets number of matches, classed matches, non classed matches, team matches done by noble characters in the week.
  604. */
  605. protected synchronized void resetWeeklyMatches()
  606. {
  607. if (_period == 1)
  608. {
  609. return;
  610. }
  611. for (StatsSet nobleInfo : _nobles.values())
  612. {
  613. nobleInfo.set(COMP_DONE_WEEK, 0);
  614. nobleInfo.set(COMP_DONE_WEEK_CLASSED, 0);
  615. nobleInfo.set(COMP_DONE_WEEK_NON_CLASSED, 0);
  616. nobleInfo.set(COMP_DONE_WEEK_TEAM, 0);
  617. }
  618. }
  619. public int getCurrentCycle()
  620. {
  621. return _currentCycle;
  622. }
  623. public boolean playerInStadia(L2PcInstance player)
  624. {
  625. return (ZoneManager.getInstance().getOlympiadStadium(player) != null);
  626. }
  627. /**
  628. * Save noblesse data to database
  629. */
  630. protected synchronized void saveNobleData()
  631. {
  632. if ((_nobles == null) || _nobles.isEmpty())
  633. {
  634. return;
  635. }
  636. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  637. {
  638. for (Entry<Integer, StatsSet> entry : _nobles.entrySet())
  639. {
  640. StatsSet nobleInfo = entry.getValue();
  641. if (nobleInfo == null)
  642. {
  643. continue;
  644. }
  645. int charId = entry.getKey();
  646. int classId = nobleInfo.getInt(CLASS_ID);
  647. int points = nobleInfo.getInt(POINTS);
  648. int compDone = nobleInfo.getInt(COMP_DONE);
  649. int compWon = nobleInfo.getInt(COMP_WON);
  650. int compLost = nobleInfo.getInt(COMP_LOST);
  651. int compDrawn = nobleInfo.getInt(COMP_DRAWN);
  652. int compDoneWeek = nobleInfo.getInt(COMP_DONE_WEEK);
  653. int compDoneWeekClassed = nobleInfo.getInt(COMP_DONE_WEEK_CLASSED);
  654. int compDoneWeekNonClassed = nobleInfo.getInt(COMP_DONE_WEEK_NON_CLASSED);
  655. int compDoneWeekTeam = nobleInfo.getInt(COMP_DONE_WEEK_TEAM);
  656. boolean toSave = nobleInfo.getBoolean("to_save");
  657. try (PreparedStatement statement = con.prepareStatement(toSave ? OLYMPIAD_SAVE_NOBLES : OLYMPIAD_UPDATE_NOBLES))
  658. {
  659. if (toSave)
  660. {
  661. statement.setInt(1, charId);
  662. statement.setInt(2, classId);
  663. statement.setInt(3, points);
  664. statement.setInt(4, compDone);
  665. statement.setInt(5, compWon);
  666. statement.setInt(6, compLost);
  667. statement.setInt(7, compDrawn);
  668. statement.setInt(8, compDoneWeek);
  669. statement.setInt(9, compDoneWeekClassed);
  670. statement.setInt(10, compDoneWeekNonClassed);
  671. statement.setInt(11, compDoneWeekTeam);
  672. nobleInfo.set("to_save", false);
  673. }
  674. else
  675. {
  676. statement.setInt(1, points);
  677. statement.setInt(2, compDone);
  678. statement.setInt(3, compWon);
  679. statement.setInt(4, compLost);
  680. statement.setInt(5, compDrawn);
  681. statement.setInt(6, compDoneWeek);
  682. statement.setInt(7, compDoneWeekClassed);
  683. statement.setInt(8, compDoneWeekNonClassed);
  684. statement.setInt(9, compDoneWeekTeam);
  685. statement.setInt(10, charId);
  686. }
  687. statement.execute();
  688. }
  689. }
  690. }
  691. catch (SQLException e)
  692. {
  693. _log.log(Level.SEVERE, "Olympiad System: Failed to save noblesse data to database: ", e);
  694. }
  695. }
  696. /**
  697. * Save olympiad.properties file with current olympiad status and update noblesse table in database
  698. */
  699. public void saveOlympiadStatus()
  700. {
  701. saveNobleData();
  702. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  703. PreparedStatement statement = con.prepareStatement(OLYMPIAD_SAVE_DATA))
  704. {
  705. statement.setInt(1, _currentCycle);
  706. statement.setInt(2, _period);
  707. statement.setLong(3, _olympiadEnd);
  708. statement.setLong(4, _validationEnd);
  709. statement.setLong(5, _nextWeeklyChange);
  710. statement.setInt(6, _currentCycle);
  711. statement.setInt(7, _period);
  712. statement.setLong(8, _olympiadEnd);
  713. statement.setLong(9, _validationEnd);
  714. statement.setLong(10, _nextWeeklyChange);
  715. statement.execute();
  716. }
  717. catch (SQLException e)
  718. {
  719. _log.log(Level.SEVERE, "Olympiad System: Failed to save olympiad data to database: ", e);
  720. }
  721. //@formatter:off
  722. /*
  723. Properties OlympiadProperties = new Properties();
  724. try (FileOutputStream fos = new FileOutputStream(new File("./" + OLYMPIAD_DATA_FILE)))
  725. {
  726. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  727. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  728. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  729. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  730. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  731. OlympiadProperties.store(fos, "Olympiad Properties");
  732. }
  733. catch (Exception e)
  734. {
  735. _log.log(Level.WARNING, "Olympiad System: Unable to save olympiad properties to file: ", e);
  736. }
  737. */
  738. //@formatter:on
  739. }
  740. protected void updateMonthlyData()
  741. {
  742. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  743. PreparedStatement ps1 = con.prepareStatement(OLYMPIAD_MONTH_CLEAR);
  744. PreparedStatement ps2 = con.prepareStatement(OLYMPIAD_MONTH_CREATE))
  745. {
  746. ps1.execute();
  747. ps2.execute();
  748. }
  749. catch (SQLException e)
  750. {
  751. _log.log(Level.SEVERE, "Olympiad System: Failed to update monthly noblese data: ", e);
  752. }
  753. }
  754. protected void sortHerosToBe()
  755. {
  756. if (_period != 1)
  757. {
  758. return;
  759. }
  760. LogRecord record;
  761. if (_nobles != null)
  762. {
  763. _logResults.info("Noble,charid,classid,compDone,points");
  764. StatsSet nobleInfo;
  765. for (Entry<Integer, StatsSet> entry : _nobles.entrySet())
  766. {
  767. nobleInfo = entry.getValue();
  768. if (nobleInfo == null)
  769. {
  770. continue;
  771. }
  772. int charId = entry.getKey();
  773. int classId = nobleInfo.getInt(CLASS_ID);
  774. String charName = nobleInfo.getString(CHAR_NAME);
  775. int points = nobleInfo.getInt(POINTS);
  776. int compDone = nobleInfo.getInt(COMP_DONE);
  777. record = new LogRecord(Level.INFO, charName);
  778. record.setParameters(new Object[]
  779. {
  780. charId,
  781. classId,
  782. compDone,
  783. points
  784. });
  785. _logResults.log(record);
  786. }
  787. }
  788. _heroesToBe = new L2FastList<>();
  789. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  790. PreparedStatement statement = con.prepareStatement(OLYMPIAD_GET_HEROS))
  791. {
  792. ResultSet rset;
  793. StatsSet hero;
  794. L2FastList<StatsSet> soulHounds = new L2FastList<>();
  795. for (int element : HERO_IDS)
  796. {
  797. statement.setInt(1, element);
  798. rset = statement.executeQuery();
  799. statement.clearParameters();
  800. if (rset.next())
  801. {
  802. hero = new StatsSet();
  803. hero.set(CLASS_ID, element);
  804. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  805. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  806. if ((element == 132) || (element == 133)) // Male & Female Soulhounds rank as one hero class
  807. {
  808. hero = _nobles.get(hero.getInt(CHAR_ID));
  809. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  810. soulHounds.add(hero);
  811. }
  812. else
  813. {
  814. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  815. record.setParameters(new Object[]
  816. {
  817. hero.getInt(CHAR_ID),
  818. hero.getInt(CLASS_ID)
  819. });
  820. _logResults.log(record);
  821. _heroesToBe.add(hero);
  822. }
  823. }
  824. rset.close();
  825. }
  826. switch (soulHounds.size())
  827. {
  828. case 0:
  829. {
  830. break;
  831. }
  832. case 1:
  833. {
  834. hero = new StatsSet();
  835. StatsSet winner = soulHounds.get(0);
  836. hero.set(CLASS_ID, winner.getInt(CLASS_ID));
  837. hero.set(CHAR_ID, winner.getInt(CHAR_ID));
  838. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  839. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  840. record.setParameters(new Object[]
  841. {
  842. hero.getInt(CHAR_ID),
  843. hero.getInt(CLASS_ID)
  844. });
  845. _logResults.log(record);
  846. _heroesToBe.add(hero);
  847. break;
  848. }
  849. case 2:
  850. {
  851. hero = new StatsSet();
  852. StatsSet winner;
  853. StatsSet hero1 = soulHounds.get(0);
  854. StatsSet hero2 = soulHounds.get(1);
  855. int hero1Points = hero1.getInt(POINTS);
  856. int hero2Points = hero2.getInt(POINTS);
  857. int hero1Comps = hero1.getInt(COMP_DONE);
  858. int hero2Comps = hero2.getInt(COMP_DONE);
  859. int hero1Wins = hero1.getInt(COMP_WON);
  860. int hero2Wins = hero2.getInt(COMP_WON);
  861. if (hero1Points > hero2Points)
  862. {
  863. winner = hero1;
  864. }
  865. else if (hero2Points > hero1Points)
  866. {
  867. winner = hero2;
  868. }
  869. else
  870. {
  871. if (hero1Comps > hero2Comps)
  872. {
  873. winner = hero1;
  874. }
  875. else if (hero2Comps > hero1Comps)
  876. {
  877. winner = hero2;
  878. }
  879. else
  880. {
  881. if (hero1Wins > hero2Wins)
  882. {
  883. winner = hero1;
  884. }
  885. else
  886. {
  887. winner = hero2;
  888. }
  889. }
  890. }
  891. hero.set(CLASS_ID, winner.getInt(CLASS_ID));
  892. hero.set(CHAR_ID, winner.getInt(CHAR_ID));
  893. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  894. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  895. record.setParameters(new Object[]
  896. {
  897. hero.getInt(CHAR_ID),
  898. hero.getInt(CLASS_ID)
  899. });
  900. _logResults.log(record);
  901. _heroesToBe.add(hero);
  902. break;
  903. }
  904. }
  905. }
  906. catch (SQLException e)
  907. {
  908. _log.warning("Olympiad System: Couldnt load heros from DB");
  909. }
  910. }
  911. public L2FastList<String> getClassLeaderBoard(int classId)
  912. {
  913. // if (_period != 1) return;
  914. final L2FastList<String> names = new L2FastList<>();
  915. String query = Config.ALT_OLY_SHOW_MONTHLY_WINNERS ? ((classId == 132) ? GET_EACH_CLASS_LEADER_SOULHOUND : GET_EACH_CLASS_LEADER) : ((classId == 132) ? GET_EACH_CLASS_LEADER_CURRENT_SOULHOUND : GET_EACH_CLASS_LEADER_CURRENT);
  916. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  917. PreparedStatement ps = con.prepareStatement(query))
  918. {
  919. ps.setInt(1, classId);
  920. try (ResultSet rset = ps.executeQuery())
  921. {
  922. while (rset.next())
  923. {
  924. names.add(rset.getString(CHAR_NAME));
  925. }
  926. }
  927. }
  928. catch (SQLException e)
  929. {
  930. _log.warning("Olympiad System: Couldn't load olympiad leaders from DB!");
  931. }
  932. return names;
  933. }
  934. public int getNoblessePasses(L2PcInstance player, boolean clear)
  935. {
  936. if ((player == null) || (_period != 1) || _noblesRank.isEmpty())
  937. {
  938. return 0;
  939. }
  940. final int objId = player.getObjectId();
  941. if (!_noblesRank.containsKey(objId))
  942. {
  943. return 0;
  944. }
  945. final StatsSet noble = _nobles.get(objId);
  946. if ((noble == null) || (noble.getInt(POINTS) == 0))
  947. {
  948. return 0;
  949. }
  950. final int rank = _noblesRank.get(objId);
  951. int points = (player.isHero() ? Config.ALT_OLY_HERO_POINTS : 0);
  952. switch (rank)
  953. {
  954. case 1:
  955. points += Config.ALT_OLY_RANK1_POINTS;
  956. break;
  957. case 2:
  958. points += Config.ALT_OLY_RANK2_POINTS;
  959. break;
  960. case 3:
  961. points += Config.ALT_OLY_RANK3_POINTS;
  962. break;
  963. case 4:
  964. points += Config.ALT_OLY_RANK4_POINTS;
  965. break;
  966. default:
  967. points += Config.ALT_OLY_RANK5_POINTS;
  968. }
  969. if (clear)
  970. {
  971. noble.set(POINTS, 0);
  972. }
  973. points *= Config.ALT_OLY_GP_PER_POINT;
  974. return points;
  975. }
  976. public int getNoblePoints(int objId)
  977. {
  978. if ((_nobles == null) || !_nobles.containsKey(objId))
  979. {
  980. return 0;
  981. }
  982. return _nobles.get(objId).getInt(POINTS);
  983. }
  984. public int getLastNobleOlympiadPoints(int objId)
  985. {
  986. int result = 0;
  987. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  988. PreparedStatement ps = con.prepareStatement("SELECT olympiad_points FROM olympiad_nobles_eom WHERE charId = ?"))
  989. {
  990. ps.setInt(1, objId);
  991. try (ResultSet rs = ps.executeQuery())
  992. {
  993. if (rs.first())
  994. {
  995. result = rs.getInt(1);
  996. }
  997. }
  998. }
  999. catch (Exception e)
  1000. {
  1001. _log.log(Level.WARNING, "Could not load last olympiad points:", e);
  1002. }
  1003. return result;
  1004. }
  1005. public int getCompetitionDone(int objId)
  1006. {
  1007. if ((_nobles == null) || !_nobles.containsKey(objId))
  1008. {
  1009. return 0;
  1010. }
  1011. return _nobles.get(objId).getInt(COMP_DONE);
  1012. }
  1013. public int getCompetitionWon(int objId)
  1014. {
  1015. if ((_nobles == null) || !_nobles.containsKey(objId))
  1016. {
  1017. return 0;
  1018. }
  1019. return _nobles.get(objId).getInt(COMP_WON);
  1020. }
  1021. public int getCompetitionLost(int objId)
  1022. {
  1023. if ((_nobles == null) || !_nobles.containsKey(objId))
  1024. {
  1025. return 0;
  1026. }
  1027. return _nobles.get(objId).getInt(COMP_LOST);
  1028. }
  1029. /**
  1030. * Gets how many matches a noble character did in the week
  1031. * @param objId id of a noble character
  1032. * @return number of weekly competitions done
  1033. */
  1034. public int getCompetitionDoneWeek(int objId)
  1035. {
  1036. if ((_nobles == null) || !_nobles.containsKey(objId))
  1037. {
  1038. return 0;
  1039. }
  1040. return _nobles.get(objId).getInt(COMP_DONE_WEEK);
  1041. }
  1042. /**
  1043. * Gets how many classed matches a noble character did in the week
  1044. * @param objId id of a noble character
  1045. * @return number of weekly <i>classed</i> competitions done
  1046. */
  1047. public int getCompetitionDoneWeekClassed(int objId)
  1048. {
  1049. if ((_nobles == null) || !_nobles.containsKey(objId))
  1050. {
  1051. return 0;
  1052. }
  1053. return _nobles.get(objId).getInt(COMP_DONE_WEEK_CLASSED);
  1054. }
  1055. /**
  1056. * Gets how many non classed matches a noble character did in the week
  1057. * @param objId id of a noble character
  1058. * @return number of weekly <i>non classed</i> competitions done
  1059. */
  1060. public int getCompetitionDoneWeekNonClassed(int objId)
  1061. {
  1062. if ((_nobles == null) || !_nobles.containsKey(objId))
  1063. {
  1064. return 0;
  1065. }
  1066. return _nobles.get(objId).getInt(COMP_DONE_WEEK_NON_CLASSED);
  1067. }
  1068. /**
  1069. * Gets how many team matches a noble character did in the week
  1070. * @param objId id of a noble character
  1071. * @return number of weekly <i>team</i> competitions done
  1072. */
  1073. public int getCompetitionDoneWeekTeam(int objId)
  1074. {
  1075. if ((_nobles == null) || !_nobles.containsKey(objId))
  1076. {
  1077. return 0;
  1078. }
  1079. return _nobles.get(objId).getInt(COMP_DONE_WEEK_TEAM);
  1080. }
  1081. /**
  1082. * Number of remaining matches a noble character can join in the week
  1083. * @param objId id of a noble character
  1084. * @return difference between maximum allowed weekly matches and currently done weekly matches.
  1085. */
  1086. public int getRemainingWeeklyMatches(int objId)
  1087. {
  1088. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES - getCompetitionDoneWeek(objId), 0);
  1089. }
  1090. /**
  1091. * Number of remaining <i>classed</i> matches a noble character can join in the week
  1092. * @param objId id of a noble character
  1093. * @return difference between maximum allowed weekly classed matches and currently done weekly classed matches.
  1094. */
  1095. public int getRemainingWeeklyMatchesClassed(int objId)
  1096. {
  1097. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_CLASSED - getCompetitionDoneWeekClassed(objId), 0);
  1098. }
  1099. /**
  1100. * Number of remaining <i>non classed</i> matches a noble character can join in the week
  1101. * @param objId id of a noble character
  1102. * @return difference between maximum allowed weekly non classed matches and currently done weekly non classed matches.
  1103. */
  1104. public int getRemainingWeeklyMatchesNonClassed(int objId)
  1105. {
  1106. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_NON_CLASSED - getCompetitionDoneWeekNonClassed(objId), 0);
  1107. }
  1108. /**
  1109. * Number of remaining <i>team</i> matches a noble character can join in the week
  1110. * @param objId id of a noble character
  1111. * @return difference between maximum allowed weekly team matches and currently done weekly team matches.
  1112. */
  1113. public int getRemainingWeeklyMatchesTeam(int objId)
  1114. {
  1115. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_TEAM - getCompetitionDoneWeekTeam(objId), 0);
  1116. }
  1117. protected void deleteNobles()
  1118. {
  1119. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1120. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL))
  1121. {
  1122. statement.execute();
  1123. }
  1124. catch (SQLException e)
  1125. {
  1126. _log.warning("Olympiad System: Couldn't delete nobles from DB!");
  1127. }
  1128. _nobles.clear();
  1129. }
  1130. /**
  1131. * @param charId the noble object Id.
  1132. * @param data the stats set data to add.
  1133. * @return the old stats set if the noble is already present, null otherwise.
  1134. */
  1135. protected static StatsSet addNobleStats(int charId, StatsSet data)
  1136. {
  1137. return _nobles.put(Integer.valueOf(charId), data);
  1138. }
  1139. public static Olympiad getInstance()
  1140. {
  1141. return SingletonHolder._instance;
  1142. }
  1143. private static class SingletonHolder
  1144. {
  1145. protected static final Olympiad _instance = new Olympiad();
  1146. }
  1147. }