Olympiad.java 48 KB

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