SevenSigns.java 50 KB

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