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