Olympiad.java 48 KB

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