Olympiad.java 42 KB

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