Olympiad.java 43 KB

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