locked
DateTime Wrong code, Please help me dear programmers. RRS feed

  • Question

  • Hi all. im sorry im beginner and i need help and sorry for bad english.
    This is game sources 1 file. inventory activation. Problem that.When i bought items using time 0 hour. my game table have 1 day, 1 week, and 1 mounth.Please help me. Best Regards.

     public class SM_INVENTORY_ITEM_EQUIP : SendBaseGamePacket    {
            private int _objId;
    
            private int item_id;
    
            private int _type;
    
            private Account _p;
    
            public SM_INVENTORY_ITEM_EQUIP(int id, int type, Account player)
            {
                makeme();
                _objId = id;
                _type = type;
                _p = player;
            }
    
            protected internal override void write()
            {
                DateTime now;
                writeH(535);
                if (_type != 2)
                {
                    writeD(1);
                    now = DateTime.Now;
                    string s = now.ToString("yyMMddHHmm");
                    writeD(Convert.ToInt32(now.ToString("yyMMddHHmm")));
                    writeD(1); //SendGamePackets
                    writeD(0); //SendGamePackets
                    writeD(0); //SendGamePackets
                    writeC(1); //SendGamePackets
                    writeD(0); //SendGamePackets
                }
                else
                {
                    if (DAOM.getInstance().getItem(_objId) == null)
                    {
                        item_id = 0;
                    }
                    else
                    {
                        item_id = DAOM.getInstance().getItem(_objId).id;
                    }
                    writeD(1); //SendGamePackets
                    now = DateTime.Now;
                    writeD(Convert.ToInt32(now.ToString("yyMMddHHmm"))); //SendGamePackets
                    writeQ(_objId);
                    if ((item_id >= 1301510000 ? true : item_id <= 1300002000))
                    {
                        writeD(item_id); //SendGamePackets
                        writeC(2); //SendGamePackets
                        int count = 86400;
                        if (DAOM.getInstance().getItem(_objId) != null) //DB connection manager
                        {
                            count = DAOM.getInstance().getItem(_objId).count;
                        }
                        int days = 86400/count;
                        DateTime data = new DateTime();
                        data.AddDays(days);
                        int tFinal = Convert.ToInt32(data.ToString("yyMMddHHmm"));
                        writeD(tFinal); //SendGamePackets
                    }
                    else
                    {
                        writeD(item_id); //SendGamePackets
                        writeC(1); //SendGamePackets
                        writeD(1); //SendGamePackets
                    }
                }
            }
        }
    }

    Thursday, May 26, 2016 7:51 AM

Answers

  • Hi zOne62,

    Thank you for feedback.

    I think I have understood what you mean. The datetime.adddays methods return a new datetime. The returned value is the sum of the date and time represented by this instance and the number of days represented by value.

    According to your snippet code. You called the datetime structure’s implicit default constructor. So the datetime is an approximate equivalent. It is declaring a datetime value without explicitly assigning a date and time to it.

    If you test the following code. You will find the method call display 1/1/0001.

    DateTime dat1 = new DateTime();
    // The following method call displays 1/1/0001 12:00:00 AM.
    Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));

    I would suggest you need to specify a certain datetime, not implicitly call default constructor. You could refer to below demo that comes from MSDN to assign current date and time to the new DateTime variables.

    DateTime date1 = DateTime.Now;
    DateTime date2 = DateTime.UtcNow;
    DateTime date3 = DateTime.Today;

    Best Regards,

    Albert Zhang


    • Edited by Albert_Zhang Wednesday, June 1, 2016 2:02 AM
    • Proposed as answer by Albert_Zhang Friday, June 3, 2016 10:00 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:23 AM
    Tuesday, May 31, 2016 5:15 AM

