SevenSigns.java 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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 java.util.logging.Level;
  31. import java.util.logging.Logger;
  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 = Logger.getLogger(SevenSigns.class.getName());
  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.log(Level.SEVERE, "SevenSigns: Failed to load configuration: " + e.getMessage(), e);
  130. }
  131. _log.info("SevenSigns: Currently in the " + getCurrentPeriodName() + " period!");
  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 " + getCabalName(getCabalHighestScore()) + " were victorious last week.");
  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 " + getCabalName(getCabalHighestScore()) + " are in the lead this week.");
  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 " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  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.log(Level.SEVERE, "SevenSigns: Unable to load Seven Signs data from database: " + e.getMessage(), 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.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), 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.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), 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.log(Level.SEVERE, "SevenSigns: Unable to save data to database: " + e.getMessage(), 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.log(Level.SEVERE, "SevenSigns: Failed to save data: " + e.getMessage(), 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 " + getCabalName(e.getValue()) + " have won the " + getSealName(e.getKey(), false) + ".");
  1055. }
  1056. else
  1057. {
  1058. _log.info("SevenSigns: The " + getSealName(e.getKey(), false) + " is currently owned by " + getCabalName(e.getValue()) + ".");
  1059. }
  1060. }
  1061. else
  1062. {
  1063. _log.info("SevenSigns: The " + getSealName(e.getKey(), false) + " remains unclaimed.");
  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 " + getCabalName(_previousWinner) + " have won the competition with " + getCurrentScore(_previousWinner) + " points!");
  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 " + getCurrentPeriodName() + " period has begun!");
  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. }