Olympiad.java 47 KB

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