Olympiad.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  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.FileWriter;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.text.SimpleDateFormat;
  29. import java.util.Calendar;
  30. import java.util.Date;
  31. import java.util.Map;
  32. import java.util.Properties;
  33. import java.util.concurrent.ScheduledFuture;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36. import javolution.util.FastMap;
  37. import net.sf.l2j.Config;
  38. import net.sf.l2j.L2DatabaseFactory;
  39. import net.sf.l2j.gameserver.Announcements;
  40. import net.sf.l2j.gameserver.ThreadPoolManager;
  41. import net.sf.l2j.gameserver.instancemanager.ZoneManager;
  42. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  43. import net.sf.l2j.gameserver.model.entity.Hero;
  44. import net.sf.l2j.gameserver.model.entity.TvTEvent;
  45. import net.sf.l2j.gameserver.network.SystemMessageId;
  46. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  47. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  48. import net.sf.l2j.gameserver.templates.StatsSet;
  49. import net.sf.l2j.gameserver.util.StringUtil;
  50. import net.sf.l2j.util.L2FastList;
  51. public class Olympiad
  52. {
  53. protected static final Logger _log = Logger.getLogger(Olympiad.class.getName());
  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 Map<Integer, Integer> _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 FastMap<Integer, Integer>();
  304. Map<Integer, Integer> tmpPlace = new FastMap<Integer, Integer>();
  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.keySet())
  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. /** End Olympiad Restrictions */
  458. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  459. {
  460. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  461. for (L2PcInstance participant : classed)
  462. {
  463. if (participant.getObjectId() == noble.getObjectId())
  464. {
  465. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_CLASS_MATCH_WAITING_LIST);
  466. sm.addPcName(noble);
  467. noble.sendPacket(sm);
  468. return false;
  469. }
  470. }
  471. }
  472. if (isRegisteredInComp(noble))
  473. {
  474. sm = new SystemMessage(SystemMessageId.C1_IS_ALREADY_REGISTERED_ON_THE_NON_CLASS_LIMITED_MATCH_WAITING_LIST);
  475. sm.addPcName(noble);
  476. noble.sendPacket(sm);
  477. return false;
  478. }
  479. if (!_nobles.containsKey(noble.getObjectId()))
  480. {
  481. StatsSet statDat = new StatsSet();
  482. statDat.set(CLASS_ID, noble.getClassId().getId());
  483. statDat.set(CHAR_NAME, noble.getName());
  484. statDat.set(POINTS, DEFAULT_POINTS);
  485. statDat.set(COMP_DONE, 0);
  486. statDat.set(COMP_WON, 0);
  487. statDat.set(COMP_LOST, 0);
  488. statDat.set(COMP_DRAWN, 0);
  489. statDat.set("to_save", true);
  490. _nobles.put(noble.getObjectId(), statDat);
  491. }
  492. if (classBased && getNoblePoints(noble.getObjectId()) < 3)
  493. {
  494. noble.sendMessage("Cant register when you have less than 3 points");
  495. return false;
  496. }
  497. if (!classBased && getNoblePoints(noble.getObjectId()) < 5)
  498. {
  499. noble.sendMessage("Cant register when you have less than 5 points");
  500. return false;
  501. }
  502. if (classBased)
  503. {
  504. if (_classBasedRegisters.containsKey(noble.getClassId().getId()))
  505. {
  506. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  507. classed.add(noble);
  508. _classBasedRegisters.remove(noble.getClassId().getId());
  509. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  510. }
  511. else
  512. {
  513. L2FastList<L2PcInstance> classed = new L2FastList<L2PcInstance>();
  514. classed.add(noble);
  515. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  516. }
  517. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_CLASSIFIED_GAMES);
  518. noble.sendPacket(sm);
  519. }
  520. else
  521. {
  522. _nonClassBasedRegisters.add(noble);
  523. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES);
  524. noble.sendPacket(sm);
  525. }
  526. return true;
  527. }
  528. protected static int getNobleCount()
  529. {
  530. return _nobles.size();
  531. }
  532. protected static StatsSet getNobleStats(int playerId)
  533. {
  534. return _nobles.get(playerId);
  535. }
  536. protected static synchronized void updateNobleStats(int playerId, StatsSet stats)
  537. {
  538. _nobles.remove(playerId);
  539. _nobles.put(playerId, stats);
  540. }
  541. protected static L2FastList<L2PcInstance> getRegisteredNonClassBased()
  542. {
  543. return _nonClassBasedRegisters;
  544. }
  545. protected static Map<Integer, L2FastList<L2PcInstance>> getRegisteredClassBased()
  546. {
  547. return _classBasedRegisters;
  548. }
  549. protected static L2FastList<Integer> hasEnoughRegisteredClassed()
  550. {
  551. L2FastList<Integer> result = new L2FastList<Integer>();
  552. for (Integer classList : getRegisteredClassBased().keySet())
  553. {
  554. if (getRegisteredClassBased().get(classList).size() >= Config.ALT_OLY_CLASSED)
  555. {
  556. result.add(classList);
  557. }
  558. }
  559. if (!result.isEmpty())
  560. {
  561. return result;
  562. }
  563. return null;
  564. }
  565. protected static boolean hasEnoughRegisteredNonClassed()
  566. {
  567. return Olympiad.getRegisteredNonClassBased().size() >= Config.ALT_OLY_NONCLASSED;
  568. }
  569. protected static void clearRegistered()
  570. {
  571. _nonClassBasedRegisters.clear();
  572. _classBasedRegisters.clear();
  573. }
  574. public boolean isRegistered(L2PcInstance noble)
  575. {
  576. boolean result = false;
  577. if (_nonClassBasedRegisters != null && _nonClassBasedRegisters.contains(noble))
  578. result = true;
  579. else if (_classBasedRegisters != null && _classBasedRegisters.containsKey(noble.getClassId().getId()))
  580. {
  581. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  582. if (classed != null && classed.contains(noble))
  583. result = true;
  584. }
  585. return result;
  586. }
  587. public boolean unRegisterNoble(L2PcInstance noble)
  588. {
  589. SystemMessage sm;
  590. /*
  591. * if (_compStarted) {
  592. * noble.sendMessage("Cant Unregister whilst competition is under way");
  593. * return false; }
  594. */
  595. if (!_inCompPeriod)
  596. {
  597. sm = new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_IS_NOT_CURRENTLY_IN_PROGRESS);
  598. noble.sendPacket(sm);
  599. return false;
  600. }
  601. if (!noble.isNoble())
  602. {
  603. sm = new SystemMessage(SystemMessageId.C1_DOES_NOT_MEET_REQUIREMENTS_ONLY_NOBLESS_CAN_PARTICIPATE_IN_THE_OLYMPIAD);
  604. sm.addString(noble.getName());
  605. noble.sendPacket(sm);
  606. return false;
  607. }
  608. if (!isRegistered(noble))
  609. {
  610. sm = new SystemMessage(SystemMessageId.YOU_HAVE_NOT_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_A_GAME);
  611. noble.sendPacket(sm);
  612. return false;
  613. }
  614. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  615. {
  616. if (game == null)
  617. continue;
  618. if (game._playerOneID == noble.getObjectId() || game._playerTwoID == noble.getObjectId())
  619. {
  620. noble.sendMessage("Can't deregister whilst you are already selected for a game");
  621. return false;
  622. }
  623. }
  624. if (_nonClassBasedRegisters.contains(noble))
  625. _nonClassBasedRegisters.remove(noble);
  626. else
  627. {
  628. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(noble.getClassId().getId());
  629. classed.remove(noble);
  630. _classBasedRegisters.remove(noble.getClassId().getId());
  631. _classBasedRegisters.put(noble.getClassId().getId(), classed);
  632. }
  633. sm = new SystemMessage(SystemMessageId.YOU_HAVE_BEEN_DELETED_FROM_THE_WAITING_LIST_OF_A_GAME);
  634. noble.sendPacket(sm);
  635. return true;
  636. }
  637. public void removeDisconnectedCompetitor(L2PcInstance player)
  638. {
  639. if (OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()) != null)
  640. OlympiadManager.getInstance().getOlympiadGame(player.getOlympiadGameId()).handleDisconnect(player);
  641. L2FastList<L2PcInstance> classed = _classBasedRegisters.get(player.getClassId().getId());
  642. if (_nonClassBasedRegisters.contains(player))
  643. _nonClassBasedRegisters.remove(player);
  644. else if (classed != null && classed.contains(player))
  645. {
  646. classed.remove(player);
  647. _classBasedRegisters.remove(player.getClassId().getId());
  648. _classBasedRegisters.put(player.getClassId().getId(), classed);
  649. }
  650. }
  651. public void notifyCompetitorDamage(L2PcInstance player, int damage, int gameId)
  652. {
  653. if (OlympiadManager.getInstance().getOlympiadGames().get(gameId) != null)
  654. OlympiadManager.getInstance().getOlympiadGames().get(gameId).addDamage(player, damage);
  655. }
  656. private void updateCompStatus()
  657. {
  658. // _compStarted = false;
  659. synchronized (this)
  660. {
  661. long milliToStart = getMillisToCompBegin();
  662. double numSecs = (milliToStart / 1000) % 60;
  663. double countDown = ((milliToStart / 1000) - numSecs) / 60;
  664. int numMins = (int) Math.floor(countDown % 60);
  665. countDown = (countDown - numMins) / 60;
  666. int numHours = (int) Math.floor(countDown % 24);
  667. int numDays = (int) Math.floor((countDown - numHours) / 24);
  668. _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins
  669. + " mins.");
  670. _log.info("Olympiad System: Event starts/started : " + _compStart.getTime());
  671. }
  672. _scheduledCompStart = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  673. public void run()
  674. {
  675. if (isOlympiadEnd())
  676. return;
  677. _inCompPeriod = true;
  678. OlympiadManager om = OlympiadManager.getInstance();
  679. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_STARTED));
  680. _log.info("Olympiad System: Olympiad Game Started");
  681. Thread olyCycle = new Thread(om);
  682. olyCycle.start();
  683. long regEnd = getMillisToCompEnd() - 600000;
  684. if (regEnd > 0)
  685. {
  686. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  687. public void run()
  688. {
  689. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.OLYMPIAD_REGISTRATION_PERIOD_ENDED));
  690. }
  691. }, regEnd);
  692. }
  693. _scheduledCompEnd = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  694. public void run()
  695. {
  696. if (isOlympiadEnd())
  697. return;
  698. _inCompPeriod = false;
  699. Announcements.getInstance().announceToAll(new SystemMessage(SystemMessageId.THE_OLYMPIAD_GAME_HAS_ENDED));
  700. _log.info("Olympiad System: Olympiad Game Ended");
  701. while (OlympiadGame._battleStarted)
  702. {
  703. try
  704. {
  705. // wait 1 minutes for end of pendings games
  706. Thread.sleep(60000);
  707. }
  708. catch (InterruptedException e)
  709. {
  710. }
  711. }
  712. saveOlympiadStatus();
  713. init();
  714. }
  715. }, getMillisToCompEnd());
  716. }
  717. }, getMillisToCompBegin());
  718. }
  719. private long getMillisToOlympiadEnd()
  720. {
  721. // if (_olympiadEnd > Calendar.getInstance().getTimeInMillis())
  722. return (_olympiadEnd - Calendar.getInstance().getTimeInMillis());
  723. // return 10L;
  724. }
  725. public void manualSelectHeroes()
  726. {
  727. if (_scheduledOlympiadEnd != null)
  728. _scheduledOlympiadEnd.cancel(true);
  729. _scheduledOlympiadEnd = ThreadPoolManager.getInstance().scheduleGeneral(new OlympiadEndTask(), 0);
  730. }
  731. protected long getMillisToValidationEnd()
  732. {
  733. if (_validationEnd > Calendar.getInstance().getTimeInMillis())
  734. return (_validationEnd - Calendar.getInstance().getTimeInMillis());
  735. return 10L;
  736. }
  737. public boolean isOlympiadEnd()
  738. {
  739. return (_period != 0);
  740. }
  741. protected void setNewOlympiadEnd()
  742. {
  743. SystemMessage sm = new SystemMessage(SystemMessageId.OLYMPIAD_PERIOD_S1_HAS_STARTED);
  744. sm.addNumber(_currentCycle);
  745. Announcements.getInstance().announceToAll(sm);
  746. Calendar currentTime = Calendar.getInstance();
  747. currentTime.add(Calendar.MONTH, 1);
  748. currentTime.set(Calendar.DAY_OF_MONTH, 1);
  749. currentTime.set(Calendar.AM_PM, Calendar.AM);
  750. currentTime.set(Calendar.HOUR, 12);
  751. currentTime.set(Calendar.MINUTE, 0);
  752. currentTime.set(Calendar.SECOND, 0);
  753. _olympiadEnd = currentTime.getTimeInMillis();
  754. Calendar nextChange = Calendar.getInstance();
  755. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  756. scheduleWeeklyChange();
  757. }
  758. public boolean inCompPeriod()
  759. {
  760. return _inCompPeriod;
  761. }
  762. private long getMillisToCompBegin()
  763. {
  764. if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() && _compEnd > Calendar.getInstance().getTimeInMillis())
  765. return 10L;
  766. if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  767. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  768. return setNewCompBegin();
  769. }
  770. private long setNewCompBegin()
  771. {
  772. _compStart = Calendar.getInstance();
  773. _compStart.set(Calendar.HOUR_OF_DAY, COMP_START);
  774. _compStart.set(Calendar.MINUTE, COMP_MIN);
  775. _compStart.add(Calendar.HOUR_OF_DAY, 24);
  776. _compEnd = _compStart.getTimeInMillis() + COMP_PERIOD;
  777. _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  778. return (_compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());
  779. }
  780. protected long getMillisToCompEnd()
  781. {
  782. // if (_compEnd > Calendar.getInstance().getTimeInMillis())
  783. return (_compEnd - Calendar.getInstance().getTimeInMillis());
  784. // return 10L;
  785. }
  786. private long getMillisToWeekChange()
  787. {
  788. if (_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  789. return (_nextWeeklyChange - Calendar.getInstance().getTimeInMillis());
  790. return 10L;
  791. }
  792. private void scheduleWeeklyChange()
  793. {
  794. _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable() {
  795. public void run()
  796. {
  797. addWeeklyPoints();
  798. _log.info("Olympiad System: Added weekly points to nobles");
  799. Calendar nextChange = Calendar.getInstance();
  800. _nextWeeklyChange = nextChange.getTimeInMillis() + WEEKLY_PERIOD;
  801. }
  802. }, getMillisToWeekChange(), WEEKLY_PERIOD);
  803. }
  804. protected synchronized void addWeeklyPoints()
  805. {
  806. if (_period == 1)
  807. return;
  808. for (Integer nobleId : _nobles.keySet())
  809. {
  810. StatsSet nobleInfo = _nobles.get(nobleId);
  811. int currentPoints = nobleInfo.getInteger(POINTS);
  812. currentPoints += WEEKLY_POINTS;
  813. nobleInfo.set(POINTS, currentPoints);
  814. updateNobleStats(nobleId, nobleInfo);
  815. }
  816. }
  817. public FastMap<Integer, String> getMatchList()
  818. {
  819. return OlympiadManager.getInstance().getAllTitles();
  820. }
  821. // returns the players for the given olympiad game Id
  822. public L2PcInstance[] getPlayers(int Id)
  823. {
  824. if (OlympiadManager.getInstance().getOlympiadGame(Id) == null)
  825. return null;
  826. else
  827. return OlympiadManager.getInstance().getOlympiadGame(Id).getPlayers();
  828. }
  829. public int getCurrentCycle()
  830. {
  831. return _currentCycle;
  832. }
  833. public static void addSpectator(int id, L2PcInstance spectator, boolean storeCoords)
  834. {
  835. if (getInstance().isRegisteredInComp(spectator))
  836. {
  837. spectator.sendPacket(new SystemMessage(SystemMessageId.WHILE_YOU_ARE_ON_THE_WAITING_LIST_YOU_ARE_NOT_ALLOWED_TO_WATCH_THE_GAME));
  838. return;
  839. }
  840. if (!TvTEvent.isInactive() && TvTEvent.isPlayerParticipant(spectator.getObjectId()))
  841. {
  842. spectator.sendMessage("You can not observe games while registered for TvT");
  843. return;
  844. }
  845. OlympiadManager.STADIUMS[id].addSpectator(id, spectator, storeCoords);
  846. }
  847. public static int getSpectatorArena(L2PcInstance player)
  848. {
  849. for (int i = 0; i < OlympiadManager.STADIUMS.length; i++)
  850. {
  851. if (OlympiadManager.STADIUMS[i].getSpectators().contains(player))
  852. return i;
  853. }
  854. return -1;
  855. }
  856. public static void removeSpectator(int id, L2PcInstance spectator)
  857. {
  858. OlympiadManager.STADIUMS[id].removeSpectator(spectator);
  859. }
  860. public L2FastList<L2PcInstance> getSpectators(int id)
  861. {
  862. if (OlympiadManager.getInstance().getOlympiadGame(id) == null)
  863. return null;
  864. return OlympiadManager.STADIUMS[id].getSpectators();
  865. }
  866. public Map<Integer, OlympiadGame> getOlympiadGames()
  867. {
  868. return OlympiadManager.getInstance().getOlympiadGames();
  869. }
  870. public boolean playerInStadia(L2PcInstance player)
  871. {
  872. return (ZoneManager.getInstance().getOlympiadStadium(player) != null);
  873. }
  874. public int[] getWaitingList()
  875. {
  876. int[] array = new int[2];
  877. if (!inCompPeriod())
  878. return null;
  879. int classCount = 0;
  880. if (_classBasedRegisters.size() != 0)
  881. for (L2FastList<L2PcInstance> classed : _classBasedRegisters.values())
  882. {
  883. classCount += classed.size();
  884. }
  885. array[0] = classCount;
  886. array[1] = _nonClassBasedRegisters.size();
  887. return array;
  888. }
  889. /**
  890. * Save noblesse data to database
  891. */
  892. protected synchronized void saveNobleData()
  893. {
  894. if (_nobles == null || _nobles.isEmpty())
  895. return;
  896. Connection con = null;
  897. try
  898. {
  899. con = L2DatabaseFactory.getInstance().getConnection();
  900. PreparedStatement statement;
  901. for (Integer nobleId : _nobles.keySet())
  902. {
  903. StatsSet nobleInfo = _nobles.get(nobleId);
  904. if (nobleInfo == null)
  905. continue;
  906. int charId = nobleId;
  907. int classId = nobleInfo.getInteger(CLASS_ID);
  908. int points = nobleInfo.getInteger(POINTS);
  909. int compDone = nobleInfo.getInteger(COMP_DONE);
  910. int compWon = nobleInfo.getInteger(COMP_WON);
  911. int compLost = nobleInfo.getInteger(COMP_LOST);
  912. int compDrawn = nobleInfo.getInteger(COMP_DRAWN);
  913. boolean toSave = nobleInfo.getBool("to_save");
  914. if (toSave)
  915. {
  916. statement = con.prepareStatement(OLYMPIAD_SAVE_NOBLES);
  917. statement.setInt(1, charId);
  918. statement.setInt(2, classId);
  919. statement.setInt(3, points);
  920. statement.setInt(4, compDone);
  921. statement.setInt(5, compWon);
  922. statement.setInt(6, compLost);
  923. statement.setInt(7, compDrawn);
  924. nobleInfo.set("to_save", false);
  925. updateNobleStats(nobleId, nobleInfo);
  926. }
  927. else
  928. {
  929. statement = con.prepareStatement(OLYMPIAD_UPDATE_NOBLES);
  930. statement.setInt(1, points);
  931. statement.setInt(2, compDone);
  932. statement.setInt(3, compWon);
  933. statement.setInt(4, compLost);
  934. statement.setInt(5, compDrawn);
  935. statement.setInt(6, charId);
  936. }
  937. statement.execute();
  938. statement.close();
  939. }
  940. }
  941. catch (SQLException e)
  942. {
  943. _log.log(Level.SEVERE, "Olympiad System: Failed to save noblesse data to database: ", e);
  944. }
  945. finally
  946. {
  947. try
  948. {
  949. con.close();
  950. }
  951. catch (Exception e)
  952. {
  953. e.printStackTrace();
  954. }
  955. }
  956. }
  957. /**
  958. * Save olympiad.properties file with current olympiad status and update noblesse table in database
  959. */
  960. public void saveOlympiadStatus()
  961. {
  962. saveNobleData();
  963. Connection con = null;
  964. try
  965. {
  966. con = L2DatabaseFactory.getInstance().getConnection();
  967. PreparedStatement statement = con.prepareStatement(OLYMPIAD_SAVE_DATA);
  968. statement.setInt(1, _currentCycle);
  969. statement.setInt(2, _period);
  970. statement.setLong(3, _olympiadEnd);
  971. statement.setLong(4, _validationEnd);
  972. statement.setLong(5, _nextWeeklyChange);
  973. statement.setInt(6, _currentCycle);
  974. statement.setInt(7, _period);
  975. statement.setLong(8, _olympiadEnd);
  976. statement.setLong(9, _validationEnd);
  977. statement.setLong(10, _nextWeeklyChange);
  978. statement.execute();
  979. statement.close();
  980. }
  981. catch (SQLException e)
  982. {
  983. _log.log(Level.SEVERE, "Olympiad System: Failed to save olympiad data to database: ", e);
  984. }
  985. finally
  986. {
  987. try
  988. {
  989. con.close();
  990. }
  991. catch (Exception e)
  992. {
  993. e.printStackTrace();
  994. }
  995. }
  996. /* Properties OlympiadProperties = new Properties();
  997. FileOutputStream fos = null;
  998. try
  999. {
  1000. fos = new FileOutputStream(new File("./" + OLYMPIAD_DATA_FILE));
  1001. OlympiadProperties.setProperty("CurrentCycle", String.valueOf(_currentCycle));
  1002. OlympiadProperties.setProperty("Period", String.valueOf(_period));
  1003. OlympiadProperties.setProperty("OlympiadEnd", String.valueOf(_olympiadEnd));
  1004. OlympiadProperties.setProperty("ValdationEnd", String.valueOf(_validationEnd));
  1005. OlympiadProperties.setProperty("NextWeeklyChange", String.valueOf(_nextWeeklyChange));
  1006. OlympiadProperties.store(fos, "Olympiad Properties");
  1007. }
  1008. catch (Exception e)
  1009. {
  1010. _log.log(Level.WARNING, "Olympiad System: Unable to save olympiad properties to file: ", e);
  1011. }
  1012. finally
  1013. {
  1014. try
  1015. {
  1016. fos.close();
  1017. }
  1018. catch (Exception e)
  1019. {
  1020. }
  1021. }*/
  1022. }
  1023. protected void updateMonthlyData()
  1024. {
  1025. Connection con = null;
  1026. try
  1027. {
  1028. con = L2DatabaseFactory.getInstance().getConnection();
  1029. PreparedStatement statement;
  1030. statement = con.prepareStatement(OLYMPIAD_MONTH_CLEAR);
  1031. statement.execute();
  1032. statement.close();
  1033. statement = con.prepareStatement(OLYMPIAD_MONTH_CREATE);
  1034. statement.execute();
  1035. statement.close();
  1036. }
  1037. catch (SQLException e)
  1038. {
  1039. _log.log(Level.SEVERE, "Olympiad System: Failed to update monthly noblese data: ", e);
  1040. }
  1041. finally
  1042. {
  1043. try
  1044. {
  1045. con.close();
  1046. }
  1047. catch (Exception e)
  1048. {
  1049. }
  1050. }
  1051. }
  1052. protected void sortHerosToBe()
  1053. {
  1054. if (_period != 1)
  1055. return;
  1056. if (_nobles != null)
  1057. {
  1058. for (Integer nobleId : _nobles.keySet())
  1059. {
  1060. StatsSet nobleInfo = _nobles.get(nobleId);
  1061. if (nobleInfo == null)
  1062. continue;
  1063. int charId = nobleId;
  1064. int classId = nobleInfo.getInteger(CLASS_ID);
  1065. String charName = nobleInfo.getString(CHAR_NAME);
  1066. int points = nobleInfo.getInteger(POINTS);
  1067. int compDone = nobleInfo.getInteger(COMP_DONE);
  1068. logResult(charName, "", Double.valueOf(charId), Double.valueOf(classId), compDone, points, "noble-charId-classId-compdone-points", 0, "");
  1069. }
  1070. }
  1071. _heroesToBe = new L2FastList<StatsSet>();
  1072. Connection con = null;
  1073. try
  1074. {
  1075. con = L2DatabaseFactory.getInstance().getConnection();
  1076. PreparedStatement statement;
  1077. ResultSet rset;
  1078. StatsSet hero;
  1079. L2FastList<StatsSet> soulHounds = new L2FastList<StatsSet>();
  1080. for (int i = 0; i < HERO_IDS.length; i++)
  1081. {
  1082. statement = con.prepareStatement(OLYMPIAD_GET_HEROS);
  1083. statement.setInt(1, HERO_IDS[i]);
  1084. rset = statement.executeQuery();
  1085. if (rset.next())
  1086. {
  1087. hero = new StatsSet();
  1088. hero.set(CLASS_ID, HERO_IDS[i]);
  1089. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1090. hero.set(CHAR_NAME, rset.getString(CHAR_NAME));
  1091. if (HERO_IDS[i] == 132 || HERO_IDS[i] == 133) // Male & Female Soulhounds rank as one hero class
  1092. {
  1093. hero = _nobles.get(hero.getInteger(CHAR_ID));
  1094. hero.set(CHAR_ID, rset.getInt(CHAR_ID));
  1095. soulHounds.add(hero);
  1096. }
  1097. else
  1098. {
  1099. logResult(hero.getString(CHAR_NAME), "", hero.getDouble(CHAR_ID), hero.getDouble(CLASS_ID), 0, 0, "awarded hero", 0, "");
  1100. _heroesToBe.add(hero);
  1101. }
  1102. }
  1103. statement.close();
  1104. rset.close();
  1105. }
  1106. switch (soulHounds.size())
  1107. {
  1108. case 0:
  1109. {
  1110. break;
  1111. }
  1112. case 1:
  1113. {
  1114. hero = new StatsSet();
  1115. StatsSet winner = soulHounds.get(0);
  1116. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1117. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1118. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1119. logResult(hero.getString(CHAR_NAME), "", hero.getDouble(CHAR_ID), hero.getDouble(CLASS_ID), 0, 0, "awarded hero", 0, "");
  1120. _heroesToBe.add(hero);
  1121. break;
  1122. }
  1123. case 2:
  1124. {
  1125. hero = new StatsSet();
  1126. StatsSet winner;
  1127. StatsSet hero1 = soulHounds.get(0);
  1128. StatsSet hero2 = soulHounds.get(1);
  1129. int hero1Points = hero1.getInteger(POINTS);
  1130. int hero2Points = hero2.getInteger(POINTS);
  1131. int hero1Comps = hero1.getInteger(COMP_DONE);
  1132. int hero2Comps = hero2.getInteger(COMP_DONE);
  1133. if (hero1Points > hero2Points)
  1134. winner = hero1;
  1135. else if (hero2Points > hero1Points)
  1136. winner = hero2;
  1137. else
  1138. {
  1139. if (hero1Comps > hero2Comps)
  1140. winner = hero1;
  1141. else
  1142. winner = hero2;
  1143. }
  1144. hero.set(CLASS_ID, winner.getInteger(CLASS_ID));
  1145. hero.set(CHAR_ID, winner.getInteger(CHAR_ID));
  1146. hero.set(CHAR_NAME, winner.getString(CHAR_NAME));
  1147. logResult(hero.getString(CHAR_NAME), "", hero.getDouble(CHAR_ID), hero.getDouble(CLASS_ID), 0, 0, "awarded hero", 0, "");
  1148. _heroesToBe.add(hero);
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. catch (SQLException e)
  1154. {
  1155. _log.warning("Olympiad System: Couldnt load heros from DB");
  1156. }
  1157. finally
  1158. {
  1159. try
  1160. {
  1161. con.close();
  1162. }
  1163. catch (Exception e)
  1164. {
  1165. e.printStackTrace();
  1166. }
  1167. }
  1168. }
  1169. public L2FastList<String> getClassLeaderBoard(int classId)
  1170. {
  1171. // if (_period != 1) return;
  1172. L2FastList<String> names = new L2FastList<String>();
  1173. Connection con = null;
  1174. try
  1175. {
  1176. con = L2DatabaseFactory.getInstance().getConnection();
  1177. PreparedStatement statement;
  1178. ResultSet rset;
  1179. if (Config.ALT_OLY_SHOW_MONTHLY_WINNERS)
  1180. statement = con.prepareStatement(GET_EACH_CLASS_LEADER);
  1181. else
  1182. statement = con.prepareStatement(GET_EACH_CLASS_LEADER_CURRENT);
  1183. statement.setInt(1, classId);
  1184. rset = statement.executeQuery();
  1185. while (rset.next())
  1186. {
  1187. names.add(rset.getString(CHAR_NAME));
  1188. }
  1189. if (classId == 132) // Male & Female SoulHounds are ranked together
  1190. {
  1191. statement.setInt(1, 133);
  1192. rset = statement.executeQuery();
  1193. while (rset.next())
  1194. {
  1195. names.add(rset.getString(CHAR_NAME));
  1196. }
  1197. }
  1198. statement.close();
  1199. rset.close();
  1200. return names;
  1201. }
  1202. catch (SQLException e)
  1203. {
  1204. _log.warning("Olympiad System: Couldnt load olympiad leaders from DB");
  1205. }
  1206. finally
  1207. {
  1208. try
  1209. {
  1210. con.close();
  1211. }
  1212. catch (Exception e)
  1213. {
  1214. e.printStackTrace();
  1215. }
  1216. }
  1217. return names;
  1218. }
  1219. public int getNoblessePasses(L2PcInstance player)
  1220. {
  1221. if (_period != 1 || _noblesRank.isEmpty())
  1222. return 0;
  1223. int objId = player.getObjectId();
  1224. if (!_noblesRank.containsKey(objId))
  1225. return 0;
  1226. StatsSet noble = _nobles.get(objId);
  1227. if (noble.getInteger(POINTS) == 0)
  1228. return 0;
  1229. int rank = _noblesRank.get(objId);
  1230. int points = (player.isHero() ? Config.ALT_OLY_HERO_POINTS : 0);
  1231. switch (rank)
  1232. {
  1233. case 1:
  1234. points += Config.ALT_OLY_RANK1_POINTS;
  1235. break;
  1236. case 2:
  1237. points += Config.ALT_OLY_RANK2_POINTS;
  1238. break;
  1239. case 3:
  1240. points += Config.ALT_OLY_RANK3_POINTS;
  1241. break;
  1242. case 4:
  1243. points += Config.ALT_OLY_RANK4_POINTS;
  1244. break;
  1245. default:
  1246. points += Config.ALT_OLY_RANK5_POINTS;
  1247. }
  1248. noble.set(POINTS, 0);
  1249. updateNobleStats(objId, noble);
  1250. points *= Config.ALT_OLY_GP_PER_POINT;
  1251. return points;
  1252. }
  1253. public boolean isRegisteredInComp(L2PcInstance player)
  1254. {
  1255. boolean result = isRegistered(player);
  1256. if (_inCompPeriod)
  1257. {
  1258. for (OlympiadGame game : OlympiadManager.getInstance().getOlympiadGames().values())
  1259. {
  1260. if ((game._playerOneID == player.getObjectId()) || (game._playerTwoID == player.getObjectId()))
  1261. {
  1262. result = true;
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. return result;
  1268. }
  1269. public int getNoblePoints(int objId)
  1270. {
  1271. if (_nobles.isEmpty())
  1272. return 0;
  1273. StatsSet noble = _nobles.get(objId);
  1274. if (noble == null)
  1275. return 0;
  1276. int points = noble.getInteger(POINTS);
  1277. return points;
  1278. }
  1279. public int getCompetitionDone(int objId)
  1280. {
  1281. if (_nobles.isEmpty())
  1282. return 0;
  1283. StatsSet noble = _nobles.get(objId);
  1284. if (noble == null)
  1285. return 0;
  1286. int points = noble.getInteger(COMP_DONE);
  1287. return points;
  1288. }
  1289. public int getCompetitionWon(int objId)
  1290. {
  1291. if (_nobles.isEmpty())
  1292. return 0;
  1293. StatsSet noble = _nobles.get(objId);
  1294. if (noble == null)
  1295. return 0;
  1296. int points = noble.getInteger(COMP_WON);
  1297. return points;
  1298. }
  1299. public int getCompetitionLost(int objId)
  1300. {
  1301. if (_nobles.isEmpty())
  1302. return 0;
  1303. StatsSet noble = _nobles.get(objId);
  1304. if (noble == null)
  1305. return 0;
  1306. int points = noble.getInteger(COMP_LOST);
  1307. return points;
  1308. }
  1309. protected void deleteNobles()
  1310. {
  1311. Connection con = null;
  1312. try
  1313. {
  1314. con = L2DatabaseFactory.getInstance().getConnection();
  1315. PreparedStatement statement = con.prepareStatement(OLYMPIAD_DELETE_ALL);
  1316. statement.execute();
  1317. statement.close();
  1318. }
  1319. catch (SQLException e)
  1320. {
  1321. _log.warning("Olympiad System: Couldnt delete nobles from DB");
  1322. }
  1323. finally
  1324. {
  1325. try
  1326. {
  1327. con.close();
  1328. }
  1329. catch (Exception e)
  1330. {
  1331. e.printStackTrace();
  1332. }
  1333. }
  1334. _nobles.clear();
  1335. }
  1336. /**
  1337. * Logs result of Olympiad to a csv file.
  1338. *
  1339. * @param playerOne
  1340. * @param playerTwo
  1341. * @param p1hp
  1342. * @param p2hp
  1343. * @param p1dmg
  1344. * @param p2dmg
  1345. * @param result
  1346. * @param points
  1347. */
  1348. public static synchronized void logResult(String playerOne, String playerTwo, Double p1hp, Double p2hp, int p1dmg, int p2dmg,
  1349. String result, int points, String classed)
  1350. {
  1351. if (!Config.ALT_OLY_LOG_FIGHTS)
  1352. return;
  1353. SimpleDateFormat formatter;
  1354. formatter = new SimpleDateFormat("dd/MM/yyyy H:mm:ss");
  1355. String date = formatter.format(new Date());
  1356. FileWriter save = null;
  1357. try
  1358. {
  1359. File file = new File("log/olympiad.csv");
  1360. boolean writeHead = !file.exists();
  1361. save = new FileWriter(file, true);
  1362. if (writeHead)
  1363. {
  1364. String header = "Date,Player1,Player2,Player1 HP,Player2 HP,Player1 Damage,Player2 Damage,Result,Points,Classed\r\n";
  1365. save.write(header);
  1366. }
  1367. String out = date + "," + playerOne + "," + playerTwo + "," + p1hp + "," + p2hp + "," + p1dmg + "," + p2dmg + "," + result
  1368. + "," + points + "," + classed + "\r\n";
  1369. save.write(out);
  1370. }
  1371. catch (IOException e)
  1372. {
  1373. _log.log(Level.WARNING, "Olympiad System: Olympiad log could not be saved: ", e);
  1374. }
  1375. finally
  1376. {
  1377. try
  1378. {
  1379. save.close();
  1380. }
  1381. catch (Exception e)
  1382. {
  1383. }
  1384. }
  1385. }
  1386. public static void sendMatchList(L2PcInstance player)
  1387. {
  1388. NpcHtmlMessage message = new NpcHtmlMessage(0);
  1389. final StringBuilder replyMSG = StringUtil.startAppend(200 + Olympiad.getStadiumCount() * 200, "<html><body>"
  1390. + "<center><br>Grand Olympiad Game View<table width=270 border=0 bgcolor=\"000000\">"
  1391. + "<tr><td fixwidth=30>NO.</td><td fixwidth=60>Status</td><td>Player1 / Player2</td></tr>");
  1392. FastMap<Integer, String> matches = getInstance().getMatchList();
  1393. for (int i = 0; i < Olympiad.getStadiumCount(); i++)
  1394. {
  1395. int arenaID = i + 1;
  1396. String players = "&nbsp;";
  1397. String state = "Initial State";
  1398. if (matches.containsKey(i))
  1399. {
  1400. state = "In Progress";
  1401. players = matches.get(i);
  1402. }
  1403. StringUtil.append(replyMSG, "<tr><td fixwidth=30><a action=\"bypass -h OlympiadArenaChange ", String.valueOf(i), "\">", String.valueOf(arenaID), "</a></td><td fixwidth=60>", state, "</td><td>", players, "</td></tr>");
  1404. }
  1405. replyMSG.append("</table></center></body></html>");
  1406. message.setHtml(replyMSG.toString());
  1407. player.sendPacket(message);
  1408. }
  1409. public static void bypassChangeArena(String command, L2PcInstance player)
  1410. {
  1411. String[] commands = command.split(" ");
  1412. int id = Integer.parseInt(commands[1]);
  1413. int arena = getSpectatorArena(player);
  1414. if (arena >= 0)
  1415. Olympiad.removeSpectator(arena, player);
  1416. Olympiad.addSpectator(id, player, false);
  1417. }
  1418. @SuppressWarnings("synthetic-access")
  1419. private static class SingletonHolder
  1420. {
  1421. protected static final Olympiad _instance = new Olympiad();
  1422. }
  1423. }