SevenSigns.java 49 KB

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