Results 1 to 10 of 10
  1. #1
    Raymond is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default How to improve this code

    Hi,

    Ill explain it here, I got a problem with stability in this code:

    Java Code:
    public List<DropEntry> retrieveDropChances(int monsterId) {
            if (drops.containsKey(monsterId)) {
                return drops.get(monsterId);
            }
            List<DropEntry> ret = new LinkedList<DropEntry>();
            if (monsterId > 9300183 && monsterId < 9300216) {
                for (int i = 2022359; i < 2022367; i++) {
                    ret.add(new DropEntry(i, 10));
                }
                drops.put(monsterId, ret);
                return ret;
            } else if (monsterId > 9300215 && monsterId < 9300269) {
                for (int i = 2022430; i < 2022434; i++) {
                    ret.add(new DropEntry(i, 3));
                }
                drops.put(monsterId, ret);
                return ret;
            }
            try {
                PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT itemid, chance, monsterid FROM monsterdrops WHERE (monsterid = ? AND chance >= 0) OR (monsterid <= 0)");
                ps.setInt(1, monsterId);
                ResultSet rs = ps.executeQuery();
                MapleMonster theMonster = null;
                while (rs.next()) {
                    int rowMonsterId = rs.getInt("monsterid");
                    int chance = rs.getInt("chance");
                    if (rowMonsterId != monsterId && rowMonsterId != 0) {
                        if (theMonster == null) {
                            theMonster = MapleLifeFactory.getMonster(monsterId);
                        }
                        chance += theMonster.getLevel() * rowMonsterId;
                    }
                    ret.add(new DropEntry(rs.getInt("itemid"), chance));
                }
                rs.close();
                ps.close();
            } catch (SQLException e) {
            }
            drops.put(monsterId, ret);
            return ret;
        }
    As you can see PreparedStatement ps = DatabaseConnection.getConnection().prepareStatemen t("SELECT itemid, chance, monsterid FROM monsterdrops WHERE (monsterid = ? AND chance >= 0) OR (monsterid <= 0)"); will eat my CPU + RAM when a player ingame kills a monster.

    Yep, there are over 4000+ values in "monsterdrops" table in MYSQL.

    How would I reduce the usage of it? I dont want 10-100 players attack different mobs and MYSQL blowing up.
    Last edited by Raymond; 03-18-2010 at 09:00 PM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Lookup by exact match of monster ID should be fast. Perhaps your problem is that you're looking up all monsters with negative IDs time and time and time again... You could try looking up all such monsters only once, on application start-up perhaps, and then simplify the SQL query.

  3. #3
    Raymond is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    Lookup by exact match of monster ID should be fast. Perhaps your problem is that you're looking up all monsters with negative IDs time and time and time again... You could try looking up all such monsters only once, on application start-up perhaps, and then simplify the SQL query.
    It would take forever loading it one by one. Loading it on the Loginserver is probaly a good idea?

  4. #4
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    I would consider using a Data Base proxy which saves results in a hashmap<monsterId,HashMap<itemId,chance>>, and if it doesnt have the value, it asks the mysql database.

    Seems like you will be needing this values over and over, so it doesnt make sense to me having them all the time in the DB.

    What do you think ?

  5. #5
    Raymond is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    I would need the best method in recoding it so it saves mem and CPU load. As I removed that code for a trial and test with over 30 people playing, mysql mem usage went down by alot.


    What's the best method?

  6. #6
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Id do this,

    interface MonsterRetriever
    HashMap<itemId,chanchce> getDropRates(int monsterId);

    and just implement it with really basic lazy-load list and you will see tremendous change.

  7. #7
    Raymond is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    What does it do first? Can you recode the code at the top for me as an example. I'm confused on what your method does.

  8. #8
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    // HashMap<MonsterId,HashMap<ItemId,Chance>>
    private HashMap<Integer,HashMap<Integer,Double>> dropRates;
    
    public HashMap<Integer,Double> getDropChances(Integer monsterId){
       if(dropRates.containsKey(monsterId){
          return dropRates.get(monsterId);
       }else{
           // connect to data base
           // update the hashmap
           // return the new entry of the hashmap
       }
    }

  9. #9
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    with this code, you can dinamically add new monsters to your database, and keeps the drop rates in the database and not hardcoded.

    You may also want to put a refresh method maybe? so you can have happy hours in your game... i dont know thats up to you

  10. #10
    Raymond is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by fceruti View Post
    with this code, you can dinamically add new monsters to your database, and keeps the drop rates in the database and not hardcoded.

    You may also want to put a refresh method maybe? so you can have happy hours in your game... i dont know thats up to you
    Thanks there, as long as it reads the drop chances and itemid its ok. How do you hook it up to MYSQL?

    }else{
    // connect to data base
    // update the hashmap
    // return the new entry of the hashmap
    I need this urgent =/

    "<Integer,Double> "
    Might get compiling errors.
    Last edited by Raymond; 03-19-2010 at 08:30 AM.

Similar Threads

  1. How to improve Jslider
    By JohnST in forum New To Java
    Replies: 3
    Last Post: 01-12-2010, 01:04 AM
  2. how to improve my security?
    By anthrax in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-13-2009, 09:08 AM
  3. Replies: 16
    Last Post: 08-05-2008, 11:34 AM
  4. how to improve java performance
    By sunjavaboy in forum Advanced Java
    Replies: 6
    Last Post: 06-15-2008, 05:58 AM
  5. how to improve the performance of JWS?
    By dinesh kaushik in forum Java Applets
    Replies: 0
    Last Post: 11-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •