Olympiad.java 42 KB

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