Olympiad.java 42 KB

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