Olympiad.java 45 KB

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