All replies

  • Hi zOne62,

    Welcome to MSDN forum.

    First we should confirm the information transfer is no problem. From you bought items to game table. You would try to debug your application.

    >> DateTime now;

    Secondly I would suggest you initialize now variable.

    >> my game table have 1 day, 1 week, and 1 month.

    Which variable is the game table and items? From the snippet code it is difficult find out these.
    If possible, could you please provide a complete  demo to OneDrive?

    Best Regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by DotNet Wang Wednesday, June 1, 2016 2:03 AM correct the paragraph style
    Friday, May 27, 2016 8:40 AM
  • Hi,

    I have to confess that I didn't understand which problem you are facing. Can you explain it in more detail? What is happening and what do you expect?

    But I find the code really hard to read. One item to show what I mean is your if clause:

    if ((item_id >= 1301510000 ? true : item_id <= 1300002000))

    What you check is if item_id is greater/equal that constant or if it is smaller/equal some other constant. So just write it that way:
    if (item_id >= 1301510000 || item_id <= 1300002000)

    I think that would make it much easier to read. And that is just one example.

    And I would not use magic numbers in code. Create a constant and use the constant instead.  

    And it seems that the object oriented analysis is not really done correctly. At least I got the feeling because in your write method you set the item_id which is an instance variable. That gives me a little bit of bad feeling because you change the state of your instance in a method that should just "write" something. Andfrom the logical view, your method is doing something (independend of any parameters) just to control / initialize / check / whatever the state before you really work on it. I am not saying that it is wrong but it seems to be a bad design. At least it is hard to read.

    And maybe you would like to use some coding guidelines as it is used inside the .Net Framework e.g. methods starting with uppercase letter.

    Sorry that I couldn't help you in any better way but maybe my remarks are of a little help to build more readable code.

    With kind regards,

    Konrad

    Friday, May 27, 2016 9:55 AM
  • i captured video for this problem.

    https://www.youtube.com/watch?v=xv8q805ZNFE
     
    i bought in the shop weapon (using 1 day) and  character (7 day). But when i activate this items, using time changed 0 hours.  
    Best Regards,
     Sergey
    • Edited by zOne62 Friday, May 27, 2016 3:55 PM
    Friday, May 27, 2016 3:51 PM
  • Hi zOne62,

    Thank you for posting here.

    I didn’t understand your problem. According to the video you provide, I cannot get more useful information. If possible, could you provide a complete demo? You maybe need to check the state in your program.

    If you have something else, please feel free to contact us.

    Best Regards,

    Albert Zhang
    Saturday, May 28, 2016 7:17 AM
  • Hi, Albert Zhang

    Thanks, for replying.
    Please, see this video.

    https://www.youtube.com/watch?v=4ey4ZGSGEI8

    I think  there have problem

                        int days = 86400/count;
                        DateTime data = new DateTime();
                        data.AddDays(days);


    Best Regards,

    Sergey



    • Edited by zOne62 Saturday, May 28, 2016 11:25 AM
    Saturday, May 28, 2016 11:20 AM
  • Hi zOne62,

    Thank you for feedback.

    I think I have understood what you mean. The datetime.adddays methods return a new datetime. The returned value is the sum of the date and time represented by this instance and the number of days represented by value.

    According to your snippet code. You called the datetime structure’s implicit default constructor. So the datetime is an approximate equivalent. It is declaring a datetime value without explicitly assigning a date and time to it.

    If you test the following code. You will find the method call display 1/1/0001.

    DateTime dat1 = new DateTime();
    // The following method call displays 1/1/0001 12:00:00 AM.
    Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));

    I would suggest you need to specify a certain datetime, not implicitly call default constructor. You could refer to below demo that comes from MSDN to assign current date and time to the new DateTime variables.

    DateTime date1 = DateTime.Now;
    DateTime date2 = DateTime.UtcNow;
    DateTime date3 = DateTime.Today;

    Best Regards,

    Albert Zhang


    • Edited by Albert_Zhang Wednesday, June 1, 2016 2:02 AM
    • Proposed as answer by Albert_Zhang Friday, June 3, 2016 10:00 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:23 AM
    Tuesday, May 31, 2016 5:15 AM