SevenSigns.java 52 KB

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