Olympiad.java 42 KB

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