SevenSigns.java 53 KB

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