SevenSigns.java 54 KB

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