SevenSigns.java 51 KB

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