SevenSigns.java 51 KB

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