SevenSigns.java 54 KB

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