Changeset 5188


Ignore:
Timestamp:
Feb 22, 2012 7:52:10 PM (3 months ago)
Author:
Zoey76
Message:

BETA: Olympiad rework:

  • Unnecessary float cast/math round.
  • Writing to static field from instanced method.
  • Wrong usage of objects and maps, if you get a reference from an object in a map and update it (the object, for example using a method to update a parameter), is not required to remove and then put the object again, unless you have created a new object instance, where also is not required to remove! put(..) method will replace/update given the proper key and the old value will be returned, if not existent return null.
  • CompetitionType? class formatting.
  • Some other minor fixes.

This fixes unexpected endless loops and data inconsistency and improves performance.

Note: Report bugs at Olympiad rework topic.

Location:
branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/CompetitionType.java

    r4432 r5188  
    1616 
    1717/** 
    18  *  
    1918 * @author DS 
    20  * 
    2119 */ 
    2220public enum CompetitionType 
     
    2624        TEAMS("teams"), 
    2725        OTHER("other"); 
    28  
     26         
    2927        private final String _name; 
    30  
     28         
    3129        private CompetitionType(String name) 
    3230        { 
    3331                _name = name; 
    3432        } 
    35  
     33         
    3634        @Override 
    3735        public final String toString() 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/Olympiad.java

    r5177 r5188  
    5858        private static final Map<Integer, StatsSet> _nobles = new FastMap<>(); 
    5959        protected static L2FastList<StatsSet> _heroesToBe; 
    60         private static TIntIntHashMap _noblesRank; 
     60        private static final TIntIntHashMap _noblesRank = new TIntIntHashMap(); 
    6161         
    6262        private static final String OLYMPIAD_DATA_FILE = "config/olympiad.properties"; 
     
    7979        private static final int[] HERO_IDS = 
    8080        { 
    81                 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 131, 132, 133, 134 
     81                88, 
     82                89, 
     83                90, 
     84                91, 
     85                92, 
     86                93, 
     87                94, 
     88                95, 
     89                96, 
     90                97, 
     91                98, 
     92                99, 
     93                100, 
     94                101, 
     95                102, 
     96                103, 
     97                104, 
     98                105, 
     99                106, 
     100                107, 
     101                108, 
     102                109, 
     103                110, 
     104                111, 
     105                112, 
     106                113, 
     107                114, 
     108                115, 
     109                116, 
     110                117, 
     111                118, 
     112                131, 
     113                132, 
     114                133, 
     115                134 
    82116        }; 
    83117         
     
    245279                { 
    246280                        con = L2DatabaseFactory.getInstance().getConnection(); 
    247                         PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES); 
    248                         ResultSet rset = statement.executeQuery(); 
    249                          
     281                        final PreparedStatement statement = con.prepareStatement(OLYMPIAD_LOAD_NOBLES); 
     282                        final ResultSet rset = statement.executeQuery(); 
     283                        StatsSet statData; 
    250284                        while (rset.next()) 
    251285                        { 
    252                                 StatsSet statData = new StatsSet(); 
    253                                 int charId = rset.getInt(CHAR_ID); 
     286                                statData = new StatsSet(); 
    254287                                statData.set(CLASS_ID, rset.getInt(CLASS_ID)); 
    255288                                statData.set(CHAR_NAME, rset.getString(CHAR_NAME)); 
     
    265298                                statData.set("to_save", false); 
    266299                                 
    267                                 _nobles.put(charId, statData); 
     300                                addNobleStats(rset.getInt(CHAR_ID), statData); 
    268301                        } 
    269302                         
     
    302335                        } 
    303336                         
    304                         _log.info("Olympiad System: " + Math.round(milliToEnd / 60000) + " minutes until period ends"); 
     337                        _log.info("Olympiad System: " + (milliToEnd / 60000) + " minutes until period ends"); 
    305338                         
    306339                        if (_period == 0) 
     
    308341                                milliToEnd = getMillisToWeekChange(); 
    309342                                 
    310                                 _log.info("Olympiad System: Next weekly change is in " + Math.round(milliToEnd / 60000) + " minutes"); 
     343                                _log.info("Olympiad System: Next weekly change is in " + (milliToEnd / 60000) + " minutes"); 
    311344                        } 
    312345                } 
     
    318351        public void loadNoblesRank() 
    319352        { 
    320                 _noblesRank = new TIntIntHashMap(); 
     353                _noblesRank.clear(); 
    321354                TIntIntHashMap tmpPlace = new TIntIntHashMap(); 
    322355                 
     
    459492        { 
    460493                return _nobles.get(playerId); 
    461         } 
    462          
    463         protected static synchronized void updateNobleStats(int playerId, StatsSet stats) 
    464         { 
    465                 _nobles.remove(playerId); 
    466                 _nobles.put(playerId, stats); 
    467494        } 
    468495         
     
    694721                } 
    695722                 
    696                 for (Entry<Integer, StatsSet> entry : _nobles.entrySet()) 
    697                 { 
    698                         final StatsSet nobleInfo = entry.getValue(); 
    699                         int currentPoints = nobleInfo.getInteger(POINTS); 
     723                int currentPoints; 
     724                for (StatsSet nobleInfo : _nobles.values()) 
     725                { 
     726                        currentPoints = nobleInfo.getInteger(POINTS); 
    700727                        currentPoints += WEEKLY_POINTS; 
    701728                        nobleInfo.set(POINTS, currentPoints); 
    702                          
    703                         updateNobleStats(entry.getKey(), nobleInfo); 
    704729                } 
    705730        } 
     
    715740                } 
    716741                 
    717                 for (Entry<Integer, StatsSet> entry : _nobles.entrySet()) 
    718                 { 
    719                         StatsSet nobleInfo = entry.getValue(); 
     742                for (StatsSet nobleInfo : _nobles.values()) 
     743                { 
    720744                        nobleInfo.set(COMP_DONE_WEEK, 0); 
    721745                        nobleInfo.set(COMP_DONE_WEEK_CLASSED, 0); 
    722746                        nobleInfo.set(COMP_DONE_WEEK_NON_CLASSED, 0); 
    723747                        nobleInfo.set(COMP_DONE_WEEK_TEAM, 0); 
    724                          
    725                         updateNobleStats(entry.getKey(), nobleInfo); 
    726748                } 
    727749        } 
     
    790812                                         
    791813                                        nobleInfo.set("to_save", false); 
    792                                          
    793                                         updateNobleStats(charId, nobleInfo); 
    794814                                } 
    795815                                else 
     
    910930                { 
    911931                        _logResults.info("Noble,charid,classid,compDone,points"); 
    912                          
     932                        StatsSet nobleInfo; 
    913933                        for (Entry<Integer, StatsSet> entry : _nobles.entrySet()) 
    914934                        { 
    915                                 final StatsSet nobleInfo = entry.getValue(); 
     935                                nobleInfo = entry.getValue(); 
    916936                                if (nobleInfo == null) 
    917937                                { 
     
    928948                                record.setParameters(new Object[] 
    929949                                { 
    930                                         charId, classId, compDone, points 
     950                                        charId, 
     951                                        classId, 
     952                                        compDone, 
     953                                        points 
    931954                                }); 
    932955                                _logResults.log(record); 
     
    968991                                                record.setParameters(new Object[] 
    969992                                                { 
    970                                                         hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID) 
     993                                                        hero.getInteger(CHAR_ID), 
     994                                                        hero.getInteger(CLASS_ID) 
    971995                                                }); 
    972996                                                _logResults.log(record); 
     
    9951019                                        record.setParameters(new Object[] 
    9961020                                        { 
    997                                                 hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID) 
     1021                                                hero.getInteger(CHAR_ID), 
     1022                                                hero.getInteger(CLASS_ID) 
    9981023                                        }); 
    9991024                                        _logResults.log(record); 
     
    10521077                                        record.setParameters(new Object[] 
    10531078                                        { 
    1054                                                 hero.getInteger(CHAR_ID), hero.getInteger(CLASS_ID) 
     1079                                                hero.getInteger(CHAR_ID), 
     1080                                                hero.getInteger(CLASS_ID) 
    10551081                                        }); 
    10561082                                        _logResults.log(record); 
     
    11631189                { 
    11641190                        noble.set(POINTS, 0); 
    1165                         updateNobleStats(objId, noble); 
    11661191                } 
    11671192                points *= Config.ALT_OLY_GP_PER_POINT; 
     
    13621387        } 
    13631388         
     1389        /** 
     1390         * @param charId the noble object Id. 
     1391         * @param data the stats set data to add. 
     1392         * @return the old stats set if the noble is already present, null otherwise. 
     1393         */ 
     1394        protected static StatsSet addNobleStats(int charId, StatsSet data) 
     1395        { 
     1396                return _nobles.put(Integer.valueOf(charId), data); 
     1397        } 
     1398         
    13641399        @SuppressWarnings("synthetic-access") 
    13651400        private static class SingletonHolder 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadGameNormal.java

    r4942 r5188  
    3636 
    3737/** 
    38  *  
    3938 * @author GodKratos, Pere, DS 
    4039 */ 
     
    311310                                                points = Math.min(playerOnePoints / 3, Config.ALT_OLY_MAX_POINTS); 
    312311                                                removePointsFromParticipant(_playerOne, points); 
    313                                                 _playerOne.updateNobleStats(); 
    314312                                                 
    315313                                                if (Config.ALT_OLY_LOG_FIGHTS) 
     
    331329                                                points = Math.min(playerTwoPoints / 3, Config.ALT_OLY_MAX_POINTS); 
    332330                                                removePointsFromParticipant(_playerTwo, points); 
    333                                                 _playerTwo.updateNobleStats(); 
    334331                                                 
    335332                                                if (Config.ALT_OLY_LOG_FIGHTS) 
     
    425422                                _playerOne.updateStat(getWeeklyMatchType(), 1); 
    426423                                _playerTwo.updateStat(getWeeklyMatchType(), 1); 
    427                                  
    428                                 _playerOne.updateNobleStats(); 
    429                                 _playerTwo.updateNobleStats(); 
    430424                                return; 
    431425                        } 
     
    532526                        _playerOne.updateStat(getWeeklyMatchType(), 1); 
    533527                        _playerTwo.updateStat(getWeeklyMatchType(), 1); 
    534                          
    535                         _playerOne.updateNobleStats(); 
    536                         _playerTwo.updateNobleStats(); 
    537528                         
    538529                        if (Config.ALT_OLY_LOG_FIGHTS) 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadGameTeams.java

    r4942 r5188  
    3333 
    3434/** 
    35  *  
    3635 * @author Pere, DS 
    3736 */ 
     
    506505                                                par = _teamOne[i]; 
    507506                                                removePointsFromParticipant(par, Math.min(par.stats.getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS)); 
    508                                                 par.updateNobleStats(); 
    509507                                        } 
    510508                                } 
     
    515513                                                par = _teamTwo[i]; 
    516514                                                removePointsFromParticipant(par, Math.min(par.stats.getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS)); 
    517                                                 par.updateNobleStats(); 
    518515                                        } 
    519516                                } 
     
    685682                                        par.updateStat(COMP_DONE_WEEK, 1); 
    686683                                        par.updateStat(getWeeklyMatchType(), 1); 
    687                                         par.updateNobleStats(); 
    688684                                } 
    689685 
     
    694690                                        par.updateStat(COMP_DONE_WEEK, 1); 
    695691                                        par.updateStat(getWeeklyMatchType(), 1); 
    696                                         par.updateNobleStats(); 
    697692                                } 
    698693                        } 
     
    817812                                par.updateStat(COMP_DONE_WEEK, 1); 
    818813                                par.updateStat(getWeeklyMatchType(), 1); 
    819                                 par.updateNobleStats(); 
    820814                        } 
    821815 
     
    826820                                par.updateStat(COMP_DONE_WEEK, 1); 
    827821                                par.updateStat(getWeeklyMatchType(), 1); 
    828                                 par.updateNobleStats(); 
    829822                        } 
    830823                } 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/OlympiadManager.java

    r5083 r5188  
    3939public class OlympiadManager 
    4040{ 
    41         private List<Integer> _nonClassBasedRegisters; 
    42         private Map<Integer, List<Integer>> _classBasedRegisters; 
    43         private List<List<Integer>> _teamsBasedRegisters; 
     41        private final List<Integer> _nonClassBasedRegisters; 
     42        private final Map<Integer, List<Integer>> _classBasedRegisters; 
     43        private final List<List<Integer>> _teamsBasedRegisters; 
    4444 
    4545        private OlympiadManager() 
     
    219219                } 
    220220                 
    221                 if (Olympiad.getInstance().getRemainingWeeklyMatches(player.getObjectId()) < 1) 
     221                final int charId = player.getObjectId(); 
     222                if (Olympiad.getInstance().getRemainingWeeklyMatches(charId) < 1) 
    222223                { 
    223224                        player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED); 
     
    232233                                        return false; 
    233234                                 
    234                                 if (Olympiad.getInstance().getRemainingWeeklyMatchesClassed(player.getObjectId()) < 1) 
     235                                if (Olympiad.getInstance().getRemainingWeeklyMatchesClassed(charId) < 1) 
    235236                                { 
    236237                                        player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED_60_NON_CLASSED_30_CLASSED_10_TEAM); 
     
    240241                                List<Integer> classed = _classBasedRegisters.get(player.getBaseClass()); 
    241242                                if (classed != null) 
    242                                         classed.add(player.getObjectId()); 
     243                                        classed.add(charId); 
    243244                                else 
    244245                                { 
    245246                                        classed = new FastList<Integer>().shared(); 
    246                                         classed.add(player.getObjectId()); 
     247                                        classed.add(charId); 
    247248                                        _classBasedRegisters.put(player.getBaseClass(), classed); 
    248249                                } 
     
    256257                                        return false; 
    257258                                 
    258                                 if (Olympiad.getInstance().getRemainingWeeklyMatchesNonClassed(player.getObjectId()) < 1) 
     259                                if (Olympiad.getInstance().getRemainingWeeklyMatchesNonClassed(charId) < 1) 
    259260                                { 
    260261                                        player.sendPacket(SystemMessageId.MAX_OLY_WEEKLY_MATCHES_REACHED_60_NON_CLASSED_30_CLASSED_10_TEAM); 
     
    262263                                } 
    263264 
    264                                 _nonClassBasedRegisters.add(player.getObjectId()); 
     265                                _nonClassBasedRegisters.add(charId); 
    265266                                player.sendPacket(SystemMessageId.YOU_HAVE_BEEN_REGISTERED_IN_A_WAITING_LIST_OF_NO_CLASS_GAMES); 
    266267                                break; 
     
    455456                        return false; 
    456457                } 
    457  
    458                 if (TvTEvent.isPlayerParticipant(noble.getObjectId())) 
     458                 
     459                final int charId = noble.getObjectId(); 
     460                if (TvTEvent.isPlayerParticipant(charId)) 
    459461                { 
    460462                        player.sendMessage("You can't join olympiad while participating on TvT Event."); 
     
    468470                        return false; 
    469471 
    470                 StatsSet statDat = Olympiad.getNobleStats(noble.getObjectId()); 
     472                StatsSet statDat = Olympiad.getNobleStats(charId); 
    471473                if (statDat == null) 
    472474                { 
     
    484486                        statDat.set(Olympiad.COMP_DONE_WEEK_TEAM, 0); 
    485487                        statDat.set("to_save", true); 
    486                         Olympiad.updateNobleStats(noble.getObjectId(), statDat); 
    487                 } 
    488  
    489                 final int points = Olympiad.getInstance().getNoblePoints(noble.getObjectId()); 
     488                        Olympiad.addNobleStats(charId, statDat); 
     489                } 
     490 
     491                final int points = Olympiad.getInstance().getNoblePoints(charId); 
    490492                if (points <= 0) 
    491493                { 
  • branches/unstable/L2J_Server_BETA/java/com/l2jserver/gameserver/model/olympiad/Participant.java

    r5083 r5188  
    2020 
    2121/** 
    22  *  
    2322 * @author DS 
    24  * 
    2523 */ 
    2624public final class Participant 
     
    6563                stats.set(statName, Math.max(stats.getInteger(statName) + increment, 0)); 
    6664        } 
    67  
    68         public final void updateNobleStats() 
    69         { 
    70                 Olympiad.updateNobleStats(objectId, stats); 
    71         } 
    7265} 
Note: See TracChangeset for help on using the changeset viewer.