SevenSigns.java 53 KB

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