SevenSigns.java 53 KB

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