SevenSigns.java 53 KB

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