Will this make the players lagg?

04/11/2013 17:26 LordGragen.#1
hello there, so i made a quest and when you done the quest will check your level and give you the correct gift. but idk why i worry to much how i made the last part and i need to make sure this wont harm the player.



Code:
if (client.Inventory.Contains(729310, 1))
                                                {

                                                    if (client.Entity.Level >= 1 && client.Entity.Level < 25)
                                                    {
                                                        client.Inventory.Add(1458756, 0, 1);
                                                        client.Inventory.DeleteItem(729310, 1);
                                                    }
                                                    else
                                                        if (client.Entity.Level >= 25 && client.Entity.Level < 50)
                                                        {
                                                            client.Inventory.Add(1458958, 0, 1);
                                                            client.Inventory.DeleteItem(729310, 1);
                                                        }
                                                        else
                                                            if (client.Entity.Level >= 50 && client.Entity.Level < 70)
                                                            {
                                                                client.Inventory.Add(1462958, 0, 1);
                                                                client.Inventory.DeleteItem(729310, 1);
                                                            }
                                                            else
                                                                if (client.Entity.Level >= 70 && client.Entity.Level < 90)
                                                                {
                                                                    client.Inventory.Add(1469854, 0, 1);
                                                                    client.Inventory.DeleteItem(729310, 1);
                                                                }
                                                                else
                                                                    if (client.Entity.Level >= 90 && client.Entity.Level < 100)
                                                                    {
                                                                        client.Inventory.Add(1447854, 0, 1);
                                                                        client.Inventory.DeleteItem(729310, 1);
                                                                    }
                                                                    else
                                                                        if (client.Entity.Level >= 100 && client.Entity.Level < 110)
                                                                        {
                                                                            client.Inventory.Add(1454875, 0, 1);
                                                                            client.Inventory.DeleteItem(729310, 1);
                                                                        }
                                                                        else
                                                                            if (client.Entity.Level >= 110 && client.Entity.Level < 120)
                                                                            {
                                                                                client.Inventory.Add(14524421, 0, 1);
                                                                                client.Inventory.DeleteItem(729310, 1);
                                                                            }
                                                                            else
                                                                                if (client.Entity.Level >= 120 && client.Entity.Level < 135)
                                                                                {
                                                                                    client.Inventory.Add(14524422, 0, 1);
                                                                                    client.Inventory.DeleteItem(729310, 1);
                                                                                }
                                                }

so lets say 10 people finish the quest and trune it same time, will this do anything bad? i feel there is better way to do the way i coded but idk, any idea?
04/11/2013 18:19 KraHen#2
Shouldn`t cause problems if your Inventory doesn`t cause any.
04/11/2013 18:49 pro4never#3
This script does nothing besides call add/delete item methods. Assuming those are written properly then there's no reason for it to lag people out.

That being said... not a big fan of that giant if/else string. You sure you wouldn't rather use a reward for each 20-30 levels and use a switch statement?
04/11/2013 19:27 Spirited#4
Well, this might be a cleaner way of doing it (however, the level 25 wouldn't work). On my server, I keep track of the level class (10 levels for every one level class) the player's in (so I don't do the arithmetic that you'd be doing every time this runs). It should be more efficient though because you're only doing one check per level class instead of two (depending on what level ranges you're checking). It also looks cleaner. I have each level class defined in an enumeration type as well; you can do that too to replace the numbers with something a bit more understandable. Really, you don't need to worry too much about efficiency with something like this. Any little change is still O(n) efficiency. We're talking about a couple nanoseconds of difference.

Code:
switch (client.Entity.Level / 10)
{
    // Example:
    case 1: // Level 10
    case 2: // Level 20
        client.Inventory.Add(1458756, 0, 1);
        break;
}

// Put this on the outside since it's always called. Looks cleaner.
client.Inventory.DeleteItem(729310, 1);
04/11/2013 19:46 LordGragen.#5
thank you all,

and fang i must take a look at that, its very clean.

