Olympiad.java 36 KB

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