Olympiad.java 40 KB

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