thx all.
04/11/2013 21:13 urgabel#6
Quote:
Originally Posted by LordGragen. View Post
i feel there is better way to do the way i coded but idk, any idea?
Code:
if (client.Inventory.Contains(729310, 1))
{
    int[] levels = {     120,      110,     100,      90,      70,      50,      25,       1}
    int[] prizes = {14524422, 14524421, 1454875, 1447854, 1469854, 1462958, 1458958, 1458756}
    int prize = prizes[levels.First(level => level <= client.Entity.Level)];
    client.Inventory.DeleteItem(729310, 1);
     client.Inventory.Add(prize, 0, 1);
}
@Fang, just curiosity: what will happen if the inventory is full before you add the prize?
Quote:
Originally Posted by Fаng View Post
Code:
// Put this on the outside since it's always called. Looks cleaner.
client.Inventory.DeleteItem(729310, 1);
04/11/2013 22:27 pro4never#7
If you're going to use an array like that... you should not generate it every time the function is called.

Store it somewhere else
04/11/2013 23:12 Spirited#8
Quote:
Originally Posted by urgabel View Post
@Fang, just curiosity: what will happen if the inventory is full before you add the prize?
That's not my problem, and not what my code was getting at. It was just showing a cleaner alternative.
Edit: I like your alternative btw. I do that for walking coordinates. I'm not sure I would like using it for features like that though (just preference - which we have room for since we're talking about a couple nanoseconds for a feature that's used once a year).
04/12/2013 00:45 urgabel#9
Quote:
Originally Posted by LordGragen. View Post
Code:
if (client.Entity.Level >= 120 && client.Entity.Level < 135)
I didn't noticed you limited it up to level 134 at most, but maybe it was not intended so...


Quote:
Originally Posted by pro4never View Post
you should not generate it every time the function is called.
Yes, that's a good appointment.


Quote:
Originally Posted by Fаng View Post
not what my code was getting at
Ok, it was just a curiosity, as I'm not used to manage private servers code. Thanks anyway for answering.
04/12/2013 12:59 shadowman123#10
Or u can use smthing Like

uint ItemID = GetPrizeID(client.Entity.Level);
client.Inventory.Add(ItemID, 0, 1);
client.Inventory.Delete(w.e, 1);

static uint GetPrizeID(byte PlayerLevel)
{
If (PlayerLevel >= 15 && PlayerLevel <= 25)
{
return (ItemID that u would like to add);
}
return 0;
}

Quote:
Originally Posted by urgabel View Post
Code:
if (client.Inventory.Contains(729310, 1))
{
    int[] levels = {     120,      110,     100,      90,      70,      50,      25,       1}
    int[] prizes = {14524422, 14524421, 1454875, 1447854, 1469854, 1462958, 1458958, 1458756}
    int prize = prizes[levels.First(level => level <= client.Entity.Level)];
    client.Inventory.DeleteItem(729310, 1);
     client.Inventory.Add(prize, 0, 1);
}
@Fang, just curiosity: what will happen if the inventory is full before you add the prize?
the method Add inside Inventory already have checker about ur Inventory Items Count if its = 40 it wont allow the item to be added
04/12/2013 15:06 LordGragen.#11
ty shadow, will take a look at that for sure.


and to answer your question bro about full inv.


i am going to add a check for that,

since the quest will give more then 4 items, so i will add a check to make sure the inv have at list 5 empty spot open.

here is a exmaple for you if you like.



if (client.Inventory.Count < 37)
{
add item here
}
else
client.Send(new Message("You need to make atleast 3 free spots in your inventory.", System.Drawing.Color.Red, Message.TopLeft));
04/12/2013 16:12 shadowman123#12
Quote:
Originally Posted by LordGragen. View Post
ty shadow, will take a look at that for sure.


and to answer your question bro about full inv.


i am going to add a check for that,

since the quest will give more then 4 items, so i will add a check to make sure the inv have at list 5 empty spot open.

here is a exmaple for you if you like.



if (client.Inventory.Count < 37)
{
add item here
}
else
client.Send(new Message("You need to make atleast 3 free spots in your inventory.", System.Drawing.Color.Red, Message.TopLeft));
y do u add checker since its already existed
04/12/2013 16:15 KraHen#13
Using many if cases is acceptable IMO, you could wrap them in another function though. Don`t neglect using ifs just because you can use other stuff, conditionals are always one of the fastest parts of a programming language.
04/12/2013 17:26 LordGragen.#14
Quote:
Originally Posted by shadowman123 View Post
y do u add checker since its already existed
oh i know it exist i just gived him a example how i can check other way.