SevenSigns.java 52 KB

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