SevenSigns.java 54 KB

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