SevenSigns.java 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Calendar;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Map.Entry;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import javolution.util.FastMap;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.L2DatabaseFactory;
  34. import com.l2jserver.gameserver.instancemanager.CastleManager;
  35. import com.l2jserver.gameserver.model.AutoSpawnHandler;
  36. import com.l2jserver.gameserver.model.AutoSpawnHandler.AutoSpawnInstance;
  37. import com.l2jserver.gameserver.model.L2World;
  38. import com.l2jserver.gameserver.model.StatsSet;
  39. import com.l2jserver.gameserver.model.TeleportWhereType;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  41. import com.l2jserver.gameserver.model.entity.Castle;
  42. import com.l2jserver.gameserver.model.interfaces.IProcedure;
  43. import com.l2jserver.gameserver.model.skills.CommonSkill;
  44. import com.l2jserver.gameserver.network.SystemMessageId;
  45. import com.l2jserver.gameserver.network.serverpackets.SSQInfo;
  46. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  47. import com.l2jserver.gameserver.util.Broadcast;
  48. /**
  49. * Seven Signs engine.
  50. * @author Tempy
  51. */
  52. public class SevenSigns
  53. {
  54. protected static final Logger _log = Logger.getLogger(SevenSigns.class.getName());
  55. // Basic Seven Signs Constants \\
  56. public static final String SEVEN_SIGNS_HTML_PATH = "data/html/seven_signs/";
  57. public static final int CABAL_NULL = 0;
  58. public static final int CABAL_DUSK = 1;
  59. public static final int CABAL_DAWN = 2;
  60. public static final int SEAL_NULL = 0;
  61. public static final int SEAL_AVARICE = 1;
  62. public static final int SEAL_GNOSIS = 2;
  63. public static final int SEAL_STRIFE = 3;
  64. public static final int PERIOD_COMP_RECRUITING = 0;
  65. public static final int PERIOD_COMPETITION = 1;
  66. public static final int PERIOD_COMP_RESULTS = 2;
  67. public static final int PERIOD_SEAL_VALIDATION = 3;
  68. public static final int PERIOD_START_HOUR = 18;
  69. public static final int PERIOD_START_MINS = 00;
  70. public static final int PERIOD_START_DAY = Calendar.MONDAY;
  71. // The quest event and seal validation periods last for approximately one week
  72. // with a 15 minutes "interval" period sandwiched between them.
  73. public static final int PERIOD_MINOR_LENGTH = 900000;
  74. public static final int PERIOD_MAJOR_LENGTH = 604800000 - PERIOD_MINOR_LENGTH;
  75. public static final int RECORD_SEVEN_SIGNS_ID = 5707;
  76. public static final int RECORD_SEVEN_SIGNS_COST = 500;
  77. // NPC Related Constants \\
  78. public static final int ORATOR_NPC_ID = 31094;
  79. public static final int PREACHER_NPC_ID = 31093;
  80. public static final int MAMMON_MERCHANT_ID = 31113;
  81. public static final int MAMMON_BLACKSMITH_ID = 31126;
  82. public static final int MAMMON_MARKETEER_ID = 31092;
  83. public static final int LILITH_NPC_ID = 25283;
  84. public static final int ANAKIM_NPC_ID = 25286;
  85. public static final int CREST_OF_DAWN_ID = 31170;
  86. public static final int CREST_OF_DUSK_ID = 31171;
  87. // Seal Stone Related Constants
  88. public static final int SEAL_STONE_BLUE_ID = 6360;
  89. public static final int SEAL_STONE_GREEN_ID = 6361;
  90. public static final int SEAL_STONE_RED_ID = 6362;
  91. public static final int[] SEAL_STONE_IDS =
  92. {
  93. SEAL_STONE_BLUE_ID,
  94. SEAL_STONE_GREEN_ID,
  95. SEAL_STONE_RED_ID
  96. };
  97. public static final int SEAL_STONE_BLUE_VALUE = 3;
  98. public static final int SEAL_STONE_GREEN_VALUE = 5;
  99. public static final int SEAL_STONE_RED_VALUE = 10;
  100. public static final int BLUE_CONTRIB_POINTS = 3;
  101. public static final int GREEN_CONTRIB_POINTS = 5;
  102. public static final int RED_CONTRIB_POINTS = 10;
  103. private final Calendar _nextPeriodChange = Calendar.getInstance();
  104. protected int _activePeriod;
  105. protected int _currentCycle;
  106. protected double _dawnStoneScore;
  107. protected double _duskStoneScore;
  108. protected int _dawnFestivalScore;
  109. protected int _duskFestivalScore;
  110. protected int _compWinner;
  111. protected int _previousWinner;
  112. protected Calendar _lastSave = Calendar.getInstance();
  113. protected Map<Integer, StatsSet> _signsPlayerData;
  114. private final Map<Integer, Integer> _signsSealOwners;
  115. private final Map<Integer, Integer> _signsDuskSealTotals;
  116. private final Map<Integer, Integer> _signsDawnSealTotals;
  117. private AutoSpawnInstance _merchantSpawn;
  118. private AutoSpawnInstance _blacksmithSpawn;
  119. private AutoSpawnInstance _lilithSpawn;
  120. private AutoSpawnInstance _anakimSpawn;
  121. private Map<Integer, AutoSpawnInstance> _crestofdawnspawns;
  122. private Map<Integer, AutoSpawnInstance> _crestofduskspawns;
  123. private Map<Integer, AutoSpawnInstance> _oratorSpawns;
  124. private Map<Integer, AutoSpawnInstance> _preacherSpawns;
  125. private Map<Integer, AutoSpawnInstance> _marketeerSpawns;
  126. private static final String LOAD_DATA = "SELECT charId, cabal, seal, red_stones, green_stones, blue_stones, " + "ancient_adena_amount, contribution_score FROM seven_signs";
  127. private static final String LOAD_STATUS = "SELECT * FROM seven_signs_status WHERE id=0";
  128. private static final String INSERT_PLAYER = "INSERT INTO seven_signs (charId, cabal, seal) VALUES (?,?,?)";
  129. private static final String UPDATE_PLAYER = "UPDATE seven_signs SET cabal=?, seal=?, red_stones=?, green_stones=?, blue_stones=?, " + "ancient_adena_amount=?, contribution_score=? WHERE charId=?";
  130. private static final String UPDATE_STATUS = "UPDATE seven_signs_status SET current_cycle=?, active_period=?, previous_winner=?, " + "dawn_stone_score=?, dawn_festival_score=?, dusk_stone_score=?, dusk_festival_score=?, " + "avarice_owner=?, gnosis_owner=?, strife_owner=?, avarice_dawn_score=?, gnosis_dawn_score=?, " + "strife_dawn_score=?, avarice_dusk_score=?, gnosis_dusk_score=?, strife_dusk_score=?, " + "festival_cycle=?, accumulated_bonus0=?, accumulated_bonus1=?, accumulated_bonus2=?," + "accumulated_bonus3=?, accumulated_bonus4=?, date=? WHERE id=0";
  131. protected SevenSigns()
  132. {
  133. _signsPlayerData = new FastMap<>();
  134. _signsSealOwners = new FastMap<>();
  135. _signsDuskSealTotals = new FastMap<>();
  136. _signsDawnSealTotals = new FastMap<>();
  137. try
  138. {
  139. restoreSevenSignsData();
  140. }
  141. catch (Exception e)
  142. {
  143. _log.log(Level.SEVERE, "SevenSigns: Failed to load configuration: " + e.getMessage(), e);
  144. }
  145. _log.info("SevenSigns: Currently in the " + getCurrentPeriodName() + " period!");
  146. initializeSeals();
  147. if (isSealValidationPeriod())
  148. {
  149. if (getCabalHighestScore() == CABAL_NULL)
  150. {
  151. _log.info("SevenSigns: The competition ended with a tie last week.");
  152. }
  153. else
  154. {
  155. _log.info("SevenSigns: The " + getCabalName(getCabalHighestScore()) + " were victorious last week.");
  156. }
  157. }
  158. else if (getCabalHighestScore() == CABAL_NULL)
  159. {
  160. _log.info("SevenSigns: The competition, if the current trend continues, will end in a tie this week.");
  161. }
  162. else
  163. {
  164. _log.info("SevenSigns: The " + getCabalName(getCabalHighestScore()) + " are in the lead this week.");
  165. }
  166. long milliToChange = 0;
  167. if (isNextPeriodChangeInPast())
  168. {
  169. _log.info("SevenSigns: Next period change was in the past (server was offline), changing periods now!");
  170. }
  171. else
  172. {
  173. setCalendarForNextPeriodChange();
  174. milliToChange = getMilliToPeriodChange();
  175. }
  176. // Schedule a time for the next period change.
  177. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  178. ThreadPoolManager.getInstance().scheduleGeneral(sspc, milliToChange);
  179. // Thanks to http://rainbow.arch.scriptmania.com/scripts/timezone_countdown.html for help with this.
  180. double numSecs = (milliToChange / 1000) % 60;
  181. double countDown = ((milliToChange / 1000.0) - numSecs) / 60;
  182. int numMins = (int) Math.floor(countDown % 60);
  183. countDown = (countDown - numMins) / 60;
  184. int numHours = (int) Math.floor(countDown % 24);
  185. int numDays = (int) Math.floor((countDown - numHours) / 24);
  186. _log.info("SevenSigns: Next period begins in " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  187. }
  188. private boolean isNextPeriodChangeInPast()
  189. {
  190. Calendar lastPeriodChange = Calendar.getInstance();
  191. switch (getCurrentPeriod())
  192. {
  193. case PERIOD_SEAL_VALIDATION:
  194. case PERIOD_COMPETITION:
  195. lastPeriodChange.set(Calendar.DAY_OF_WEEK, PERIOD_START_DAY);
  196. lastPeriodChange.set(Calendar.HOUR_OF_DAY, PERIOD_START_HOUR);
  197. lastPeriodChange.set(Calendar.MINUTE, PERIOD_START_MINS);
  198. lastPeriodChange.set(Calendar.SECOND, 0);
  199. // if we hit next week, just turn back 1 week
  200. if (Calendar.getInstance().before(lastPeriodChange))
  201. {
  202. lastPeriodChange.add(Calendar.HOUR, -24 * 7);
  203. }
  204. break;
  205. case PERIOD_COMP_RECRUITING:
  206. case PERIOD_COMP_RESULTS:
  207. // because of the short duration of this period, just check it from last save
  208. lastPeriodChange.setTimeInMillis(_lastSave.getTimeInMillis() + PERIOD_MINOR_LENGTH);
  209. break;
  210. }
  211. // because of previous "date" column usage, check only if it already contains usable data for us
  212. if ((_lastSave.getTimeInMillis() > 7) && _lastSave.before(lastPeriodChange))
  213. {
  214. return true;
  215. }
  216. return false;
  217. }
  218. /**
  219. * Registers all random spawns and auto-chats for Seven Signs NPCs, along with spawns for the Preachers of Doom and Orators of Revelations at the beginning of the Seal Validation period.
  220. */
  221. public void spawnSevenSignsNPC()
  222. {
  223. _merchantSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(MAMMON_MERCHANT_ID, false);
  224. _blacksmithSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(MAMMON_BLACKSMITH_ID, false);
  225. _marketeerSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(MAMMON_MARKETEER_ID);
  226. _lilithSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(LILITH_NPC_ID, false);
  227. _anakimSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(ANAKIM_NPC_ID, false);
  228. _crestofdawnspawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(CREST_OF_DAWN_ID);
  229. _crestofduskspawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(CREST_OF_DUSK_ID);
  230. _oratorSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(ORATOR_NPC_ID);
  231. _preacherSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(PREACHER_NPC_ID);
  232. if (isSealValidationPeriod() || isCompResultsPeriod())
  233. {
  234. for (AutoSpawnInstance spawnInst : _marketeerSpawns.values())
  235. {
  236. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  237. }
  238. if ((getSealOwner(SEAL_GNOSIS) == getCabalHighestScore()) && (getSealOwner(SEAL_GNOSIS) != CABAL_NULL))
  239. {
  240. if (!Config.ANNOUNCE_MAMMON_SPAWN)
  241. {
  242. _blacksmithSpawn.setBroadcast(false);
  243. }
  244. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(_blacksmithSpawn.getObjectId(), true).isSpawnActive())
  245. {
  246. AutoSpawnHandler.getInstance().setSpawnActive(_blacksmithSpawn, true);
  247. }
  248. for (AutoSpawnInstance spawnInst : _oratorSpawns.values())
  249. {
  250. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(spawnInst.getObjectId(), true).isSpawnActive())
  251. {
  252. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  253. }
  254. }
  255. for (AutoSpawnInstance spawnInst : _preacherSpawns.values())
  256. {
  257. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(spawnInst.getObjectId(), true).isSpawnActive())
  258. {
  259. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  260. }
  261. }
  262. }
  263. else
  264. {
  265. AutoSpawnHandler.getInstance().setSpawnActive(_blacksmithSpawn, false);
  266. for (AutoSpawnInstance spawnInst : _oratorSpawns.values())
  267. {
  268. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, false);
  269. }
  270. for (AutoSpawnInstance spawnInst : _preacherSpawns.values())
  271. {
  272. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, false);
  273. }
  274. }
  275. if ((getSealOwner(SEAL_AVARICE) == getCabalHighestScore()) && (getSealOwner(SEAL_AVARICE) != CABAL_NULL))
  276. {
  277. if (!Config.ANNOUNCE_MAMMON_SPAWN)
  278. {
  279. _merchantSpawn.setBroadcast(false);
  280. }
  281. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(_merchantSpawn.getObjectId(), true).isSpawnActive())
  282. {
  283. AutoSpawnHandler.getInstance().setSpawnActive(_merchantSpawn, true);
  284. }
  285. switch (getCabalHighestScore())
  286. {
  287. case CABAL_DAWN:
  288. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(_lilithSpawn.getObjectId(), true).isSpawnActive())
  289. {
  290. AutoSpawnHandler.getInstance().setSpawnActive(_lilithSpawn, true);
  291. }
  292. AutoSpawnHandler.getInstance().setSpawnActive(_anakimSpawn, false);
  293. for (AutoSpawnInstance dawnCrest : _crestofdawnspawns.values())
  294. {
  295. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(dawnCrest.getObjectId(), true).isSpawnActive())
  296. {
  297. AutoSpawnHandler.getInstance().setSpawnActive(dawnCrest, true);
  298. }
  299. }
  300. for (AutoSpawnInstance duskCrest : _crestofduskspawns.values())
  301. {
  302. AutoSpawnHandler.getInstance().setSpawnActive(duskCrest, false);
  303. }
  304. break;
  305. case CABAL_DUSK:
  306. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(_anakimSpawn.getObjectId(), true).isSpawnActive())
  307. {
  308. AutoSpawnHandler.getInstance().setSpawnActive(_anakimSpawn, true);
  309. }
  310. AutoSpawnHandler.getInstance().setSpawnActive(_lilithSpawn, false);
  311. for (AutoSpawnInstance duskCrest : _crestofduskspawns.values())
  312. {
  313. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(duskCrest.getObjectId(), true).isSpawnActive())
  314. {
  315. AutoSpawnHandler.getInstance().setSpawnActive(duskCrest, true);
  316. }
  317. }
  318. for (AutoSpawnInstance dawnCrest : _crestofdawnspawns.values())
  319. {
  320. AutoSpawnHandler.getInstance().setSpawnActive(dawnCrest, false);
  321. }
  322. break;
  323. }
  324. }
  325. else
  326. {
  327. AutoSpawnHandler.getInstance().setSpawnActive(_merchantSpawn, false);
  328. AutoSpawnHandler.getInstance().setSpawnActive(_lilithSpawn, false);
  329. AutoSpawnHandler.getInstance().setSpawnActive(_anakimSpawn, false);
  330. for (AutoSpawnInstance dawnCrest : _crestofdawnspawns.values())
  331. {
  332. AutoSpawnHandler.getInstance().setSpawnActive(dawnCrest, false);
  333. }
  334. for (AutoSpawnInstance duskCrest : _crestofduskspawns.values())
  335. {
  336. AutoSpawnHandler.getInstance().setSpawnActive(duskCrest, false);
  337. }
  338. }
  339. }
  340. else
  341. {
  342. AutoSpawnHandler.getInstance().setSpawnActive(_merchantSpawn, false);
  343. AutoSpawnHandler.getInstance().setSpawnActive(_blacksmithSpawn, false);
  344. AutoSpawnHandler.getInstance().setSpawnActive(_lilithSpawn, false);
  345. AutoSpawnHandler.getInstance().setSpawnActive(_anakimSpawn, false);
  346. for (AutoSpawnInstance dawnCrest : _crestofdawnspawns.values())
  347. {
  348. AutoSpawnHandler.getInstance().setSpawnActive(dawnCrest, false);
  349. }
  350. for (AutoSpawnInstance duskCrest : _crestofduskspawns.values())
  351. {
  352. AutoSpawnHandler.getInstance().setSpawnActive(duskCrest, false);
  353. }
  354. for (AutoSpawnInstance spawnInst : _oratorSpawns.values())
  355. {
  356. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, false);
  357. }
  358. for (AutoSpawnInstance spawnInst : _preacherSpawns.values())
  359. {
  360. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, false);
  361. }
  362. for (AutoSpawnInstance spawnInst : _marketeerSpawns.values())
  363. {
  364. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, false);
  365. }
  366. }
  367. }
  368. public static SevenSigns getInstance()
  369. {
  370. return SingletonHolder._instance;
  371. }
  372. public static long calcContributionScore(long blueCount, long greenCount, long redCount)
  373. {
  374. long contrib = blueCount * BLUE_CONTRIB_POINTS;
  375. contrib += greenCount * GREEN_CONTRIB_POINTS;
  376. contrib += redCount * RED_CONTRIB_POINTS;
  377. return contrib;
  378. }
  379. public static long calcAncientAdenaReward(long blueCount, long greenCount, long redCount)
  380. {
  381. long reward = blueCount * SEAL_STONE_BLUE_VALUE;
  382. reward += greenCount * SEAL_STONE_GREEN_VALUE;
  383. reward += redCount * SEAL_STONE_RED_VALUE;
  384. return reward;
  385. }
  386. public static final String getCabalShortName(int cabal)
  387. {
  388. switch (cabal)
  389. {
  390. case CABAL_DAWN:
  391. return "dawn";
  392. case CABAL_DUSK:
  393. return "dusk";
  394. }
  395. return "No Cabal";
  396. }
  397. public static final String getCabalName(int cabal)
  398. {
  399. switch (cabal)
  400. {
  401. case CABAL_DAWN:
  402. return "Lords of Dawn";
  403. case CABAL_DUSK:
  404. return "Revolutionaries of Dusk";
  405. }
  406. return "No Cabal";
  407. }
  408. public static final String getSealName(int seal, boolean shortName)
  409. {
  410. String sealName = (!shortName) ? "Seal of " : "";
  411. switch (seal)
  412. {
  413. case SEAL_AVARICE:
  414. sealName += "Avarice";
  415. break;
  416. case SEAL_GNOSIS:
  417. sealName += "Gnosis";
  418. break;
  419. case SEAL_STRIFE:
  420. sealName += "Strife";
  421. break;
  422. }
  423. return sealName;
  424. }
  425. public final int getCurrentCycle()
  426. {
  427. return _currentCycle;
  428. }
  429. public final int getCurrentPeriod()
  430. {
  431. return _activePeriod;
  432. }
  433. private final int getDaysToPeriodChange()
  434. {
  435. int numDays = _nextPeriodChange.get(Calendar.DAY_OF_WEEK) - PERIOD_START_DAY;
  436. if (numDays < 0)
  437. {
  438. return 0 - numDays;
  439. }
  440. return 7 - numDays;
  441. }
  442. public final long getMilliToPeriodChange()
  443. {
  444. return (_nextPeriodChange.getTimeInMillis() - System.currentTimeMillis());
  445. }
  446. protected void setCalendarForNextPeriodChange()
  447. {
  448. // Calculate the number of days until the next period
  449. // A period starts at 18:00 pm (local time), like on official servers.
  450. switch (getCurrentPeriod())
  451. {
  452. case PERIOD_SEAL_VALIDATION:
  453. case PERIOD_COMPETITION:
  454. int daysToChange = getDaysToPeriodChange();
  455. if (daysToChange == 7)
  456. {
  457. if (_nextPeriodChange.get(Calendar.HOUR_OF_DAY) < PERIOD_START_HOUR)
  458. {
  459. daysToChange = 0;
  460. }
  461. else if ((_nextPeriodChange.get(Calendar.HOUR_OF_DAY) == PERIOD_START_HOUR) && (_nextPeriodChange.get(Calendar.MINUTE) < PERIOD_START_MINS))
  462. {
  463. daysToChange = 0;
  464. }
  465. }
  466. // Otherwise...
  467. if (daysToChange > 0)
  468. {
  469. _nextPeriodChange.add(Calendar.DATE, daysToChange);
  470. }
  471. _nextPeriodChange.set(Calendar.HOUR_OF_DAY, PERIOD_START_HOUR);
  472. _nextPeriodChange.set(Calendar.MINUTE, PERIOD_START_MINS);
  473. break;
  474. case PERIOD_COMP_RECRUITING:
  475. case PERIOD_COMP_RESULTS:
  476. _nextPeriodChange.add(Calendar.MILLISECOND, PERIOD_MINOR_LENGTH);
  477. break;
  478. }
  479. _log.info("SevenSigns: Next period change set to " + _nextPeriodChange.getTime());
  480. }
  481. public final String getCurrentPeriodName()
  482. {
  483. String periodName = null;
  484. switch (_activePeriod)
  485. {
  486. case PERIOD_COMP_RECRUITING:
  487. periodName = "Quest Event Initialization";
  488. break;
  489. case PERIOD_COMPETITION:
  490. periodName = "Competition (Quest Event)";
  491. break;
  492. case PERIOD_COMP_RESULTS:
  493. periodName = "Quest Event Results";
  494. break;
  495. case PERIOD_SEAL_VALIDATION:
  496. periodName = "Seal Validation";
  497. break;
  498. }
  499. return periodName;
  500. }
  501. /**
  502. * @return {@code true} if it's competition period, {@code false} otherwise
  503. */
  504. public final boolean isCompetitionPeriod()
  505. {
  506. return (_activePeriod == PERIOD_COMPETITION);
  507. }
  508. public final boolean isSealValidationPeriod()
  509. {
  510. return (_activePeriod == PERIOD_SEAL_VALIDATION);
  511. }
  512. public final boolean isCompResultsPeriod()
  513. {
  514. return (_activePeriod == PERIOD_COMP_RESULTS);
  515. }
  516. /**
  517. * returns true if the given date is in Seal Validation or in Quest Event Results period
  518. * @param date
  519. * @return
  520. */
  521. public boolean isDateInSealValidPeriod(Calendar date)
  522. {
  523. long nextPeriodChange = getMilliToPeriodChange();
  524. long nextQuestStart = 0;
  525. long nextValidStart = 0;
  526. long tillDate = date.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  527. while (((2 * PERIOD_MAJOR_LENGTH) + (2 * PERIOD_MINOR_LENGTH)) < tillDate)
  528. {
  529. tillDate -= ((2 * PERIOD_MAJOR_LENGTH) + (2 * PERIOD_MINOR_LENGTH));
  530. }
  531. while (tillDate < 0)
  532. {
  533. tillDate += ((2 * PERIOD_MAJOR_LENGTH) + (2 * PERIOD_MINOR_LENGTH));
  534. }
  535. switch (getCurrentPeriod())
  536. {
  537. case PERIOD_COMP_RECRUITING:
  538. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  539. nextQuestStart = nextValidStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  540. break;
  541. case PERIOD_COMPETITION:
  542. nextValidStart = nextPeriodChange;
  543. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  544. break;
  545. case PERIOD_COMP_RESULTS:
  546. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  547. nextValidStart = nextQuestStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  548. break;
  549. case PERIOD_SEAL_VALIDATION:
  550. nextQuestStart = nextPeriodChange;
  551. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  552. break;
  553. }
  554. if (((nextQuestStart < tillDate) && (tillDate < nextValidStart)) || ((nextValidStart < nextQuestStart) && ((tillDate < nextValidStart) || (nextQuestStart < tillDate))))
  555. {
  556. return false;
  557. }
  558. return true;
  559. }
  560. public final int getCurrentScore(int cabal)
  561. {
  562. double totalStoneScore = _dawnStoneScore + _duskStoneScore;
  563. switch (cabal)
  564. {
  565. case CABAL_NULL:
  566. return 0;
  567. case CABAL_DAWN:
  568. return Math.round((float) (_dawnStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500) + _dawnFestivalScore;
  569. case CABAL_DUSK:
  570. return Math.round((float) (_duskStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500) + _duskFestivalScore;
  571. }
  572. return 0;
  573. }
  574. public final double getCurrentStoneScore(int cabal)
  575. {
  576. switch (cabal)
  577. {
  578. case CABAL_NULL:
  579. return 0;
  580. case CABAL_DAWN:
  581. return _dawnStoneScore;
  582. case CABAL_DUSK:
  583. return _duskStoneScore;
  584. }
  585. return 0;
  586. }
  587. public final int getCurrentFestivalScore(int cabal)
  588. {
  589. switch (cabal)
  590. {
  591. case CABAL_NULL:
  592. return 0;
  593. case CABAL_DAWN:
  594. return _dawnFestivalScore;
  595. case CABAL_DUSK:
  596. return _duskFestivalScore;
  597. }
  598. return 0;
  599. }
  600. public final int getCabalHighestScore()
  601. {
  602. if (getCurrentScore(CABAL_DUSK) == getCurrentScore(CABAL_DAWN))
  603. {
  604. return CABAL_NULL;
  605. }
  606. else if (getCurrentScore(CABAL_DUSK) > getCurrentScore(CABAL_DAWN))
  607. {
  608. return CABAL_DUSK;
  609. }
  610. else
  611. {
  612. return CABAL_DAWN;
  613. }
  614. }
  615. public final int getSealOwner(int seal)
  616. {
  617. return _signsSealOwners.get(seal);
  618. }
  619. public final int getSealProportion(int seal, int cabal)
  620. {
  621. if (cabal == CABAL_NULL)
  622. {
  623. return 0;
  624. }
  625. else if (cabal == CABAL_DUSK)
  626. {
  627. return _signsDuskSealTotals.get(seal);
  628. }
  629. else
  630. {
  631. return _signsDawnSealTotals.get(seal);
  632. }
  633. }
  634. public final int getTotalMembers(int cabal)
  635. {
  636. int cabalMembers = 0;
  637. String cabalName = getCabalShortName(cabal);
  638. for (StatsSet sevenDat : _signsPlayerData.values())
  639. {
  640. if (sevenDat.getString("cabal").equals(cabalName))
  641. {
  642. cabalMembers++;
  643. }
  644. }
  645. return cabalMembers;
  646. }
  647. public int getPlayerStoneContrib(int objectId)
  648. {
  649. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  650. if (currPlayer == null)
  651. {
  652. return 0;
  653. }
  654. int stoneCount = 0;
  655. stoneCount += currPlayer.getInt("red_stones");
  656. stoneCount += currPlayer.getInt("green_stones");
  657. stoneCount += currPlayer.getInt("blue_stones");
  658. return stoneCount;
  659. }
  660. public int getPlayerContribScore(int objectId)
  661. {
  662. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  663. if (currPlayer == null)
  664. {
  665. return 0;
  666. }
  667. return currPlayer.getInt("contribution_score");
  668. }
  669. public int getPlayerAdenaCollect(int objectId)
  670. {
  671. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  672. if (currPlayer == null)
  673. {
  674. return 0;
  675. }
  676. return currPlayer.getInt("ancient_adena_amount");
  677. }
  678. public int getPlayerSeal(int objectId)
  679. {
  680. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  681. if (currPlayer == null)
  682. {
  683. return SEAL_NULL;
  684. }
  685. return currPlayer.getInt("seal");
  686. }
  687. public int getPlayerCabal(int objectId)
  688. {
  689. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  690. if (currPlayer == null)
  691. {
  692. return CABAL_NULL;
  693. }
  694. String playerCabal = currPlayer.getString("cabal");
  695. if (playerCabal.equalsIgnoreCase("dawn"))
  696. {
  697. return CABAL_DAWN;
  698. }
  699. else if (playerCabal.equalsIgnoreCase("dusk"))
  700. {
  701. return CABAL_DUSK;
  702. }
  703. else
  704. {
  705. return CABAL_NULL;
  706. }
  707. }
  708. /**
  709. * Restores all Seven Signs data and settings, usually called at server startup.
  710. */
  711. protected void restoreSevenSignsData()
  712. {
  713. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  714. {
  715. try (Statement s = con.createStatement();
  716. ResultSet rs = s.executeQuery(LOAD_DATA))
  717. {
  718. StatsSet sevenDat = null;
  719. int charObjId;
  720. while (rs.next())
  721. {
  722. charObjId = rs.getInt("charId");
  723. sevenDat = new StatsSet();
  724. sevenDat.set("charId", charObjId);
  725. sevenDat.set("cabal", rs.getString("cabal"));
  726. sevenDat.set("seal", rs.getInt("seal"));
  727. sevenDat.set("red_stones", rs.getInt("red_stones"));
  728. sevenDat.set("green_stones", rs.getInt("green_stones"));
  729. sevenDat.set("blue_stones", rs.getInt("blue_stones"));
  730. sevenDat.set("ancient_adena_amount", rs.getDouble("ancient_adena_amount"));
  731. sevenDat.set("contribution_score", rs.getDouble("contribution_score"));
  732. _signsPlayerData.put(charObjId, sevenDat);
  733. }
  734. }
  735. try (Statement s = con.createStatement();
  736. ResultSet rs = s.executeQuery(LOAD_STATUS))
  737. {
  738. while (rs.next())
  739. {
  740. _currentCycle = rs.getInt("current_cycle");
  741. _activePeriod = rs.getInt("active_period");
  742. _previousWinner = rs.getInt("previous_winner");
  743. _dawnStoneScore = rs.getDouble("dawn_stone_score");
  744. _dawnFestivalScore = rs.getInt("dawn_festival_score");
  745. _duskStoneScore = rs.getDouble("dusk_stone_score");
  746. _duskFestivalScore = rs.getInt("dusk_festival_score");
  747. _signsSealOwners.put(SEAL_AVARICE, rs.getInt("avarice_owner"));
  748. _signsSealOwners.put(SEAL_GNOSIS, rs.getInt("gnosis_owner"));
  749. _signsSealOwners.put(SEAL_STRIFE, rs.getInt("strife_owner"));
  750. _signsDawnSealTotals.put(SEAL_AVARICE, rs.getInt("avarice_dawn_score"));
  751. _signsDawnSealTotals.put(SEAL_GNOSIS, rs.getInt("gnosis_dawn_score"));
  752. _signsDawnSealTotals.put(SEAL_STRIFE, rs.getInt("strife_dawn_score"));
  753. _signsDuskSealTotals.put(SEAL_AVARICE, rs.getInt("avarice_dusk_score"));
  754. _signsDuskSealTotals.put(SEAL_GNOSIS, rs.getInt("gnosis_dusk_score"));
  755. _signsDuskSealTotals.put(SEAL_STRIFE, rs.getInt("strife_dusk_score"));
  756. _lastSave.setTimeInMillis(rs.getLong("date"));
  757. }
  758. }
  759. }
  760. catch (SQLException e)
  761. {
  762. _log.log(Level.SEVERE, "SevenSigns: Unable to load Seven Signs data from database: " + e.getMessage(), e);
  763. }
  764. // Festival data is loaded now after the Seven Signs engine data.
  765. }
  766. /**
  767. * Saves all Seven Signs player data.<br>
  768. * Should be called on period change and shutdown only.
  769. */
  770. public void saveSevenSignsData()
  771. {
  772. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  773. PreparedStatement ps = con.prepareStatement(UPDATE_PLAYER))
  774. {
  775. for (StatsSet sevenDat : _signsPlayerData.values())
  776. {
  777. ps.setString(1, sevenDat.getString("cabal"));
  778. ps.setInt(2, sevenDat.getInt("seal"));
  779. ps.setInt(3, sevenDat.getInt("red_stones"));
  780. ps.setInt(4, sevenDat.getInt("green_stones"));
  781. ps.setInt(5, sevenDat.getInt("blue_stones"));
  782. ps.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  783. ps.setDouble(7, sevenDat.getDouble("contribution_score"));
  784. ps.setInt(8, sevenDat.getInt("charId"));
  785. ps.execute();
  786. ps.clearParameters();
  787. }
  788. }
  789. catch (SQLException e)
  790. {
  791. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  792. }
  793. }
  794. public final void saveSevenSignsData(int objectId)
  795. {
  796. StatsSet sevenDat = _signsPlayerData.get(objectId);
  797. if (sevenDat == null)
  798. {
  799. return;
  800. }
  801. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  802. PreparedStatement ps = con.prepareStatement(UPDATE_PLAYER))
  803. {
  804. ps.setString(1, sevenDat.getString("cabal"));
  805. ps.setInt(2, sevenDat.getInt("seal"));
  806. ps.setInt(3, sevenDat.getInt("red_stones"));
  807. ps.setInt(4, sevenDat.getInt("green_stones"));
  808. ps.setInt(5, sevenDat.getInt("blue_stones"));
  809. ps.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  810. ps.setDouble(7, sevenDat.getDouble("contribution_score"));
  811. ps.setInt(8, sevenDat.getInt("charId"));
  812. ps.execute();
  813. }
  814. catch (SQLException e)
  815. {
  816. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  817. }
  818. }
  819. public final void saveSevenSignsStatus()
  820. {
  821. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  822. PreparedStatement ps = con.prepareStatement(UPDATE_STATUS))
  823. {
  824. ps.setInt(1, _currentCycle);
  825. ps.setInt(2, _activePeriod);
  826. ps.setInt(3, _previousWinner);
  827. ps.setDouble(4, _dawnStoneScore);
  828. ps.setInt(5, _dawnFestivalScore);
  829. ps.setDouble(6, _duskStoneScore);
  830. ps.setInt(7, _duskFestivalScore);
  831. ps.setInt(8, _signsSealOwners.get(SEAL_AVARICE));
  832. ps.setInt(9, _signsSealOwners.get(SEAL_GNOSIS));
  833. ps.setInt(10, _signsSealOwners.get(SEAL_STRIFE));
  834. ps.setInt(11, _signsDawnSealTotals.get(SEAL_AVARICE));
  835. ps.setInt(12, _signsDawnSealTotals.get(SEAL_GNOSIS));
  836. ps.setInt(13, _signsDawnSealTotals.get(SEAL_STRIFE));
  837. ps.setInt(14, _signsDuskSealTotals.get(SEAL_AVARICE));
  838. ps.setInt(15, _signsDuskSealTotals.get(SEAL_GNOSIS));
  839. ps.setInt(16, _signsDuskSealTotals.get(SEAL_STRIFE));
  840. ps.setInt(17, SevenSignsFestival.getInstance().getCurrentFestivalCycle());
  841. for (int i = 0; i < SevenSignsFestival.FESTIVAL_COUNT; i++)
  842. {
  843. ps.setInt(18 + i, SevenSignsFestival.getInstance().getAccumulatedBonus(i));
  844. }
  845. _lastSave = Calendar.getInstance();
  846. ps.setLong(18 + SevenSignsFestival.FESTIVAL_COUNT, _lastSave.getTimeInMillis());
  847. ps.execute();
  848. }
  849. catch (SQLException e)
  850. {
  851. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  852. }
  853. }
  854. /**
  855. * Used to reset the cabal details of all players, and update the database.<BR>
  856. * Primarily used when beginning a new cycle, and should otherwise never be called.
  857. */
  858. protected void resetPlayerData()
  859. {
  860. int charObjId;
  861. // Reset each player's contribution data as well as seal and cabal.
  862. for (StatsSet sevenDat : _signsPlayerData.values())
  863. {
  864. charObjId = sevenDat.getInt("charId");
  865. // Reset the player's cabal and seal information
  866. sevenDat.set("cabal", "");
  867. sevenDat.set("seal", SEAL_NULL);
  868. sevenDat.set("contribution_score", 0);
  869. _signsPlayerData.put(charObjId, sevenDat);
  870. }
  871. }
  872. /**
  873. * Used to specify cabal-related details for the specified player.<br>
  874. * This method checks to see if the player has registered before and will update the database if necessary.
  875. * @param objectId
  876. * @param chosenCabal
  877. * @param chosenSeal
  878. * @return the cabal ID the player has joined.
  879. */
  880. public int setPlayerInfo(int objectId, int chosenCabal, int chosenSeal)
  881. {
  882. StatsSet currPlayerData = _signsPlayerData.get(objectId);
  883. if (currPlayerData != null)
  884. {
  885. // If the seal validation period has passed,
  886. // cabal information was removed and so "re-register" player
  887. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  888. currPlayerData.set("seal", chosenSeal);
  889. _signsPlayerData.put(objectId, currPlayerData);
  890. }
  891. else
  892. {
  893. currPlayerData = new StatsSet();
  894. currPlayerData.set("charId", objectId);
  895. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  896. currPlayerData.set("seal", chosenSeal);
  897. currPlayerData.set("red_stones", 0);
  898. currPlayerData.set("green_stones", 0);
  899. currPlayerData.set("blue_stones", 0);
  900. currPlayerData.set("ancient_adena_amount", 0);
  901. currPlayerData.set("contribution_score", 0);
  902. _signsPlayerData.put(objectId, currPlayerData);
  903. // Update data in database, as we have a new player signing up.
  904. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  905. PreparedStatement ps = con.prepareStatement(INSERT_PLAYER))
  906. {
  907. ps.setInt(1, objectId);
  908. ps.setString(2, getCabalShortName(chosenCabal));
  909. ps.setInt(3, chosenSeal);
  910. ps.execute();
  911. }
  912. catch (SQLException e)
  913. {
  914. _log.log(Level.SEVERE, "SevenSigns: Failed to save data: " + e.getMessage(), e);
  915. }
  916. }
  917. // Increasing Seal total score for the player chosen Seal.
  918. if ("dawn".equals(currPlayerData.getString("cabal")))
  919. {
  920. _signsDawnSealTotals.put(chosenSeal, _signsDawnSealTotals.get(chosenSeal) + 1);
  921. }
  922. else
  923. {
  924. _signsDuskSealTotals.put(chosenSeal, _signsDuskSealTotals.get(chosenSeal) + 1);
  925. }
  926. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  927. {
  928. saveSevenSignsStatus();
  929. }
  930. return chosenCabal;
  931. }
  932. /**
  933. * Returns the amount of ancient adena the specified player can claim, if any.<BR>
  934. * If removeReward = True, all the ancient adena owed to them is removed, then DB is updated.
  935. * @param objectId
  936. * @param removeReward
  937. * @return
  938. */
  939. public int getAncientAdenaReward(int objectId, boolean removeReward)
  940. {
  941. StatsSet currPlayer = _signsPlayerData.get(objectId);
  942. int rewardAmount = currPlayer.getInt("ancient_adena_amount");
  943. currPlayer.set("red_stones", 0);
  944. currPlayer.set("green_stones", 0);
  945. currPlayer.set("blue_stones", 0);
  946. currPlayer.set("ancient_adena_amount", 0);
  947. if (removeReward)
  948. {
  949. _signsPlayerData.put(objectId, currPlayer);
  950. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  951. {
  952. saveSevenSignsData(objectId);
  953. saveSevenSignsStatus();
  954. }
  955. }
  956. return rewardAmount;
  957. }
  958. /**
  959. * Used to add the specified player's seal stone contribution points to the current total for their cabal.<br>
  960. * Returns the point score the contribution was worth.<br>
  961. * Each stone count <B>must be</B> broken down and specified by the stone's color.
  962. * @param objectId
  963. * @param blueCount
  964. * @param greenCount
  965. * @param redCount
  966. * @return
  967. */
  968. public long addPlayerStoneContrib(int objectId, long blueCount, long greenCount, long redCount)
  969. {
  970. StatsSet currPlayer = _signsPlayerData.get(objectId);
  971. long contribScore = calcContributionScore(blueCount, greenCount, redCount);
  972. long totalAncientAdena = currPlayer.getLong("ancient_adena_amount") + calcAncientAdenaReward(blueCount, greenCount, redCount);
  973. long totalContribScore = currPlayer.getLong("contribution_score") + contribScore;
  974. if (totalContribScore > Config.ALT_MAXIMUM_PLAYER_CONTRIB)
  975. {
  976. return -1;
  977. }
  978. currPlayer.set("red_stones", currPlayer.getInt("red_stones") + redCount);
  979. currPlayer.set("green_stones", currPlayer.getInt("green_stones") + greenCount);
  980. currPlayer.set("blue_stones", currPlayer.getInt("blue_stones") + blueCount);
  981. currPlayer.set("ancient_adena_amount", totalAncientAdena);
  982. currPlayer.set("contribution_score", totalContribScore);
  983. _signsPlayerData.put(objectId, currPlayer);
  984. switch (getPlayerCabal(objectId))
  985. {
  986. case CABAL_DAWN:
  987. _dawnStoneScore += contribScore;
  988. break;
  989. case CABAL_DUSK:
  990. _duskStoneScore += contribScore;
  991. break;
  992. }
  993. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  994. {
  995. saveSevenSignsData(objectId);
  996. saveSevenSignsStatus();
  997. }
  998. return contribScore;
  999. }
  1000. /**
  1001. * Adds the specified number of festival points to the specified cabal.<br>
  1002. * Remember, the same number of points are <b>deducted from the rival cabal</b> to maintain proportionality.
  1003. * @param cabal
  1004. * @param amount
  1005. */
  1006. public void addFestivalScore(int cabal, int amount)
  1007. {
  1008. if (cabal == CABAL_DUSK)
  1009. {
  1010. _duskFestivalScore += amount;
  1011. // To prevent negative scores!
  1012. if (_dawnFestivalScore >= amount)
  1013. {
  1014. _dawnFestivalScore -= amount;
  1015. }
  1016. }
  1017. else
  1018. {
  1019. _dawnFestivalScore += amount;
  1020. if (_duskFestivalScore >= amount)
  1021. {
  1022. _duskFestivalScore -= amount;
  1023. }
  1024. }
  1025. }
  1026. /**
  1027. * Send info on the current Seven Signs period to the specified player.
  1028. * @param player
  1029. */
  1030. public void sendCurrentPeriodMsg(L2PcInstance player)
  1031. {
  1032. SystemMessage sm = null;
  1033. switch (getCurrentPeriod())
  1034. {
  1035. case PERIOD_COMP_RECRUITING:
  1036. sm = SystemMessage.getSystemMessage(SystemMessageId.PREPARATIONS_PERIOD_BEGUN);
  1037. break;
  1038. case PERIOD_COMPETITION:
  1039. sm = SystemMessage.getSystemMessage(SystemMessageId.COMPETITION_PERIOD_BEGUN);
  1040. break;
  1041. case PERIOD_COMP_RESULTS:
  1042. sm = SystemMessage.getSystemMessage(SystemMessageId.RESULTS_PERIOD_BEGUN);
  1043. break;
  1044. case PERIOD_SEAL_VALIDATION:
  1045. sm = SystemMessage.getSystemMessage(SystemMessageId.VALIDATION_PERIOD_BEGUN);
  1046. break;
  1047. }
  1048. player.sendPacket(sm);
  1049. }
  1050. /**
  1051. * Sends the built-in system message specified by sysMsgId to all online players.
  1052. * @param sysMsgId
  1053. */
  1054. public void sendMessageToAll(SystemMessageId sysMsgId)
  1055. {
  1056. Broadcast.toAllOnlinePlayers(SystemMessage.getSystemMessage(sysMsgId));
  1057. }
  1058. /**
  1059. * Used to initialize the seals for each cabal.<bR>
  1060. * (Used at startup or at beginning of a new cycle).<br>
  1061. * This method should be called after <b>resetSeals()</b> and <b>calcNewSealOwners()</b> on a new cycle.
  1062. */
  1063. protected void initializeSeals()
  1064. {
  1065. for (Entry<Integer, Integer> e : _signsSealOwners.entrySet())
  1066. {
  1067. if (e.getValue() != CABAL_NULL)
  1068. {
  1069. if (isSealValidationPeriod())
  1070. {
  1071. _log.info("SevenSigns: The " + getCabalName(e.getValue()) + " have won the " + getSealName(e.getKey(), false) + ".");
  1072. }
  1073. else
  1074. {
  1075. _log.info("SevenSigns: The " + getSealName(e.getKey(), false) + " is currently owned by " + getCabalName(e.getValue()) + ".");
  1076. }
  1077. }
  1078. else
  1079. {
  1080. _log.info("SevenSigns: The " + getSealName(e.getKey(), false) + " remains unclaimed.");
  1081. }
  1082. }
  1083. }
  1084. /**
  1085. * Only really used at the beginning of a new cycle, this method resets all seal-related data.
  1086. */
  1087. protected void resetSeals()
  1088. {
  1089. _signsDawnSealTotals.put(SEAL_AVARICE, 0);
  1090. _signsDawnSealTotals.put(SEAL_GNOSIS, 0);
  1091. _signsDawnSealTotals.put(SEAL_STRIFE, 0);
  1092. _signsDuskSealTotals.put(SEAL_AVARICE, 0);
  1093. _signsDuskSealTotals.put(SEAL_GNOSIS, 0);
  1094. _signsDuskSealTotals.put(SEAL_STRIFE, 0);
  1095. }
  1096. /**
  1097. * Calculates the ownership of the three Seals of the Seven Signs, based on various criterion. <BR>
  1098. * <BR>
  1099. * Should only ever called at the beginning of a new cycle.
  1100. */
  1101. protected void calcNewSealOwners()
  1102. {
  1103. for (Integer currSeal : _signsDawnSealTotals.keySet())
  1104. {
  1105. int prevSealOwner = _signsSealOwners.get(currSeal);
  1106. int newSealOwner = CABAL_NULL;
  1107. int dawnProportion = getSealProportion(currSeal, CABAL_DAWN);
  1108. int totalDawnMembers = getTotalMembers(CABAL_DAWN) == 0 ? 1 : getTotalMembers(CABAL_DAWN);
  1109. int dawnPercent = Math.round(((float) dawnProportion / (float) totalDawnMembers) * 100);
  1110. int duskProportion = getSealProportion(currSeal, CABAL_DUSK);
  1111. int totalDuskMembers = getTotalMembers(CABAL_DUSK) == 0 ? 1 : getTotalMembers(CABAL_DUSK);
  1112. int duskPercent = Math.round(((float) duskProportion / (float) totalDuskMembers) * 100);
  1113. /**
  1114. * If a Seal was already closed or owned by the opponent and the new winner wants to assume ownership of the Seal, 35% or more of the members of the Cabal must have chosen the Seal.<br>
  1115. * If they chose less than 35%, they cannot own the Seal.<br>
  1116. * If the Seal was owned by the winner in the previous Seven Signs, they can retain that seal if 10% or more members have chosen it.<br>
  1117. * If they want to possess a new Seal, at least 35% of the members of the Cabal must have chosen the new Seal.
  1118. */
  1119. switch (prevSealOwner)
  1120. {
  1121. case CABAL_NULL:
  1122. switch (getCabalHighestScore())
  1123. {
  1124. case CABAL_NULL:
  1125. newSealOwner = CABAL_NULL;
  1126. break;
  1127. case CABAL_DAWN:
  1128. if (dawnPercent >= 35)
  1129. {
  1130. newSealOwner = CABAL_DAWN;
  1131. }
  1132. else
  1133. {
  1134. newSealOwner = CABAL_NULL;
  1135. }
  1136. break;
  1137. case CABAL_DUSK:
  1138. if (duskPercent >= 35)
  1139. {
  1140. newSealOwner = CABAL_DUSK;
  1141. }
  1142. else
  1143. {
  1144. newSealOwner = CABAL_NULL;
  1145. }
  1146. break;
  1147. }
  1148. break;
  1149. case CABAL_DAWN:
  1150. switch (getCabalHighestScore())
  1151. {
  1152. case CABAL_NULL:
  1153. if (dawnPercent >= 10)
  1154. {
  1155. newSealOwner = CABAL_DAWN;
  1156. }
  1157. else
  1158. {
  1159. newSealOwner = CABAL_NULL;
  1160. }
  1161. break;
  1162. case CABAL_DAWN:
  1163. if (dawnPercent >= 10)
  1164. {
  1165. newSealOwner = CABAL_DAWN;
  1166. }
  1167. else
  1168. {
  1169. newSealOwner = CABAL_NULL;
  1170. }
  1171. break;
  1172. case CABAL_DUSK:
  1173. if (duskPercent >= 35)
  1174. {
  1175. newSealOwner = CABAL_DUSK;
  1176. }
  1177. else if (dawnPercent >= 10)
  1178. {
  1179. newSealOwner = CABAL_DAWN;
  1180. }
  1181. else
  1182. {
  1183. newSealOwner = CABAL_NULL;
  1184. }
  1185. break;
  1186. }
  1187. break;
  1188. case CABAL_DUSK:
  1189. switch (getCabalHighestScore())
  1190. {
  1191. case CABAL_NULL:
  1192. if (duskPercent >= 10)
  1193. {
  1194. newSealOwner = CABAL_DUSK;
  1195. }
  1196. else
  1197. {
  1198. newSealOwner = CABAL_NULL;
  1199. }
  1200. break;
  1201. case CABAL_DAWN:
  1202. if (dawnPercent >= 35)
  1203. {
  1204. newSealOwner = CABAL_DAWN;
  1205. }
  1206. else if (duskPercent >= 10)
  1207. {
  1208. newSealOwner = CABAL_DUSK;
  1209. }
  1210. else
  1211. {
  1212. newSealOwner = CABAL_NULL;
  1213. }
  1214. break;
  1215. case CABAL_DUSK:
  1216. if (duskPercent >= 10)
  1217. {
  1218. newSealOwner = CABAL_DUSK;
  1219. }
  1220. else
  1221. {
  1222. newSealOwner = CABAL_NULL;
  1223. }
  1224. break;
  1225. }
  1226. break;
  1227. }
  1228. _signsSealOwners.put(currSeal, newSealOwner);
  1229. // Alert all online players to new seal status.
  1230. switch (currSeal)
  1231. {
  1232. case SEAL_AVARICE:
  1233. if (newSealOwner == CABAL_DAWN)
  1234. {
  1235. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_AVARICE);
  1236. }
  1237. else if (newSealOwner == CABAL_DUSK)
  1238. {
  1239. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_AVARICE);
  1240. }
  1241. break;
  1242. case SEAL_GNOSIS:
  1243. if (newSealOwner == CABAL_DAWN)
  1244. {
  1245. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_GNOSIS);
  1246. }
  1247. else if (newSealOwner == CABAL_DUSK)
  1248. {
  1249. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_GNOSIS);
  1250. }
  1251. break;
  1252. case SEAL_STRIFE:
  1253. if (newSealOwner == CABAL_DAWN)
  1254. {
  1255. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_STRIFE);
  1256. }
  1257. else if (newSealOwner == CABAL_DUSK)
  1258. {
  1259. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_STRIFE);
  1260. }
  1261. CastleManager.getInstance().validateTaxes(newSealOwner);
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. /**
  1267. * This method is called to remove all players from catacombs and necropolises, who belong to the losing cabal.<br>
  1268. * Should only ever called at the beginning of Seal Validation.
  1269. * @param compWinner
  1270. */
  1271. protected void teleLosingCabalFromDungeons(String compWinner)
  1272. {
  1273. L2World.getInstance().forEachPlayer(new TeleLosingCabalFromDungeons(compWinner));
  1274. }
  1275. private final class TeleLosingCabalFromDungeons implements IProcedure<L2PcInstance, Boolean>
  1276. {
  1277. private final String _cmpWinner;
  1278. protected TeleLosingCabalFromDungeons(final String compWinner)
  1279. {
  1280. _cmpWinner = compWinner;
  1281. }
  1282. @Override
  1283. public final Boolean execute(final L2PcInstance onlinePlayer)
  1284. {
  1285. if (onlinePlayer != null)
  1286. {
  1287. StatsSet currPlayer = _signsPlayerData.get(onlinePlayer.getObjectId());
  1288. if (isSealValidationPeriod() || isCompResultsPeriod())
  1289. {
  1290. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && ((currPlayer == null) || !currPlayer.getString("cabal").equals(_cmpWinner)))
  1291. {
  1292. onlinePlayer.teleToLocation(TeleportWhereType.TOWN);
  1293. onlinePlayer.setIsIn7sDungeon(false);
  1294. onlinePlayer.sendMessage("You have been teleported to the nearest town due to the beginning of the Seal Validation period.");
  1295. }
  1296. }
  1297. else
  1298. {
  1299. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && ((currPlayer == null) || !currPlayer.getString("cabal").isEmpty()))
  1300. {
  1301. onlinePlayer.teleToLocation(TeleportWhereType.TOWN);
  1302. onlinePlayer.setIsIn7sDungeon(false);
  1303. onlinePlayer.sendMessage("You have been teleported to the nearest town because you have not signed for any cabal.");
  1304. }
  1305. }
  1306. }
  1307. return true;
  1308. }
  1309. }
  1310. /**
  1311. * The primary controller of period change of the Seven Signs system.<br>
  1312. * This runs all related tasks depending on the period that is about to begin.
  1313. * @author Tempy
  1314. */
  1315. protected class SevenSignsPeriodChange implements Runnable
  1316. {
  1317. @Override
  1318. public void run()
  1319. {
  1320. // Remember the period check here refers to the period just ENDED!
  1321. final int periodEnded = getCurrentPeriod();
  1322. _activePeriod++;
  1323. switch (periodEnded)
  1324. {
  1325. case PERIOD_COMP_RECRUITING: // Initialization
  1326. // Start the Festival of Darkness cycle.
  1327. SevenSignsFestival.getInstance().startFestivalManager();
  1328. // Send message that Competition has begun.
  1329. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_BEGUN);
  1330. break;
  1331. case PERIOD_COMPETITION: // Results Calculation
  1332. // Send message that Competition has ended.
  1333. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_ENDED);
  1334. int compWinner = getCabalHighestScore();
  1335. // Schedule a stop of the festival engine and reward highest ranking members from cycle
  1336. SevenSignsFestival.getInstance().getFestivalManagerSchedule().cancel(false);
  1337. SevenSignsFestival.getInstance().rewardHighestRanked();
  1338. calcNewSealOwners();
  1339. switch (compWinner)
  1340. {
  1341. case CABAL_DAWN:
  1342. sendMessageToAll(SystemMessageId.DAWN_WON);
  1343. break;
  1344. case CABAL_DUSK:
  1345. sendMessageToAll(SystemMessageId.DUSK_WON);
  1346. break;
  1347. }
  1348. _previousWinner = compWinner;
  1349. // Reset Castle ticket buy count
  1350. List<Castle> castles = CastleManager.getInstance().getCastles();
  1351. for (Castle castle : castles)
  1352. {
  1353. castle.setTicketBuyCount(0);
  1354. }
  1355. break;
  1356. case PERIOD_COMP_RESULTS: // Seal Validation
  1357. // Perform initial Seal Validation set up.
  1358. initializeSeals();
  1359. // Buff/Debuff members of the event when Seal of Strife captured.
  1360. giveCPMult(getSealOwner(SEAL_STRIFE));
  1361. // Send message that Seal Validation has begun.
  1362. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_BEGUN);
  1363. _log.info("SevenSigns: The " + getCabalName(_previousWinner) + " have won the competition with " + getCurrentScore(_previousWinner) + " points!");
  1364. break;
  1365. case PERIOD_SEAL_VALIDATION: // Reset for New Cycle
  1366. // Ensure a cycle restart when this period ends.
  1367. _activePeriod = PERIOD_COMP_RECRUITING;
  1368. // Send message that Seal Validation has ended.
  1369. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_ENDED);
  1370. // Clear Seal of Strife influence.
  1371. removeCPMult();
  1372. // Reset all data
  1373. resetPlayerData();
  1374. resetSeals();
  1375. _currentCycle++;
  1376. // Reset all Festival-related data and remove any unused blood offerings.
  1377. // NOTE: A full update of Festival data in the database is also performed.
  1378. SevenSignsFestival.getInstance().resetFestivalData(false);
  1379. _dawnStoneScore = 0;
  1380. _duskStoneScore = 0;
  1381. _dawnFestivalScore = 0;
  1382. _duskFestivalScore = 0;
  1383. break;
  1384. }
  1385. // Make sure all Seven Signs data is saved for future use.
  1386. saveSevenSignsData();
  1387. saveSevenSignsStatus();
  1388. teleLosingCabalFromDungeons(getCabalShortName(getCabalHighestScore()));
  1389. SSQInfo ss = new SSQInfo();
  1390. Broadcast.toAllOnlinePlayers(ss);
  1391. spawnSevenSignsNPC();
  1392. _log.info("SevenSigns: The " + getCurrentPeriodName() + " period has begun!");
  1393. setCalendarForNextPeriodChange();
  1394. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  1395. ThreadPoolManager.getInstance().scheduleGeneral(sspc, getMilliToPeriodChange());
  1396. }
  1397. }
  1398. public boolean checkIsDawnPostingTicket(int itemId)
  1399. {
  1400. // TODO: I think it should be some kind of a list in the datapack for compare.
  1401. if ((itemId > 6114) && (itemId < 6175))
  1402. {
  1403. return true;
  1404. }
  1405. if ((itemId > 6801) && (itemId < 6812))
  1406. {
  1407. return true;
  1408. }
  1409. if ((itemId > 7997) && (itemId < 8008))
  1410. {
  1411. return true;
  1412. }
  1413. if ((itemId > 7940) && (itemId < 7951))
  1414. {
  1415. return true;
  1416. }
  1417. if ((itemId > 6294) && (itemId < 6307))
  1418. {
  1419. return true;
  1420. }
  1421. if ((itemId > 6831) && (itemId < 6834))
  1422. {
  1423. return true;
  1424. }
  1425. if ((itemId > 8027) && (itemId < 8030))
  1426. {
  1427. return true;
  1428. }
  1429. if ((itemId > 7970) && (itemId < 7973))
  1430. {
  1431. return true;
  1432. }
  1433. return false;
  1434. }
  1435. public boolean checkIsRookiePostingTicket(int itemId)
  1436. {
  1437. // TODO: I think it should be some kind of a list in the datapack for compare.
  1438. if ((itemId > 6174) && (itemId < 6295))
  1439. {
  1440. return true;
  1441. }
  1442. if ((itemId > 6811) && (itemId < 6832))
  1443. {
  1444. return true;
  1445. }
  1446. if ((itemId > 7950) && (itemId < 7971))
  1447. {
  1448. return true;
  1449. }
  1450. if ((itemId > 8007) && (itemId < 8028))
  1451. {
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. public void giveCPMult(int StrifeOwner)
  1457. {
  1458. L2World.getInstance().forEachPlayer(new GiveCPMult(StrifeOwner));
  1459. }
  1460. private final class GiveCPMult implements IProcedure<L2PcInstance, Boolean>
  1461. {
  1462. private final int _strifeOwner;
  1463. protected GiveCPMult(int strifeOwner)
  1464. {
  1465. _strifeOwner = strifeOwner;
  1466. }
  1467. @Override
  1468. public final Boolean execute(final L2PcInstance character)
  1469. {
  1470. if (character != null)
  1471. {
  1472. // Gives "Victor of War" passive skill to all online characters with Cabal, which controls Seal of Strife
  1473. int cabal = getPlayerCabal(character.getObjectId());
  1474. if (cabal != SevenSigns.CABAL_NULL)
  1475. {
  1476. if (cabal == _strifeOwner)
  1477. {
  1478. character.addSkill(CommonSkill.THE_VICTOR_OF_WAR.getSkill());
  1479. }
  1480. else
  1481. {
  1482. // Gives "The Vanquished of War" passive skill to all online characters with Cabal, which does not control Seal of Strife
  1483. character.addSkill(CommonSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1484. }
  1485. }
  1486. }
  1487. return true;
  1488. }
  1489. }
  1490. public void removeCPMult()
  1491. {
  1492. L2World.getInstance().forEachPlayer(new RemoveCPMult());
  1493. }
  1494. protected final class RemoveCPMult implements IProcedure<L2PcInstance, Boolean>
  1495. {
  1496. @Override
  1497. public final Boolean execute(final L2PcInstance character)
  1498. {
  1499. if (character != null)
  1500. {
  1501. // Remove SevenSigns' buffs/debuffs.
  1502. character.removeSkill(CommonSkill.THE_VICTOR_OF_WAR.getSkill());
  1503. character.removeSkill(CommonSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1504. }
  1505. return true;
  1506. }
  1507. }
  1508. public boolean checkSummonConditions(L2PcInstance activeChar)
  1509. {
  1510. if (activeChar == null)
  1511. {
  1512. return true;
  1513. }
  1514. // Golems cannot be summoned by Dusk when the Seal of Strife is controlled by the Dawn
  1515. if (isSealValidationPeriod())
  1516. {
  1517. if (getSealOwner(SEAL_STRIFE) == CABAL_DAWN)
  1518. {
  1519. if (getPlayerCabal(activeChar.getObjectId()) == CABAL_DUSK)
  1520. {
  1521. activeChar.sendPacket(SystemMessageId.SEAL_OF_STRIFE_FORBIDS_SUMMONING);
  1522. return true;
  1523. }
  1524. }
  1525. }
  1526. return false;
  1527. }
  1528. private static class SingletonHolder
  1529. {
  1530. protected static final SevenSigns _instance = new SevenSigns();
  1531. }
  1532. }