Olympiad.java 42 KB

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