SevenSigns.java 51 KB

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