SevenSigns.java 54 KB

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