Olympiad.java 40 KB

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