2
0

Olympiad.java 40 KB

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