SevenSigns.java 54 KB

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