SevenSigns.java 53 KB

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