Olympiad.java 41 KB

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