SevenSigns.java 54 KB

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