SevenSigns.java 54 KB

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