SevenSigns.java 54 KB

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