SevenSigns.java 53 KB

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