SevenSigns.java 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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.procedure.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.StatsSet;
  39. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  40. import com.l2jserver.gameserver.model.entity.Castle;
  41. import com.l2jserver.gameserver.model.quest.Quest;
  42. import com.l2jserver.gameserver.network.SystemMessageId;
  43. import com.l2jserver.gameserver.network.serverpackets.SSQInfo;
  44. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  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 RECORD_SEVEN_SIGNS_ID = 5707;
  75. public static final int CERTIFICATE_OF_APPROVAL_ID = 6388;
  76. public static final int RECORD_SEVEN_SIGNS_COST = 500;
  77. public static final int ADENA_JOIN_DAWN_COST = 50000;
  78. // NPC Related Constants \\
  79. public static final int ORATOR_NPC_ID = 31094;
  80. public static final int PREACHER_NPC_ID = 31093;
  81. public static final int MAMMON_MERCHANT_ID = 31113;
  82. public static final int MAMMON_BLACKSMITH_ID = 31126;
  83. public static final int MAMMON_MARKETEER_ID = 31092;
  84. public static final int LILITH_NPC_ID = 25283;
  85. public static final int ANAKIM_NPC_ID = 25286;
  86. public static final int CREST_OF_DAWN_ID = 31170;
  87. public static final int CREST_OF_DUSK_ID = 31171;
  88. // Seal Stone Related Constants
  89. public static final int SEAL_STONE_BLUE_ID = 6360;
  90. public static final int SEAL_STONE_GREEN_ID = 6361;
  91. public static final int SEAL_STONE_RED_ID = 6362;
  92. public static final int[] SEAL_STONE_IDS = { SEAL_STONE_BLUE_ID, SEAL_STONE_GREEN_ID, SEAL_STONE_RED_ID };
  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. protected Map<Integer, StatsSet> _signsPlayerData;
  110. private Map<Integer, Integer> _signsSealOwners;
  111. private Map<Integer, Integer> _signsDuskSealTotals;
  112. private Map<Integer, Integer> _signsDawnSealTotals;
  113. private AutoSpawnInstance _merchantSpawn;
  114. private AutoSpawnInstance _blacksmithSpawn;
  115. private AutoSpawnInstance _lilithSpawn;
  116. private AutoSpawnInstance _anakimSpawn;
  117. private Map<Integer, AutoSpawnInstance> _crestofdawnspawns;
  118. private Map<Integer, AutoSpawnInstance> _crestofduskspawns;
  119. private Map<Integer, AutoSpawnInstance> _oratorSpawns;
  120. private Map<Integer, AutoSpawnInstance> _preacherSpawns;
  121. private 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. protected SevenSigns()
  139. {
  140. _signsPlayerData = new FastMap<>();
  141. _signsSealOwners = new FastMap<>();
  142. _signsDuskSealTotals = new FastMap<>();
  143. _signsDawnSealTotals = new FastMap<>();
  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. * @return
  474. */
  475. public boolean isDateInSealValidPeriod(Calendar date)
  476. {
  477. long nextPeriodChange = getMilliToPeriodChange();
  478. long nextQuestStart = 0;
  479. long nextValidStart = 0;
  480. long tillDate = date.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  481. while ((2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH) < tillDate)
  482. tillDate -= (2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH);
  483. while (tillDate < 0)
  484. tillDate += (2 * PERIOD_MAJOR_LENGTH + 2 * PERIOD_MINOR_LENGTH);
  485. switch (getCurrentPeriod())
  486. {
  487. case PERIOD_COMP_RECRUITING:
  488. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  489. nextQuestStart = nextValidStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  490. break;
  491. case PERIOD_COMPETITION:
  492. nextValidStart = nextPeriodChange;
  493. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  494. break;
  495. case PERIOD_COMP_RESULTS:
  496. nextQuestStart = nextPeriodChange + PERIOD_MAJOR_LENGTH;
  497. nextValidStart = nextQuestStart + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  498. break;
  499. case PERIOD_SEAL_VALIDATION:
  500. nextQuestStart = nextPeriodChange;
  501. nextValidStart = nextPeriodChange + PERIOD_MAJOR_LENGTH + PERIOD_MINOR_LENGTH;
  502. break;
  503. }
  504. if ((nextQuestStart < tillDate && tillDate < nextValidStart)
  505. || (nextValidStart < nextQuestStart && (tillDate < nextValidStart || nextQuestStart < tillDate)))
  506. return false;
  507. return true;
  508. }
  509. public final int getCurrentScore(int cabal)
  510. {
  511. double totalStoneScore = _dawnStoneScore + _duskStoneScore;
  512. switch (cabal)
  513. {
  514. case CABAL_NULL:
  515. return 0;
  516. case CABAL_DAWN:
  517. return Math.round((float) (_dawnStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500)
  518. + _dawnFestivalScore;
  519. case CABAL_DUSK:
  520. return Math.round((float) (_duskStoneScore / ((float) totalStoneScore == 0 ? 1 : totalStoneScore)) * 500)
  521. + _duskFestivalScore;
  522. }
  523. return 0;
  524. }
  525. public final double getCurrentStoneScore(int cabal)
  526. {
  527. switch (cabal)
  528. {
  529. case CABAL_NULL:
  530. return 0;
  531. case CABAL_DAWN:
  532. return _dawnStoneScore;
  533. case CABAL_DUSK:
  534. return _duskStoneScore;
  535. }
  536. return 0;
  537. }
  538. public final int getCurrentFestivalScore(int cabal)
  539. {
  540. switch (cabal)
  541. {
  542. case CABAL_NULL:
  543. return 0;
  544. case CABAL_DAWN:
  545. return _dawnFestivalScore;
  546. case CABAL_DUSK:
  547. return _duskFestivalScore;
  548. }
  549. return 0;
  550. }
  551. public final int getCabalHighestScore()
  552. {
  553. if (getCurrentScore(CABAL_DUSK) == getCurrentScore(CABAL_DAWN))
  554. return CABAL_NULL;
  555. else if (getCurrentScore(CABAL_DUSK) > getCurrentScore(CABAL_DAWN))
  556. return CABAL_DUSK;
  557. else
  558. return CABAL_DAWN;
  559. }
  560. public final int getSealOwner(int seal)
  561. {
  562. return _signsSealOwners.get(seal);
  563. }
  564. public final int getSealProportion(int seal, int cabal)
  565. {
  566. if (cabal == CABAL_NULL)
  567. return 0;
  568. else if (cabal == CABAL_DUSK)
  569. return _signsDuskSealTotals.get(seal);
  570. else
  571. return _signsDawnSealTotals.get(seal);
  572. }
  573. public final int getTotalMembers(int cabal)
  574. {
  575. int cabalMembers = 0;
  576. String cabalName = getCabalShortName(cabal);
  577. for (StatsSet sevenDat : _signsPlayerData.values())
  578. if (sevenDat.getString("cabal").equals(cabalName))
  579. cabalMembers++;
  580. return cabalMembers;
  581. }
  582. public int getPlayerStoneContrib(int objectId)
  583. {
  584. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  585. if (currPlayer == null)
  586. return 0;
  587. int stoneCount = 0;
  588. stoneCount += currPlayer.getInteger("red_stones");
  589. stoneCount += currPlayer.getInteger("green_stones");
  590. stoneCount += currPlayer.getInteger("blue_stones");
  591. return stoneCount;
  592. }
  593. public int getPlayerContribScore(int objectId)
  594. {
  595. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  596. if (currPlayer == null)
  597. return 0;
  598. return currPlayer.getInteger("contribution_score");
  599. }
  600. public int getPlayerAdenaCollect(int objectId)
  601. {
  602. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  603. if (currPlayer == null)
  604. return 0;
  605. return currPlayer.getInteger("ancient_adena_amount");
  606. }
  607. public int getPlayerSeal(int objectId)
  608. {
  609. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  610. if (currPlayer == null)
  611. return SEAL_NULL;
  612. return currPlayer.getInteger("seal");
  613. }
  614. public int getPlayerCabal(int objectId)
  615. {
  616. final StatsSet currPlayer = _signsPlayerData.get(objectId);
  617. if (currPlayer == null)
  618. return CABAL_NULL;
  619. String playerCabal = currPlayer.getString("cabal");
  620. if (playerCabal.equalsIgnoreCase("dawn"))
  621. return CABAL_DAWN;
  622. else if (playerCabal.equalsIgnoreCase("dusk"))
  623. return CABAL_DUSK;
  624. else
  625. return CABAL_NULL;
  626. }
  627. /**
  628. * Restores all Seven Signs data and settings, usually called at server startup.
  629. */
  630. protected void restoreSevenSignsData()
  631. {
  632. Connection con = null;
  633. try
  634. {
  635. con = L2DatabaseFactory.getInstance().getConnection();
  636. PreparedStatement statement = con.prepareStatement(LOAD_DATA);
  637. ResultSet rset = statement.executeQuery();
  638. StatsSet sevenDat = null;
  639. int charObjId;
  640. while (rset.next())
  641. {
  642. charObjId = rset.getInt("charId");
  643. sevenDat = new StatsSet();
  644. sevenDat.set("charId", charObjId);
  645. sevenDat.set("cabal", rset.getString("cabal"));
  646. sevenDat.set("seal", rset.getInt("seal"));
  647. sevenDat.set("red_stones", rset.getInt("red_stones"));
  648. sevenDat.set("green_stones", rset.getInt("green_stones"));
  649. sevenDat.set("blue_stones", rset.getInt("blue_stones"));
  650. sevenDat.set("ancient_adena_amount", rset.getDouble("ancient_adena_amount"));
  651. sevenDat.set("contribution_score", rset.getDouble("contribution_score"));
  652. if (Config.DEBUG)
  653. _log.info("SevenSigns: Loaded data from DB for char ID " + charObjId + " (" + sevenDat.getString("cabal") + ")");
  654. _signsPlayerData.put(charObjId, sevenDat);
  655. }
  656. rset.close();
  657. statement.close();
  658. statement = con.prepareStatement(LOAD_STATUS);
  659. rset = statement.executeQuery();
  660. while (rset.next())
  661. {
  662. _currentCycle = rset.getInt("current_cycle");
  663. _activePeriod = rset.getInt("active_period");
  664. _previousWinner = rset.getInt("previous_winner");
  665. _dawnStoneScore = rset.getDouble("dawn_stone_score");
  666. _dawnFestivalScore = rset.getInt("dawn_festival_score");
  667. _duskStoneScore = rset.getDouble("dusk_stone_score");
  668. _duskFestivalScore = rset.getInt("dusk_festival_score");
  669. _signsSealOwners.put(SEAL_AVARICE, rset.getInt("avarice_owner"));
  670. _signsSealOwners.put(SEAL_GNOSIS, rset.getInt("gnosis_owner"));
  671. _signsSealOwners.put(SEAL_STRIFE, rset.getInt("strife_owner"));
  672. _signsDawnSealTotals.put(SEAL_AVARICE, rset.getInt("avarice_dawn_score"));
  673. _signsDawnSealTotals.put(SEAL_GNOSIS, rset.getInt("gnosis_dawn_score"));
  674. _signsDawnSealTotals.put(SEAL_STRIFE, rset.getInt("strife_dawn_score"));
  675. _signsDuskSealTotals.put(SEAL_AVARICE, rset.getInt("avarice_dusk_score"));
  676. _signsDuskSealTotals.put(SEAL_GNOSIS, rset.getInt("gnosis_dusk_score"));
  677. _signsDuskSealTotals.put(SEAL_STRIFE, rset.getInt("strife_dusk_score"));
  678. _lastSave.setTimeInMillis(rset.getLong("date"));
  679. }
  680. rset.close();
  681. statement.close();
  682. }
  683. catch (SQLException e)
  684. {
  685. _log.log(Level.SEVERE, "SevenSigns: Unable to load Seven Signs data from database: " + e.getMessage(), e);
  686. }
  687. finally
  688. {
  689. L2DatabaseFactory.close(con);
  690. }
  691. // Festival data is loaded now after the Seven Signs engine data.
  692. }
  693. /**
  694. * Saves all Seven Signs player data.<br>
  695. * Should be called on period change and shutdown only.
  696. */
  697. public void saveSevenSignsData()
  698. {
  699. if (Config.DEBUG)
  700. _log.info("SevenSigns: Saving data to disk.");
  701. Connection con = null;
  702. try
  703. {
  704. con = L2DatabaseFactory.getInstance().getConnection();
  705. PreparedStatement statement = con.prepareStatement(UPDATE_PLAYER);
  706. for (StatsSet sevenDat : _signsPlayerData.values())
  707. {
  708. statement.setString(1, sevenDat.getString("cabal"));
  709. statement.setInt(2, sevenDat.getInteger("seal"));
  710. statement.setInt(3, sevenDat.getInteger("red_stones"));
  711. statement.setInt(4, sevenDat.getInteger("green_stones"));
  712. statement.setInt(5, sevenDat.getInteger("blue_stones"));
  713. statement.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  714. statement.setDouble(7, sevenDat.getDouble("contribution_score"));
  715. statement.setInt(8, sevenDat.getInteger("charId"));
  716. statement.execute();
  717. statement.clearParameters();
  718. if (Config.DEBUG)
  719. _log.info("SevenSigns: Updated data in database for char ID " + sevenDat.getInteger("charId") + " (" + sevenDat.getString("cabal") + ")");
  720. }
  721. statement.close();
  722. }
  723. catch (SQLException e)
  724. {
  725. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  726. }
  727. finally
  728. {
  729. L2DatabaseFactory.close(con);
  730. }
  731. }
  732. public final void saveSevenSignsData(int objectId)
  733. {
  734. StatsSet sevenDat = _signsPlayerData.get(objectId);
  735. if (sevenDat == null)
  736. return;
  737. Connection con = null;
  738. try
  739. {
  740. con = L2DatabaseFactory.getInstance().getConnection();
  741. PreparedStatement statement = con.prepareStatement(UPDATE_PLAYER);
  742. statement.setString(1, sevenDat.getString("cabal"));
  743. statement.setInt(2, sevenDat.getInteger("seal"));
  744. statement.setInt(3, sevenDat.getInteger("red_stones"));
  745. statement.setInt(4, sevenDat.getInteger("green_stones"));
  746. statement.setInt(5, sevenDat.getInteger("blue_stones"));
  747. statement.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  748. statement.setDouble(7, sevenDat.getDouble("contribution_score"));
  749. statement.setInt(8, sevenDat.getInteger("charId"));
  750. statement.execute();
  751. statement.close();
  752. }
  753. catch (SQLException e)
  754. {
  755. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  756. }
  757. finally
  758. {
  759. L2DatabaseFactory.close(con);
  760. }
  761. }
  762. public final void saveSevenSignsStatus()
  763. {
  764. Connection con = null;
  765. try
  766. {
  767. con = L2DatabaseFactory.getInstance().getConnection();
  768. PreparedStatement statement = con.prepareStatement(UPDATE_STATUS);
  769. statement.setInt(1, _currentCycle);
  770. statement.setInt(2, _activePeriod);
  771. statement.setInt(3, _previousWinner);
  772. statement.setDouble(4, _dawnStoneScore);
  773. statement.setInt(5, _dawnFestivalScore);
  774. statement.setDouble(6, _duskStoneScore);
  775. statement.setInt(7, _duskFestivalScore);
  776. statement.setInt(8, _signsSealOwners.get(SEAL_AVARICE));
  777. statement.setInt(9, _signsSealOwners.get(SEAL_GNOSIS));
  778. statement.setInt(10, _signsSealOwners.get(SEAL_STRIFE));
  779. statement.setInt(11, _signsDawnSealTotals.get(SEAL_AVARICE));
  780. statement.setInt(12, _signsDawnSealTotals.get(SEAL_GNOSIS));
  781. statement.setInt(13, _signsDawnSealTotals.get(SEAL_STRIFE));
  782. statement.setInt(14, _signsDuskSealTotals.get(SEAL_AVARICE));
  783. statement.setInt(15, _signsDuskSealTotals.get(SEAL_GNOSIS));
  784. statement.setInt(16, _signsDuskSealTotals.get(SEAL_STRIFE));
  785. statement.setInt(17, SevenSignsFestival.getInstance().getCurrentFestivalCycle());
  786. for (int i = 0; i < SevenSignsFestival.FESTIVAL_COUNT; i++)
  787. statement.setInt(18 + i, SevenSignsFestival.getInstance().getAccumulatedBonus(i));
  788. _lastSave = Calendar.getInstance();
  789. statement.setLong(18 + SevenSignsFestival.FESTIVAL_COUNT, _lastSave.getTimeInMillis());
  790. statement.execute();
  791. statement.close();
  792. if (Config.DEBUG)
  793. _log.info("SevenSigns: Updated data in database.");
  794. }
  795. catch (SQLException e)
  796. {
  797. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  798. }
  799. finally
  800. {
  801. L2DatabaseFactory.close(con);
  802. }
  803. }
  804. /**
  805. * Used to reset the cabal details of all players, and update the database.<BR>
  806. * Primarily used when beginning a new cycle, and should otherwise never be called.
  807. */
  808. protected void resetPlayerData()
  809. {
  810. if (Config.DEBUG)
  811. _log.info("SevenSigns: Resetting player data for new event period.");
  812. int charObjId;
  813. // Reset each player's contribution data as well as seal and cabal.
  814. for (StatsSet sevenDat : _signsPlayerData.values())
  815. {
  816. charObjId = sevenDat.getInteger("charId");
  817. // Reset the player's cabal and seal information
  818. sevenDat.set("cabal", "");
  819. sevenDat.set("seal", SEAL_NULL);
  820. sevenDat.set("contribution_score", 0);
  821. _signsPlayerData.put(charObjId, sevenDat);
  822. }
  823. }
  824. /**
  825. * Used to specify cabal-related details for the specified player.<br>
  826. * This method checks to see if the player has registered before and will update the database if necessary.
  827. * @param objectId
  828. * @param chosenCabal
  829. * @param chosenSeal
  830. * @return the cabal ID the player has joined.
  831. */
  832. public int setPlayerInfo(int objectId, int chosenCabal, int chosenSeal)
  833. {
  834. StatsSet currPlayerData = _signsPlayerData.get(objectId);
  835. if (currPlayerData != null)
  836. {
  837. // If the seal validation period has passed,
  838. // cabal information was removed and so "re-register" player
  839. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  840. currPlayerData.set("seal", chosenSeal);
  841. _signsPlayerData.put(objectId, currPlayerData);
  842. }
  843. else
  844. {
  845. currPlayerData = new StatsSet();
  846. currPlayerData.set("charId", objectId);
  847. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  848. currPlayerData.set("seal", chosenSeal);
  849. currPlayerData.set("red_stones", 0);
  850. currPlayerData.set("green_stones", 0);
  851. currPlayerData.set("blue_stones", 0);
  852. currPlayerData.set("ancient_adena_amount", 0);
  853. currPlayerData.set("contribution_score", 0);
  854. _signsPlayerData.put(objectId, currPlayerData);
  855. // Update data in database, as we have a new player signing up.
  856. Connection con = null;
  857. try
  858. {
  859. con = L2DatabaseFactory.getInstance().getConnection();
  860. PreparedStatement statement = con.prepareStatement(INSERT_PLAYER);
  861. statement.setInt(1, objectId);
  862. statement.setString(2, getCabalShortName(chosenCabal));
  863. statement.setInt(3, chosenSeal);
  864. statement.execute();
  865. statement.close();
  866. if (Config.DEBUG)
  867. _log.info("SevenSigns: Inserted data in DB for char ID " + currPlayerData.getInteger("charId") + " ("
  868. + currPlayerData.getString("cabal") + ")");
  869. }
  870. catch (SQLException e)
  871. {
  872. _log.log(Level.SEVERE, "SevenSigns: Failed to save data: " + e.getMessage(), e);
  873. }
  874. finally
  875. {
  876. L2DatabaseFactory.close(con);
  877. }
  878. }
  879. // Increasing Seal total score for the player chosen Seal.
  880. if ("dawn".equals(currPlayerData.getString("cabal")))
  881. _signsDawnSealTotals.put(chosenSeal, _signsDawnSealTotals.get(chosenSeal) + 1);
  882. else
  883. _signsDuskSealTotals.put(chosenSeal, _signsDuskSealTotals.get(chosenSeal) + 1);
  884. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  885. saveSevenSignsStatus();
  886. return chosenCabal;
  887. }
  888. /**
  889. * Returns the amount of ancient adena the specified player can claim, if any.<BR>
  890. * If removeReward = True, all the ancient adena owed to them is removed, then
  891. * DB is updated.
  892. * @param objectId
  893. * @param removeReward
  894. * @return
  895. */
  896. public int getAncientAdenaReward(int objectId, boolean removeReward)
  897. {
  898. StatsSet currPlayer = _signsPlayerData.get(objectId);
  899. int rewardAmount = currPlayer.getInteger("ancient_adena_amount");
  900. currPlayer.set("red_stones", 0);
  901. currPlayer.set("green_stones", 0);
  902. currPlayer.set("blue_stones", 0);
  903. currPlayer.set("ancient_adena_amount", 0);
  904. if (removeReward)
  905. {
  906. _signsPlayerData.put(objectId, currPlayer);
  907. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  908. {
  909. saveSevenSignsData(objectId);
  910. saveSevenSignsStatus();
  911. }
  912. }
  913. return rewardAmount;
  914. }
  915. /**
  916. * Used to add the specified player's seal stone contribution points
  917. * to the current total for their cabal. Returns the point score the
  918. * contribution was worth.<br>
  919. *
  920. * Each stone count <B>must be</B> broken down and specified by the stone's color.
  921. * @param objectId
  922. * @param blueCount
  923. * @param greenCount
  924. * @param redCount
  925. * @return
  926. */
  927. public long addPlayerStoneContrib(int objectId, long blueCount, long greenCount, long redCount)
  928. {
  929. StatsSet currPlayer = _signsPlayerData.get(objectId);
  930. long contribScore = calcContributionScore(blueCount, greenCount, redCount);
  931. long totalAncientAdena = currPlayer.getLong("ancient_adena_amount") + calcAncientAdenaReward(blueCount, greenCount, redCount);
  932. long totalContribScore = currPlayer.getLong("contribution_score") + contribScore;
  933. if (totalContribScore > Config.ALT_MAXIMUM_PLAYER_CONTRIB)
  934. return -1;
  935. currPlayer.set("red_stones", currPlayer.getInteger("red_stones") + redCount);
  936. currPlayer.set("green_stones", currPlayer.getInteger("green_stones") + greenCount);
  937. currPlayer.set("blue_stones", currPlayer.getInteger("blue_stones") + blueCount);
  938. currPlayer.set("ancient_adena_amount", totalAncientAdena);
  939. currPlayer.set("contribution_score", totalContribScore);
  940. _signsPlayerData.put(objectId, currPlayer);
  941. switch (getPlayerCabal(objectId))
  942. {
  943. case CABAL_DAWN:
  944. _dawnStoneScore += contribScore;
  945. break;
  946. case CABAL_DUSK:
  947. _duskStoneScore += contribScore;
  948. break;
  949. }
  950. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  951. {
  952. saveSevenSignsData(objectId);
  953. saveSevenSignsStatus();
  954. }
  955. return contribScore;
  956. }
  957. /**
  958. * Adds the specified number of festival points to the specified cabal.
  959. * Remember, the same number of points are <B>deducted from the rival cabal</B>
  960. * to maintain proportionality.
  961. *
  962. * @param cabal
  963. * @param amount
  964. */
  965. public void addFestivalScore(int cabal, int amount)
  966. {
  967. if (cabal == CABAL_DUSK)
  968. {
  969. _duskFestivalScore += amount;
  970. // To prevent negative scores!
  971. if (_dawnFestivalScore >= amount)
  972. _dawnFestivalScore -= amount;
  973. }
  974. else
  975. {
  976. _dawnFestivalScore += amount;
  977. if (_duskFestivalScore >= amount)
  978. _duskFestivalScore -= amount;
  979. }
  980. }
  981. /**
  982. * Send info on the current Seven Signs period to the specified player.
  983. *
  984. * @param player
  985. */
  986. public void sendCurrentPeriodMsg(L2PcInstance player)
  987. {
  988. SystemMessage sm = null;
  989. switch (getCurrentPeriod())
  990. {
  991. case PERIOD_COMP_RECRUITING:
  992. sm = SystemMessage.getSystemMessage(SystemMessageId.PREPARATIONS_PERIOD_BEGUN);
  993. break;
  994. case PERIOD_COMPETITION:
  995. sm = SystemMessage.getSystemMessage(SystemMessageId.COMPETITION_PERIOD_BEGUN);
  996. break;
  997. case PERIOD_COMP_RESULTS:
  998. sm = SystemMessage.getSystemMessage(SystemMessageId.RESULTS_PERIOD_BEGUN);
  999. break;
  1000. case PERIOD_SEAL_VALIDATION:
  1001. sm = SystemMessage.getSystemMessage(SystemMessageId.VALIDATION_PERIOD_BEGUN);
  1002. break;
  1003. }
  1004. player.sendPacket(sm);
  1005. }
  1006. /**
  1007. * Sends the built-in system message specified by sysMsgId to all online players.
  1008. *
  1009. * @param sysMsgId
  1010. */
  1011. public void sendMessageToAll(SystemMessageId sysMsgId)
  1012. {
  1013. SystemMessage sm = SystemMessage.getSystemMessage(sysMsgId);
  1014. Broadcast.toAllOnlinePlayers(sm);
  1015. }
  1016. /**
  1017. * Used to initialize the seals for each cabal. (Used at startup or at beginning of a new cycle).
  1018. * This method should be called after <B>resetSeals()</B> and <B>calcNewSealOwners()</B> on a new cycle.
  1019. */
  1020. protected void initializeSeals()
  1021. {
  1022. for (Integer currSeal : _signsSealOwners.keySet())
  1023. {
  1024. int sealOwner = _signsSealOwners.get(currSeal);
  1025. if (sealOwner != CABAL_NULL)
  1026. if (isSealValidationPeriod())
  1027. _log.info("SevenSigns: The " + getCabalName(sealOwner) + " have won the " + getSealName(currSeal, false) + ".");
  1028. else
  1029. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " is currently owned by " + getCabalName(sealOwner) + ".");
  1030. else
  1031. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " remains unclaimed.");
  1032. }
  1033. }
  1034. /**
  1035. * Only really used at the beginning of a new cycle, this method resets all seal-related data.
  1036. */
  1037. protected void resetSeals()
  1038. {
  1039. _signsDawnSealTotals.put(SEAL_AVARICE, 0);
  1040. _signsDawnSealTotals.put(SEAL_GNOSIS, 0);
  1041. _signsDawnSealTotals.put(SEAL_STRIFE, 0);
  1042. _signsDuskSealTotals.put(SEAL_AVARICE, 0);
  1043. _signsDuskSealTotals.put(SEAL_GNOSIS, 0);
  1044. _signsDuskSealTotals.put(SEAL_STRIFE, 0);
  1045. }
  1046. /**
  1047. * Calculates the ownership of the three Seals of the Seven Signs,
  1048. * based on various criterion.
  1049. * <BR><BR>
  1050. * Should only ever called at the beginning of a new cycle.
  1051. */
  1052. protected void calcNewSealOwners()
  1053. {
  1054. if (Config.DEBUG)
  1055. {
  1056. _log.info("SevenSigns: (Avarice) Dawn = " + _signsDawnSealTotals.get(SEAL_AVARICE) + ", Dusk = "
  1057. + _signsDuskSealTotals.get(SEAL_AVARICE));
  1058. _log.info("SevenSigns: (Gnosis) Dawn = " + _signsDawnSealTotals.get(SEAL_GNOSIS) + ", Dusk = "
  1059. + _signsDuskSealTotals.get(SEAL_GNOSIS));
  1060. _log.info("SevenSigns: (Strife) Dawn = " + _signsDawnSealTotals.get(SEAL_STRIFE) + ", Dusk = "
  1061. + _signsDuskSealTotals.get(SEAL_STRIFE));
  1062. }
  1063. for (Integer currSeal : _signsDawnSealTotals.keySet())
  1064. {
  1065. int prevSealOwner = _signsSealOwners.get(currSeal);
  1066. int newSealOwner = CABAL_NULL;
  1067. int dawnProportion = getSealProportion(currSeal, CABAL_DAWN);
  1068. int totalDawnMembers = getTotalMembers(CABAL_DAWN) == 0 ? 1 : getTotalMembers(CABAL_DAWN);
  1069. int dawnPercent = Math.round(((float) dawnProportion / (float) totalDawnMembers) * 100);
  1070. int duskProportion = getSealProportion(currSeal, CABAL_DUSK);
  1071. int totalDuskMembers = getTotalMembers(CABAL_DUSK) == 0 ? 1 : getTotalMembers(CABAL_DUSK);
  1072. int duskPercent = Math.round(((float) duskProportion / (float) totalDuskMembers) * 100);
  1073. /*
  1074. * - If a Seal was already closed or owned by the opponent and the new winner wants
  1075. * to assume ownership of the Seal, 35% or more of the members of the Cabal must
  1076. * have chosen the Seal. If they chose less than 35%, they cannot own the Seal.
  1077. *
  1078. * - If the Seal was owned by the winner in the previous Seven Signs, they can retain
  1079. * that seal if 10% or more members have chosen it. If they want to possess a new Seal,
  1080. * at least 35% of the members of the Cabal must have chosen the new Seal.
  1081. */
  1082. switch (prevSealOwner)
  1083. {
  1084. case CABAL_NULL:
  1085. switch (getCabalHighestScore())
  1086. {
  1087. case CABAL_NULL:
  1088. newSealOwner = CABAL_NULL;
  1089. break;
  1090. case CABAL_DAWN:
  1091. if (dawnPercent >= 35)
  1092. newSealOwner = CABAL_DAWN;
  1093. else
  1094. newSealOwner = CABAL_NULL;
  1095. break;
  1096. case CABAL_DUSK:
  1097. if (duskPercent >= 35)
  1098. newSealOwner = CABAL_DUSK;
  1099. else
  1100. newSealOwner = CABAL_NULL;
  1101. break;
  1102. }
  1103. break;
  1104. case CABAL_DAWN:
  1105. switch (getCabalHighestScore())
  1106. {
  1107. case CABAL_NULL:
  1108. if (dawnPercent >= 10)
  1109. newSealOwner = CABAL_DAWN;
  1110. else
  1111. newSealOwner = CABAL_NULL;
  1112. break;
  1113. case CABAL_DAWN:
  1114. if (dawnPercent >= 10)
  1115. newSealOwner = CABAL_DAWN;
  1116. else
  1117. newSealOwner = CABAL_NULL;
  1118. break;
  1119. case CABAL_DUSK:
  1120. if (duskPercent >= 35)
  1121. newSealOwner = CABAL_DUSK;
  1122. else if (dawnPercent >= 10)
  1123. newSealOwner = CABAL_DAWN;
  1124. else
  1125. newSealOwner = CABAL_NULL;
  1126. break;
  1127. }
  1128. break;
  1129. case CABAL_DUSK:
  1130. switch (getCabalHighestScore())
  1131. {
  1132. case CABAL_NULL:
  1133. if (duskPercent >= 10)
  1134. newSealOwner = CABAL_DUSK;
  1135. else
  1136. newSealOwner = CABAL_NULL;
  1137. break;
  1138. case CABAL_DAWN:
  1139. if (dawnPercent >= 35)
  1140. newSealOwner = CABAL_DAWN;
  1141. else if (duskPercent >= 10)
  1142. newSealOwner = CABAL_DUSK;
  1143. else
  1144. newSealOwner = CABAL_NULL;
  1145. break;
  1146. case CABAL_DUSK:
  1147. if (duskPercent >= 10)
  1148. newSealOwner = CABAL_DUSK;
  1149. else
  1150. newSealOwner = CABAL_NULL;
  1151. break;
  1152. }
  1153. break;
  1154. }
  1155. _signsSealOwners.put(currSeal, newSealOwner);
  1156. // Alert all online players to new seal status.
  1157. switch (currSeal)
  1158. {
  1159. case SEAL_AVARICE:
  1160. if (newSealOwner == CABAL_DAWN)
  1161. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_AVARICE);
  1162. else if (newSealOwner == CABAL_DUSK)
  1163. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_AVARICE);
  1164. break;
  1165. case SEAL_GNOSIS:
  1166. if (newSealOwner == CABAL_DAWN)
  1167. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_GNOSIS);
  1168. else if (newSealOwner == CABAL_DUSK)
  1169. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_GNOSIS);
  1170. break;
  1171. case SEAL_STRIFE:
  1172. if (newSealOwner == CABAL_DAWN)
  1173. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_STRIFE);
  1174. else if (newSealOwner == CABAL_DUSK)
  1175. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_STRIFE);
  1176. CastleManager.getInstance().validateTaxes(newSealOwner);
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. /**
  1182. * This method is called to remove all players from catacombs and necropolises, who belong to the losing cabal.<br>
  1183. * Should only ever called at the beginning of Seal Validation.
  1184. * @param compWinner
  1185. */
  1186. protected void teleLosingCabalFromDungeons(String compWinner)
  1187. {
  1188. L2World.getInstance().forEachPlayer(new TeleLosingCabalFromDungeons(compWinner));
  1189. }
  1190. private final class TeleLosingCabalFromDungeons implements TObjectProcedure<L2PcInstance>
  1191. {
  1192. private final String _cmpWinner;
  1193. protected TeleLosingCabalFromDungeons(final String compWinner)
  1194. {
  1195. _cmpWinner = compWinner;
  1196. }
  1197. @Override
  1198. public final boolean execute(final L2PcInstance onlinePlayer)
  1199. {
  1200. if (onlinePlayer != null)
  1201. {
  1202. StatsSet currPlayer = _signsPlayerData.get(onlinePlayer.getObjectId());
  1203. if (isSealValidationPeriod() || isCompResultsPeriod())
  1204. {
  1205. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && (currPlayer == null || !currPlayer.getString("cabal").equals(_cmpWinner)))
  1206. {
  1207. onlinePlayer.teleToLocation(MapRegionManager.TeleportWhereType.Town);
  1208. onlinePlayer.setIsIn7sDungeon(false);
  1209. onlinePlayer.sendMessage("You have been teleported to the nearest town due to the beginning of the Seal Validation period.");
  1210. }
  1211. }
  1212. else
  1213. {
  1214. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && (currPlayer == null || !currPlayer.getString("cabal").isEmpty()))
  1215. {
  1216. onlinePlayer.teleToLocation(MapRegionManager.TeleportWhereType.Town);
  1217. onlinePlayer.setIsIn7sDungeon(false);
  1218. onlinePlayer.sendMessage("You have been teleported to the nearest town because you have not signed for any cabal.");
  1219. }
  1220. }
  1221. }
  1222. return true;
  1223. }
  1224. }
  1225. /**
  1226. * The primary controller of period change of the Seven Signs system.
  1227. * This runs all related tasks depending on the period that is about to begin.
  1228. *
  1229. * @author Tempy
  1230. */
  1231. protected class SevenSignsPeriodChange implements Runnable
  1232. {
  1233. @Override
  1234. public void run()
  1235. {
  1236. /*
  1237. * Remember the period check here refers to the period just ENDED!
  1238. */
  1239. final int periodEnded = getCurrentPeriod();
  1240. _activePeriod++;
  1241. switch (periodEnded)
  1242. {
  1243. case PERIOD_COMP_RECRUITING: // Initialization
  1244. // Start the Festival of Darkness cycle.
  1245. SevenSignsFestival.getInstance().startFestivalManager();
  1246. // Send message that Competition has begun.
  1247. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_BEGUN);
  1248. break;
  1249. case PERIOD_COMPETITION: // Results Calculation
  1250. // Send message that Competition has ended.
  1251. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_ENDED);
  1252. int compWinner = getCabalHighestScore();
  1253. // Schedule a stop of the festival engine and reward highest ranking members from cycle
  1254. SevenSignsFestival.getInstance().getFestivalManagerSchedule().cancel(false);
  1255. SevenSignsFestival.getInstance().rewardHighestRanked();
  1256. calcNewSealOwners();
  1257. switch (compWinner)
  1258. {
  1259. case CABAL_DAWN:
  1260. sendMessageToAll(SystemMessageId.DAWN_WON);
  1261. break;
  1262. case CABAL_DUSK:
  1263. sendMessageToAll(SystemMessageId.DUSK_WON);
  1264. break;
  1265. }
  1266. _previousWinner = compWinner;
  1267. break;
  1268. case PERIOD_COMP_RESULTS: // Seal Validation
  1269. // Perform initial Seal Validation set up.
  1270. initializeSeals();
  1271. //Buff/Debuff members of the event when Seal of Strife captured.
  1272. giveCPMult(getSealOwner(SEAL_STRIFE));
  1273. // Send message that Seal Validation has begun.
  1274. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_BEGUN);
  1275. // Change next Territory War date
  1276. Quest twQuest = QuestManager.getInstance().getQuest(TerritoryWarManager.qn);
  1277. if (twQuest != null)
  1278. twQuest.startQuestTimer("setNextTWDate", 30000, null, null);
  1279. _log.info("SevenSigns: The " + getCabalName(_previousWinner) + " have won the competition with " + getCurrentScore(_previousWinner) + " points!");
  1280. break;
  1281. case PERIOD_SEAL_VALIDATION: // Reset for New Cycle
  1282. // Ensure a cycle restart when this period ends.
  1283. _activePeriod = PERIOD_COMP_RECRUITING;
  1284. // Send message that Seal Validation has ended.
  1285. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_ENDED);
  1286. //Clear Seal of Strife influence.
  1287. removeCPMult();
  1288. // Reset all data
  1289. resetPlayerData();
  1290. resetSeals();
  1291. _currentCycle++;
  1292. // Reset all Festival-related data and remove any unused blood offerings.
  1293. // NOTE: A full update of Festival data in the database is also performed.
  1294. SevenSignsFestival.getInstance().resetFestivalData(false);
  1295. _dawnStoneScore = 0;
  1296. _duskStoneScore = 0;
  1297. _dawnFestivalScore = 0;
  1298. _duskFestivalScore = 0;
  1299. break;
  1300. }
  1301. // Make sure all Seven Signs data is saved for future use.
  1302. saveSevenSignsData();
  1303. saveSevenSignsStatus();
  1304. teleLosingCabalFromDungeons(getCabalShortName(getCabalHighestScore()));
  1305. SSQInfo ss = new SSQInfo();
  1306. Broadcast.toAllOnlinePlayers(ss);
  1307. spawnSevenSignsNPC();
  1308. _log.info("SevenSigns: The " + getCurrentPeriodName() + " period has begun!");
  1309. setCalendarForNextPeriodChange();
  1310. // make sure that all the scheduled siege dates are in the Seal Validation period
  1311. List<Castle> castles = CastleManager.getInstance().getCastles();
  1312. for (Castle castle : castles)
  1313. {
  1314. castle.getSiege().correctSiegeDateTime();
  1315. }
  1316. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  1317. ThreadPoolManager.getInstance().scheduleGeneral(sspc, getMilliToPeriodChange());
  1318. }
  1319. }
  1320. public boolean checkIsDawnPostingTicket(int itemId)
  1321. {
  1322. //TODO I think it should be some kind of a list in the datapack for compare;
  1323. if (itemId > 6114 && itemId < 6175)
  1324. return true;
  1325. if (itemId > 6801 && itemId < 6812)
  1326. return true;
  1327. if (itemId > 7997 && itemId < 8008)
  1328. return true;
  1329. if (itemId > 7940 && itemId < 7951)
  1330. return true;
  1331. if (itemId > 6294 && itemId < 6307)
  1332. return true;
  1333. if (itemId > 6831 && itemId < 6834)
  1334. return true;
  1335. if (itemId > 8027 && itemId < 8030)
  1336. return true;
  1337. if (itemId > 7970 && itemId < 7973)
  1338. return true;
  1339. return false;
  1340. }
  1341. public boolean checkIsRookiePostingTicket(int itemId)
  1342. {
  1343. //TODO I think it should be some kind of a list in the datapack for compare;
  1344. if (itemId > 6174 && itemId < 6295)
  1345. return true;
  1346. if (itemId > 6811 && itemId < 6832)
  1347. return true;
  1348. if (itemId > 7950 && itemId < 7971)
  1349. return true;
  1350. if (itemId > 8007 && itemId < 8028)
  1351. return true;
  1352. return false;
  1353. }
  1354. public void giveCPMult(int StrifeOwner)
  1355. {
  1356. L2World.getInstance().forEachPlayer(new GiveCPMult(StrifeOwner));
  1357. }
  1358. private final class GiveCPMult implements TObjectProcedure<L2PcInstance>
  1359. {
  1360. private final int _strifeOwner;
  1361. protected GiveCPMult(int strifeOwner)
  1362. {
  1363. _strifeOwner = strifeOwner;
  1364. }
  1365. @Override
  1366. public final boolean execute(final L2PcInstance character)
  1367. {
  1368. if (character != null)
  1369. {
  1370. //Gives "Victor of War" passive skill to all online characters with Cabal, which controls Seal of Strife
  1371. int cabal = getPlayerCabal(character.getObjectId());
  1372. if (cabal != SevenSigns.CABAL_NULL)
  1373. if (cabal == _strifeOwner)
  1374. character.addSkill(SkillTable.FrequentSkill.THE_VICTOR_OF_WAR.getSkill());
  1375. else
  1376. //Gives "The Vanquished of War" passive skill to all online characters with Cabal, which does not control Seal of Strife
  1377. character.addSkill(SkillTable.FrequentSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1378. }
  1379. return true;
  1380. }
  1381. }
  1382. public void removeCPMult()
  1383. {
  1384. L2World.getInstance().forEachPlayer(new RemoveCPMult());
  1385. }
  1386. protected final class RemoveCPMult implements TObjectProcedure<L2PcInstance>
  1387. {
  1388. @Override
  1389. public final boolean execute(final L2PcInstance character)
  1390. {
  1391. if (character != null)
  1392. {
  1393. //Remove SevenSigns' buffs/debuffs.
  1394. character.removeSkill(SkillTable.FrequentSkill.THE_VICTOR_OF_WAR.getSkill());
  1395. character.removeSkill(SkillTable.FrequentSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1396. }
  1397. return true;
  1398. }
  1399. }
  1400. public boolean checkSummonConditions(L2PcInstance activeChar)
  1401. {
  1402. if (activeChar == null)
  1403. return true;
  1404. //Golems cannot be summoned by Dusk when the Seal of Strife is controlled by the Dawn
  1405. if (isSealValidationPeriod())
  1406. if (getSealOwner(SEAL_STRIFE) == CABAL_DAWN)
  1407. if (getPlayerCabal(activeChar.getObjectId()) == CABAL_DUSK)
  1408. {
  1409. activeChar.sendMessage("You cannot summon Siege Golem or Cannon while Seal of Strife posessed by Lords of Dawn.");
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. private static class SingletonHolder
  1415. {
  1416. protected static final SevenSigns _instance = new SevenSigns();
  1417. }
  1418. }