SevenSigns.java 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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. // Basic Seven Signs Constants \\
  54. public static final String SEVEN_SIGNS_DATA_FILE = "config/signs.properties";
  55. public static final String SEVEN_SIGNS_HTML_PATH = "data/html/seven_signs/";
  56. public static final int CABAL_NULL = 0;
  57. public static final int CABAL_DUSK = 1;
  58. public static final int CABAL_DAWN = 2;
  59. public static final int SEAL_NULL = 0;
  60. public static final int SEAL_AVARICE = 1;
  61. public static final int SEAL_GNOSIS = 2;
  62. public static final int SEAL_STRIFE = 3;
  63. public static final int PERIOD_COMP_RECRUITING = 0;
  64. public static final int PERIOD_COMPETITION = 1;
  65. public static final int PERIOD_COMP_RESULTS = 2;
  66. public static final int PERIOD_SEAL_VALIDATION = 3;
  67. public static final int PERIOD_START_HOUR = 18;
  68. public static final int PERIOD_START_MINS = 00;
  69. public static final int PERIOD_START_DAY = Calendar.MONDAY;
  70. // The quest event and seal validation periods last for approximately one week
  71. // with a 15 minutes "interval" period sandwiched between them.
  72. public static final int PERIOD_MINOR_LENGTH = 900000;
  73. public static final int PERIOD_MAJOR_LENGTH = 604800000 - PERIOD_MINOR_LENGTH;
  74. public static final int ANCIENT_ADENA_ID = 5575;
  75. public static final int RECORD_SEVEN_SIGNS_ID = 5707;
  76. public static final int CERTIFICATE_OF_APPROVAL_ID = 6388;
  77. public static final int RECORD_SEVEN_SIGNS_COST = 500;
  78. public static final int ADENA_JOIN_DAWN_COST = 50000;
  79. // NPC Related Constants \\
  80. public static final int ORATOR_NPC_ID = 31094;
  81. public static final int PREACHER_NPC_ID = 31093;
  82. public static final int MAMMON_MERCHANT_ID = 31113;
  83. public static final int MAMMON_BLACKSMITH_ID = 31126;
  84. public static final int MAMMON_MARKETEER_ID = 31092;
  85. public static final int SPIRIT_IN_ID = 31111;
  86. public static final int SPIRIT_OUT_ID = 31112;
  87. public static final int LILITH_NPC_ID = 25283;
  88. public static final int ANAKIM_NPC_ID = 25286;
  89. public static final int CREST_OF_DAWN_ID = 31170;
  90. public static final int CREST_OF_DUSK_ID = 31171;
  91. // Seal Stone Related Constants \\
  92. public static final int SEAL_STONE_BLUE_ID = 6360;
  93. public static final int SEAL_STONE_GREEN_ID = 6361;
  94. public static final int SEAL_STONE_RED_ID = 6362;
  95. public static final int SEAL_STONE_BLUE_VALUE = 3;
  96. public static final int SEAL_STONE_GREEN_VALUE = 5;
  97. public static final int SEAL_STONE_RED_VALUE = 10;
  98. public static final int BLUE_CONTRIB_POINTS = 3;
  99. public static final int GREEN_CONTRIB_POINTS = 5;
  100. public static final int RED_CONTRIB_POINTS = 10;
  101. private final Calendar _calendar = Calendar.getInstance();
  102. protected int _activePeriod;
  103. protected int _currentCycle;
  104. protected double _dawnStoneScore;
  105. protected double _duskStoneScore;
  106. protected int _dawnFestivalScore;
  107. protected int _duskFestivalScore;
  108. protected int _compWinner;
  109. protected int _previousWinner;
  110. private Map<Integer, StatsSet> _signsPlayerData;
  111. private Map<Integer, Integer> _signsSealOwners;
  112. private Map<Integer, Integer> _signsDuskSealTotals;
  113. private Map<Integer, Integer> _signsDawnSealTotals;
  114. private static AutoSpawnInstance _merchantSpawn;
  115. private static AutoSpawnInstance _blacksmithSpawn;
  116. private static AutoSpawnInstance _spiritInSpawn;
  117. private static AutoSpawnInstance _spiritOutSpawn;
  118. private static AutoSpawnInstance _lilithSpawn;
  119. private static AutoSpawnInstance _anakimSpawn;
  120. private static AutoSpawnInstance _crestofdawnspawn;
  121. private static AutoSpawnInstance _crestofduskspawn;
  122. private static Map<Integer, AutoSpawnInstance> _oratorSpawns;
  123. private static Map<Integer, AutoSpawnInstance> _preacherSpawns;
  124. private static Map<Integer, AutoSpawnInstance> _marketeerSpawns;
  125. private SevenSigns()
  126. {
  127. _signsPlayerData = new FastMap<Integer, StatsSet>();
  128. _signsSealOwners = new FastMap<Integer, Integer>();
  129. _signsDuskSealTotals = new FastMap<Integer, Integer>();
  130. _signsDawnSealTotals = new FastMap<Integer, Integer>();
  131. try
  132. {
  133. restoreSevenSignsData();
  134. }
  135. catch (Exception e)
  136. {
  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 if (getCabalHighestScore() == CABAL_NULL)
  147. _log.info("SevenSigns: The competition, if the current trend continues, will end in a tie this week.");
  148. else
  149. _log.info("SevenSigns: The " + getCabalName(getCabalHighestScore()) + " are in the lead this week.");
  150. synchronized (this)
  151. {
  152. setCalendarForNextPeriodChange();
  153. long milliToChange = getMilliToPeriodChange();
  154. // Schedule a time for the next period change.
  155. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  156. ThreadPoolManager.getInstance().scheduleGeneral(sspc, milliToChange);
  157. // Thanks to http://rainbow.arch.scriptmania.com/scripts/timezone_countdown.html for help with this.
  158. double numSecs = (milliToChange / 1000) % 60;
  159. double countDown = ((milliToChange / 1000) - numSecs) / 60;
  160. int numMins = (int) Math.floor(countDown % 60);
  161. countDown = (countDown - numMins) / 60;
  162. int numHours = (int) Math.floor(countDown % 24);
  163. int numDays = (int) Math.floor((countDown - numHours) / 24);
  164. _log.info("SevenSigns: Next period begins in " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  165. }
  166. }
  167. /**
  168. * Registers all random spawns and auto-chats for Seven Signs NPCs,
  169. * along with spawns for the Preachers of Doom and Orators of Revelations
  170. * at the beginning of the Seal Validation period.
  171. *
  172. */
  173. public void spawnSevenSignsNPC()
  174. {
  175. _merchantSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(MAMMON_MERCHANT_ID, false);
  176. _blacksmithSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(MAMMON_BLACKSMITH_ID, false);
  177. _marketeerSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(MAMMON_MARKETEER_ID);
  178. _spiritInSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(SPIRIT_IN_ID, false);
  179. _spiritOutSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(SPIRIT_OUT_ID, false);
  180. _lilithSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(LILITH_NPC_ID, false);
  181. _anakimSpawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(ANAKIM_NPC_ID, false);
  182. _crestofdawnspawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(CREST_OF_DAWN_ID, false);
  183. _crestofduskspawn = AutoSpawnHandler.getInstance().getAutoSpawnInstance(CREST_OF_DUSK_ID, false);
  184. _oratorSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(ORATOR_NPC_ID);
  185. _preacherSpawns = AutoSpawnHandler.getInstance().getAutoSpawnInstances(PREACHER_NPC_ID);
  186. if (isSealValidationPeriod() || isCompResultsPeriod())
  187. {
  188. for (AutoSpawnInstance spawnInst : _marketeerSpawns.values())
  189. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  190. if (getSealOwner(SEAL_GNOSIS) == getCabalHighestScore() && getSealOwner(SEAL_GNOSIS) != CABAL_NULL)
  191. {
  192. if (!Config.ANNOUNCE_MAMMON_SPAWN)
  193. _blacksmithSpawn.setBroadcast(false);
  194. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(_blacksmithSpawn.getObjectId(), true).isSpawnActive())
  195. AutoSpawnHandler.getInstance().setSpawnActive(_blacksmithSpawn, true);
  196. for (AutoSpawnInstance spawnInst : _oratorSpawns.values())
  197. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(spawnInst.getObjectId(), true).isSpawnActive())
  198. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  199. for (AutoSpawnInstance spawnInst : _preacherSpawns.values())
  200. if (!AutoSpawnHandler.getInstance().getAutoSpawnInstance(spawnInst.getObjectId(), true).isSpawnActive())
  201. AutoSpawnHandler.getInstance().setSpawnActive(spawnInst, true);
  202. if (!AutoChatHandler.getInstance().getAutoChatInstance(PREACHER_NPC_ID, false).isActive()
  203. && !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. return SingletonHolder._instance;
  278. }
  279. public static long calcContributionScore(long blueCount, long greenCount, long redCount)
  280. {
  281. long contrib = blueCount * BLUE_CONTRIB_POINTS;
  282. contrib += greenCount * GREEN_CONTRIB_POINTS;
  283. contrib += redCount * RED_CONTRIB_POINTS;
  284. return contrib;
  285. }
  286. public static long calcAncientAdenaReward(long blueCount, long greenCount, long redCount)
  287. {
  288. long 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. /**
  408. * returns true if the given date is in Seal Validation or in Quest Event Results period
  409. * @param date
  410. */
  411. public boolean isDateInSealValidPeriod(Calendar date)
  412. {
  413. long nextPeriodChange = getMilliToPeriodChange();
  414. long nextQuestStart = 0;
  415. long nextValidStart = 0;
  416. long tillDate = date.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  417. while ((2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH) < tillDate)
  418. tillDate -= (2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH);
  419. while (tillDate < 0)
  420. tillDate += (2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH);
  421. switch (getCurrentPeriod())
  422. {
  423. case PERIOD_COMP_RECRUITING:
  424. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  425. nextQuestStart = nextValidStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  426. break;
  427. case PERIOD_COMPETITION:
  428. nextValidStart = nextPeriodChange;
  429. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  430. break;
  431. case PERIOD_COMP_RESULTS:
  432. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  433. nextValidStart = nextQuestStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  434. break;
  435. case PERIOD_SEAL_VALIDATION:
  436. nextQuestStart = nextPeriodChange;
  437. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  438. break;
  439. }
  440. if ((nextQuestStart < tillDate && tillDate < nextValidStart)
  441. || (nextValidStart < nextQuestStart && (tillDate < nextValidStart || nextQuestStart < tillDate)))
  442. return false;
  443. return true;
  444. }
  445. public final int getCurrentScore(int cabal)
  446. {
  447. double totalStoneScore = _dawnStoneScore + _duskStoneScore;
  448. switch (cabal)
  449. {
  450. case CABAL_NULL:
  451. return 0;
  452. case CABAL_DAWN:
  453. return Math.round((float) (_dawnStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500)
  454. + _dawnFestivalScore;
  455. case CABAL_DUSK:
  456. return Math.round((float) (_duskStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500)
  457. + _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. }
  644. }
  645. // Festival data is loaded now after the Seven Signs engine data.
  646. }
  647. /**
  648. * Saves all Seven Signs data, both to the database and properties file (if updateSettings = True).
  649. * Often called to preserve data integrity and synchronization with DB, in case of errors.
  650. * <BR>
  651. * If player != null, just that player's data is updated in the database, otherwise all player's data is
  652. * sequentially updated.
  653. *
  654. * @param player
  655. * @param updateSettings
  656. * @throws Exception
  657. */
  658. public void saveSevenSignsData(L2PcInstance player, boolean updateSettings)
  659. {
  660. Connection con = null;
  661. PreparedStatement statement = null;
  662. if (Config.DEBUG)
  663. _log.info("SevenSigns: Saving data to disk.");
  664. try
  665. {
  666. con = L2DatabaseFactory.getInstance().getConnection();
  667. for (StatsSet sevenDat : _signsPlayerData.values())
  668. {
  669. if (player != null)
  670. if (sevenDat.getInteger("charId") != player.getObjectId())
  671. continue;
  672. statement = con.prepareStatement("UPDATE seven_signs SET cabal=?, seal=?, red_stones=?, "
  673. + "green_stones=?, blue_stones=?, " + "ancient_adena_amount=?, contribution_score=? " + "WHERE charId=?");
  674. statement.setString(1, sevenDat.getString("cabal"));
  675. statement.setInt(2, sevenDat.getInteger("seal"));
  676. statement.setInt(3, sevenDat.getInteger("red_stones"));
  677. statement.setInt(4, sevenDat.getInteger("green_stones"));
  678. statement.setInt(5, sevenDat.getInteger("blue_stones"));
  679. statement.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  680. statement.setDouble(7, sevenDat.getDouble("contribution_score"));
  681. statement.setInt(8, sevenDat.getInteger("charId"));
  682. statement.execute();
  683. statement.close();
  684. if (Config.DEBUG)
  685. _log.info("SevenSigns: Updated data in database for char ID " + sevenDat.getInteger("charId") + " ("
  686. + 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=?, " + "festival_cycle=?, ";
  694. for (int i = 0; i < (SevenSignsFestival.FESTIVAL_COUNT); i++)
  695. sqlQuery += "accumulated_bonus" + String.valueOf(i) + "=?, ";
  696. sqlQuery += "date=? WHERE id=0";
  697. statement = con.prepareStatement(sqlQuery);
  698. statement.setInt(1, _currentCycle);
  699. statement.setInt(2, _activePeriod);
  700. statement.setInt(3, _previousWinner);
  701. statement.setDouble(4, _dawnStoneScore);
  702. statement.setInt(5, _dawnFestivalScore);
  703. statement.setDouble(6, _duskStoneScore);
  704. statement.setInt(7, _duskFestivalScore);
  705. statement.setInt(8, _signsSealOwners.get(SEAL_AVARICE));
  706. statement.setInt(9, _signsSealOwners.get(SEAL_GNOSIS));
  707. statement.setInt(10, _signsSealOwners.get(SEAL_STRIFE));
  708. statement.setInt(11, _signsDawnSealTotals.get(SEAL_AVARICE));
  709. statement.setInt(12, _signsDawnSealTotals.get(SEAL_GNOSIS));
  710. statement.setInt(13, _signsDawnSealTotals.get(SEAL_STRIFE));
  711. statement.setInt(14, _signsDuskSealTotals.get(SEAL_AVARICE));
  712. statement.setInt(15, _signsDuskSealTotals.get(SEAL_GNOSIS));
  713. statement.setInt(16, _signsDuskSealTotals.get(SEAL_STRIFE));
  714. statement.setInt(17, SevenSignsFestival.getInstance().getCurrentFestivalCycle());
  715. for (int i = 0; i < SevenSignsFestival.FESTIVAL_COUNT; i++)
  716. statement.setInt(18 + i, SevenSignsFestival.getInstance().getAccumulatedBonus(i));
  717. statement.setInt(18 + SevenSignsFestival.FESTIVAL_COUNT, Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
  718. statement.execute();
  719. statement.close();
  720. con.close();
  721. if (Config.DEBUG)
  722. _log.info("SevenSigns: Updated data in database.");
  723. }
  724. }
  725. catch (SQLException e)
  726. {
  727. _log.severe("SevenSigns: Unable to save data to database: " + e);
  728. }
  729. finally
  730. {
  731. try
  732. {
  733. statement.close();
  734. con.close();
  735. }
  736. catch (Exception e)
  737. {
  738. }
  739. }
  740. }
  741. /**
  742. * Used to reset the cabal details of all players, and update the database.<BR>
  743. * Primarily used when beginning a new cycle, and should otherwise never be called.
  744. */
  745. protected void resetPlayerData()
  746. {
  747. if (Config.DEBUG)
  748. _log.info("SevenSigns: Resetting player data for new event period.");
  749. // Reset each player's contribution data as well as seal and cabal.
  750. for (StatsSet sevenDat : _signsPlayerData.values())
  751. {
  752. int charObjId = sevenDat.getInteger("charId");
  753. // Reset the player's cabal and seal information
  754. sevenDat.set("cabal", "");
  755. sevenDat.set("seal", SEAL_NULL);
  756. sevenDat.set("contribution_score", 0);
  757. _signsPlayerData.put(charObjId, sevenDat);
  758. }
  759. }
  760. /**
  761. * Tests whether the specified player has joined a cabal in the past.
  762. *
  763. * @param player
  764. * @return boolean hasRegistered
  765. */
  766. private boolean hasRegisteredBefore(L2PcInstance player)
  767. {
  768. return _signsPlayerData.containsKey(player.getObjectId());
  769. }
  770. /**
  771. * Used to specify cabal-related details for the specified player. This method
  772. * checks to see if the player has registered before and will update the database
  773. * if necessary.
  774. * <BR>
  775. * Returns the cabal ID the player has joined.
  776. *
  777. * @param player
  778. * @param chosenCabal
  779. * @param chosenSeal
  780. * @return int cabal
  781. */
  782. public int setPlayerInfo(L2PcInstance player, int chosenCabal, int chosenSeal)
  783. {
  784. int charObjId = player.getObjectId();
  785. Connection con = null;
  786. PreparedStatement statement = null;
  787. StatsSet currPlayerData = getPlayerData(player);
  788. if (currPlayerData != null)
  789. {
  790. // If the seal validation period has passed,
  791. // cabal information was removed and so "re-register" player
  792. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  793. currPlayerData.set("seal", chosenSeal);
  794. _signsPlayerData.put(charObjId, currPlayerData);
  795. }
  796. else
  797. {
  798. currPlayerData = new StatsSet();
  799. currPlayerData.set("charId", charObjId);
  800. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  801. currPlayerData.set("seal", chosenSeal);
  802. currPlayerData.set("red_stones", 0);
  803. currPlayerData.set("green_stones", 0);
  804. currPlayerData.set("blue_stones", 0);
  805. currPlayerData.set("ancient_adena_amount", 0);
  806. currPlayerData.set("contribution_score", 0);
  807. _signsPlayerData.put(charObjId, currPlayerData);
  808. // Update data in database, as we have a new player signing up.
  809. try
  810. {
  811. con = L2DatabaseFactory.getInstance().getConnection();
  812. statement = con.prepareStatement("INSERT INTO seven_signs (charId, cabal, seal) VALUES (?,?,?)");
  813. statement.setInt(1, charObjId);
  814. statement.setString(2, getCabalShortName(chosenCabal));
  815. statement.setInt(3, chosenSeal);
  816. statement.execute();
  817. statement.close();
  818. con.close();
  819. if (Config.DEBUG)
  820. _log.info("SevenSigns: Inserted data in DB for char ID " + currPlayerData.getInteger("charId") + " ("
  821. + currPlayerData.getString("cabal") + ")");
  822. }
  823. catch (SQLException e)
  824. {
  825. _log.severe("SevenSigns: Failed to save data: " + e);
  826. }
  827. finally
  828. {
  829. try
  830. {
  831. statement.close();
  832. con.close();
  833. }
  834. catch (Exception e)
  835. {
  836. }
  837. }
  838. }
  839. // Increasing Seal total score for the player chosen Seal.
  840. if ("dawn".equals(currPlayerData.getString("cabal")))
  841. _signsDawnSealTotals.put(chosenSeal, _signsDawnSealTotals.get(chosenSeal) + 1);
  842. else
  843. _signsDuskSealTotals.put(chosenSeal, _signsDuskSealTotals.get(chosenSeal) + 1);
  844. saveSevenSignsData(player, true);
  845. if (Config.DEBUG)
  846. _log.info("SevenSigns: " + player.getName() + " has joined the " + getCabalName(chosenCabal) + " for the "
  847. + getSealName(chosenSeal, false) + "!");
  848. return chosenCabal;
  849. }
  850. /**
  851. * Returns the amount of ancient adena the specified player can claim, if any.<BR>
  852. * If removeReward = True, all the ancient adena owed to them is removed, then
  853. * DB is updated.
  854. *
  855. * @param player
  856. * @param removeReward
  857. * @return int rewardAmount
  858. */
  859. public int getAncientAdenaReward(L2PcInstance player, boolean removeReward)
  860. {
  861. StatsSet currPlayer = getPlayerData(player);
  862. int rewardAmount = currPlayer.getInteger("ancient_adena_amount");
  863. currPlayer.set("red_stones", 0);
  864. currPlayer.set("green_stones", 0);
  865. currPlayer.set("blue_stones", 0);
  866. currPlayer.set("ancient_adena_amount", 0);
  867. if (removeReward)
  868. {
  869. _signsPlayerData.put(player.getObjectId(), currPlayer);
  870. saveSevenSignsData(player, true);
  871. }
  872. return rewardAmount;
  873. }
  874. /**
  875. * Used to add the specified player's seal stone contribution points
  876. * to the current total for their cabal. Returns the point score the
  877. * contribution was worth.
  878. *
  879. * Each stone count <B>must be</B> broken down and specified by the stone's color.
  880. *
  881. * @param player
  882. * @param blueCount
  883. * @param greenCount
  884. * @param redCount
  885. * @return int contribScore
  886. */
  887. public long addPlayerStoneContrib(L2PcInstance player, long blueCount, long greenCount, long redCount)
  888. {
  889. StatsSet currPlayer = getPlayerData(player);
  890. long contribScore = calcContributionScore(blueCount, greenCount, redCount);
  891. long totalAncientAdena = currPlayer.getLong("ancient_adena_amount") + calcAncientAdenaReward(blueCount, greenCount, redCount);
  892. long totalContribScore = currPlayer.getLong("contribution_score") + contribScore;
  893. if (totalContribScore > Config.ALT_MAXIMUM_PLAYER_CONTRIB)
  894. return -1;
  895. currPlayer.set("red_stones", currPlayer.getInteger("red_stones") + redCount);
  896. currPlayer.set("green_stones", currPlayer.getInteger("green_stones") + greenCount);
  897. currPlayer.set("blue_stones", currPlayer.getInteger("blue_stones") + blueCount);
  898. currPlayer.set("ancient_adena_amount", totalAncientAdena);
  899. currPlayer.set("contribution_score", totalContribScore);
  900. _signsPlayerData.put(player.getObjectId(), currPlayer);
  901. switch (getPlayerCabal(player))
  902. {
  903. case CABAL_DAWN:
  904. _dawnStoneScore += contribScore;
  905. break;
  906. case CABAL_DUSK:
  907. _duskStoneScore += contribScore;
  908. break;
  909. }
  910. saveSevenSignsData(player, true);
  911. if (Config.DEBUG)
  912. _log.info("SevenSigns: " + player.getName() + " contributed " + contribScore + " seal stone points to their cabal.");
  913. return contribScore;
  914. }
  915. /**
  916. * Adds the specified number of festival points to the specified cabal.
  917. * Remember, the same number of points are <B>deducted from the rival cabal</B>
  918. * to maintain proportionality.
  919. *
  920. * @param cabal
  921. * @param amount
  922. */
  923. public void addFestivalScore(int cabal, int amount)
  924. {
  925. if (cabal == CABAL_DUSK)
  926. {
  927. _duskFestivalScore += amount;
  928. // To prevent negative scores!
  929. if (_dawnFestivalScore >= amount)
  930. _dawnFestivalScore -= amount;
  931. }
  932. else
  933. {
  934. _dawnFestivalScore += amount;
  935. if (_duskFestivalScore >= amount)
  936. _duskFestivalScore -= amount;
  937. }
  938. }
  939. /**
  940. * Send info on the current Seven Signs period to the specified player.
  941. *
  942. * @param player
  943. */
  944. public void sendCurrentPeriodMsg(L2PcInstance player)
  945. {
  946. SystemMessage sm = null;
  947. switch (getCurrentPeriod())
  948. {
  949. case PERIOD_COMP_RECRUITING:
  950. sm = new SystemMessage(SystemMessageId.PREPARATIONS_PERIOD_BEGUN);
  951. break;
  952. case PERIOD_COMPETITION:
  953. sm = new SystemMessage(SystemMessageId.COMPETITION_PERIOD_BEGUN);
  954. break;
  955. case PERIOD_COMP_RESULTS:
  956. sm = new SystemMessage(SystemMessageId.RESULTS_PERIOD_BEGUN);
  957. break;
  958. case PERIOD_SEAL_VALIDATION:
  959. sm = new SystemMessage(SystemMessageId.VALIDATION_PERIOD_BEGUN);
  960. break;
  961. }
  962. player.sendPacket(sm);
  963. }
  964. /**
  965. * Sends the built-in system message specified by sysMsgId to all online players.
  966. *
  967. * @param sysMsgId
  968. */
  969. public void sendMessageToAll(SystemMessageId sysMsgId)
  970. {
  971. SystemMessage sm = new SystemMessage(sysMsgId);
  972. Broadcast.toAllOnlinePlayers(sm);
  973. }
  974. /**
  975. * Used to initialize the seals for each cabal. (Used at startup or at beginning of a new cycle).
  976. * This method should be called after <B>resetSeals()</B> and <B>calcNewSealOwners()</B> on a new cycle.
  977. */
  978. protected void initializeSeals()
  979. {
  980. for (Integer currSeal : _signsSealOwners.keySet())
  981. {
  982. int sealOwner = _signsSealOwners.get(currSeal);
  983. if (sealOwner != CABAL_NULL)
  984. if (isSealValidationPeriod())
  985. _log.info("SevenSigns: The " + getCabalName(sealOwner) + " have won the " + getSealName(currSeal, false) + ".");
  986. else
  987. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " is currently owned by " + getCabalName(sealOwner) + ".");
  988. else
  989. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " remains unclaimed.");
  990. }
  991. }
  992. /**
  993. * Only really used at the beginning of a new cycle, this method resets all seal-related data.
  994. */
  995. protected void resetSeals()
  996. {
  997. _signsDawnSealTotals.put(SEAL_AVARICE, 0);
  998. _signsDawnSealTotals.put(SEAL_GNOSIS, 0);
  999. _signsDawnSealTotals.put(SEAL_STRIFE, 0);
  1000. _signsDuskSealTotals.put(SEAL_AVARICE, 0);
  1001. _signsDuskSealTotals.put(SEAL_GNOSIS, 0);
  1002. _signsDuskSealTotals.put(SEAL_STRIFE, 0);
  1003. }
  1004. /**
  1005. * Calculates the ownership of the three Seals of the Seven Signs,
  1006. * based on various criterion.
  1007. * <BR><BR>
  1008. * Should only ever called at the beginning of a new cycle.
  1009. */
  1010. protected void calcNewSealOwners()
  1011. {
  1012. if (Config.DEBUG)
  1013. {
  1014. _log.info("SevenSigns: (Avarice) Dawn = " + _signsDawnSealTotals.get(SEAL_AVARICE) + ", Dusk = "
  1015. + _signsDuskSealTotals.get(SEAL_AVARICE));
  1016. _log.info("SevenSigns: (Gnosis) Dawn = " + _signsDawnSealTotals.get(SEAL_GNOSIS) + ", Dusk = "
  1017. + _signsDuskSealTotals.get(SEAL_GNOSIS));
  1018. _log.info("SevenSigns: (Strife) Dawn = " + _signsDawnSealTotals.get(SEAL_STRIFE) + ", Dusk = "
  1019. + _signsDuskSealTotals.get(SEAL_STRIFE));
  1020. }
  1021. for (Integer currSeal : _signsDawnSealTotals.keySet())
  1022. {
  1023. int prevSealOwner = _signsSealOwners.get(currSeal);
  1024. int newSealOwner = CABAL_NULL;
  1025. int dawnProportion = getSealProportion(currSeal, CABAL_DAWN);
  1026. int totalDawnMembers = getTotalMembers(CABAL_DAWN) == 0 ? 1 : getTotalMembers(CABAL_DAWN);
  1027. int dawnPercent = Math.round(((float) dawnProportion / (float) totalDawnMembers) * 100);
  1028. int duskProportion = getSealProportion(currSeal, CABAL_DUSK);
  1029. int totalDuskMembers = getTotalMembers(CABAL_DUSK) == 0 ? 1 : getTotalMembers(CABAL_DUSK);
  1030. int duskPercent = Math.round(((float) duskProportion / (float) totalDuskMembers) * 100);
  1031. /*
  1032. * - If a Seal was already closed or owned by the opponent and the new winner wants
  1033. * to assume ownership of the Seal, 35% or more of the members of the Cabal must
  1034. * have chosen the Seal. If they chose less than 35%, they cannot own the Seal.
  1035. *
  1036. * - If the Seal was owned by the winner in the previous Seven Signs, they can retain
  1037. * that seal if 10% or more members have chosen it. If they want to possess a new Seal,
  1038. * at least 35% of the members of the Cabal must have chosen the new Seal.
  1039. */
  1040. switch (prevSealOwner)
  1041. {
  1042. case CABAL_NULL:
  1043. switch (getCabalHighestScore())
  1044. {
  1045. case CABAL_NULL:
  1046. newSealOwner = CABAL_NULL;
  1047. break;
  1048. case CABAL_DAWN:
  1049. if (dawnPercent >= 35)
  1050. newSealOwner = CABAL_DAWN;
  1051. else
  1052. newSealOwner = CABAL_NULL;
  1053. break;
  1054. case CABAL_DUSK:
  1055. if (duskPercent >= 35)
  1056. newSealOwner = CABAL_DUSK;
  1057. else
  1058. newSealOwner = CABAL_NULL;
  1059. break;
  1060. }
  1061. break;
  1062. case CABAL_DAWN:
  1063. switch (getCabalHighestScore())
  1064. {
  1065. case CABAL_NULL:
  1066. if (dawnPercent >= 10)
  1067. newSealOwner = CABAL_DAWN;
  1068. else
  1069. newSealOwner = CABAL_NULL;
  1070. break;
  1071. case CABAL_DAWN:
  1072. if (dawnPercent >= 10)
  1073. newSealOwner = CABAL_DAWN;
  1074. else
  1075. newSealOwner = CABAL_NULL;
  1076. break;
  1077. case CABAL_DUSK:
  1078. if (duskPercent >= 35)
  1079. newSealOwner = CABAL_DUSK;
  1080. else if (dawnPercent >= 10)
  1081. newSealOwner = CABAL_DAWN;
  1082. else
  1083. newSealOwner = CABAL_NULL;
  1084. break;
  1085. }
  1086. break;
  1087. case CABAL_DUSK:
  1088. switch (getCabalHighestScore())
  1089. {
  1090. case CABAL_NULL:
  1091. if (duskPercent >= 10)
  1092. newSealOwner = CABAL_DUSK;
  1093. else
  1094. newSealOwner = CABAL_NULL;
  1095. break;
  1096. case CABAL_DAWN:
  1097. if (dawnPercent >= 35)
  1098. newSealOwner = CABAL_DAWN;
  1099. else if (duskPercent >= 10)
  1100. newSealOwner = CABAL_DUSK;
  1101. else
  1102. newSealOwner = CABAL_NULL;
  1103. break;
  1104. case CABAL_DUSK:
  1105. if (duskPercent >= 10)
  1106. newSealOwner = CABAL_DUSK;
  1107. else
  1108. newSealOwner = CABAL_NULL;
  1109. break;
  1110. }
  1111. break;
  1112. }
  1113. _signsSealOwners.put(currSeal, newSealOwner);
  1114. // Alert all online players to new seal status.
  1115. switch (currSeal)
  1116. {
  1117. case SEAL_AVARICE:
  1118. if (newSealOwner == CABAL_DAWN)
  1119. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_AVARICE);
  1120. else if (newSealOwner == CABAL_DUSK)
  1121. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_AVARICE);
  1122. break;
  1123. case SEAL_GNOSIS:
  1124. if (newSealOwner == CABAL_DAWN)
  1125. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_GNOSIS);
  1126. else if (newSealOwner == CABAL_DUSK)
  1127. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_GNOSIS);
  1128. break;
  1129. case SEAL_STRIFE:
  1130. if (newSealOwner == CABAL_DAWN)
  1131. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_STRIFE);
  1132. else if (newSealOwner == CABAL_DUSK)
  1133. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_STRIFE);
  1134. CastleManager.getInstance().validateTaxes(newSealOwner);
  1135. break;
  1136. }
  1137. }
  1138. }
  1139. /**
  1140. * This method is called to remove all players from catacombs and
  1141. * necropolises, who belong to the losing cabal.
  1142. * <BR><BR>
  1143. * Should only ever called at the beginning of Seal Validation.
  1144. */
  1145. protected void teleLosingCabalFromDungeons(String compWinner)
  1146. {
  1147. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  1148. //synchronized (L2World.getInstance().getAllPlayers())
  1149. {
  1150. for (L2PcInstance onlinePlayer : pls)
  1151. {
  1152. StatsSet currPlayer = getPlayerData(onlinePlayer);
  1153. if (isSealValidationPeriod() || isCompResultsPeriod())
  1154. {
  1155. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && !currPlayer.getString("cabal").equals(compWinner))
  1156. {
  1157. onlinePlayer.teleToLocation(MapRegionTable.TeleportWhereType.Town);
  1158. onlinePlayer.setIsIn7sDungeon(false);
  1159. onlinePlayer.sendMessage("You have been teleported to the nearest town due to the beginning of the Seal Validation period.");
  1160. }
  1161. }
  1162. else
  1163. {
  1164. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && !currPlayer.getString("cabal").isEmpty())
  1165. {
  1166. onlinePlayer.teleToLocation(MapRegionTable.TeleportWhereType.Town);
  1167. onlinePlayer.setIsIn7sDungeon(false);
  1168. onlinePlayer.sendMessage("You have been teleported to the nearest town because you have not signed for any cabal.");
  1169. }
  1170. }
  1171. }
  1172. }
  1173. }
  1174. /**
  1175. * The primary controller of period change of the Seven Signs system.
  1176. * This runs all related tasks depending on the period that is about to begin.
  1177. *
  1178. * @author Tempy
  1179. */
  1180. protected class SevenSignsPeriodChange implements Runnable
  1181. {
  1182. public void run()
  1183. {
  1184. /*
  1185. * Remember the period check here refers to the period just ENDED!
  1186. */
  1187. final int periodEnded = getCurrentPeriod();
  1188. _activePeriod++;
  1189. switch (periodEnded)
  1190. {
  1191. case PERIOD_COMP_RECRUITING: // Initialization
  1192. // Start the Festival of Darkness cycle.
  1193. SevenSignsFestival.getInstance().startFestivalManager();
  1194. // Send message that Competition has begun.
  1195. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_BEGUN);
  1196. break;
  1197. case PERIOD_COMPETITION: // Results Calculation
  1198. // Send message that Competition has ended.
  1199. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_ENDED);
  1200. int compWinner = getCabalHighestScore();
  1201. // Schedule a stop of the festival engine and reward highest ranking members from cycle
  1202. SevenSignsFestival.getInstance().getFestivalManagerSchedule().cancel(false);
  1203. SevenSignsFestival.getInstance().rewardHighestRanked();
  1204. calcNewSealOwners();
  1205. switch (compWinner)
  1206. {
  1207. case CABAL_DAWN:
  1208. sendMessageToAll(SystemMessageId.DAWN_WON);
  1209. break;
  1210. case CABAL_DUSK:
  1211. sendMessageToAll(SystemMessageId.DUSK_WON);
  1212. break;
  1213. }
  1214. _previousWinner = compWinner;
  1215. break;
  1216. case PERIOD_COMP_RESULTS: // Seal Validation
  1217. // Perform initial Seal Validation set up.
  1218. initializeSeals();
  1219. //Buff/Debuff members of the event when Seal of Strife captured.
  1220. GiveCPMult(getSealOwner(SEAL_STRIFE));
  1221. // Send message that Seal Validation has begun.
  1222. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_BEGUN);
  1223. _log.info("SevenSigns: The " + getCabalName(_previousWinner) + " have won the competition with "
  1224. + getCurrentScore(_previousWinner) + " points!");
  1225. break;
  1226. case PERIOD_SEAL_VALIDATION: // Reset for New Cycle
  1227. // Ensure a cycle restart when this period ends.
  1228. _activePeriod = PERIOD_COMP_RECRUITING;
  1229. // Send message that Seal Validation has ended.
  1230. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_ENDED);
  1231. //Clear Seal of Strife influence.
  1232. RemoveCPMult();
  1233. // Reset all data
  1234. resetPlayerData();
  1235. resetSeals();
  1236. _currentCycle++;
  1237. // Reset all Festival-related data and remove any unused blood offerings.
  1238. // NOTE: A full update of Festival data in the database is also performed.
  1239. SevenSignsFestival.getInstance().resetFestivalData(false);
  1240. _dawnStoneScore = 0;
  1241. _duskStoneScore = 0;
  1242. _dawnFestivalScore = 0;
  1243. _duskFestivalScore = 0;
  1244. break;
  1245. }
  1246. // Make sure all Seven Signs data is saved for future use.
  1247. saveSevenSignsData(null, true);
  1248. teleLosingCabalFromDungeons(getCabalShortName(getCabalHighestScore()));
  1249. SSQInfo ss = new SSQInfo();
  1250. Broadcast.toAllOnlinePlayers(ss);
  1251. spawnSevenSignsNPC();
  1252. _log.info("SevenSigns: The " + getCurrentPeriodName() + " period has begun!");
  1253. setCalendarForNextPeriodChange();
  1254. // make sure that all the scheduled siege dates are in the Seal Validation period
  1255. List<Castle> castles = CastleManager.getInstance().getCastles();
  1256. for (Castle castle : castles)
  1257. {
  1258. castle.getSiege().correctSiegeDateTime();
  1259. }
  1260. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  1261. ThreadPoolManager.getInstance().scheduleGeneral(sspc, getMilliToPeriodChange());
  1262. }
  1263. }
  1264. public boolean CheckIsDawnPostingTicket(int itemId)
  1265. {
  1266. //TODO I think it should be some kind of a list in the datapack for compare;
  1267. if (itemId > 6114 && itemId < 6175)
  1268. return true;
  1269. if (itemId > 6801 && itemId < 6812)
  1270. return true;
  1271. if (itemId > 7997 && itemId < 8008)
  1272. return true;
  1273. if (itemId > 7940 && itemId < 7951)
  1274. return true;
  1275. if (itemId > 6294 && itemId < 6307)
  1276. return true;
  1277. if (itemId > 6831 && itemId < 6834)
  1278. return true;
  1279. if (itemId > 8027 && itemId < 8030)
  1280. return true;
  1281. if (itemId > 7970 && itemId < 7973)
  1282. return true;
  1283. return false;
  1284. }
  1285. public boolean CheckIsRookiePostingTicket(int itemId)
  1286. {
  1287. //TODO I think it should be some kind of a list in the datapack for compare;
  1288. if (itemId > 6174 && itemId < 6295)
  1289. return true;
  1290. if (itemId > 6811 && itemId < 6832)
  1291. return true;
  1292. if (itemId > 7950 && itemId < 7971)
  1293. return true;
  1294. if (itemId > 8007 && itemId < 8028)
  1295. return true;
  1296. return false;
  1297. }
  1298. public void GiveCPMult(int StrifeOwner)
  1299. {
  1300. //Gives "Victor of War" passive skill to all online characters with Cabal, which controls Seal of Strife
  1301. for (L2PcInstance character : L2World.getInstance().getAllPlayers().values())
  1302. {
  1303. if (getPlayerCabal(character) != SevenSigns.CABAL_NULL)
  1304. if (getPlayerCabal(character) == StrifeOwner)
  1305. character.addSkill(SkillTable.getInstance().getInfo(5074, 1));
  1306. else
  1307. //Gives "The Vanquished of War" passive skill to all online characters with Cabal, which does not control Seal of Strife
  1308. character.addSkill(SkillTable.getInstance().getInfo(5075, 1));
  1309. }
  1310. }
  1311. public void RemoveCPMult()
  1312. {
  1313. for (L2PcInstance character : L2World.getInstance().getAllPlayers().values())
  1314. {
  1315. //Remove SevenSigns' buffs/debuffs.
  1316. character.removeSkill(SkillTable.getInstance().getInfo(5074, 1));
  1317. character.removeSkill(SkillTable.getInstance().getInfo(5075, 1));
  1318. }
  1319. }
  1320. public boolean CheckSummonConditions(L2PcInstance activeChar)
  1321. {
  1322. if (activeChar == null)
  1323. return true;
  1324. //Golems cannot be summoned by Dusk when the Seal of Strife is controlled by the Dawn
  1325. if (isSealValidationPeriod())
  1326. if (getSealOwner(SEAL_STRIFE) == CABAL_DAWN)
  1327. if (getPlayerCabal(activeChar) == CABAL_DUSK)
  1328. {
  1329. activeChar.sendMessage("You cannot summon Siege Golem or Cannon while Seal of Strife posessed by Lords of Dawn.");
  1330. return true;
  1331. }
  1332. return false;
  1333. }
  1334. @SuppressWarnings("synthetic-access")
  1335. private static class SingletonHolder
  1336. {
  1337. protected static final SevenSigns _instance = new SevenSigns();
  1338. }
  1339. }