Olympiad.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright (C) 2004-2015 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.ConcurrentHashMap;
  34. import java.util.concurrent.ScheduledFuture;
  35. import java.util.logging.Level;
  36. import java.util.logging.LogRecord;
  37. import java.util.logging.Logger;
  38. import com.l2jserver.Config;
  39. import com.l2jserver.L2DatabaseFactory;
  40. import com.l2jserver.gameserver.ThreadPoolManager;
  41. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  42. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  43. import com.l2jserver.gameserver.model.StatsSet;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  45. import com.l2jserver.gameserver.model.entity.Hero;
  46. import com.l2jserver.gameserver.model.events.ListenersContainer;
  47. import com.l2jserver.gameserver.network.SystemMessageId;
  48. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  49. import com.l2jserver.gameserver.util.Broadcast;
  50. /**
  51. * @author godson
  52. */
  53. public class Olympiad extends ListenersContainer
  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 ConcurrentHashMap<>();
  58. private static final List<StatsSet> HEROS_TO_BE = new ArrayList<>();
  59. private static final Map<Integer, Integer> NOBLES_RANK = 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. NOBLES_RANK.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. NOBLES_RANK.put(chr.getKey(), 1);
  323. }
  324. else if (tmpPlace.get(chr.getKey()) <= rank2)
  325. {
  326. NOBLES_RANK.put(chr.getKey(), 2);
  327. }
  328. else if (tmpPlace.get(chr.getKey()) <= rank3)
  329. {
  330. NOBLES_RANK.put(chr.getKey(), 3);
  331. }
  332. else if (tmpPlace.get(chr.getKey()) <= rank4)
  333. {
  334. NOBLES_RANK.put(chr.getKey(), 4);
  335. }
  336. else
  337. {
  338. NOBLES_RANK.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(HEROS_TO_BE), getMillisToOlympiadEnd());
  357. updateCompStatus();
  358. }
  359. protected class OlympiadEndTask implements Runnable
  360. {
  361. private final List<StatsSet> _herosToBe;
  362. public OlympiadEndTask(List<StatsSet> herosToBe)
  363. {
  364. _herosToBe = herosToBe;
  365. }
  366. @Override
  367. public void run()
  368. {
  369. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  370. sm.addInt(_currentCycle);
  371. Broadcast.toAllOnlinePlayers(sm);
  372. Broadcast.toAllOnlinePlayers("Olympiad Validation Period has began");
  373. if (_scheduledWeeklyTask != null)
  374. {
  375. _scheduledWeeklyTask.cancel(true);
  376. }
  377. saveNobleData();
  378. _period = 1;
  379. sortHerosToBe();
  380. Hero.getInstance().resetData();
  381. Hero.getInstance().computeNewHeroes(_herosToBe);
  382. saveOlympiadStatus();
  383. updateMonthlyData();
  384. Calendar validationEnd = Calendar.getInstance();
  385. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  386. loadNoblesRank();
  387. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  388. }
  389. }
  390. protected class ValidationEndTask implements Runnable
  391. {
  392. @Override
  393. public void run()
  394. {
  395. Broadcast.toAllOnlinePlayers("Olympiad Validation Period has ended");
  396. _period = 0;
  397. _currentCycle++;
  398. deleteNobles();
  399. setNewOlympiadEnd();
  400. init();
  401. }
  402. }
  403. protected static int getNobleCount()
  404. {
  405. return NOBLES.size();
  406. }
  407. protected static StatsSet getNobleStats(int playerId)
  408. {
  409. return NOBLES.get(playerId);
  410. }
  411. private void updateCompStatus()
  412. {
  413. // _compStarted = false;
  414. synchronized (this)
  415. {
  416. long milliToStart = getMillisToCompBegin();
  417. double numSecs = (milliToStart / 1000) % 60;
  418. double countDown = ((milliToStart / 1000.) - numSecs) / 60;
  419. int numMins = (int) Math.floor(countDown % 60);
  420. countDown = (countDown - numMins) / 60;
  421. int numHours = (int) Math.floor(countDown % 24);
  422. int numDays = (int) Math.floor((countDown - numHours) / 24);
  423. _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  424. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  425. }
  426. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(() ->
  427. {
  428. if (isOlympiadEnd())
  429. {
  430. return;
  431. }
  432. _inCompPeriod = true;
  433. Broadcast.toAllOnlinePlayers(SystemMessage.getSystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  434. _log.info("Olympiad System: Olympiad Game Started");
  435. _logResults.info("Result,Player1,Player2,Player1 HP,Player2 HP,Player1 Damage,Player2 Damage,Points,Classed");
  436. _gameManager = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(OlympiadGameManager.getInstance(), 30000, 30000);
  437. if (Config.ALT_OLY_ANNOUNCE_GAMES)
  438. {
  439. _gameAnnouncer = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new OlympiadAnnouncer(), 30000, 500);
  440. }
  441. long regEnd = getMillisToCompEnd() - 600000;
  442. if (regEnd > 0)
  443. {
  444. ThreadPoolManager.getInstance().scheduleGeneral(() -> Broadcast.toAllOnlinePlayers(SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_REGISTRATION_PERIOD_ENDED)), regEnd);
  445. }
  446. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(() ->
  447. {
  448. if (isOlympiadEnd())
  449. {
  450. return;
  451. }
  452. _inCompPeriod = false;
  453. Broadcast.toAllOnlinePlayers(SystemMessage.getSystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  454. _log.info("Olympiad System: Olympiad Game Ended");
  455. while (OlympiadGameManager.getInstance().isBattleStarted()) // cleared in game manager
  456. {
  457. try
  458. {
  459. // wait 1 minutes for end of pendings games
  460. Thread.sleep(60000);
  461. }
  462. catch (InterruptedException e)
  463. {
  464. }
  465. }
  466. if (_gameManager != null)
  467. {
  468. _gameManager.cancel(false);
  469. _gameManager = null;
  470. }
  471. if (_gameAnnouncer != null)
  472. {
  473. _gameAnnouncer.cancel(false);
  474. _gameAnnouncer = null;
  475. }
  476. saveOlympiadStatus();
  477. init();
  478. }, getMillisToCompEnd());
  479. }, getMillisToCompBegin());
  480. }
  481. private long getMillisToOlympiadEnd()
  482. {
  483. // if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  484. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  485. // return 10L;
  486. }
  487. public void manualSelectHeroes()
  488. {
  489. if (_scheduledOlympiadEnd != null)
  490. {
  491. _scheduledOlympiadEnd.cancel(true);
  492. }
  493. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(HEROS_TO_BE), 0);
  494. }
  495. protected long getMillisToValidationEnd()
  496. {
  497. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  498. {
  499. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  500. }
  501. return 10L;
  502. }
  503. public boolean isOlympiadEnd()
  504. {
  505. return (_period != 0);
  506. }
  507. protected void setNewOlympiadEnd()
  508. {
  509. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  510. sm.addInt(_currentCycle);
  511. Broadcast.toAllOnlinePlayers(sm);
  512. Calendar currentTime = Calendar.getInstance();
  513. currentTime.add(Calendar.MONTH, 1);
  514. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  515. currentTime.set(Calendar.AM_PM, Calendar.AM);
  516. currentTime.set(Calendar.HOUR, 12);
  517. currentTime.set(Calendar.MINUTE, 0);
  518. currentTime.set(Calendar.SECOND, 0);
  519. _olympiadEnd = currentTime.getTimeInMillis();
  520. Calendar nextChange = Calendar.getInstance();
  521. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  522. scheduleWeeklyChange();
  523. }
  524. public boolean inCompPeriod()
  525. {
  526. return _inCompPeriod;
  527. }
  528. private long getMillisToCompBegin()
  529. {
  530. if ((_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis()) && (_compEnd > Calendar.getInstance().getTimeInMillis()))
  531. {
  532. return 10L;
  533. }
  534. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  535. {
  536. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  537. }
  538. return setNewCompBegin();
  539. }
  540. private long setNewCompBegin()
  541. {
  542. _compStart = Calendar.getInstance();
  543. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  544. _compStart.set(Calendar.MINUTE, COMP_MIN);
  545. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  546. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  547. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  548. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  549. }
  550. protected long getMillisToCompEnd()
  551. {
  552. // if (_compEnd > Calendar.getInstance().getTimeInMillis())
  553. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  554. // return 10L;
  555. }
  556. private long getMillisToWeekChange()
  557. {
  558. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  559. {
  560. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  561. }
  562. return 10L;
  563. }
  564. private void scheduleWeeklyChange()
  565. {
  566. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(() ->
  567. {
  568. addWeeklyPoints();
  569. _log.info("Olympiad System: Added weekly points to nobles");
  570. resetWeeklyMatches();
  571. _log.info("Olympiad System: Reset weekly matches to nobles");
  572. Calendar nextChange = Calendar.getInstance();
  573. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  574. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  575. }
  576. protected synchronized void addWeeklyPoints()
  577. {
  578. if (_period == 1)
  579. {
  580. return;
  581. }
  582. int currentPoints;
  583. for (StatsSet nobleInfo : NOBLES.values())
  584. {
  585. currentPoints = nobleInfo.getInt(POINTS);
  586. currentPoints += WEEKLY_POINTS;
  587. nobleInfo.set(POINTS, currentPoints);
  588. }
  589. }
  590. /**
  591. * Resets number of matches, classed matches, non classed matches, team matches done by noble characters in the week.
  592. */
  593. protected synchronized void resetWeeklyMatches()
  594. {
  595. if (_period == 1)
  596. {
  597. return;
  598. }
  599. for (StatsSet nobleInfo : NOBLES.values())
  600. {
  601. nobleInfo.set(COMP_DONE_WEEK, 0);
  602. nobleInfo.set(COMP_DONE_WEEK_CLASSED, 0);
  603. nobleInfo.set(COMP_DONE_WEEK_NON_CLASSED, 0);
  604. nobleInfo.set(COMP_DONE_WEEK_TEAM, 0);
  605. }
  606. }
  607. public int getCurrentCycle()
  608. {
  609. return _currentCycle;
  610. }
  611. public int getPeriod()
  612. {
  613. return _period;
  614. }
  615. public boolean playerInStadia(L2PcInstance player)
  616. {
  617. return (ZoneManager.getInstance().getOlympiadStadium(player) != null);
  618. }
  619. /**
  620. * Save noblesse data to database
  621. */
  622. protected synchronized void saveNobleData()
  623. {
  624. if (NOBLES.isEmpty())
  625. {
  626. return;
  627. }
  628. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  629. {
  630. for (Entry<Integer, StatsSet> entry : NOBLES.entrySet())
  631. {
  632. StatsSet nobleInfo = entry.getValue();
  633. if (nobleInfo == null)
  634. {
  635. continue;
  636. }
  637. int charId = entry.getKey();
  638. int classId = nobleInfo.getInt(CLASS_ID);
  639. int points = nobleInfo.getInt(POINTS);
  640. int compDone = nobleInfo.getInt(COMP_DONE);
  641. int compWon = nobleInfo.getInt(COMP_WON);
  642. int compLost = nobleInfo.getInt(COMP_LOST);
  643. int compDrawn = nobleInfo.getInt(COMP_DRAWN);
  644. int compDoneWeek = nobleInfo.getInt(COMP_DONE_WEEK);
  645. int compDoneWeekClassed = nobleInfo.getInt(COMP_DONE_WEEK_CLASSED);
  646. int compDoneWeekNonClassed = nobleInfo.getInt(COMP_DONE_WEEK_NON_CLASSED);
  647. int compDoneWeekTeam = nobleInfo.getInt(COMP_DONE_WEEK_TEAM);
  648. boolean toSave = nobleInfo.getBoolean("to_save");
  649. try (PreparedStatement statement = con.prepareStatement(toSave ? OLYMPIAD_SAVE_NOBLES : OLYMPIAD_UPDATE_NOBLES))
  650. {
  651. if (toSave)
  652. {
  653. statement.setInt(1, charId);
  654. statement.setInt(2, classId);
  655. statement.setInt(3, points);
  656. statement.setInt(4, compDone);
  657. statement.setInt(5, compWon);
  658. statement.setInt(6, compLost);
  659. statement.setInt(7, compDrawn);
  660. statement.setInt(8, compDoneWeek);
  661. statement.setInt(9, compDoneWeekClassed);
  662. statement.setInt(10, compDoneWeekNonClassed);
  663. statement.setInt(11, compDoneWeekTeam);
  664. nobleInfo.set("to_save", false);
  665. }
  666. else
  667. {
  668. statement.setInt(1, points);
  669. statement.setInt(2, compDone);
  670. statement.setInt(3, compWon);
  671. statement.setInt(4, compLost);
  672. statement.setInt(5, compDrawn);
  673. statement.setInt(6, compDoneWeek);
  674. statement.setInt(7, compDoneWeekClassed);
  675. statement.setInt(8, compDoneWeekNonClassed);
  676. statement.setInt(9, compDoneWeekTeam);
  677. statement.setInt(10, charId);
  678. }
  679. statement.execute();
  680. }
  681. }
  682. }
  683. catch (SQLException e)
  684. {
  685. _log.log(Level.SEVERE, "Olympiad System: Failed to save noblesse data to database: ", e);
  686. }
  687. }
  688. /**
  689. * Save olympiad.properties file with current olympiad status and update noblesse table in database
  690. */
  691. public void saveOlympiadStatus()
  692. {
  693. saveNobleData();
  694. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  695. PreparedStatement statement = con.prepareStatement(OLYMPIAD_SAVE_DATA))
  696. {
  697. statement.setInt(1, _currentCycle);
  698. statement.setInt(2, _period);
  699. statement.setLong(3, _olympiadEnd);
  700. statement.setLong(4, _validationEnd);
  701. statement.setLong(5, _nextWeeklyChange);
  702. statement.setInt(6, _currentCycle);
  703. statement.setInt(7, _period);
  704. statement.setLong(8, _olympiadEnd);
  705. statement.setLong(9, _validationEnd);
  706. statement.setLong(10, _nextWeeklyChange);
  707. statement.execute();
  708. }
  709. catch (SQLException e)
  710. {
  711. _log.log(Level.SEVERE, "Olympiad System: Failed to save olympiad data to database: ", e);
  712. }
  713. //@formatter:off
  714. /*
  715. Properties OlympiadProperties = new Properties();
  716. try (FileOutputStream fos = new FileOutputStream(new File("./" + OLYMPIAD_DATA_FILE)))
  717. {
  718. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  719. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  720. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  721. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  722. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  723. OlympiadProperties.store(fos, "Olympiad Properties");
  724. }
  725. catch (Exception e)
  726. {
  727. _log.log(Level.WARNING, "Olympiad System: Unable to save olympiad properties to file: ", e);
  728. }
  729. */
  730. //@formatter:on
  731. }
  732. protected void updateMonthlyData()
  733. {
  734. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  735. PreparedStatement ps1 = con.prepareStatement(OLYMPIAD_MONTH_CLEAR);
  736. PreparedStatement ps2 = con.prepareStatement(OLYMPIAD_MONTH_CREATE))
  737. {
  738. ps1.execute();
  739. ps2.execute();
  740. }
  741. catch (SQLException e)
  742. {
  743. _log.log(Level.SEVERE, "Olympiad System: Failed to update monthly noblese data: ", e);
  744. }
  745. }
  746. protected void sortHerosToBe()
  747. {
  748. if (_period != 1)
  749. {
  750. return;
  751. }
  752. LogRecord record;
  753. _logResults.info("Noble,charid,classid,compDone,points");
  754. for (Entry<Integer, StatsSet> entry : NOBLES.entrySet())
  755. {
  756. StatsSet nobleInfo = entry.getValue();
  757. if (nobleInfo == null)
  758. {
  759. continue;
  760. }
  761. int charId = entry.getKey();
  762. int classId = nobleInfo.getInt(CLASS_ID);
  763. String charName = nobleInfo.getString(CHAR_NAME);
  764. int points = nobleInfo.getInt(POINTS);
  765. int compDone = nobleInfo.getInt(COMP_DONE);
  766. record = new LogRecord(Level.INFO, charName);
  767. record.setParameters(new Object[]
  768. {
  769. charId,
  770. classId,
  771. compDone,
  772. points
  773. });
  774. _logResults.log(record);
  775. }
  776. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  777. PreparedStatement statement = con.prepareStatement(OLYMPIAD_GET_HEROS))
  778. {
  779. StatsSet hero;
  780. List<StatsSet> soulHounds = new ArrayList<>();
  781. for (int element : HERO_IDS)
  782. {
  783. statement.setInt(1, element);
  784. try (ResultSet rset = statement.executeQuery())
  785. {
  786. if (rset.next())
  787. {
  788. hero = new StatsSet();
  789. hero.set(CLASS_ID, element);
  790. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  791. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  792. if ((element == 132) || (element == 133)) // Male & Female Soulhounds rank as one hero class
  793. {
  794. hero = NOBLES.get(hero.getInt(CHAR_ID));
  795. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  796. soulHounds.add(hero);
  797. }
  798. else
  799. {
  800. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  801. record.setParameters(new Object[]
  802. {
  803. hero.getInt(CHAR_ID),
  804. hero.getInt(CLASS_ID)
  805. });
  806. _logResults.log(record);
  807. HEROS_TO_BE.add(hero);
  808. }
  809. }
  810. }
  811. }
  812. switch (soulHounds.size())
  813. {
  814. case 0:
  815. {
  816. break;
  817. }
  818. case 1:
  819. {
  820. hero = new StatsSet();
  821. StatsSet winner = soulHounds.get(0);
  822. hero.set(CLASS_ID, winner.getInt(CLASS_ID));
  823. hero.set(CHAR_ID, winner.getInt(CHAR_ID));
  824. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  825. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  826. record.setParameters(new Object[]
  827. {
  828. hero.getInt(CHAR_ID),
  829. hero.getInt(CLASS_ID)
  830. });
  831. _logResults.log(record);
  832. HEROS_TO_BE.add(hero);
  833. break;
  834. }
  835. case 2:
  836. {
  837. hero = new StatsSet();
  838. StatsSet winner;
  839. StatsSet hero1 = soulHounds.get(0);
  840. StatsSet hero2 = soulHounds.get(1);
  841. int hero1Points = hero1.getInt(POINTS);
  842. int hero2Points = hero2.getInt(POINTS);
  843. int hero1Comps = hero1.getInt(COMP_DONE);
  844. int hero2Comps = hero2.getInt(COMP_DONE);
  845. int hero1Wins = hero1.getInt(COMP_WON);
  846. int hero2Wins = hero2.getInt(COMP_WON);
  847. if (hero1Points > hero2Points)
  848. {
  849. winner = hero1;
  850. }
  851. else if (hero2Points > hero1Points)
  852. {
  853. winner = hero2;
  854. }
  855. else
  856. {
  857. if (hero1Comps > hero2Comps)
  858. {
  859. winner = hero1;
  860. }
  861. else if (hero2Comps > hero1Comps)
  862. {
  863. winner = hero2;
  864. }
  865. else
  866. {
  867. if (hero1Wins > hero2Wins)
  868. {
  869. winner = hero1;
  870. }
  871. else
  872. {
  873. winner = hero2;
  874. }
  875. }
  876. }
  877. hero.set(CLASS_ID, winner.getInt(CLASS_ID));
  878. hero.set(CHAR_ID, winner.getInt(CHAR_ID));
  879. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  880. record = new LogRecord(Level.INFO, "Hero " + hero.getString(CHAR_NAME));
  881. record.setParameters(new Object[]
  882. {
  883. hero.getInt(CHAR_ID),
  884. hero.getInt(CLASS_ID)
  885. });
  886. _logResults.log(record);
  887. HEROS_TO_BE.add(hero);
  888. break;
  889. }
  890. }
  891. }
  892. catch (SQLException e)
  893. {
  894. _log.warning("Olympiad System: Couldnt load heros from DB");
  895. }
  896. }
  897. public List<String> getClassLeaderBoard(int classId)
  898. {
  899. final List<String> names = new ArrayList<>();
  900. 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);
  901. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  902. PreparedStatement ps = con.prepareStatement(query))
  903. {
  904. ps.setInt(1, classId);
  905. try (ResultSet rset = ps.executeQuery())
  906. {
  907. while (rset.next())
  908. {
  909. names.add(rset.getString(CHAR_NAME));
  910. }
  911. }
  912. }
  913. catch (SQLException e)
  914. {
  915. _log.warning("Olympiad System: Couldn't load olympiad leaders from DB!");
  916. }
  917. return names;
  918. }
  919. public int getNoblessePasses(L2PcInstance player, boolean clear)
  920. {
  921. if ((player == null) || (_period != 1) || NOBLES_RANK.isEmpty())
  922. {
  923. return 0;
  924. }
  925. final int objId = player.getObjectId();
  926. if (!NOBLES_RANK.containsKey(objId))
  927. {
  928. return 0;
  929. }
  930. final StatsSet noble = NOBLES.get(objId);
  931. if ((noble == null) || (noble.getInt(POINTS) == 0))
  932. {
  933. return 0;
  934. }
  935. final int rank = NOBLES_RANK.get(objId);
  936. int points = (player.isHero() ? Config.ALT_OLY_HERO_POINTS : 0);
  937. switch (rank)
  938. {
  939. case 1:
  940. points += Config.ALT_OLY_RANK1_POINTS;
  941. break;
  942. case 2:
  943. points += Config.ALT_OLY_RANK2_POINTS;
  944. break;
  945. case 3:
  946. points += Config.ALT_OLY_RANK3_POINTS;
  947. break;
  948. case 4:
  949. points += Config.ALT_OLY_RANK4_POINTS;
  950. break;
  951. default:
  952. points += Config.ALT_OLY_RANK5_POINTS;
  953. }
  954. if (clear)
  955. {
  956. noble.set(POINTS, 0);
  957. }
  958. points *= Config.ALT_OLY_GP_PER_POINT;
  959. return points;
  960. }
  961. public int getNoblePoints(int objId)
  962. {
  963. if (!NOBLES.containsKey(objId))
  964. {
  965. return 0;
  966. }
  967. return NOBLES.get(objId).getInt(POINTS);
  968. }
  969. public int getLastNobleOlympiadPoints(int objId)
  970. {
  971. int result = 0;
  972. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  973. PreparedStatement ps = con.prepareStatement("SELECT olympiad_points FROM olympiad_nobles_eom WHERE charId = ?"))
  974. {
  975. ps.setInt(1, objId);
  976. try (ResultSet rs = ps.executeQuery())
  977. {
  978. if (rs.first())
  979. {
  980. result = rs.getInt(1);
  981. }
  982. }
  983. }
  984. catch (Exception e)
  985. {
  986. _log.log(Level.WARNING, "Could not load last olympiad points:", e);
  987. }
  988. return result;
  989. }
  990. public int getCompetitionDone(int objId)
  991. {
  992. if (!NOBLES.containsKey(objId))
  993. {
  994. return 0;
  995. }
  996. return NOBLES.get(objId).getInt(COMP_DONE);
  997. }
  998. public int getCompetitionWon(int objId)
  999. {
  1000. if (!NOBLES.containsKey(objId))
  1001. {
  1002. return 0;
  1003. }
  1004. return NOBLES.get(objId).getInt(COMP_WON);
  1005. }
  1006. public int getCompetitionLost(int objId)
  1007. {
  1008. if (!NOBLES.containsKey(objId))
  1009. {
  1010. return 0;
  1011. }
  1012. return NOBLES.get(objId).getInt(COMP_LOST);
  1013. }
  1014. /**
  1015. * Gets how many matches a noble character did in the week
  1016. * @param objId id of a noble character
  1017. * @return number of weekly competitions done
  1018. */
  1019. public int getCompetitionDoneWeek(int objId)
  1020. {
  1021. if (!NOBLES.containsKey(objId))
  1022. {
  1023. return 0;
  1024. }
  1025. return NOBLES.get(objId).getInt(COMP_DONE_WEEK);
  1026. }
  1027. /**
  1028. * Gets how many classed matches a noble character did in the week
  1029. * @param objId id of a noble character
  1030. * @return number of weekly <i>classed</i> competitions done
  1031. */
  1032. public int getCompetitionDoneWeekClassed(int objId)
  1033. {
  1034. if (!NOBLES.containsKey(objId))
  1035. {
  1036. return 0;
  1037. }
  1038. return NOBLES.get(objId).getInt(COMP_DONE_WEEK_CLASSED);
  1039. }
  1040. /**
  1041. * Gets how many non classed matches a noble character did in the week
  1042. * @param objId id of a noble character
  1043. * @return number of weekly <i>non classed</i> competitions done
  1044. */
  1045. public int getCompetitionDoneWeekNonClassed(int objId)
  1046. {
  1047. if (!NOBLES.containsKey(objId))
  1048. {
  1049. return 0;
  1050. }
  1051. return NOBLES.get(objId).getInt(COMP_DONE_WEEK_NON_CLASSED);
  1052. }
  1053. /**
  1054. * Gets how many team matches a noble character did in the week
  1055. * @param objId id of a noble character
  1056. * @return number of weekly <i>team</i> competitions done
  1057. */
  1058. public int getCompetitionDoneWeekTeam(int objId)
  1059. {
  1060. if (!NOBLES.containsKey(objId))
  1061. {
  1062. return 0;
  1063. }
  1064. return NOBLES.get(objId).getInt(COMP_DONE_WEEK_TEAM);
  1065. }
  1066. /**
  1067. * Number of remaining matches a noble character can join in the week
  1068. * @param objId id of a noble character
  1069. * @return difference between maximum allowed weekly matches and currently done weekly matches.
  1070. */
  1071. public int getRemainingWeeklyMatches(int objId)
  1072. {
  1073. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES - getCompetitionDoneWeek(objId), 0);
  1074. }
  1075. /**
  1076. * Number of remaining <i>classed</i> matches a noble character can join in the week
  1077. * @param objId id of a noble character
  1078. * @return difference between maximum allowed weekly classed matches and currently done weekly classed matches.
  1079. */
  1080. public int getRemainingWeeklyMatchesClassed(int objId)
  1081. {
  1082. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_CLASSED - getCompetitionDoneWeekClassed(objId), 0);
  1083. }
  1084. /**
  1085. * Number of remaining <i>non classed</i> matches a noble character can join in the week
  1086. * @param objId id of a noble character
  1087. * @return difference between maximum allowed weekly non classed matches and currently done weekly non classed matches.
  1088. */
  1089. public int getRemainingWeeklyMatchesNonClassed(int objId)
  1090. {
  1091. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_NON_CLASSED - getCompetitionDoneWeekNonClassed(objId), 0);
  1092. }
  1093. /**
  1094. * Number of remaining <i>team</i> matches a noble character can join in the week
  1095. * @param objId id of a noble character
  1096. * @return difference between maximum allowed weekly team matches and currently done weekly team matches.
  1097. */
  1098. public int getRemainingWeeklyMatchesTeam(int objId)
  1099. {
  1100. return Math.max(Config.ALT_OLY_MAX_WEEKLY_MATCHES_TEAM - getCompetitionDoneWeekTeam(objId), 0);
  1101. }
  1102. protected void deleteNobles()
  1103. {
  1104. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  1105. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL))
  1106. {
  1107. statement.execute();
  1108. }
  1109. catch (SQLException e)
  1110. {
  1111. _log.warning("Olympiad System: Couldn't delete nobles from DB!");
  1112. }
  1113. NOBLES.clear();
  1114. }
  1115. /**
  1116. * @param charId the noble object Id.
  1117. * @param data the stats set data to add.
  1118. * @return the old stats set if the noble is already present, null otherwise.
  1119. */
  1120. protected static StatsSet addNobleStats(int charId, StatsSet data)
  1121. {
  1122. return NOBLES.put(Integer.valueOf(charId), data);
  1123. }
  1124. public static Olympiad getInstance()
  1125. {
  1126. return SingletonHolder._instance;
  1127. }
  1128. private static class SingletonHolder
  1129. {
  1130. protected static final Olympiad _instance = new Olympiad();
  1131. }
  1132. }