SevenSigns.java 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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.sql.Statement;
  22. import java.util.Calendar;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastMap;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.datatables.SkillTable;
  31. import com.l2jserver.gameserver.instancemanager.CastleManager;
  32. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  33. import com.l2jserver.gameserver.instancemanager.QuestManager;
  34. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  35. import com.l2jserver.gameserver.model.AutoChatHandler;
  36. import com.l2jserver.gameserver.model.AutoSpawnHandler;
  37. import com.l2jserver.gameserver.model.AutoSpawnHandler.AutoSpawnInstance;
  38. import com.l2jserver.gameserver.model.L2World;
  39. import com.l2jserver.gameserver.model.StatsSet;
  40. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  41. import com.l2jserver.gameserver.model.entity.Castle;
  42. import com.l2jserver.gameserver.model.quest.Quest;
  43. import com.l2jserver.gameserver.network.SystemMessageId;
  44. import com.l2jserver.gameserver.network.serverpackets.SSQInfo;
  45. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  46. import com.l2jserver.gameserver.util.Broadcast;
  47. /**
  48. * Seven Signs Engine
  49. * @author Tempy
  50. */
  51. public class SevenSigns
  52. {
  53. protected static final Logger _log = Logger.getLogger(SevenSigns.class.getName());
  54. // Basic Seven Signs Constants \\
  55. public static final String SEVEN_SIGNS_HTML_PATH = "data/html/seven_signs/";
  56. public static final int CABAL_NULL = 0;
  57. public static final int CABAL_DUSK = 1;
  58. public static final int CABAL_DAWN = 2;
  59. public static final int SEAL_NULL = 0;
  60. public static final int SEAL_AVARICE = 1;
  61. public static final int SEAL_GNOSIS = 2;
  62. public static final int SEAL_STRIFE = 3;
  63. public static final int PERIOD_COMP_RECRUITING = 0;
  64. public static final int PERIOD_COMPETITION = 1;
  65. public static final int PERIOD_COMP_RESULTS = 2;
  66. public static final int PERIOD_SEAL_VALIDATION = 3;
  67. public static final int PERIOD_START_HOUR = 18;
  68. public static final int PERIOD_START_MINS = 00;
  69. public static final int PERIOD_START_DAY = Calendar.MONDAY;
  70. // The quest event and seal validation periods last for approximately one week
  71. // with a 15 minutes "interval" period sandwiched between them.
  72. public static final int PERIOD_MINOR_LENGTH = 900000;
  73. public static final int PERIOD_MAJOR_LENGTH = 604800000 - PERIOD_MINOR_LENGTH;
  74. public static final int RECORD_SEVEN_SIGNS_ID = 5707;
  75. public static final int 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. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  633. {
  634. try (Statement s = con.createStatement();
  635. ResultSet rs = s.executeQuery(LOAD_DATA))
  636. {
  637. StatsSet sevenDat = null;
  638. int charObjId;
  639. while (rs.next())
  640. {
  641. charObjId = rs.getInt("charId");
  642. sevenDat = new StatsSet();
  643. sevenDat.set("charId", charObjId);
  644. sevenDat.set("cabal", rs.getString("cabal"));
  645. sevenDat.set("seal", rs.getInt("seal"));
  646. sevenDat.set("red_stones", rs.getInt("red_stones"));
  647. sevenDat.set("green_stones", rs.getInt("green_stones"));
  648. sevenDat.set("blue_stones", rs.getInt("blue_stones"));
  649. sevenDat.set("ancient_adena_amount", rs.getDouble("ancient_adena_amount"));
  650. sevenDat.set("contribution_score", rs.getDouble("contribution_score"));
  651. if (Config.DEBUG)
  652. _log.info("SevenSigns: Loaded data from DB for char ID " + charObjId + " (" + sevenDat.getString("cabal") + ")");
  653. _signsPlayerData.put(charObjId, sevenDat);
  654. }
  655. }
  656. try (Statement s = con.createStatement();
  657. ResultSet rs = s.executeQuery(LOAD_STATUS))
  658. {
  659. while (rs.next())
  660. {
  661. _currentCycle = rs.getInt("current_cycle");
  662. _activePeriod = rs.getInt("active_period");
  663. _previousWinner = rs.getInt("previous_winner");
  664. _dawnStoneScore = rs.getDouble("dawn_stone_score");
  665. _dawnFestivalScore = rs.getInt("dawn_festival_score");
  666. _duskStoneScore = rs.getDouble("dusk_stone_score");
  667. _duskFestivalScore = rs.getInt("dusk_festival_score");
  668. _signsSealOwners.put(SEAL_AVARICE, rs.getInt("avarice_owner"));
  669. _signsSealOwners.put(SEAL_GNOSIS, rs.getInt("gnosis_owner"));
  670. _signsSealOwners.put(SEAL_STRIFE, rs.getInt("strife_owner"));
  671. _signsDawnSealTotals.put(SEAL_AVARICE, rs.getInt("avarice_dawn_score"));
  672. _signsDawnSealTotals.put(SEAL_GNOSIS, rs.getInt("gnosis_dawn_score"));
  673. _signsDawnSealTotals.put(SEAL_STRIFE, rs.getInt("strife_dawn_score"));
  674. _signsDuskSealTotals.put(SEAL_AVARICE, rs.getInt("avarice_dusk_score"));
  675. _signsDuskSealTotals.put(SEAL_GNOSIS, rs.getInt("gnosis_dusk_score"));
  676. _signsDuskSealTotals.put(SEAL_STRIFE, rs.getInt("strife_dusk_score"));
  677. _lastSave.setTimeInMillis(rs.getLong("date"));
  678. }
  679. }
  680. }
  681. catch (SQLException e)
  682. {
  683. _log.log(Level.SEVERE, "SevenSigns: Unable to load Seven Signs data from database: " + e.getMessage(), e);
  684. }
  685. // Festival data is loaded now after the Seven Signs engine data.
  686. }
  687. /**
  688. * Saves all Seven Signs player data.<br>
  689. * Should be called on period change and shutdown only.
  690. */
  691. public void saveSevenSignsData()
  692. {
  693. if (Config.DEBUG)
  694. _log.info("SevenSigns: Saving data to disk.");
  695. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  696. PreparedStatement ps = con.prepareStatement(UPDATE_PLAYER))
  697. {
  698. for (StatsSet sevenDat : _signsPlayerData.values())
  699. {
  700. ps.setString(1, sevenDat.getString("cabal"));
  701. ps.setInt(2, sevenDat.getInteger("seal"));
  702. ps.setInt(3, sevenDat.getInteger("red_stones"));
  703. ps.setInt(4, sevenDat.getInteger("green_stones"));
  704. ps.setInt(5, sevenDat.getInteger("blue_stones"));
  705. ps.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  706. ps.setDouble(7, sevenDat.getDouble("contribution_score"));
  707. ps.setInt(8, sevenDat.getInteger("charId"));
  708. ps.execute();
  709. ps.clearParameters();
  710. if (Config.DEBUG)
  711. {
  712. _log.info("SevenSigns: Updated data in database for char ID " + sevenDat.getInteger("charId") + " (" + sevenDat.getString("cabal") + ")");
  713. }
  714. }
  715. }
  716. catch (SQLException e)
  717. {
  718. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  719. }
  720. }
  721. public final void saveSevenSignsData(int objectId)
  722. {
  723. StatsSet sevenDat = _signsPlayerData.get(objectId);
  724. if (sevenDat == null)
  725. return;
  726. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  727. PreparedStatement ps = con.prepareStatement(UPDATE_PLAYER))
  728. {
  729. ps.setString(1, sevenDat.getString("cabal"));
  730. ps.setInt(2, sevenDat.getInteger("seal"));
  731. ps.setInt(3, sevenDat.getInteger("red_stones"));
  732. ps.setInt(4, sevenDat.getInteger("green_stones"));
  733. ps.setInt(5, sevenDat.getInteger("blue_stones"));
  734. ps.setDouble(6, sevenDat.getDouble("ancient_adena_amount"));
  735. ps.setDouble(7, sevenDat.getDouble("contribution_score"));
  736. ps.setInt(8, sevenDat.getInteger("charId"));
  737. ps.execute();
  738. }
  739. catch (SQLException e)
  740. {
  741. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  742. }
  743. }
  744. public final void saveSevenSignsStatus()
  745. {
  746. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  747. PreparedStatement ps = con.prepareStatement(UPDATE_STATUS))
  748. {
  749. ps.setInt(1, _currentCycle);
  750. ps.setInt(2, _activePeriod);
  751. ps.setInt(3, _previousWinner);
  752. ps.setDouble(4, _dawnStoneScore);
  753. ps.setInt(5, _dawnFestivalScore);
  754. ps.setDouble(6, _duskStoneScore);
  755. ps.setInt(7, _duskFestivalScore);
  756. ps.setInt(8, _signsSealOwners.get(SEAL_AVARICE));
  757. ps.setInt(9, _signsSealOwners.get(SEAL_GNOSIS));
  758. ps.setInt(10, _signsSealOwners.get(SEAL_STRIFE));
  759. ps.setInt(11, _signsDawnSealTotals.get(SEAL_AVARICE));
  760. ps.setInt(12, _signsDawnSealTotals.get(SEAL_GNOSIS));
  761. ps.setInt(13, _signsDawnSealTotals.get(SEAL_STRIFE));
  762. ps.setInt(14, _signsDuskSealTotals.get(SEAL_AVARICE));
  763. ps.setInt(15, _signsDuskSealTotals.get(SEAL_GNOSIS));
  764. ps.setInt(16, _signsDuskSealTotals.get(SEAL_STRIFE));
  765. ps.setInt(17, SevenSignsFestival.getInstance().getCurrentFestivalCycle());
  766. for (int i = 0; i < SevenSignsFestival.FESTIVAL_COUNT; i++)
  767. {
  768. ps.setInt(18 + i, SevenSignsFestival.getInstance().getAccumulatedBonus(i));
  769. }
  770. _lastSave = Calendar.getInstance();
  771. ps.setLong(18 + SevenSignsFestival.FESTIVAL_COUNT, _lastSave.getTimeInMillis());
  772. ps.execute();
  773. if (Config.DEBUG)
  774. _log.info("SevenSigns: Updated data in database.");
  775. }
  776. catch (SQLException e)
  777. {
  778. _log.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), e);
  779. }
  780. }
  781. /**
  782. * Used to reset the cabal details of all players, and update the database.<BR>
  783. * Primarily used when beginning a new cycle, and should otherwise never be called.
  784. */
  785. protected void resetPlayerData()
  786. {
  787. if (Config.DEBUG)
  788. _log.info("SevenSigns: Resetting player data for new event period.");
  789. int charObjId;
  790. // Reset each player's contribution data as well as seal and cabal.
  791. for (StatsSet sevenDat : _signsPlayerData.values())
  792. {
  793. charObjId = sevenDat.getInteger("charId");
  794. // Reset the player's cabal and seal information
  795. sevenDat.set("cabal", "");
  796. sevenDat.set("seal", SEAL_NULL);
  797. sevenDat.set("contribution_score", 0);
  798. _signsPlayerData.put(charObjId, sevenDat);
  799. }
  800. }
  801. /**
  802. * Used to specify cabal-related details for the specified player.<br>
  803. * This method checks to see if the player has registered before and will update the database if necessary.
  804. * @param objectId
  805. * @param chosenCabal
  806. * @param chosenSeal
  807. * @return the cabal ID the player has joined.
  808. */
  809. public int setPlayerInfo(int objectId, int chosenCabal, int chosenSeal)
  810. {
  811. StatsSet currPlayerData = _signsPlayerData.get(objectId);
  812. if (currPlayerData != null)
  813. {
  814. // If the seal validation period has passed,
  815. // cabal information was removed and so "re-register" player
  816. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  817. currPlayerData.set("seal", chosenSeal);
  818. _signsPlayerData.put(objectId, currPlayerData);
  819. }
  820. else
  821. {
  822. currPlayerData = new StatsSet();
  823. currPlayerData.set("charId", objectId);
  824. currPlayerData.set("cabal", getCabalShortName(chosenCabal));
  825. currPlayerData.set("seal", chosenSeal);
  826. currPlayerData.set("red_stones", 0);
  827. currPlayerData.set("green_stones", 0);
  828. currPlayerData.set("blue_stones", 0);
  829. currPlayerData.set("ancient_adena_amount", 0);
  830. currPlayerData.set("contribution_score", 0);
  831. _signsPlayerData.put(objectId, currPlayerData);
  832. // Update data in database, as we have a new player signing up.
  833. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  834. PreparedStatement ps = con.prepareStatement(INSERT_PLAYER))
  835. {
  836. ps.setInt(1, objectId);
  837. ps.setString(2, getCabalShortName(chosenCabal));
  838. ps.setInt(3, chosenSeal);
  839. ps.execute();
  840. if (Config.DEBUG)
  841. _log.info("SevenSigns: Inserted data in DB for char ID " + currPlayerData.getInteger("charId") + " (" + currPlayerData.getString("cabal") + ")");
  842. }
  843. catch (SQLException e)
  844. {
  845. _log.log(Level.SEVERE, "SevenSigns: Failed to save data: " + e.getMessage(), e);
  846. }
  847. }
  848. // Increasing Seal total score for the player chosen Seal.
  849. if ("dawn".equals(currPlayerData.getString("cabal")))
  850. _signsDawnSealTotals.put(chosenSeal, _signsDawnSealTotals.get(chosenSeal) + 1);
  851. else
  852. _signsDuskSealTotals.put(chosenSeal, _signsDuskSealTotals.get(chosenSeal) + 1);
  853. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  854. saveSevenSignsStatus();
  855. return chosenCabal;
  856. }
  857. /**
  858. * Returns the amount of ancient adena the specified player can claim, if any.<BR>
  859. * If removeReward = True, all the ancient adena owed to them is removed, then
  860. * DB is updated.
  861. * @param objectId
  862. * @param removeReward
  863. * @return
  864. */
  865. public int getAncientAdenaReward(int objectId, boolean removeReward)
  866. {
  867. StatsSet currPlayer = _signsPlayerData.get(objectId);
  868. int rewardAmount = currPlayer.getInteger("ancient_adena_amount");
  869. currPlayer.set("red_stones", 0);
  870. currPlayer.set("green_stones", 0);
  871. currPlayer.set("blue_stones", 0);
  872. currPlayer.set("ancient_adena_amount", 0);
  873. if (removeReward)
  874. {
  875. _signsPlayerData.put(objectId, currPlayer);
  876. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  877. {
  878. saveSevenSignsData(objectId);
  879. saveSevenSignsStatus();
  880. }
  881. }
  882. return rewardAmount;
  883. }
  884. /**
  885. * Used to add the specified player's seal stone contribution points
  886. * to the current total for their cabal. Returns the point score the
  887. * contribution was worth.<br>
  888. *
  889. * Each stone count <B>must be</B> broken down and specified by the stone's color.
  890. * @param objectId
  891. * @param blueCount
  892. * @param greenCount
  893. * @param redCount
  894. * @return
  895. */
  896. public long addPlayerStoneContrib(int objectId, long blueCount, long greenCount, long redCount)
  897. {
  898. StatsSet currPlayer = _signsPlayerData.get(objectId);
  899. long contribScore = calcContributionScore(blueCount, greenCount, redCount);
  900. long totalAncientAdena = currPlayer.getLong("ancient_adena_amount") + calcAncientAdenaReward(blueCount, greenCount, redCount);
  901. long totalContribScore = currPlayer.getLong("contribution_score") + contribScore;
  902. if (totalContribScore > Config.ALT_MAXIMUM_PLAYER_CONTRIB)
  903. return -1;
  904. currPlayer.set("red_stones", currPlayer.getInteger("red_stones") + redCount);
  905. currPlayer.set("green_stones", currPlayer.getInteger("green_stones") + greenCount);
  906. currPlayer.set("blue_stones", currPlayer.getInteger("blue_stones") + blueCount);
  907. currPlayer.set("ancient_adena_amount", totalAncientAdena);
  908. currPlayer.set("contribution_score", totalContribScore);
  909. _signsPlayerData.put(objectId, currPlayer);
  910. switch (getPlayerCabal(objectId))
  911. {
  912. case CABAL_DAWN:
  913. _dawnStoneScore += contribScore;
  914. break;
  915. case CABAL_DUSK:
  916. _duskStoneScore += contribScore;
  917. break;
  918. }
  919. if (!Config.ALT_SEVENSIGNS_LAZY_UPDATE)
  920. {
  921. saveSevenSignsData(objectId);
  922. saveSevenSignsStatus();
  923. }
  924. return contribScore;
  925. }
  926. /**
  927. * Adds the specified number of festival points to the specified cabal.
  928. * Remember, the same number of points are <B>deducted from the rival cabal</B>
  929. * to maintain proportionality.
  930. *
  931. * @param cabal
  932. * @param amount
  933. */
  934. public void addFestivalScore(int cabal, int amount)
  935. {
  936. if (cabal == CABAL_DUSK)
  937. {
  938. _duskFestivalScore += amount;
  939. // To prevent negative scores!
  940. if (_dawnFestivalScore >= amount)
  941. _dawnFestivalScore -= amount;
  942. }
  943. else
  944. {
  945. _dawnFestivalScore += amount;
  946. if (_duskFestivalScore >= amount)
  947. _duskFestivalScore -= amount;
  948. }
  949. }
  950. /**
  951. * Send info on the current Seven Signs period to the specified player.
  952. *
  953. * @param player
  954. */
  955. public void sendCurrentPeriodMsg(L2PcInstance player)
  956. {
  957. SystemMessage sm = null;
  958. switch (getCurrentPeriod())
  959. {
  960. case PERIOD_COMP_RECRUITING:
  961. sm = SystemMessage.getSystemMessage(SystemMessageId.PREPARATIONS_PERIOD_BEGUN);
  962. break;
  963. case PERIOD_COMPETITION:
  964. sm = SystemMessage.getSystemMessage(SystemMessageId.COMPETITION_PERIOD_BEGUN);
  965. break;
  966. case PERIOD_COMP_RESULTS:
  967. sm = SystemMessage.getSystemMessage(SystemMessageId.RESULTS_PERIOD_BEGUN);
  968. break;
  969. case PERIOD_SEAL_VALIDATION:
  970. sm = SystemMessage.getSystemMessage(SystemMessageId.VALIDATION_PERIOD_BEGUN);
  971. break;
  972. }
  973. player.sendPacket(sm);
  974. }
  975. /**
  976. * Sends the built-in system message specified by sysMsgId to all online players.
  977. *
  978. * @param sysMsgId
  979. */
  980. public void sendMessageToAll(SystemMessageId sysMsgId)
  981. {
  982. SystemMessage sm = SystemMessage.getSystemMessage(sysMsgId);
  983. Broadcast.toAllOnlinePlayers(sm);
  984. }
  985. /**
  986. * Used to initialize the seals for each cabal. (Used at startup or at beginning of a new cycle).
  987. * This method should be called after <B>resetSeals()</B> and <B>calcNewSealOwners()</B> on a new cycle.
  988. */
  989. protected void initializeSeals()
  990. {
  991. for (Integer currSeal : _signsSealOwners.keySet())
  992. {
  993. int sealOwner = _signsSealOwners.get(currSeal);
  994. if (sealOwner != CABAL_NULL)
  995. if (isSealValidationPeriod())
  996. _log.info("SevenSigns: The " + getCabalName(sealOwner) + " have won the " + getSealName(currSeal, false) + ".");
  997. else
  998. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " is currently owned by " + getCabalName(sealOwner) + ".");
  999. else
  1000. _log.info("SevenSigns: The " + getSealName(currSeal, false) + " remains unclaimed.");
  1001. }
  1002. }
  1003. /**
  1004. * Only really used at the beginning of a new cycle, this method resets all seal-related data.
  1005. */
  1006. protected void resetSeals()
  1007. {
  1008. _signsDawnSealTotals.put(SEAL_AVARICE, 0);
  1009. _signsDawnSealTotals.put(SEAL_GNOSIS, 0);
  1010. _signsDawnSealTotals.put(SEAL_STRIFE, 0);
  1011. _signsDuskSealTotals.put(SEAL_AVARICE, 0);
  1012. _signsDuskSealTotals.put(SEAL_GNOSIS, 0);
  1013. _signsDuskSealTotals.put(SEAL_STRIFE, 0);
  1014. }
  1015. /**
  1016. * Calculates the ownership of the three Seals of the Seven Signs,
  1017. * based on various criterion.
  1018. * <BR><BR>
  1019. * Should only ever called at the beginning of a new cycle.
  1020. */
  1021. protected void calcNewSealOwners()
  1022. {
  1023. if (Config.DEBUG)
  1024. {
  1025. _log.info("SevenSigns: (Avarice) Dawn = " + _signsDawnSealTotals.get(SEAL_AVARICE) + ", Dusk = "
  1026. + _signsDuskSealTotals.get(SEAL_AVARICE));
  1027. _log.info("SevenSigns: (Gnosis) Dawn = " + _signsDawnSealTotals.get(SEAL_GNOSIS) + ", Dusk = "
  1028. + _signsDuskSealTotals.get(SEAL_GNOSIS));
  1029. _log.info("SevenSigns: (Strife) Dawn = " + _signsDawnSealTotals.get(SEAL_STRIFE) + ", Dusk = "
  1030. + _signsDuskSealTotals.get(SEAL_STRIFE));
  1031. }
  1032. for (Integer currSeal : _signsDawnSealTotals.keySet())
  1033. {
  1034. int prevSealOwner = _signsSealOwners.get(currSeal);
  1035. int newSealOwner = CABAL_NULL;
  1036. int dawnProportion = getSealProportion(currSeal, CABAL_DAWN);
  1037. int totalDawnMembers = getTotalMembers(CABAL_DAWN) == 0 ? 1 : getTotalMembers(CABAL_DAWN);
  1038. int dawnPercent = Math.round(((float) dawnProportion / (float) totalDawnMembers) * 100);
  1039. int duskProportion = getSealProportion(currSeal, CABAL_DUSK);
  1040. int totalDuskMembers = getTotalMembers(CABAL_DUSK) == 0 ? 1 : getTotalMembers(CABAL_DUSK);
  1041. int duskPercent = Math.round(((float) duskProportion / (float) totalDuskMembers) * 100);
  1042. /*
  1043. * - If a Seal was already closed or owned by the opponent and the new winner wants
  1044. * to assume ownership of the Seal, 35% or more of the members of the Cabal must
  1045. * have chosen the Seal. If they chose less than 35%, they cannot own the Seal.
  1046. *
  1047. * - If the Seal was owned by the winner in the previous Seven Signs, they can retain
  1048. * that seal if 10% or more members have chosen it. If they want to possess a new Seal,
  1049. * at least 35% of the members of the Cabal must have chosen the new Seal.
  1050. */
  1051. switch (prevSealOwner)
  1052. {
  1053. case CABAL_NULL:
  1054. switch (getCabalHighestScore())
  1055. {
  1056. case CABAL_NULL:
  1057. newSealOwner = CABAL_NULL;
  1058. break;
  1059. case CABAL_DAWN:
  1060. if (dawnPercent >= 35)
  1061. newSealOwner = CABAL_DAWN;
  1062. else
  1063. newSealOwner = CABAL_NULL;
  1064. break;
  1065. case CABAL_DUSK:
  1066. if (duskPercent >= 35)
  1067. newSealOwner = CABAL_DUSK;
  1068. else
  1069. newSealOwner = CABAL_NULL;
  1070. break;
  1071. }
  1072. break;
  1073. case CABAL_DAWN:
  1074. switch (getCabalHighestScore())
  1075. {
  1076. case CABAL_NULL:
  1077. if (dawnPercent >= 10)
  1078. newSealOwner = CABAL_DAWN;
  1079. else
  1080. newSealOwner = CABAL_NULL;
  1081. break;
  1082. case CABAL_DAWN:
  1083. if (dawnPercent >= 10)
  1084. newSealOwner = CABAL_DAWN;
  1085. else
  1086. newSealOwner = CABAL_NULL;
  1087. break;
  1088. case CABAL_DUSK:
  1089. if (duskPercent >= 35)
  1090. newSealOwner = CABAL_DUSK;
  1091. else if (dawnPercent >= 10)
  1092. newSealOwner = CABAL_DAWN;
  1093. else
  1094. newSealOwner = CABAL_NULL;
  1095. break;
  1096. }
  1097. break;
  1098. case CABAL_DUSK:
  1099. switch (getCabalHighestScore())
  1100. {
  1101. case CABAL_NULL:
  1102. if (duskPercent >= 10)
  1103. newSealOwner = CABAL_DUSK;
  1104. else
  1105. newSealOwner = CABAL_NULL;
  1106. break;
  1107. case CABAL_DAWN:
  1108. if (dawnPercent >= 35)
  1109. newSealOwner = CABAL_DAWN;
  1110. else if (duskPercent >= 10)
  1111. newSealOwner = CABAL_DUSK;
  1112. else
  1113. newSealOwner = CABAL_NULL;
  1114. break;
  1115. case CABAL_DUSK:
  1116. if (duskPercent >= 10)
  1117. newSealOwner = CABAL_DUSK;
  1118. else
  1119. newSealOwner = CABAL_NULL;
  1120. break;
  1121. }
  1122. break;
  1123. }
  1124. _signsSealOwners.put(currSeal, newSealOwner);
  1125. // Alert all online players to new seal status.
  1126. switch (currSeal)
  1127. {
  1128. case SEAL_AVARICE:
  1129. if (newSealOwner == CABAL_DAWN)
  1130. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_AVARICE);
  1131. else if (newSealOwner == CABAL_DUSK)
  1132. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_AVARICE);
  1133. break;
  1134. case SEAL_GNOSIS:
  1135. if (newSealOwner == CABAL_DAWN)
  1136. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_GNOSIS);
  1137. else if (newSealOwner == CABAL_DUSK)
  1138. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_GNOSIS);
  1139. break;
  1140. case SEAL_STRIFE:
  1141. if (newSealOwner == CABAL_DAWN)
  1142. sendMessageToAll(SystemMessageId.DAWN_OBTAINED_STRIFE);
  1143. else if (newSealOwner == CABAL_DUSK)
  1144. sendMessageToAll(SystemMessageId.DUSK_OBTAINED_STRIFE);
  1145. CastleManager.getInstance().validateTaxes(newSealOwner);
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. /**
  1151. * This method is called to remove all players from catacombs and necropolises, who belong to the losing cabal.<br>
  1152. * Should only ever called at the beginning of Seal Validation.
  1153. * @param compWinner
  1154. */
  1155. protected void teleLosingCabalFromDungeons(String compWinner)
  1156. {
  1157. L2World.getInstance().forEachPlayer(new TeleLosingCabalFromDungeons(compWinner));
  1158. }
  1159. private final class TeleLosingCabalFromDungeons implements TObjectProcedure<L2PcInstance>
  1160. {
  1161. private final String _cmpWinner;
  1162. protected TeleLosingCabalFromDungeons(final String compWinner)
  1163. {
  1164. _cmpWinner = compWinner;
  1165. }
  1166. @Override
  1167. public final boolean execute(final L2PcInstance onlinePlayer)
  1168. {
  1169. if (onlinePlayer != null)
  1170. {
  1171. StatsSet currPlayer = _signsPlayerData.get(onlinePlayer.getObjectId());
  1172. if (isSealValidationPeriod() || isCompResultsPeriod())
  1173. {
  1174. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && (currPlayer == null || !currPlayer.getString("cabal").equals(_cmpWinner)))
  1175. {
  1176. onlinePlayer.teleToLocation(MapRegionManager.TeleportWhereType.Town);
  1177. onlinePlayer.setIsIn7sDungeon(false);
  1178. onlinePlayer.sendMessage("You have been teleported to the nearest town due to the beginning of the Seal Validation period.");
  1179. }
  1180. }
  1181. else
  1182. {
  1183. if (!onlinePlayer.isGM() && onlinePlayer.isIn7sDungeon() && (currPlayer == null || !currPlayer.getString("cabal").isEmpty()))
  1184. {
  1185. onlinePlayer.teleToLocation(MapRegionManager.TeleportWhereType.Town);
  1186. onlinePlayer.setIsIn7sDungeon(false);
  1187. onlinePlayer.sendMessage("You have been teleported to the nearest town because you have not signed for any cabal.");
  1188. }
  1189. }
  1190. }
  1191. return true;
  1192. }
  1193. }
  1194. /**
  1195. * The primary controller of period change of the Seven Signs system.
  1196. * This runs all related tasks depending on the period that is about to begin.
  1197. *
  1198. * @author Tempy
  1199. */
  1200. protected class SevenSignsPeriodChange implements Runnable
  1201. {
  1202. @Override
  1203. public void run()
  1204. {
  1205. /*
  1206. * Remember the period check here refers to the period just ENDED!
  1207. */
  1208. final int periodEnded = getCurrentPeriod();
  1209. _activePeriod++;
  1210. switch (periodEnded)
  1211. {
  1212. case PERIOD_COMP_RECRUITING: // Initialization
  1213. // Start the Festival of Darkness cycle.
  1214. SevenSignsFestival.getInstance().startFestivalManager();
  1215. // Send message that Competition has begun.
  1216. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_BEGUN);
  1217. break;
  1218. case PERIOD_COMPETITION: // Results Calculation
  1219. // Send message that Competition has ended.
  1220. sendMessageToAll(SystemMessageId.QUEST_EVENT_PERIOD_ENDED);
  1221. int compWinner = getCabalHighestScore();
  1222. // Schedule a stop of the festival engine and reward highest ranking members from cycle
  1223. SevenSignsFestival.getInstance().getFestivalManagerSchedule().cancel(false);
  1224. SevenSignsFestival.getInstance().rewardHighestRanked();
  1225. calcNewSealOwners();
  1226. switch (compWinner)
  1227. {
  1228. case CABAL_DAWN:
  1229. sendMessageToAll(SystemMessageId.DAWN_WON);
  1230. break;
  1231. case CABAL_DUSK:
  1232. sendMessageToAll(SystemMessageId.DUSK_WON);
  1233. break;
  1234. }
  1235. _previousWinner = compWinner;
  1236. break;
  1237. case PERIOD_COMP_RESULTS: // Seal Validation
  1238. // Perform initial Seal Validation set up.
  1239. initializeSeals();
  1240. //Buff/Debuff members of the event when Seal of Strife captured.
  1241. giveCPMult(getSealOwner(SEAL_STRIFE));
  1242. // Send message that Seal Validation has begun.
  1243. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_BEGUN);
  1244. // Change next Territory War date
  1245. Quest twQuest = QuestManager.getInstance().getQuest(TerritoryWarManager.qn);
  1246. if (twQuest != null)
  1247. twQuest.startQuestTimer("setNextTWDate", 30000, null, null);
  1248. _log.info("SevenSigns: The " + getCabalName(_previousWinner) + " have won the competition with " + getCurrentScore(_previousWinner) + " points!");
  1249. break;
  1250. case PERIOD_SEAL_VALIDATION: // Reset for New Cycle
  1251. // Ensure a cycle restart when this period ends.
  1252. _activePeriod = PERIOD_COMP_RECRUITING;
  1253. // Send message that Seal Validation has ended.
  1254. sendMessageToAll(SystemMessageId.SEAL_VALIDATION_PERIOD_ENDED);
  1255. //Clear Seal of Strife influence.
  1256. removeCPMult();
  1257. // Reset all data
  1258. resetPlayerData();
  1259. resetSeals();
  1260. _currentCycle++;
  1261. // Reset all Festival-related data and remove any unused blood offerings.
  1262. // NOTE: A full update of Festival data in the database is also performed.
  1263. SevenSignsFestival.getInstance().resetFestivalData(false);
  1264. _dawnStoneScore = 0;
  1265. _duskStoneScore = 0;
  1266. _dawnFestivalScore = 0;
  1267. _duskFestivalScore = 0;
  1268. break;
  1269. }
  1270. // Make sure all Seven Signs data is saved for future use.
  1271. saveSevenSignsData();
  1272. saveSevenSignsStatus();
  1273. teleLosingCabalFromDungeons(getCabalShortName(getCabalHighestScore()));
  1274. SSQInfo ss = new SSQInfo();
  1275. Broadcast.toAllOnlinePlayers(ss);
  1276. spawnSevenSignsNPC();
  1277. _log.info("SevenSigns: The " + getCurrentPeriodName() + " period has begun!");
  1278. setCalendarForNextPeriodChange();
  1279. // make sure that all the scheduled siege dates are in the Seal Validation period
  1280. List<Castle> castles = CastleManager.getInstance().getCastles();
  1281. for (Castle castle : castles)
  1282. {
  1283. castle.getSiege().correctSiegeDateTime();
  1284. }
  1285. SevenSignsPeriodChange sspc = new SevenSignsPeriodChange();
  1286. ThreadPoolManager.getInstance().scheduleGeneral(sspc, getMilliToPeriodChange());
  1287. }
  1288. }
  1289. public boolean checkIsDawnPostingTicket(int itemId)
  1290. {
  1291. //TODO I think it should be some kind of a list in the datapack for compare;
  1292. if (itemId > 6114 && itemId < 6175)
  1293. return true;
  1294. if (itemId > 6801 && itemId < 6812)
  1295. return true;
  1296. if (itemId > 7997 && itemId < 8008)
  1297. return true;
  1298. if (itemId > 7940 && itemId < 7951)
  1299. return true;
  1300. if (itemId > 6294 && itemId < 6307)
  1301. return true;
  1302. if (itemId > 6831 && itemId < 6834)
  1303. return true;
  1304. if (itemId > 8027 && itemId < 8030)
  1305. return true;
  1306. if (itemId > 7970 && itemId < 7973)
  1307. return true;
  1308. return false;
  1309. }
  1310. public boolean checkIsRookiePostingTicket(int itemId)
  1311. {
  1312. //TODO I think it should be some kind of a list in the datapack for compare;
  1313. if (itemId > 6174 && itemId < 6295)
  1314. return true;
  1315. if (itemId > 6811 && itemId < 6832)
  1316. return true;
  1317. if (itemId > 7950 && itemId < 7971)
  1318. return true;
  1319. if (itemId > 8007 && itemId < 8028)
  1320. return true;
  1321. return false;
  1322. }
  1323. public void giveCPMult(int StrifeOwner)
  1324. {
  1325. L2World.getInstance().forEachPlayer(new GiveCPMult(StrifeOwner));
  1326. }
  1327. private final class GiveCPMult implements TObjectProcedure<L2PcInstance>
  1328. {
  1329. private final int _strifeOwner;
  1330. protected GiveCPMult(int strifeOwner)
  1331. {
  1332. _strifeOwner = strifeOwner;
  1333. }
  1334. @Override
  1335. public final boolean execute(final L2PcInstance character)
  1336. {
  1337. if (character != null)
  1338. {
  1339. //Gives "Victor of War" passive skill to all online characters with Cabal, which controls Seal of Strife
  1340. int cabal = getPlayerCabal(character.getObjectId());
  1341. if (cabal != SevenSigns.CABAL_NULL)
  1342. if (cabal == _strifeOwner)
  1343. character.addSkill(SkillTable.FrequentSkill.THE_VICTOR_OF_WAR.getSkill());
  1344. else
  1345. //Gives "The Vanquished of War" passive skill to all online characters with Cabal, which does not control Seal of Strife
  1346. character.addSkill(SkillTable.FrequentSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1347. }
  1348. return true;
  1349. }
  1350. }
  1351. public void removeCPMult()
  1352. {
  1353. L2World.getInstance().forEachPlayer(new RemoveCPMult());
  1354. }
  1355. protected final class RemoveCPMult implements TObjectProcedure<L2PcInstance>
  1356. {
  1357. @Override
  1358. public final boolean execute(final L2PcInstance character)
  1359. {
  1360. if (character != null)
  1361. {
  1362. //Remove SevenSigns' buffs/debuffs.
  1363. character.removeSkill(SkillTable.FrequentSkill.THE_VICTOR_OF_WAR.getSkill());
  1364. character.removeSkill(SkillTable.FrequentSkill.THE_VANQUISHED_OF_WAR.getSkill());
  1365. }
  1366. return true;
  1367. }
  1368. }
  1369. public boolean checkSummonConditions(L2PcInstance activeChar)
  1370. {
  1371. if (activeChar == null)
  1372. return true;
  1373. //Golems cannot be summoned by Dusk when the Seal of Strife is controlled by the Dawn
  1374. if (isSealValidationPeriod())
  1375. if (getSealOwner(SEAL_STRIFE) == CABAL_DAWN)
  1376. if (getPlayerCabal(activeChar.getObjectId()) == CABAL_DUSK)
  1377. {
  1378. activeChar.sendMessage("You cannot summon Siege Golem or Cannon while Seal of Strife posessed by Lords of Dawn.");
  1379. return true;
  1380. }
  1381. return false;
  1382. }
  1383. private static class SingletonHolder
  1384. {
  1385. protected static final SevenSigns _instance = new SevenSigns();
  1386. }
  1387. }