Olympiad.java 40 KB

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