Olympiad.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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 net.sf.l2j.gameserver.model.olympiad;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.InputStream;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.util.Calendar;
  27. import java.util.Map;
  28. import java.util.Properties;
  29. import java.util.concurrent.ScheduledFuture;
  30. import java.util.logging.Level;
  31. import java.util.logging.LogRecord;
  32. import java.util.logging.Logger;
  33. import javolution.util.FastMap;
  34. import net.sf.l2j.Config;
  35. import net.sf.l2j.L2DatabaseFactory;
  36. import net.sf.l2j.gameserver.Announcements;
  37. import net.sf.l2j.gameserver.ThreadPoolManager;
  38. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  40. import net.sf.l2j.gameserver.model.entity.Hero;
  41. import net.sf.l2j.gameserver.model.entity.TvTEvent;
  42. import net.sf.l2j.gameserver.network.SystemMessageId;
  43. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  44. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  45. import net.sf.l2j.gameserver.templates.StatsSet;
  46. import net.sf.l2j.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 L2FastList<L2PcInstance> _nonClassBasedRegisters;
  54. private static Map<Integer, L2FastList<L2PcInstance>> _classBasedRegisters;
  55. private static Map<Integer, Integer> _noblesRank;
  56. private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties";
  57. public static final String OLYMPIAD_HTML_PATH = "data/html/olympiad/";
  58. private static final String OLYMPIAD_LOAD_DATA = "SELECT current_cycle, period, olympiad_end, validation_end, "
  59. + "next_weekly_change FROM olympiad_data WHERE id = 0";
  60. private static final String OLYMPIAD_SAVE_DATA = "INSERT INTO olympiad_data (id, current_cycle, "
  61. + "period, olympiad_end, validation_end, next_weekly_change) VALUES (0,?,?,?,?,?) "
  62. + "ON DUPLICATE KEY UPDATE current_cycle=?, period=?, olympiad_end=?, "
  63. + "validation_end=?, next_weekly_change=?";
  64. private static final String OLYMPIAD_LOAD_NOBLES = "SELECT olympiad_nobles.charId, olympiad_nobles.class_id, "
  65. + "characters.char_name, olympiad_nobles.olympiad_points, olympiad_nobles.competitions_done, "
  66. + "olympiad_nobles.competitions_won, olympiad_nobles.competitions_lost, olympiad_nobles.competitions_drawn "
  67. + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId";
  68. private static final String OLYMPIAD_SAVE_NOBLES = "INSERT INTO olympiad_nobles "
  69. + "(`charId`,`class_id`,`olympiad_points`,`competitions_done`,`competitions_won`,`competitions_lost`,"
  70. + "`competitions_drawn`) VALUES (?,?,?,?,?,?,?)";
  71. private static final String OLYMPIAD_UPDATE_NOBLES = "UPDATE olympiad_nobles SET "
  72. + "olympiad_points = ?, competitions_done = ?, competitions_won = ?, competitions_lost = ?, competitions_drawn = ? WHERE charId = ?";
  73. private static final String OLYMPIAD_GET_HEROS = "SELECT olympiad_nobles.charId, characters.char_name "
  74. + "FROM olympiad_nobles, characters WHERE characters.charId = olympiad_nobles.charId "
  75. + "AND olympiad_nobles.class_id = ? AND olympiad_nobles.competitions_done >= 9 "
  76. + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC";
  77. private static final String GET_ALL_CLASSIFIED_NOBLESS = "SELECT charId from olympiad_nobles_eom "
  78. + "WHERE competitions_done >= 9 ORDER BY olympiad_points DESC, competitions_done DESC";
  79. private static final String GET_EACH_CLASS_LEADER = "SELECT characters.char_name from olympiad_nobles_eom, characters "
  80. + "WHERE characters.charId = olympiad_nobles_eom.charId AND olympiad_nobles_eom.class_id = ? "
  81. + "AND olympiad_nobles_eom.competitions_done >= 9 "
  82. + "ORDER BY olympiad_nobles_eom.olympiad_points DESC, olympiad_nobles_eom.competitions_done DESC LIMIT 10";
  83. private static final String GET_EACH_CLASS_LEADER_CURRENT = "SELECT characters.char_name from olympiad_nobles, characters "
  84. + "WHERE characters.charId = olympiad_nobles.charId AND olympiad_nobles.class_id = ? "
  85. + "AND olympiad_nobles.competitions_done >= 9 "
  86. + "ORDER BY olympiad_nobles.olympiad_points DESC, olympiad_nobles.competitions_done DESC LIMIT 10";
  87. private static final String OLYMPIAD_DELETE_ALL = "TRUNCATE olympiad_nobles";
  88. private static final String OLYMPIAD_MONTH_CLEAR = "TRUNCATE olympiad_nobles_eom";
  89. private static final String OLYMPIAD_MONTH_CREATE = "INSERT INTO olympiad_nobles_eom SELECT * FROM olympiad_nobles";
  90. private static final int[] HERO_IDS = { 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
  91. 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 131, 132, 133, 134 };
  92. private static final int COMP_START = Config.ALT_OLY_START_TIME; // 6PM
  93. private static final int COMP_MIN = Config.ALT_OLY_MIN; // 00 mins
  94. private static final long COMP_PERIOD = Config.ALT_OLY_CPERIOD; // 6 hours
  95. protected static final long WEEKLY_PERIOD = Config.ALT_OLY_WPERIOD; // 1 week
  96. protected static final long VALIDATION_PERIOD = Config.ALT_OLY_VPERIOD; // 24 hours
  97. private static final int DEFAULT_POINTS = 18;
  98. protected static final int WEEKLY_POINTS = 3;
  99. public static final String CHAR_ID = "charId";
  100. public static final String CLASS_ID = "class_id";
  101. public static final String CHAR_NAME = "char_name";
  102. public static final String POINTS = "olympiad_points";
  103. public static final String COMP_DONE = "competitions_done";
  104. public static final String COMP_WON = "competitions_won";
  105. public static final String COMP_LOST = "competitions_lost";
  106. public static final String COMP_DRAWN = "competitions_drawn";
  107. protected long _olympiadEnd;
  108. protected long _validationEnd;
  109. /**
  110. * The current period of the olympiad.<br>
  111. * <b>0 -</b> Competition period<br>
  112. * <b>1 -</b> Validation Period
  113. */
  114. protected int _period;
  115. protected long _nextWeeklyChange;
  116. protected int _currentCycle;
  117. private long _compEnd;
  118. private Calendar _compStart;
  119. protected static boolean _inCompPeriod;
  120. protected static boolean _compStarted = false;
  121. protected ScheduledFuture<?> _scheduledCompStart;
  122. protected ScheduledFuture<?> _scheduledCompEnd;
  123. protected ScheduledFuture<?> _scheduledOlympiadEnd;
  124. protected ScheduledFuture<?> _scheduledWeeklyTask;
  125. protected ScheduledFuture<?> _scheduledValdationTask;
  126. protected static enum COMP_TYPE
  127. {
  128. CLASSED,
  129. NON_CLASSED
  130. }
  131. public static Olympiad getInstance()
  132. {
  133. return SingletonHolder._instance;
  134. }
  135. private Olympiad()
  136. {
  137. load();
  138. if (_period == 0)
  139. init();
  140. }
  141. public static Integer getStadiumCount()
  142. {
  143. return OlympiadManager.STADIUMS.length;
  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. try
  174. {
  175. con.close();
  176. }
  177. catch (Exception e)
  178. {
  179. e.printStackTrace();
  180. }
  181. }
  182. if (!loaded)
  183. {
  184. _log.log(Level.INFO, "Olympiad System: failed to load data from database, trying to load from file.");
  185. Properties OlympiadProperties = new Properties();
  186. InputStream is = null;
  187. try
  188. {
  189. is = new FileInputStream(new File("./" + OLYMPIAD_DATA_FILE));
  190. OlympiadProperties.load(is);
  191. }
  192. catch (Exception e)
  193. {
  194. _log.log(Level.SEVERE, "Olympiad System: Error loading olympiad properties: ", e);
  195. return;
  196. }
  197. finally
  198. {
  199. try
  200. {
  201. is.close();
  202. }
  203. catch (Exception e)
  204. {
  205. e.printStackTrace();
  206. }
  207. }
  208. _currentCycle = Integer.parseInt(OlympiadProperties.getProperty("CurrentCycle", "1"));
  209. _period = Integer.parseInt(OlympiadProperties.getProperty("Period", "0"));
  210. _olympiadEnd = Long.parseLong(OlympiadProperties.getProperty("OlympiadEnd", "0"));
  211. _validationEnd = Long.parseLong(OlympiadProperties.getProperty("ValidationEnd", "0"));
  212. _nextWeeklyChange = Long.parseLong(OlympiadProperties.getProperty("NextWeeklyChange", "0"));
  213. }
  214. switch (_period)
  215. {
  216. case 0:
  217. if (_olympiadEnd == 0 || _olympiadEnd < Calendar.getInstance().getTimeInMillis())
  218. setNewOlympiadEnd();
  219. else
  220. scheduleWeeklyChange();
  221. break;
  222. case 1:
  223. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  224. {
  225. loadNoblesRank();
  226. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  227. }
  228. else
  229. {
  230. _currentCycle++;
  231. _period = 0;
  232. deleteNobles();
  233. setNewOlympiadEnd();
  234. }
  235. break;
  236. default:
  237. _log.warning("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  238. return;
  239. }
  240. try
  241. {
  242. con = L2DatabaseFactory.getInstance().getConnection();
  243. PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES);
  244. ResultSet rset = statement.executeQuery();
  245. while (rset.next())
  246. {
  247. StatsSet statData = new StatsSet();
  248. int charId = rset.getInt(CHAR_ID);
  249. statData.set(CLASS_ID, rset.getInt(CLASS_ID));
  250. statData.set(CHAR_NAME, rset.getString(CHAR_NAME));
  251. statData.set(POINTS, rset.getInt(POINTS));
  252. statData.set(COMP_DONE, rset.getInt(COMP_DONE));
  253. statData.set(COMP_WON, rset.getInt(COMP_WON));
  254. statData.set(COMP_LOST, rset.getInt(COMP_LOST));
  255. statData.set(COMP_DRAWN, rset.getInt(COMP_DRAWN));
  256. statData.set("to_save", false);
  257. _nobles.put(charId, statData);
  258. }
  259. rset.close();
  260. statement.close();
  261. }
  262. catch (Exception e)
  263. {
  264. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database: ", e);
  265. }
  266. finally
  267. {
  268. try
  269. {
  270. con.close();
  271. }
  272. catch (Exception e)
  273. {
  274. e.printStackTrace();
  275. }
  276. }
  277. synchronized (this)
  278. {
  279. _log.info("Olympiad System: Loading Olympiad System....");
  280. if (_period == 0)
  281. _log.info("Olympiad System: Currently in Olympiad Period");
  282. else
  283. _log.info("Olympiad System: Currently in Validation Period");
  284. long milliToEnd;
  285. if (_period == 0)
  286. milliToEnd = getMillisToOlympiadEnd();
  287. else
  288. milliToEnd = getMillisToValidationEnd();
  289. _log.info("Olympiad System: " + Math.round(milliToEnd / 60000) + " minutes until period ends");
  290. if (_period == 0)
  291. {
  292. milliToEnd = getMillisToWeekChange();
  293. _log.info("Olympiad System: Next weekly change is in " + Math.round(milliToEnd / 60000) + " minutes");
  294. }
  295. }
  296. _log.info("Olympiad System: Loaded " + _nobles.size() + " Nobles");
  297. }
  298. public void loadNoblesRank()
  299. {
  300. _noblesRank = new FastMap<Integer, Integer>();
  301. Map<Integer, Integer> tmpPlace = new FastMap<Integer, Integer>();
  302. Connection con = null;
  303. try
  304. {
  305. con = L2DatabaseFactory.getInstance().getConnection();
  306. PreparedStatement statement = con.prepareStatement(GET_ALL_CLASSIFIED_NOBLESS);
  307. ResultSet rset = statement.executeQuery();
  308. int place = 1;
  309. while (rset.next())
  310. {
  311. tmpPlace.put(rset.getInt(CHAR_ID), place++);
  312. }
  313. rset.close();
  314. statement.close();
  315. }
  316. catch (Exception e)
  317. {
  318. _log.log(Level.WARNING, "Olympiad System: Error loading noblesse data from database for Ranking: ", e);
  319. }
  320. finally
  321. {
  322. try
  323. {
  324. con.close();
  325. }
  326. catch (Exception e)
  327. {
  328. }
  329. }
  330. int rank1 = (int) Math.round(tmpPlace.size() * 0.01);
  331. int rank2 = (int) Math.round(tmpPlace.size() * 0.10);
  332. int rank3 = (int) Math.round(tmpPlace.size() * 0.25);
  333. int rank4 = (int) Math.round(tmpPlace.size() * 0.50);
  334. if (rank1 == 0)
  335. {
  336. rank1 = 1;
  337. rank2++;
  338. rank3++;
  339. rank4++;
  340. }
  341. for (int charId : tmpPlace.keySet())
  342. {
  343. if (tmpPlace.get(charId) <= rank1)
  344. _noblesRank.put(charId, 1);
  345. else if (tmpPlace.get(charId) <= rank2)
  346. _noblesRank.put(charId, 2);
  347. else if (tmpPlace.get(charId) <= rank3)
  348. _noblesRank.put(charId, 3);
  349. else if (tmpPlace.get(charId) <= rank4)
  350. _noblesRank.put(charId, 4);
  351. else
  352. _noblesRank.put(charId, 5);
  353. }
  354. }
  355. protected void init()
  356. {
  357. if (_period == 1)
  358. return;
  359. _nonClassBasedRegisters = new L2FastList<L2PcInstance>();
  360. _classBasedRegisters = new FastMap<Integer, L2FastList<L2PcInstance>>();
  361. _compStart = Calendar.getInstance();
  362. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  363. _compStart.set(Calendar.MINUTE, COMP_MIN);
  364. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  365. if (_scheduledOlympiadEnd != null)
  366. _scheduledOlympiadEnd.cancel(true);
  367. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), getMillisToOlympiadEnd());
  368. updateCompStatus();
  369. }
  370. protected class OlympiadEndTask implements Runnable
  371. {
  372. public void run()
  373. {
  374. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_ENDED);
  375. sm.addNumber(_currentCycle);
  376. Announcements.getInstance().announceToAll(sm);
  377. Announcements.getInstance().announceToAll("Olympiad Validation Period has began");
  378. if (_scheduledWeeklyTask != null)
  379. _scheduledWeeklyTask.cancel(true);
  380. saveNobleData();
  381. _period = 1;
  382. sortHerosToBe();
  383. Hero.getInstance().computeNewHeroes(_heroesToBe);
  384. saveOlympiadStatus();
  385. updateMonthlyData();
  386. Calendar validationEnd = Calendar.getInstance();
  387. _validationEnd = validationEnd.getTimeInMillis() + VALIDATION_PERIOD;
  388. loadNoblesRank();
  389. _scheduledValdationTask = ThreadPoolManager.getInstance().scheduleGeneral(new ValidationEndTask(), getMillisToValidationEnd());
  390. }
  391. }
  392. protected class ValidationEndTask implements Runnable
  393. {
  394. public void run()
  395. {
  396. Announcements.getInstance().announceToAll("Olympiad Validation Period has ended");
  397. _period = 0;
  398. _currentCycle++;
  399. deleteNobles();
  400. setNewOlympiadEnd();
  401. init();
  402. }
  403. }
  404. public boolean registerNoble(L2PcInstance noble, boolean classBased)
  405. {
  406. SystemMessage sm;
  407. /*
  408. * if (_compStarted) {
  409. * noble.sendMessage("Cant Register whilst competition is under way");
  410. * return false; }
  411. */
  412. if (!_inCompPeriod)
  413. {
  414. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  415. noble.sendPacket(sm);
  416. return false;
  417. }
  418. if (!noble.isNoble())
  419. {
  420. sm = new SystemMessage(SystemMessageId.C1_DOES_NOT_MEET_REQUIREMENTS_ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  421. sm.addPcName(noble);
  422. noble.sendPacket(sm);
  423. return false;
  424. }
  425. /** Begin Olympiad Restrictions */
  426. if (noble.getBaseClass() != noble.getClassId().getId())
  427. {
  428. sm = new SystemMessage(SystemMessageId.C1_CANT_JOIN_THE_OLYMPIAD_WITH_A_SUB_CLASS_CHARACTER);
  429. sm.addPcName(noble);
  430. noble.sendPacket(sm);
  431. return false;
  432. }
  433. if (noble.isCursedWeaponEquipped())
  434. {
  435. sm = new SystemMessage(SystemMessageId.C1_CANNOT_JOIN_OLYMPIAD_POSSESSING_S2);
  436. sm.addPcName(noble);
  437. sm.addItemName(noble.getCursedWeaponEquippedId());
  438. noble.sendPacket(sm);
  439. return false;
  440. }
  441. if (noble.getInventoryLimit() * 0.8 <= noble.getInventory().getSize())
  442. {
  443. sm = new SystemMessage(SystemMessageId.C1_CANNOT_PARTICIPATE_IN_OLYMPIAD_INVENTORY_SLOT_EXCEEDS_80_PERCENT);
  444. sm.addPcName(noble);
  445. noble.sendPacket(sm);
  446. return false;
  447. }
  448. if (getMillisToCompEnd() < 600000)
  449. {
  450. sm = new SystemMessage(SystemMessageId.GAME_REQUEST_CANNOT_BE_MADE);
  451. noble.sendPacket(sm);
  452. return false;
  453. }
  454. if (TvTEvent.isPlayerParticipant(noble.getObjectId()))
  455. {
  456. noble.sendMessage("You can't join olympiad while participating on TvT Event.");
  457. return false;
  458. }
  459. /** End Olympiad Restrictions */
  460. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  461. {
  462. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  463. for (L2PcInstance participant : classed)
  464. {
  465. if (participant.getObjectId() == noble.getObjectId())
  466. {
  467. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_CLASS_MATCH_WAITING_LIST);
  468. sm.addPcName(noble);
  469. noble.sendPacket(sm);
  470. return false;
  471. }
  472. }
  473. }
  474. if (isRegisteredInComp(noble))
  475. {
  476. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_NON_CLASS_LIMITED_MATCH_WAITING_LIST);
  477. sm.addPcName(noble);
  478. noble.sendPacket(sm);
  479. return false;
  480. }
  481. if (!_nobles.containsKey(noble.getObjectId()))
  482. {
  483. StatsSet statDat = new StatsSet();
  484. statDat.set(CLASS_ID, noble.getClassId().getId());
  485. statDat.set(CHAR_NAME, noble.getName());
  486. statDat.set(POINTS, DEFAULT_POINTS);
  487. statDat.set(COMP_DONE, 0);
  488. statDat.set(COMP_WON, 0);
  489. statDat.set(COMP_LOST, 0);
  490. statDat.set(COMP_DRAWN, 0);
  491. statDat.set("to_save", true);
  492. _nobles.put(noble.getObjectId(), statDat);
  493. }
  494. if (classBased && getNoblePoints(noble.getObjectId()) < 3)
  495. {
  496. noble.sendMessage("Cant register when you have less than 3 points");
  497. return false;
  498. }
  499. if (!classBased && getNoblePoints(noble.getObjectId()) < 5)
  500. {
  501. noble.sendMessage("Cant register when you have less than 5 points");
  502. return false;
  503. }
  504. if (classBased)
  505. {
  506. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  507. {
  508. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  509. classed.add(noble);
  510. _classBasedRegisters.remove(noble.getClassId().getId());
  511. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  512. }
  513. else
  514. {
  515. L2FastList<L2PcInstance> classed = new L2FastList<L2PcInstance>();
  516. classed.add(noble);
  517. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  518. }
  519. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  520. noble.sendPacket(sm);
  521. }
  522. else
  523. {
  524. _nonClassBasedRegisters.add(noble);
  525. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  526. noble.sendPacket(sm);
  527. }
  528. return true;
  529. }
  530. protected static int getNobleCount()
  531. {
  532. return _nobles.size();
  533. }
  534. protected static StatsSet getNobleStats(int playerId)
  535. {
  536. return _nobles.get(playerId);
  537. }
  538. protected static synchronized void updateNobleStats(int playerId, StatsSet stats)
  539. {
  540. _nobles.remove(playerId);
  541. _nobles.put(playerId, stats);
  542. }
  543. protected static L2FastList<L2PcInstance> getRegisteredNonClassBased()
  544. {
  545. return _nonClassBasedRegisters;
  546. }
  547. protected static Map<Integer, L2FastList<L2PcInstance>> getRegisteredClassBased()
  548. {
  549. return _classBasedRegisters;
  550. }
  551. protected static L2FastList<Integer> hasEnoughRegisteredClassed()
  552. {
  553. L2FastList<Integer> result = new L2FastList<Integer>();
  554. for (Integer classList : getRegisteredClassBased().keySet())
  555. {
  556. if (getRegisteredClassBased().get(classList).size() >= Config.ALT_OLY_CLASSED)
  557. {
  558. result.add(classList);
  559. }
  560. }
  561. if (!result.isEmpty())
  562. {
  563. return result;
  564. }
  565. return null;
  566. }
  567. protected static boolean hasEnoughRegisteredNonClassed()
  568. {
  569. return Olympiad.getRegisteredNonClassBased().size() >= Config.ALT_OLY_NONCLASSED;
  570. }
  571. protected static void clearRegistered()
  572. {
  573. _nonClassBasedRegisters.clear();
  574. _classBasedRegisters.clear();
  575. }
  576. public boolean isRegistered(L2PcInstance noble)
  577. {
  578. boolean result = false;
  579. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(noble))
  580. result = true;
  581. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(noble.getClassId().getId()))
  582. {
  583. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  584. if (classed != null && classed.contains(noble))
  585. result = true;
  586. }
  587. return result;
  588. }
  589. public boolean unRegisterNoble(L2PcInstance noble)
  590. {
  591. SystemMessage sm;
  592. /*
  593. * if (_compStarted) {
  594. * noble.sendMessage("Cant Unregister whilst competition is under way");
  595. * return false; }
  596. */
  597. if (!_inCompPeriod)
  598. {
  599. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  600. noble.sendPacket(sm);
  601. return false;
  602. }
  603. if (!noble.isNoble())
  604. {
  605. sm = new SystemMessage(SystemMessageId.C1_DOES_NOT_MEET_REQUIREMENTS_ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  606. sm.addString(noble.getName());
  607. noble.sendPacket(sm);
  608. return false;
  609. }
  610. if (!isRegistered(noble))
  611. {
  612. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  613. noble.sendPacket(sm);
  614. return false;
  615. }
  616. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  617. {
  618. if (game == null)
  619. continue;
  620. if (game._playerOneID == noble.getObjectId() || game._playerTwoID == noble.getObjectId())
  621. {
  622. noble.sendMessage("Can't deregister whilst you are already selected for a game");
  623. return false;
  624. }
  625. }
  626. if (_nonClassBasedRegisters.contains(noble))
  627. _nonClassBasedRegisters.remove(noble);
  628. else
  629. {
  630. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  631. classed.remove(noble);
  632. _classBasedRegisters.remove(noble.getClassId().getId());
  633. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  634. }
  635. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  636. noble.sendPacket(sm);
  637. return true;
  638. }
  639. public void removeDisconnectedCompetitor(L2PcInstance player)
  640. {
  641. if (OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()) != null)
  642. OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()).handleDisconnect(player);
  643. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  644. if (_nonClassBasedRegisters.contains(player))
  645. _nonClassBasedRegisters.remove(player);
  646. else if (classed != null && classed.contains(player))
  647. {
  648. classed.remove(player);
  649. _classBasedRegisters.remove(player.getClassId().getId());
  650. _classBasedRegisters.put(player.getClassId().getId(), classed);
  651. }
  652. }
  653. public void notifyCompetitorDamage(L2PcInstance player, int damage, int gameId)
  654. {
  655. if (OlympiadManager.getInstance().getOlympiadGames().get(gameId) != null)
  656. OlympiadManager.getInstance().getOlympiadGames().get(gameId).addDamage(player, damage);
  657. }
  658. private void updateCompStatus()
  659. {
  660. // _compStarted = false;
  661. synchronized (this)
  662. {
  663. long milliToStart = getMillisToCompBegin();
  664. double numSecs = (milliToStart / 1000) % 60;
  665. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  666. int numMins = (int) Math.floor(countDown % 60);
  667. countDown = (countDown - numMins) / 60;
  668. int numHours = (int) Math.floor(countDown % 24);
  669. int numDays = (int) Math.floor((countDown - numHours) / 24);
  670. _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins
  671. + " mins.");
  672. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  673. }
  674. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  675. public void run()
  676. {
  677. if (isOlympiadEnd())
  678. return;
  679. _inCompPeriod = true;
  680. OlympiadManager om = OlympiadManager.getInstance();
  681. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  682. _log.info("Olympiad System: Olympiad Game Started");
  683. _logResults.info("Result,Player1,Player2,Player1 HP,Player2 HP,Player1 Damage,Player2 Damage,Points,Classed");
  684. Thread olyCycle = new Thread(om);
  685. olyCycle.start();
  686. long regEnd = getMillisToCompEnd() - 600000;
  687. if (regEnd > 0)
  688. {
  689. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  690. public void run()
  691. {
  692. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.OLYMPIAD_REGISTRATION_PERIOD_ENDED));
  693. }
  694. }, regEnd);
  695. }
  696. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  697. public void run()
  698. {
  699. if (isOlympiadEnd())
  700. return;
  701. _inCompPeriod = false;
  702. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  703. _log.info("Olympiad System: Olympiad Game Ended");
  704. while (OlympiadGame._battleStarted)
  705. {
  706. try
  707. {
  708. // wait 1 minutes for end of pendings games
  709. Thread.sleep(60000);
  710. }
  711. catch (InterruptedException e)
  712. {
  713. }
  714. }
  715. saveOlympiadStatus();
  716. init();
  717. }
  718. }, getMillisToCompEnd());
  719. }
  720. }, getMillisToCompBegin());
  721. }
  722. private long getMillisToOlympiadEnd()
  723. {
  724. // if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  725. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  726. // return 10L;
  727. }
  728. public void manualSelectHeroes()
  729. {
  730. if (_scheduledOlympiadEnd != null)
  731. _scheduledOlympiadEnd.cancel(true);
  732. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), 0);
  733. }
  734. protected long getMillisToValidationEnd()
  735. {
  736. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  737. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  738. return 10L;
  739. }
  740. public boolean isOlympiadEnd()
  741. {
  742. return (_period != 0);
  743. }
  744. protected void setNewOlympiadEnd()
  745. {
  746. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  747. sm.addNumber(_currentCycle);
  748. Announcements.getInstance().announceToAll(sm);
  749. Calendar currentTime = Calendar.getInstance();
  750. currentTime.add(Calendar.MONTH, 1);
  751. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  752. currentTime.set(Calendar.AM_PM, Calendar.AM);
  753. currentTime.set(Calendar.HOUR, 12);
  754. currentTime.set(Calendar.MINUTE, 0);
  755. currentTime.set(Calendar.SECOND, 0);
  756. _olympiadEnd = currentTime.getTimeInMillis();
  757. Calendar nextChange = Calendar.getInstance();
  758. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  759. scheduleWeeklyChange();
  760. }
  761. public boolean inCompPeriod()
  762. {
  763. return _inCompPeriod;
  764. }
  765. private long getMillisToCompBegin()
  766. {
  767. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() && _compEnd > Calendar.getInstance().getTimeInMillis())
  768. return 10L;
  769. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  770. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  771. return setNewCompBegin();
  772. }
  773. private long setNewCompBegin()
  774. {
  775. _compStart = Calendar.getInstance();
  776. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  777. _compStart.set(Calendar.MINUTE, COMP_MIN);
  778. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  779. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  780. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  781. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  782. }
  783. protected long getMillisToCompEnd()
  784. {
  785. // if (_compEnd > Calendar.getInstance().getTimeInMillis())
  786. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  787. // return 10L;
  788. }
  789. private long getMillisToWeekChange()
  790. {
  791. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  792. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  793. return 10L;
  794. }
  795. private void scheduleWeeklyChange()
  796. {
  797. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  798. public void run()
  799. {
  800. addWeeklyPoints();
  801. _log.info("Olympiad System: Added weekly points to nobles");
  802. Calendar nextChange = Calendar.getInstance();
  803. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  804. }
  805. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  806. }
  807. protected synchronized void addWeeklyPoints()
  808. {
  809. if (_period == 1)
  810. return;
  811. for (Integer nobleId : _nobles.keySet())
  812. {
  813. StatsSet nobleInfo = _nobles.get(nobleId);
  814. int currentPoints = nobleInfo.getInteger(POINTS);
  815. currentPoints += WEEKLY_POINTS;
  816. nobleInfo.set(POINTS, currentPoints);
  817. updateNobleStats(nobleId, nobleInfo);
  818. }
  819. }
  820. public FastMap<Integer, String> getMatchList()
  821. {
  822. return OlympiadManager.getInstance().getAllTitles();
  823. }
  824. // returns the players for the given olympiad game Id
  825. public L2PcInstance[] getPlayers(int Id)
  826. {
  827. if (OlympiadManager.getInstance().getOlympiadGame(Id) == null)
  828. return null;
  829. else
  830. return OlympiadManager.getInstance().getOlympiadGame(Id).getPlayers();
  831. }
  832. public int getCurrentCycle()
  833. {
  834. return _currentCycle;
  835. }
  836. public static void addSpectator(int id, L2PcInstance spectator, boolean storeCoords)
  837. {
  838. if (getInstance().isRegisteredInComp(spectator))
  839. {
  840. spectator.sendPacket(new SystemMessage(SystemMessageId.WHILE_YOU_ARE_ON_THE_WAITING_LIST_YOU_ARE_NOT_ALLOWED_TO_WATCH_THE_GAME));
  841. return;
  842. }
  843. if (!TvTEvent.isInactive() && TvTEvent.isPlayerParticipant(spectator.getObjectId()))
  844. {
  845. spectator.sendMessage("You can not observe games while registered for TvT");
  846. return;
  847. }
  848. OlympiadManager.STADIUMS[id].addSpectator(id, spectator, storeCoords);
  849. }
  850. public static int getSpectatorArena(L2PcInstance player)
  851. {
  852. for (int i = 0; i < OlympiadManager.STADIUMS.length; i++)
  853. {
  854. if (OlympiadManager.STADIUMS[i].getSpectators().contains(player))
  855. return i;
  856. }
  857. return -1;
  858. }
  859. public static void removeSpectator(int id, L2PcInstance spectator)
  860. {
  861. try
  862. {
  863. OlympiadManager.STADIUMS[id].removeSpectator(spectator);
  864. }
  865. catch (ArrayIndexOutOfBoundsException e)
  866. {
  867. }
  868. }
  869. public L2FastList<L2PcInstance> getSpectators(int id)
  870. {
  871. try
  872. {
  873. if (OlympiadManager.getInstance().getOlympiadGame(id) == null)
  874. return null;
  875. return OlympiadManager.STADIUMS[id].getSpectators();
  876. }
  877. catch (ArrayIndexOutOfBoundsException e)
  878. {
  879. return null;
  880. }
  881. }
  882. public Map<Integer, OlympiadGame> getOlympiadGames()
  883. {
  884. return OlympiadManager.getInstance().getOlympiadGames();
  885. }
  886. public boolean playerInStadia(L2PcInstance player)
  887. {
  888. return (ZoneManager.getInstance().getOlympiadStadium(player) != null);
  889. }
  890. public int[] getWaitingList()
  891. {
  892. int[] array = new int[2];
  893. if (!inCompPeriod())
  894. return null;
  895. int classCount = 0;
  896. if (_classBasedRegisters.size() != 0)
  897. for (L2FastList<L2PcInstance> classed : _classBasedRegisters.values())
  898. {
  899. classCount += classed.size();
  900. }
  901. array[0] = classCount;
  902. array[1] = _nonClassBasedRegisters.size();
  903. return array;
  904. }
  905. /**
  906. * Save noblesse data to database
  907. */
  908. protected synchronized void saveNobleData()
  909. {
  910. if (_nobles == null || _nobles.isEmpty())
  911. return;
  912. Connection con = null;
  913. try
  914. {
  915. con = L2DatabaseFactory.getInstance().getConnection();
  916. PreparedStatement statement;
  917. for (Integer nobleId : _nobles.keySet())
  918. {
  919. StatsSet nobleInfo = _nobles.get(nobleId);
  920. if (nobleInfo == null)
  921. continue;
  922. int charId = nobleId;
  923. int classId = nobleInfo.getInteger(CLASS_ID);
  924. int points = nobleInfo.getInteger(POINTS);
  925. int compDone = nobleInfo.getInteger(COMP_DONE);
  926. int compWon = nobleInfo.getInteger(COMP_WON);
  927. int compLost = nobleInfo.getInteger(COMP_LOST);
  928. int compDrawn = nobleInfo.getInteger(COMP_DRAWN);
  929. boolean toSave = nobleInfo.getBool("to_save");
  930. if (toSave)
  931. {
  932. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  933. statement.setInt(1, charId);
  934. statement.setInt(2, classId);
  935. statement.setInt(3, points);
  936. statement.setInt(4, compDone);
  937. statement.setInt(5, compWon);
  938. statement.setInt(6, compLost);
  939. statement.setInt(7, compDrawn);
  940. nobleInfo.set("to_save", false);
  941. updateNobleStats(nobleId, nobleInfo);
  942. }
  943. else
  944. {
  945. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  946. statement.setInt(1, points);
  947. statement.setInt(2, compDone);
  948. statement.setInt(3, compWon);
  949. statement.setInt(4, compLost);
  950. statement.setInt(5, compDrawn);
  951. statement.setInt(6, charId);
  952. }
  953. statement.execute();
  954. statement.close();
  955. }
  956. }
  957. catch (SQLException e)
  958. {
  959. _log.log(Level.SEVERE, "Olympiad System: Failed to save noblesse data to database: ", e);
  960. }
  961. finally
  962. {
  963. try
  964. {
  965. con.close();
  966. }
  967. catch (Exception e)
  968. {
  969. e.printStackTrace();
  970. }
  971. }
  972. }
  973. /**
  974. * Save olympiad.properties file with current olympiad status and update noblesse table in database
  975. */
  976. public void saveOlympiadStatus()
  977. {
  978. saveNobleData();
  979. Connection con = null;
  980. try
  981. {
  982. con = L2DatabaseFactory.getInstance().getConnection();
  983. PreparedStatement statement = con.prepareStatement(OLYMPIAD_SAVE_DATA);
  984. statement.setInt(1, _currentCycle);
  985. statement.setInt(2, _period);
  986. statement.setLong(3, _olympiadEnd);
  987. statement.setLong(4, _validationEnd);
  988. statement.setLong(5, _nextWeeklyChange);
  989. statement.setInt(6, _currentCycle);
  990. statement.setInt(7, _period);
  991. statement.setLong(8, _olympiadEnd);
  992. statement.setLong(9, _validationEnd);
  993. statement.setLong(10, _nextWeeklyChange);
  994. statement.execute();
  995. statement.close();
  996. }
  997. catch (SQLException e)
  998. {
  999. _log.log(Level.SEVERE, "Olympiad System: Failed to save olympiad data to database: ", e);
  1000. }
  1001. finally
  1002. {
  1003. try
  1004. {
  1005. con.close();
  1006. }
  1007. catch (Exception e)
  1008. {
  1009. e.printStackTrace();
  1010. }
  1011. }
  1012. /* Properties OlympiadProperties = new Properties();
  1013. FileOutputStream fos = null;
  1014. try
  1015. {
  1016. fos = new FileOutputStream(new File("./" + OLYMPIAD_DATA_FILE));
  1017. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  1018. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  1019. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  1020. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  1021. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  1022. OlympiadProperties.store(fos, "Olympiad Properties");
  1023. }
  1024. catch (Exception e)
  1025. {
  1026. _log.log(Level.WARNING, "Olympiad System: Unable to save olympiad properties to file: ", e);
  1027. }
  1028. finally
  1029. {
  1030. try
  1031. {
  1032. fos.close();
  1033. }
  1034. catch (Exception e)
  1035. {
  1036. }
  1037. }*/
  1038. }
  1039. protected void updateMonthlyData()
  1040. {
  1041. Connection con = null;
  1042. try
  1043. {
  1044. con = L2DatabaseFactory.getInstance().getConnection();
  1045. PreparedStatement statement;
  1046. statement = con.prepareStatement(OLYMPIAD_MONTH_CLEAR);
  1047. statement.execute();
  1048. statement.close();
  1049. statement = con.prepareStatement(OLYMPIAD_MONTH_CREATE);
  1050. statement.execute();
  1051. statement.close();
  1052. }
  1053. catch (SQLException e)
  1054. {
  1055. _log.log(Level.SEVERE, "Olympiad System: Failed to update monthly noblese data: ", e);
  1056. }
  1057. finally
  1058. {
  1059. try
  1060. {
  1061. con.close();
  1062. }
  1063. catch (Exception e)
  1064. {
  1065. }
  1066. }
  1067. }
  1068. protected void sortHerosToBe()
  1069. {
  1070. if (_period != 1)
  1071. return;
  1072. LogRecord record;
  1073. if (_nobles != null)
  1074. {
  1075. _logResults.info("Noble,charid,classid,compDone,points");
  1076. for (Integer nobleId : _nobles.keySet())
  1077. {
  1078. StatsSet nobleInfo = _nobles.get(nobleId);
  1079. if (nobleInfo == null)
  1080. continue;
  1081. int charId = nobleId;
  1082. int classId = nobleInfo.getInteger(CLASS_ID);
  1083. String charName = nobleInfo.getString(CHAR_NAME);
  1084. int points = nobleInfo.getInteger(POINTS);
  1085. int compDone = nobleInfo.getInteger(COMP_DONE);
  1086. record = new LogRecord(Level.INFO, charName);
  1087. record.setParameters(new Object[]{charId, classId, compDone, points});
  1088. _logResults.log(record);
  1089. }
  1090. }
  1091. _heroesToBe = new L2FastList<StatsSet>();
  1092. Connection con = null;
  1093. try
  1094. {
  1095. con = L2DatabaseFactory.getInstance().getConnection();
  1096. PreparedStatement statement;
  1097. ResultSet rset;
  1098. StatsSet hero;
  1099. L2FastList<StatsSet> soulHounds = new L2FastList<StatsSet>();
  1100. for (int i = 0; i < HERO_IDS.length; i++)
  1101. {
  1102. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  1103. statement.setInt(1, HERO_IDS[i]);
  1104. rset = statement.executeQuery();
  1105. if (rset.next())
  1106. {
  1107. hero = new StatsSet();
  1108. hero.set(CLASS_ID, HERO_IDS[i]);
  1109. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1110. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  1111. if (HERO_IDS[i] == 132 || HERO_IDS[i] == 133) // Male & Female Soulhounds rank as one hero class
  1112. {
  1113. hero = _nobles.get(hero.getInteger(CHAR_ID));
  1114. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1115. soulHounds.add(hero);
  1116. }
  1117. else
  1118. {
  1119. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1120. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1121. _logResults.log(record);
  1122. _heroesToBe.add(hero);
  1123. }
  1124. }
  1125. statement.close();
  1126. rset.close();
  1127. }
  1128. switch (soulHounds.size())
  1129. {
  1130. case 0:
  1131. {
  1132. break;
  1133. }
  1134. case 1:
  1135. {
  1136. hero = new StatsSet();
  1137. StatsSet winner = soulHounds.get(0);
  1138. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1139. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1140. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1141. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1142. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1143. _logResults.log(record);
  1144. _heroesToBe.add(hero);
  1145. break;
  1146. }
  1147. case 2:
  1148. {
  1149. hero = new StatsSet();
  1150. StatsSet winner;
  1151. StatsSet hero1 = soulHounds.get(0);
  1152. StatsSet hero2 = soulHounds.get(1);
  1153. int hero1Points = hero1.getInteger(POINTS);
  1154. int hero2Points = hero2.getInteger(POINTS);
  1155. int hero1Comps = hero1.getInteger(COMP_DONE);
  1156. int hero2Comps = hero2.getInteger(COMP_DONE);
  1157. if (hero1Points > hero2Points)
  1158. winner = hero1;
  1159. else if (hero2Points > hero1Points)
  1160. winner = hero2;
  1161. else
  1162. {
  1163. if (hero1Comps > hero2Comps)
  1164. winner = hero1;
  1165. else
  1166. winner = hero2;
  1167. }
  1168. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1169. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1170. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1171. record = new LogRecord(Level.INFO, "Hero "+hero.getString(CHAR_NAME));
  1172. record.setParameters(new Object[]{hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID)});
  1173. _logResults.log(record);
  1174. _heroesToBe.add(hero);
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. catch (SQLException e)
  1180. {
  1181. _log.warning("Olympiad System: Couldnt load heros from DB");
  1182. }
  1183. finally
  1184. {
  1185. try
  1186. {
  1187. con.close();
  1188. }
  1189. catch (Exception e)
  1190. {
  1191. e.printStackTrace();
  1192. }
  1193. }
  1194. }
  1195. public L2FastList<String> getClassLeaderBoard(int classId)
  1196. {
  1197. // if (_period != 1) return;
  1198. L2FastList<String> names = new L2FastList<String>();
  1199. Connection con = null;
  1200. try
  1201. {
  1202. con = L2DatabaseFactory.getInstance().getConnection();
  1203. PreparedStatement statement;
  1204. ResultSet rset;
  1205. if (Config.ALT_OLY_SHOW_MONTHLY_WINNERS)
  1206. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  1207. else
  1208. statement = con.prepareStatement(GET_EACH_CLASS_LEADER_CURRENT);
  1209. statement.setInt(1, classId);
  1210. rset = statement.executeQuery();
  1211. while (rset.next())
  1212. {
  1213. names.add(rset.getString(CHAR_NAME));
  1214. }
  1215. if (classId == 132) // Male & Female SoulHounds are ranked together
  1216. {
  1217. statement.setInt(1, 133);
  1218. rset = statement.executeQuery();
  1219. while (rset.next())
  1220. {
  1221. names.add(rset.getString(CHAR_NAME));
  1222. }
  1223. }
  1224. statement.close();
  1225. rset.close();
  1226. return names;
  1227. }
  1228. catch (SQLException e)
  1229. {
  1230. _log.warning("Olympiad System: Couldnt load olympiad leaders from DB");
  1231. }
  1232. finally
  1233. {
  1234. try
  1235. {
  1236. con.close();
  1237. }
  1238. catch (Exception e)
  1239. {
  1240. e.printStackTrace();
  1241. }
  1242. }
  1243. return names;
  1244. }
  1245. public int getNoblessePasses(L2PcInstance player, boolean clear)
  1246. {
  1247. if (_period != 1 || _noblesRank.isEmpty())
  1248. return 0;
  1249. int objId = player.getObjectId();
  1250. if (!_noblesRank.containsKey(objId))
  1251. return 0;
  1252. StatsSet noble = _nobles.get(objId);
  1253. if (noble.getInteger(POINTS) == 0)
  1254. return 0;
  1255. int rank = _noblesRank.get(objId);
  1256. int points = (player.isHero() ? Config.ALT_OLY_HERO_POINTS : 0);
  1257. switch (rank)
  1258. {
  1259. case 1:
  1260. points += Config.ALT_OLY_RANK1_POINTS;
  1261. break;
  1262. case 2:
  1263. points += Config.ALT_OLY_RANK2_POINTS;
  1264. break;
  1265. case 3:
  1266. points += Config.ALT_OLY_RANK3_POINTS;
  1267. break;
  1268. case 4:
  1269. points += Config.ALT_OLY_RANK4_POINTS;
  1270. break;
  1271. default:
  1272. points += Config.ALT_OLY_RANK5_POINTS;
  1273. }
  1274. if (clear)
  1275. {
  1276. noble.set(POINTS, 0);
  1277. updateNobleStats(objId, noble);
  1278. }
  1279. points *= Config.ALT_OLY_GP_PER_POINT;
  1280. return points;
  1281. }
  1282. public boolean isRegisteredInComp(L2PcInstance player)
  1283. {
  1284. boolean result = isRegistered(player);
  1285. if (_inCompPeriod)
  1286. {
  1287. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  1288. {
  1289. if ((game._playerOneID == player.getObjectId()) || (game._playerTwoID == player.getObjectId()))
  1290. {
  1291. result = true;
  1292. break;
  1293. }
  1294. }
  1295. }
  1296. return result;
  1297. }
  1298. public int getNoblePoints(int objId)
  1299. {
  1300. if (_nobles.isEmpty())
  1301. return 0;
  1302. StatsSet noble = _nobles.get(objId);
  1303. if (noble == null)
  1304. return 0;
  1305. int points = noble.getInteger(POINTS);
  1306. return points;
  1307. }
  1308. public int getLastNobleOlympiadPoints(int objId)
  1309. {
  1310. int result = 0;
  1311. Connection con = null;
  1312. try
  1313. {
  1314. con = L2DatabaseFactory.getInstance().getConnection();
  1315. PreparedStatement statement;
  1316. statement = con.prepareStatement("SELECT olympiad_points FROM olympiad_nobles_eom WHERE charId = ?");
  1317. statement.setInt(1, objId);
  1318. ResultSet rs = statement.executeQuery();
  1319. if (rs.first())
  1320. result = rs.getInt(1);
  1321. rs.close();
  1322. statement.close();
  1323. }
  1324. catch (Exception e)
  1325. {
  1326. _log.log(Level.WARNING, "Could not load last olympiad points:", e);
  1327. }
  1328. finally
  1329. {
  1330. try
  1331. {
  1332. con.close();
  1333. }
  1334. catch (Exception e){}
  1335. }
  1336. return result;
  1337. }
  1338. public int getCompetitionDone(int objId)
  1339. {
  1340. if (_nobles.isEmpty())
  1341. return 0;
  1342. StatsSet noble = _nobles.get(objId);
  1343. if (noble == null)
  1344. return 0;
  1345. int points = noble.getInteger(COMP_DONE);
  1346. return points;
  1347. }
  1348. public int getCompetitionWon(int objId)
  1349. {
  1350. if (_nobles.isEmpty())
  1351. return 0;
  1352. StatsSet noble = _nobles.get(objId);
  1353. if (noble == null)
  1354. return 0;
  1355. int points = noble.getInteger(COMP_WON);
  1356. return points;
  1357. }
  1358. public int getCompetitionLost(int objId)
  1359. {
  1360. if (_nobles.isEmpty())
  1361. return 0;
  1362. StatsSet noble = _nobles.get(objId);
  1363. if (noble == null)
  1364. return 0;
  1365. int points = noble.getInteger(COMP_LOST);
  1366. return points;
  1367. }
  1368. protected void deleteNobles()
  1369. {
  1370. Connection con = null;
  1371. try
  1372. {
  1373. con = L2DatabaseFactory.getInstance().getConnection();
  1374. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  1375. statement.execute();
  1376. statement.close();
  1377. }
  1378. catch (SQLException e)
  1379. {
  1380. _log.warning("Olympiad System: Couldnt delete nobles from DB");
  1381. }
  1382. finally
  1383. {
  1384. try
  1385. {
  1386. con.close();
  1387. }
  1388. catch (Exception e)
  1389. {
  1390. e.printStackTrace();
  1391. }
  1392. }
  1393. _nobles.clear();
  1394. }
  1395. public static void sendMatchList(L2PcInstance player)
  1396. {
  1397. NpcHtmlMessage message = new NpcHtmlMessage(0);
  1398. message.setFile(Olympiad.OLYMPIAD_HTML_PATH + "olympiad_observe2.htm");
  1399. FastMap<Integer, String> matches = getInstance().getMatchList();
  1400. for (int i = 0; i < Olympiad.getStadiumCount(); i++)
  1401. {
  1402. int arenaId = i + 1;
  1403. String state = "Initial State";
  1404. String players = "&nbsp;";
  1405. if (matches.containsKey(i))
  1406. {
  1407. if (OlympiadGame._gameIsStarted)
  1408. state = "Playing";
  1409. else
  1410. state = "Standby";
  1411. players = matches.get(i);
  1412. }
  1413. message.replace("%state"+ arenaId +"%", state);
  1414. message.replace("%players"+ arenaId +"%", players);
  1415. }
  1416. player.sendPacket(message);
  1417. }
  1418. public static void bypassChangeArena(String command, L2PcInstance player)
  1419. {
  1420. if (!player.inObserverMode())
  1421. return;
  1422. String[] commands = command.split(" ");
  1423. int id = Integer.parseInt(commands[1]);
  1424. int arena = getSpectatorArena(player);
  1425. if (arena >= 0)
  1426. Olympiad.removeSpectator(arena, player);
  1427. else
  1428. return;
  1429. Olympiad.addSpectator(id, player, false);
  1430. }
  1431. @SuppressWarnings("synthetic-access")
  1432. private static class SingletonHolder
  1433. {
  1434. protected static final Olympiad _instance = new Olympiad();
  1435. }
  1436. }