SevenSigns.java 53 KB

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