Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.16 KB | None | 0 0
  1. package l2f.gameserver.model.entity.olympiad;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.concurrent.CopyOnWriteArrayList;
  10. import java.util.concurrent.ScheduledFuture;
  11.  
  12. import l2f.commons.configuration.ExProperties;
  13. import l2f.gameserver.Config;
  14. import l2f.gameserver.ThreadPoolManager;
  15. import l2f.gameserver.dao.OlympiadNobleDAO;
  16. import l2f.gameserver.instancemanager.OlympiadHistoryManager;
  17. import l2f.gameserver.instancemanager.ServerVariables;
  18. import l2f.gameserver.model.GameObjectsStorage;
  19. import l2f.gameserver.model.Party;
  20. import l2f.gameserver.model.Player;
  21. import l2f.gameserver.model.base.ClassId;
  22. import l2f.gameserver.model.entity.Hero;
  23. import l2f.gameserver.model.instances.NpcInstance;
  24. import l2f.gameserver.network.serverpackets.SystemMessage2;
  25. import l2f.gameserver.network.serverpackets.components.CustomMessage;
  26. import l2f.gameserver.network.serverpackets.components.SystemMsg;
  27. import l2f.gameserver.templates.StatsSet;
  28. import l2f.gameserver.utils.Location;
  29. import l2f.gameserver.utils.MultiValueIntegerMap;
  30. import l2f.gameserver.utils.TimeUtils;
  31.  
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34.  
  35. public class Olympiad
  36. {
  37.     private static final Logger _log = LoggerFactory.getLogger(Olympiad.class);
  38.  
  39.     public static Map<Integer, StatsSet> _nobles;
  40.     public static Map<Integer, Integer> _noblesRank;
  41.     public static List<StatsSet> _heroesToBe;
  42.     public static List<Integer> _nonClassBasedRegisters = new CopyOnWriteArrayList<Integer>();
  43.     public static MultiValueIntegerMap _classBasedRegisters = new MultiValueIntegerMap();
  44.     public static MultiValueIntegerMap _teamBasedRegisters = new MultiValueIntegerMap();
  45.  
  46.     //public static final int DEFAULT_POINTS = 50;
  47.     //private static final int WEEKLY_POINTS = 10;
  48.     public static final int TEAM_PARTY_SIZE = 3;
  49.  
  50.     public static final String OLYMPIAD_HTML_PATH = "olympiad/";
  51.  
  52.     public static final String CHAR_ID = "char_id";
  53.     public static final String CLASS_ID = "class_id";
  54.     public static final String CHAR_NAME = "char_name";
  55.     public static final String POINTS = "olympiad_points";
  56.     public static final String POINTS_PAST = "olympiad_points_past";
  57.     public static final String POINTS_PAST_STATIC = "olympiad_points_past_static";
  58.     public static final String COMP_DONE = "competitions_done";
  59.     public static final String COMP_WIN = "competitions_win";
  60.     public static final String COMP_LOOSE = "competitions_loose";
  61.     public static final String GAME_CLASSES_COUNT = "game_classes_count";
  62.     public static final String GAME_NOCLASSES_COUNT = "game_noclasses_count";
  63.     public static final String GAME_TEAM_COUNT = "game_team_count";
  64.  
  65.     public static long _olympiadEnd;
  66.     public static long _validationEnd;
  67.     public static int _period;
  68.     public static long _nextWeeklyChange;
  69.     public static int _currentCycle;
  70.     private static long _compEnd;
  71.     private static Calendar _compStart;
  72.     public static boolean _inCompPeriod;
  73.     public static boolean _isOlympiadEnd;
  74.  
  75.     private static ScheduledFuture<?> _scheduledOlympiadEnd;
  76.     public static ScheduledFuture<?> _scheduledManagerTask;
  77.     public static ScheduledFuture<?> _scheduledWeeklyTask;
  78.     public static ScheduledFuture<?> _scheduledValdationTask;
  79.  
  80.     public static List<String> _playersIp = new ArrayList<String>();
  81.     public static List<String> _playersHWID = new ArrayList<String>();
  82.  
  83.     public static final Stadia[] STADIUMS = new Stadia[Config.OLYMPIAD_STADIAS_COUNT];
  84.  
  85.     public static OlympiadManager _manager;
  86.     private static List<NpcInstance> _npcs = new ArrayList<NpcInstance>();
  87.  
  88.     public static void load()
  89.     {
  90.         _nobles = new ConcurrentHashMap<>();
  91.         _currentCycle = ServerVariables.getInt("Olympiad_CurrentCycle", -1);
  92.         _period = ServerVariables.getInt("Olympiad_Period", -1);
  93.         _olympiadEnd = ServerVariables.getLong("Olympiad_End", -1);
  94.         _validationEnd = ServerVariables.getLong("Olympiad_ValdationEnd", -1);
  95.         _nextWeeklyChange = ServerVariables.getLong("Olympiad_NextWeeklyChange", -1);
  96.  
  97.         ExProperties olympiadProperties = Config.load(Config.OLYMPIAD);
  98.  
  99.         if (_currentCycle == -1)
  100.             _currentCycle = olympiadProperties.getProperty("CurrentCycle", 1);
  101.         if (_period == -1)
  102.             _period = olympiadProperties.getProperty("Period", 0);
  103.         if(_olympiadEnd == -1)
  104.             _olympiadEnd = olympiadProperties.getProperty("OlympiadEnd", 0L);
  105.         if (_validationEnd == -1)
  106.             _validationEnd = olympiadProperties.getProperty("ValdationEnd", 0L);
  107.         if(_nextWeeklyChange == -1)
  108.             _nextWeeklyChange = olympiadProperties.getProperty("NextWeeklyChange", 0L);
  109.  
  110.         initStadiums();
  111.  
  112.         OlympiadHistoryManager.getInstance();
  113.         OlympiadNobleDAO.getInstance().select();
  114.         OlympiadDatabase.loadNoblesRank();
  115.  
  116.         switch (_period)
  117.         {
  118.             case 0:
  119.                 if (_olympiadEnd == 0 || _olympiadEnd < Calendar.getInstance().getTimeInMillis())
  120.                     OlympiadDatabase.setNewOlympiadEnd();
  121.                 else
  122.                     _isOlympiadEnd = false;
  123.                 break;
  124.             case 1:
  125.                 _isOlympiadEnd = true;
  126.                 _scheduledValdationTask = ThreadPoolManager.getInstance().schedule(new ValidationTask(), getMillisToValidationEnd());
  127.                 break;
  128.             default:
  129.                 _log.warn("Olympiad System: Omg something went wrong in loading!! Period = " + _period);
  130.                 return;
  131.         }
  132.  
  133.         _log.info("Olympiad System: Loading Olympiad System....");
  134.         if (_period == 0)
  135.             _log.info("Olympiad System: Currently in Olympiad Period");
  136.         else
  137.             _log.info("Olympiad System: Currently in Validation Period");
  138.  
  139.         _log.info("Olympiad System: Period Ends....");
  140.  
  141.         long milliToEnd;
  142.         if (_period == 0)
  143.             milliToEnd = getMillisToOlympiadEnd();
  144.         else
  145.             milliToEnd = getMillisToValidationEnd();
  146.  
  147.         double numSecs = milliToEnd / 1000 % 60;
  148.         double countDown = (milliToEnd / 1000 - numSecs) / 60;
  149.         int numMins = (int) Math.floor(countDown % 60);
  150.         countDown = (countDown - numMins) / 60;
  151.         int numHours = (int) Math.floor(countDown % 24);
  152.         int numDays = (int) Math.floor((countDown - numHours) / 24);
  153.  
  154.         _log.info("Olympiad System: In " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  155.  
  156.         if (_period == 0)
  157.         {
  158.             _log.info("Olympiad System: Next Weekly Change is in....");
  159.  
  160.             milliToEnd = getMillisToWeekChange();
  161.  
  162.             double numSecs2 = milliToEnd / 1000 % 60;
  163.             double countDown2 = (milliToEnd / 1000 - numSecs2) / 60;
  164.             int numMins2 = (int) Math.floor(countDown2 % 60);
  165.             countDown2 = (countDown2 - numMins2) / 60;
  166.             int numHours2 = (int) Math.floor(countDown2 % 24);
  167.             int numDays2 = (int) Math.floor((countDown2 - numHours2) / 24);
  168.  
  169.             _log.info("Olympiad System: In " + numDays2 + " days, " + numHours2 + " hours and " + numMins2 + " mins.");
  170.         }
  171.  
  172.         _log.info("Olympiad System: Loaded " + _nobles.size() + " Noblesses");
  173.  
  174.         if (_period == 0)
  175.             init();
  176.     }
  177.  
  178.     public static void cancelPeriodTasks()
  179.     {
  180.         if (_scheduledValdationTask != null)
  181.             _scheduledValdationTask.cancel(false);
  182.         if (_scheduledOlympiadEnd != null)
  183.             _scheduledOlympiadEnd.cancel(false);
  184.     }
  185.  
  186.     private static void initStadiums()
  187.     {
  188.         for (int i = 0; i < STADIUMS.length; i++)
  189.             if (STADIUMS[i] == null)
  190.                 STADIUMS[i] = new Stadia();
  191.     }
  192.  
  193.     public static void init()
  194.     {
  195.         if (_period == 1)
  196.             return;
  197.  
  198.         _compStart = Calendar.getInstance();
  199.         _compStart.set(Calendar.HOUR_OF_DAY, Config.ALT_OLY_START_TIME);
  200.         _compStart.set(Calendar.MINUTE, Config.ALT_OLY_MIN);
  201.         _compEnd = _compStart.getTimeInMillis() + Config.ALT_OLY_CPERIOD;
  202.  
  203.         if (_scheduledOlympiadEnd != null)
  204.             _scheduledOlympiadEnd.cancel(false);
  205.         _scheduledOlympiadEnd = ThreadPoolManager.getInstance().schedule(new OlympiadEndTask(), getMillisToOlympiadEnd());
  206.  
  207.         updateCompStatus();
  208.  
  209.         if (_scheduledWeeklyTask != null)
  210.             _scheduledWeeklyTask.cancel(false);
  211.         _scheduledWeeklyTask = ThreadPoolManager.getInstance().scheduleAtFixedRate(new WeeklyTask(), getMillisToWeekChange(), Config.ALT_OLY_WPERIOD);
  212.     }
  213.  
  214.     public static synchronized boolean registerNoble(Player noble, CompType type)
  215.     {
  216.         if (noble.getClassId().getLevel() != 4)
  217.         {
  218.             return false;
  219.         }
  220.  
  221.         if (!_inCompPeriod || _isOlympiadEnd)
  222.         {
  223.             noble.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  224.             return false;
  225.         }
  226.  
  227.         if(getMillisToOlympiadEnd() <= 600 * 1000)
  228.         {
  229.             noble.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  230.             return false;
  231.         }
  232.  
  233.         if (getMillisToCompEnd() <= 600 * 1000)
  234.         {
  235.             noble.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  236.             return false;
  237.         }
  238.  
  239.         if (noble.isCursedWeaponEquipped())
  240.         {
  241.             noble.sendPacket(SystemMsg.YOU_CANNOT_REGISTER_WHILE_IN_POSSESSION_OF_A_CURSED_WEAPON);
  242.             return false;
  243.         }
  244.  
  245.         StatsSet nobleInfo = _nobles.get(noble.getObjectId());
  246.  
  247.         if (!validPlayer(noble, noble, type))
  248.         {
  249.             return false;
  250.         }
  251.  
  252.         if(getNoblePoints(noble.getObjectId()) < 1)
  253.         {
  254.             noble.sendMessage(new CustomMessage("l2f.gameserver.model.entity.Olympiad.LessPoints", noble));
  255.             return false;
  256.         }
  257.  
  258.         if (noble.getOlympiadGame() != null)
  259.         {
  260.             //
  261.             return false;
  262.         }
  263.  
  264.         if (Config.OLYMPIAD_PLAYER_IP && isHWIDRegistered(noble.getHWID()))
  265.         {
  266.             noble.sendMessage("Only one player per PC can register in Olympiad at the time");
  267.             return false;
  268.         }
  269.  
  270.         int classId = nobleInfo.getInteger(CLASS_ID, 0);
  271.  
  272.         // SoulHound hack
  273.         if (classId == 133)
  274.             classId = 132;
  275.  
  276.         switch (type)
  277.         {
  278.             case CLASSED:
  279.             {
  280.                 _classBasedRegisters.put(classId, noble.getObjectId());
  281.                 noble.sendPacket(SystemMsg.YOU_HAVE_BEEN_REGISTERED_FOR_THE_GRAND_OLYMPIAD_WAITING_LIST_FOR_A_CLASS_SPECIFIC_MATCH);
  282.                 break;
  283.             }
  284.             case NON_CLASSED:
  285.             {
  286.                 _nonClassBasedRegisters.add(noble.getObjectId());
  287.                 noble.sendPacket(SystemMsg.YOU_ARE_CURRENTLY_REGISTERED_FOR_A_1V1_CLASS_IRRELEVANT_MATCH);
  288.                 break;
  289.             }
  290.             case TEAM:
  291.             {
  292.                 Party party = noble.getParty();
  293.                 if (party == null)
  294.                 {
  295.                     noble.sendPacket(SystemMsg.ONLY_A_PARTY_LEADER_CAN_REQUEST_A_TEAM_MATCH);
  296.                     return false;
  297.                 }
  298.  
  299.                 if (party.size() != TEAM_PARTY_SIZE)
  300.                 {
  301.                     noble.sendPacket(SystemMsg.THE_REQUEST_CANNOT_BE_MADE_BECAUSE_THE_REQUIREMENTS_HAVE_NOT_BEEN_MET);
  302.                     return false;
  303.                 }
  304.  
  305.                 for (Player member : party.getMembers())
  306.                 {
  307.                     if (!validPlayer(noble, member, type))
  308.                     {
  309.                         return false;
  310.                     }
  311.                 }
  312.  
  313.                 _teamBasedRegisters.putAll(noble.getObjectId(), party.getMembersObjIds());
  314.                 noble.sendPacket(SystemMsg.YOU_ARE_CURRENTLY_REGISTERED_FOR_A_3_VS_3_CLASS_IRRELEVANT_TEAM_MATCH);
  315.                 break;
  316.             }
  317.         }
  318.  
  319.         return true;
  320.     }
  321.  
  322.     private static boolean validPlayer(Player sendPlayer, Player validPlayer, CompType type)
  323.     {
  324.         if (!validPlayer.isNoble())
  325.         {
  326.             sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_DOES_NOT_MEET_THE_PARTICIPATION_REQUIREMENTS_ONLY_NOBLESSE_CHARACTERS_CAN_PARTICIPATE_IN_THE_OLYMPIAD).addName(validPlayer));
  327.             return false;
  328.         }
  329.  
  330.         if (validPlayer.getBaseClassId() != validPlayer.getClassId().getId())
  331.         {
  332.             sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_DOES_NOT_MEET_THE_PARTICIPATION_REQUIREMENTS_SUBCLASS_CHARACTER_CANNOT_PARTICIPATE_IN_THE_OLYMPIAD).addName(validPlayer));
  333.             return false;
  334.         }
  335.  
  336.         int[] ar = getWeekGameCounts(validPlayer.getObjectId());
  337.  
  338.         switch (type)
  339.         {
  340.             case CLASSED:
  341.                 if (_classBasedRegisters.containsValue(validPlayer.getObjectId()))
  342.                 {
  343.                     sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_IS_ALREADY_REGISTERED_ON_THE_CLASS_MATCH_WAITING_LIST).addName(validPlayer));
  344.                     return false;
  345.                 }
  346.  
  347.                 if (ar[1] == 0)
  348.                 {
  349.                     validPlayer.sendPacket(SystemMsg.THE_TOTAL_NUMBER_OF_MATCHES_THAT_CAN_BE_ENTERED_IN_1_WEEK_IS_60_CLASS_IRRELEVANT_INDIVIDUAL_MATCHES_30_SPECIFIC_MATCHES_AND_10_TEAM_MATCHES);
  350.                     return false;
  351.                 }
  352.                 break;
  353.             case NON_CLASSED:
  354.                 if (_nonClassBasedRegisters.contains(validPlayer.getObjectId()))
  355.                 {
  356.                     sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_IS_ALREADY_REGISTERED_ON_THE_WAITING_LIST_FOR_THE_CLASS_IRRELEVANT_INDIVIDUAL_MATCH).addName(validPlayer));
  357.                     return false;
  358.                 }
  359.                 if (ar[2] == 0)
  360.                 {
  361.                     validPlayer.sendPacket(SystemMsg.THE_TOTAL_NUMBER_OF_MATCHES_THAT_CAN_BE_ENTERED_IN_1_WEEK_IS_60_CLASS_IRRELEVANT_INDIVIDUAL_MATCHES_30_SPECIFIC_MATCHES_AND_10_TEAM_MATCHES);
  362.                     return false;
  363.                 }
  364.                 break;
  365.             case TEAM:
  366.                 if (_teamBasedRegisters.containsValue(validPlayer.getObjectId()))
  367.                 {
  368.                     sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_IS_ALREADY_REGISTERED_ON_THE_WAITING_LIST_FOR_THE_3_VS_3_CLASS_IRRELEVANT_TEAM_MATCH).addName(validPlayer));
  369.                     return false;
  370.                 }
  371.                 if (ar[3] == 0)
  372.                 {
  373.                     validPlayer.sendPacket(SystemMsg.THE_TOTAL_NUMBER_OF_MATCHES_THAT_CAN_BE_ENTERED_IN_1_WEEK_IS_60_CLASS_IRRELEVANT_INDIVIDUAL_MATCHES_30_SPECIFIC_MATCHES_AND_10_TEAM_MATCHES);
  374.                     return false;
  375.                 }
  376.                 break;
  377.         }
  378.  
  379.         if (ar[0] == 0)
  380.         {
  381.             validPlayer.sendPacket(SystemMsg.THE_MAXIMUM_MATCHES_YOU_CAN_PARTICIPATE_IN_1_WEEK_IS_70);
  382.             return false;
  383.         }
  384.  
  385.         if (isRegisteredInComp(validPlayer))
  386.         {
  387.             sendPlayer.sendPacket(new SystemMessage2(SystemMsg.C1_IS_ALREADY_REGISTERED_ON_THE_MATCH_WAITING_LIST).addName(validPlayer));
  388.             return false;
  389.         }
  390.  
  391.         return true;
  392.     }
  393.  
  394.     public static synchronized void logoutPlayer(Player player)
  395.     {
  396.         _classBasedRegisters.removeValue(player.getObjectId());
  397.         _nonClassBasedRegisters.remove(new Integer(player.getObjectId()));
  398.         _teamBasedRegisters.removeValue(player.getObjectId());
  399.  
  400.         OlympiadGame game = player.getOlympiadGame();
  401.         if (game != null)
  402.             try
  403.             {
  404.                 if (!game.logoutPlayer(player) && !game.validated)
  405.                     game.endGame(20, true);
  406.             }
  407.             catch(Exception e)
  408.             {
  409.                 _log.error("Error on Olympiad logout Player", e);
  410.             }
  411.     }
  412.  
  413.     public static synchronized boolean unRegisterNoble(Player noble)
  414.     {
  415.         if (!_inCompPeriod || _isOlympiadEnd)
  416.         {
  417.             noble.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  418.             return false;
  419.         }
  420.  
  421.         if (!noble.isNoble())
  422.         {
  423.             noble.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  424.             return false;
  425.         }
  426.  
  427.         if (!isRegistered(noble))
  428.         {
  429.             noble.sendPacket(SystemMsg.YOU_ARE_NOT_CURRENTLY_REGISTERED_FOR_THE_GRAND_OLYMPIAD);
  430.             return false;
  431.         }
  432.  
  433.         OlympiadGame game = noble.getOlympiadGame();
  434.         if (game != null)
  435.         {
  436.             if (game.getStatus() == BattleStatus.Begin_Countdown)
  437.             {
  438.                 // TODO: System Message
  439.                 //TODO [VISTALL] узнать ли прерываетÑ�Ñ� бой и еÑ�ли так ли Ñ�то та меÑ�Ñ�ага SystemMsg.YOUR_OPPONENT_MADE_HASTE_WITH_THEIR_TAIL_BETWEEN_THEIR_LEGS_THE_MATCH_HAS_BEEN_CANCELLED
  440.                 noble.sendMessage("Now you can't cancel participation in the Grand Olympiad.");
  441.                 return false;
  442.             }
  443.  
  444.             try
  445.             {
  446.                 if (!game.logoutPlayer(noble) && !game.validated)
  447.                     game.endGame(20, true);
  448.             }
  449.             catch(Exception e)
  450.             {
  451.                 _log.error("Error on olympiad unRegister Noble", e);
  452.             }
  453.         }
  454.         _classBasedRegisters.removeValue(noble.getObjectId());
  455.         _nonClassBasedRegisters.remove(new Integer(noble.getObjectId()));
  456.         _teamBasedRegisters.removeValue(noble.getObjectId());
  457.  
  458.         noble.sendPacket(SystemMsg.YOU_HAVE_BEEN_REMOVED_FROM_THE_GRAND_OLYMPIAD_WAITING_LIST);
  459.  
  460.         return true;
  461.     }
  462.  
  463.     private static synchronized void updateCompStatus()
  464.     {
  465.         long milliToStart = getMillisToCompBegin();
  466.         double numSecs = milliToStart / 1000 % 60;
  467.         double countDown = (milliToStart / 1000 - numSecs) / 60;
  468.         int numMins = (int) Math.floor(countDown % 60);
  469.         countDown = (countDown - numMins) / 60;
  470.         int numHours = (int) Math.floor(countDown % 24);
  471.         int numDays = (int) Math.floor((countDown - numHours) / 24);
  472.  
  473.         _log.info("Olympiad System: Competition Period Starts in " + numDays + " days, " + numHours + " hours and " + numMins + " mins.");
  474.         _log.info("Olympiad System: Event starts/started: " + _compStart.getTime());
  475.  
  476.         ThreadPoolManager.getInstance().schedule(new CompStartTask(), getMillisToCompBegin());
  477.     }
  478.  
  479.     private static long getMillisToOlympiadEnd()
  480.     {
  481.         return _olympiadEnd - System.currentTimeMillis();
  482.     }
  483.  
  484.     static long getMillisToValidationEnd()
  485.     {
  486.         if (_validationEnd > System.currentTimeMillis())
  487.             return _validationEnd - System.currentTimeMillis();
  488.         return 10L;
  489.     }
  490.  
  491.     public static boolean isOlympiadEnd()
  492.     {
  493.         return _isOlympiadEnd;
  494.     }
  495.  
  496.     public static boolean inCompPeriod()
  497.     {
  498.         return _inCompPeriod;
  499.     }
  500.  
  501.     private static long getMillisToCompBegin()
  502.     {
  503.         if (_compStart.getTimeInMillis() < Calendar.getInstance().getTimeInMillis() && _compEnd > Calendar.getInstance().getTimeInMillis())
  504.             return 10L;
  505.         if (_compStart.getTimeInMillis() > Calendar.getInstance().getTimeInMillis())
  506.             return _compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  507.         return setNewCompBegin();
  508.     }
  509.  
  510.     private static long setNewCompBegin()
  511.     {
  512.         _compStart = Calendar.getInstance();
  513.         _compStart.set(Calendar.HOUR_OF_DAY, Config.ALT_OLY_START_TIME);
  514.         _compStart.set(Calendar.MINUTE, Config.ALT_OLY_MIN);
  515.         _compStart.add(Calendar.HOUR_OF_DAY, 24);
  516.         _compEnd = _compStart.getTimeInMillis() + Config.ALT_OLY_CPERIOD;
  517.  
  518.         _log.info("Olympiad System: New Schedule @ " + _compStart.getTime());
  519.  
  520.         return _compStart.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  521.     }
  522.  
  523.     public static long getMillisToCompEnd()
  524.     {
  525.         return _compEnd - Calendar.getInstance().getTimeInMillis();
  526.     }
  527.  
  528.     private static long getMillisToWeekChange()
  529.     {
  530.         if(_nextWeeklyChange > Calendar.getInstance().getTimeInMillis())
  531.             return _nextWeeklyChange - Calendar.getInstance().getTimeInMillis();
  532.         return 10L;
  533.     }
  534.  
  535.     protected static void reloadOlympiadEnd()
  536.     {
  537.         _olympiadEnd = TimeUtils.getMilisecondsToNextDay(Config.ALT_OLY_DATE_END, 0, 1);
  538.     }
  539.  
  540.     public static synchronized void doWeekTasks()
  541.     {
  542.         if (_period == 1)
  543.             return;
  544.         for (Map.Entry<Integer, StatsSet> entry : _nobles.entrySet())
  545.         {
  546.             StatsSet set = entry.getValue();
  547.             Player player = GameObjectsStorage.getPlayer(entry.getKey());
  548.  
  549.             if (_period != 1)
  550.                 set.set(POINTS, set.getInteger(POINTS) + Config.OLYMPIAD_POINTS_WEEKLY);
  551.             set.set(GAME_CLASSES_COUNT, 0);
  552.             set.set(GAME_NOCLASSES_COUNT, 0);
  553.             set.set(GAME_TEAM_COUNT, 0);
  554.  
  555.             if (player != null)
  556.                 player.sendPacket(new SystemMessage2(SystemMsg.C1_HAS_EARNED_S2_POINTS_IN_THE_GRAND_OLYMPIAD_GAMES).addName(player).addInteger(Config.OLYMPIAD_POINTS_WEEKLY));
  557.         }
  558.     }
  559.  
  560.     public static int getCurrentCycle()
  561.     {
  562.         return _currentCycle;
  563.     }
  564.  
  565.     public static synchronized void addSpectator(int id, Player spectator)
  566.     {
  567.         if(spectator.isInOlympiadMode() || isRegistered(spectator) || Olympiad.isRegisteredInComp(spectator))
  568.         {
  569.             spectator.sendPacket(SystemMsg.YOU_MAY_NOT_OBSERVE_A_GRAND_OLYMPIAD_GAMES_MATCH_WHILE_YOU_ARE_ON_THE_WAITING_LIST);
  570.             return;
  571.         }
  572.  
  573.         if (spectator.isInCombat() || spectator.getPvpFlag() > 0)
  574.         {
  575.             spectator.sendPacket(SystemMsg.YOU_CANNOT_OBSERVE_WHILE_YOU_ARE_IN_COMBAT);
  576.             return;
  577.         }
  578.  
  579.         final OlympiadGame game = getOlympiadGame(id);
  580.         if(game == null || game.getStatus() == BattleStatus.Begining || game.getStatus() == BattleStatus.Begin_Countdown || game.getStatus() == BattleStatus.Ending)
  581.         {
  582.             spectator.sendPacket(SystemMsg.THE_GRAND_OLYMPIAD_GAMES_ARE_NOT_CURRENTLY_IN_PROGRESS);
  583.             return;
  584.         }
  585.  
  586.         if (spectator.getPet() != null)
  587.             spectator.getPet().unSummon();
  588.  
  589.         List<Location> spawns = game.getReflection().getInstancedZone().getTeleportCoords();
  590.         if (spawns.size() < 3)
  591.         {
  592.             Location c1 = spawns.get(0);
  593.             Location c2 = spawns.get(1);
  594.             spectator.enterOlympiadObserverMode(new Location((c1.x + c2.x) / 2, (c1.y + c2.y) / 2, (c1.z + c2.z) / 2), game, game.getReflection());
  595.         }
  596.         else
  597.             spectator.enterOlympiadObserverMode(spawns.get(2), game, game.getReflection());
  598.     }
  599.  
  600.     public static synchronized void removeSpectator(int id, Player spectator)
  601.     {
  602.         if (_manager == null || _manager.getOlympiadInstance(id) == null)
  603.             return;
  604.  
  605.         _manager.getOlympiadInstance(id).removeSpectator(spectator);
  606.     }
  607.  
  608.     public static List<Player> getSpectators(int id)
  609.     {
  610.         if (_manager == null || _manager.getOlympiadInstance(id) == null)
  611.             return null;
  612.         return _manager.getOlympiadInstance(id).getSpectators();
  613.     }
  614.  
  615.     public static OlympiadGame getOlympiadGame(int gameId)
  616.     {
  617.         if (_manager == null || gameId < 0)
  618.             return null;
  619.         return _manager.getOlympiadGames().get(gameId);
  620.     }
  621.  
  622.     public static synchronized int[] getWaitingList()
  623.     {
  624.         if (!inCompPeriod())
  625.             return null;
  626.  
  627.         int[] array = new int[3];
  628.         array[0] = _classBasedRegisters.totalSize();
  629.         array[1] = _nonClassBasedRegisters.size();
  630.         array[2] = _teamBasedRegisters.totalSize();
  631.  
  632.         return array;
  633.     }
  634.  
  635.     public static synchronized int getNoblessePasses(Player player)
  636.     {
  637.         int objId = player.getObjectId();
  638.  
  639.         StatsSet noble = _nobles.get(objId);
  640.         if (noble == null)
  641.             return 0;
  642.  
  643.         int points = noble.getInteger(POINTS_PAST);
  644.         if (points == 0) // Уже получил бонуÑ�
  645.             return 0;
  646.  
  647.         int rank = _noblesRank.get(objId);
  648.         switch (rank)
  649.         {
  650.             case 1:
  651.                 points = Config.ALT_OLY_RANK1_POINTS;
  652.                 break;
  653.             case 2:
  654.                 points = Config.ALT_OLY_RANK2_POINTS;
  655.                 break;
  656.             case 3:
  657.                 points = Config.ALT_OLY_RANK3_POINTS;
  658.                 break;
  659.             case 4:
  660.                 points = Config.ALT_OLY_RANK4_POINTS;
  661.                 break;
  662.             default:
  663.                 points = Config.ALT_OLY_RANK5_POINTS;
  664.         }
  665.  
  666.         if (player.isHero() || Hero.getInstance().isInactiveHero(player.getObjectId()))
  667.             points += Config.ALT_OLY_HERO_POINTS;
  668.  
  669.         noble.set(POINTS_PAST, 0);
  670.         OlympiadDatabase.saveNobleData(objId);
  671.  
  672.         return points * Config.ALT_OLY_GP_PER_POINT;
  673.     }
  674.  
  675.     public static synchronized boolean isRegistered(Player noble)
  676.     {
  677.         if (_classBasedRegisters.containsValue(noble.getObjectId()))
  678.             return true;
  679.         if (_nonClassBasedRegisters.contains(noble.getObjectId()))
  680.             return true;
  681.         if (_teamBasedRegisters.containsValue(noble.getObjectId()))
  682.             return true;
  683.         return false;
  684.     }
  685.  
  686.     public static synchronized boolean isRegisteredInComp(Player player)
  687.     {
  688.         if (isRegistered(player))
  689.             return true;
  690.         if (_manager == null || _manager.getOlympiadGames() == null)
  691.             return false;
  692.         for (OlympiadGame g : _manager.getOlympiadGames().values())
  693.             if (g != null && g.isRegistered(player.getObjectId()))
  694.                 return true;
  695.         return false;
  696.     }
  697.  
  698.     public static synchronized boolean isHWIDRegistered(String hwid)
  699.     {
  700.         Collection<Integer> allIds = new ArrayList<>();
  701.         allIds.addAll(_classBasedRegisters.allValues());
  702.         allIds.addAll(_nonClassBasedRegisters);
  703.         allIds.addAll(_teamBasedRegisters.allValues());
  704.  
  705.         for (Integer playerId : allIds)
  706.         {
  707.             Player player = GameObjectsStorage.getPlayer(playerId.intValue());
  708.             if (player != null && player.getHWID().equals(hwid))
  709.                 return true;
  710.         }
  711.         return false;
  712.     }
  713.  
  714.     /**
  715.      * Возвращает олимпийÑ�кие очки за текущий период
  716.      * @param objId
  717.      * @return
  718.      */
  719.     public static synchronized int getNoblePoints(int objId)
  720.     {
  721.         StatsSet noble = _nobles.get(objId);
  722.         if (noble == null)
  723.             return 0;
  724.         return noble.getInteger(POINTS, 0);
  725.     }
  726.  
  727.     /**
  728.      * Возвращает олимпийÑ�кие очки за прошлый период
  729.      * @param objId
  730.      * @return
  731.      */
  732.     public static synchronized int getNoblePointsPast(int objId)
  733.     {
  734.         StatsSet noble = _nobles.get(objId);
  735.         if (noble == null)
  736.             return 0;
  737.         return noble.getInteger(POINTS_PAST, 0);
  738.     }
  739.  
  740.     public static synchronized int getCompetitionDone(int objId)
  741.     {
  742.         StatsSet noble = _nobles.get(objId);
  743.         if (noble == null)
  744.             return 0;
  745.         return noble.getInteger(COMP_DONE, 0);
  746.     }
  747.  
  748.     public static synchronized int getCompetitionWin(int objId)
  749.     {
  750.         StatsSet noble = _nobles.get(objId);
  751.         if (noble == null)
  752.             return 0;
  753.         return noble.getInteger(COMP_WIN, 0);
  754.     }
  755.  
  756.     public static synchronized int getCompetitionLoose(int objId)
  757.     {
  758.         StatsSet noble = _nobles.get(objId);
  759.         if (noble == null)
  760.             return 0;
  761.         return noble.getInteger(COMP_LOOSE, 0);
  762.     }
  763.  
  764.     public static synchronized int[] getWeekGameCounts(int objId)
  765.     {
  766.         int[] ar = new int[4];
  767.  
  768.         StatsSet noble = _nobles.get(objId);
  769.         if (noble == null)
  770.             return ar;
  771.  
  772.         ar[0] = Config.GAME_MAX_LIMIT - noble.getInteger(GAME_CLASSES_COUNT, 0) - noble.getInteger(GAME_NOCLASSES_COUNT) - noble.getInteger(GAME_TEAM_COUNT, 0);
  773.         ar[1] = Config.GAME_CLASSES_COUNT_LIMIT - noble.getInteger(GAME_CLASSES_COUNT, 0);
  774.         ar[2] = Config.GAME_NOCLASSES_COUNT_LIMIT - noble.getInteger(GAME_NOCLASSES_COUNT, 0);
  775.         ar[3] =  Config.GAME_TEAM_COUNT_LIMIT - noble.getInteger(GAME_TEAM_COUNT, 0);
  776.  
  777.         return ar;
  778.     }
  779.  
  780.     public static Stadia[] getStadiums()
  781.     {
  782.         return STADIUMS;
  783.     }
  784.  
  785.     public static List<NpcInstance> getNpcs()
  786.     {
  787.         return _npcs;
  788.     }
  789.  
  790.     public static void addOlympiadNpc(NpcInstance npc)
  791.     {
  792.         _npcs.add(npc);
  793.     }
  794.  
  795.     public static void changeNobleName(int objId, String newName)
  796.     {
  797.         StatsSet noble = _nobles.get(objId);
  798.         if (noble == null)
  799.             return;
  800.         noble.set(CHAR_NAME, newName);
  801.         OlympiadDatabase.saveNobleData(objId);
  802.     }
  803.  
  804.     public static String getNobleName(int objId)
  805.     {
  806.         StatsSet noble = _nobles.get(objId);
  807.         if (noble == null)
  808.             return null;
  809.         return noble.getString(CHAR_NAME, "");
  810.     }
  811.  
  812.     public static int getNobleClass(int objId)
  813.     {
  814.         StatsSet noble = _nobles.get(objId);
  815.         if (noble == null)
  816.             return 0;
  817.         return noble.getInteger(CLASS_ID, 0);
  818.     }
  819.  
  820.     public static void manualSetNoblePoints(int objId, int points)
  821.     {
  822.         StatsSet noble = _nobles.get(objId);
  823.         if (noble == null)
  824.             return;
  825.         noble.set(POINTS, points);
  826.         OlympiadDatabase.saveNobleData(objId);
  827.     }
  828.  
  829.     public static synchronized boolean isNoble(int objId)
  830.     {
  831.         return _nobles.get(objId) != null;
  832.     }
  833.  
  834.     public static synchronized void addNoble(Player noble)
  835.     {
  836.         if (!_nobles.containsKey(noble.getObjectId()))
  837.         {
  838.             int classId = noble.getBaseClassId();
  839.             if(classId < 88 || classId >= 123 && classId <= 130) // ЕÑ�ли Ñ�то не 3-Ñ� профа, то иÑ�правлÑ�ем Ñ�о 2-й на 3-ÑŽ.
  840.                 for (ClassId id : ClassId.VALUES)
  841.                     if (id.level() == 3 && id.getParent(0).getId() == classId)
  842.                     {
  843.                         classId = id.getId();
  844.                         break;
  845.                     }
  846.  
  847.             StatsSet statDat = new StatsSet();
  848.             statDat.set(CLASS_ID, classId);
  849.             statDat.set(CHAR_NAME, noble.getName());
  850.             statDat.set(POINTS, Config.OLYMPIAD_POINTS_DEFAULT);
  851.             statDat.set(POINTS_PAST, 0);
  852.             statDat.set(POINTS_PAST_STATIC, 0);
  853.             statDat.set(COMP_DONE, 0);
  854.             statDat.set(COMP_WIN, 0);
  855.             statDat.set(COMP_LOOSE, 0);
  856.             statDat.set(GAME_CLASSES_COUNT, 0);
  857.             statDat.set(GAME_NOCLASSES_COUNT, 0);
  858.             statDat.set(GAME_TEAM_COUNT, 0);
  859.  
  860.             _nobles.put(noble.getObjectId(), statDat);
  861.             OlympiadDatabase.saveNobleData();
  862.         }
  863.     }
  864.  
  865.     public static synchronized void removeNoble(Player noble)
  866.     {
  867.         _nobles.remove(noble.getObjectId());
  868.         OlympiadDatabase.saveNobleData();
  869.     }
  870.  
  871.     public static int getPeriod()
  872.     {
  873.         return _period;
  874.     }
  875.  
  876.     public static int getCountOpponents()
  877.     {
  878.         return _nonClassBasedRegisters.size() + _classBasedRegisters.size() + _teamBasedRegisters.size();
  879.     }
  880. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement