Class structure for the proposed data and its containers ?

Posted by Prix on Stack Overflow See other posts from Stack Overflow or by Prix
Published on 2010-12-31T23:45:08Z Indexed on 2010/12/31 23:54 UTC
Read the original article Hit count: 158

Filed under:
|
|
|
|

First I would like to wish a happy new year to everyone that may read this :)

I am having trouble on how to make a container for some data that I am importing into my application, and I am not sure on how to explain this very well and my english is not really a that good so I hope you can bear with my mistake and help me with some guidance.

Currently with a foreach I am importing the follow fields from the data I receive: guid, itemid, name, owner(optional, can be null), zoneid, titleid, subid, heading, x, y, z, typeA, typeB, typeC

From the above fields I need to store a Waypoint list of all coords a given item has moved to BUT for each guid I have a new list of waypoints.

And from the waypoint list the first entry is also my initial item start location which would be my item initial position (if you notice i have a separate list for it which I was not sure would be better or not) not all items have a waypoint list but all items have the first position.

So the first idea I came with to store this data was a list with a class with 2 inner classes with their list:

public List<ItemList> myList = new List<ItemList>();
public class ItemList
{
    public int guid { get; set; }
    public int itemid { get; set; }
    public string name { get; set; }
    public int titleid { get; set; }
    public itemType status { get; set; }

    public class Waypoint
    {
        public float posX { get; set; }
        public float posY { get; set; }
        public float posZ { get; set; }
    }
    public List<Waypoint> waypoint = new List<Waypoint>();

    public class Location
    {
        public int zone { get; set; }
        public int subid { get; set; }
        public int heading { get; set; }
        public float posX { get; set; }
        public float posY { get; set; }
        public float posZ { get; set; }
    }
    public List<Location> position = new List<Location>();
}

So here is an example of how I would add a new waypoint to a GUID that exists in the list

                    bool itemExists = myList.Exists(item => item.guid == guid && item.itemid == itemid);
                    if (itemExists)
                    {
                        int lastDistance = 3;

                        ItemList.Waypoint nextWaypoint;
                        ItemList myItem = myList.Find(item => item.guid == guid && item.itemid == itemid);
                        if (myItem.waypoint.Count == 0)
                        {
                            nextWaypoint = new ItemList.Waypoint();
                            nextWaypoint.posX = PosX;
                            nextWaypoint.posY = PosY;
                            nextWaypoint.posZ = PosZ;
                        }
                        else
                        {
                            ItemList.Waypoint lastWaypoint = myItem.waypoint[myItem.waypoint.Count - 1];
                            if (lastWaypoint != null)
                            {
                                lastDistance = getDistance(x, y, z, lastWaypoint.posX, lastWaypoint.posY, lastWaypoint.posZ);
                            }

                            if (lastDistance > 2)
                            {
                                nextWaypoint = new ItemList.Waypoint();
                                nextWaypoint.posX = PosX;
                                nextWaypoint.posY = PosY;
                                nextWaypoint.posZ = PosZ;
                            }
                        }
                        myItem.waypoint.Add(nextWaypoint);
                    }

Then to register a new item I would take advantage of the itemExist above so I won't register the same GUID again:

                    ItemList newItem = new ItemList();
                    newItem.guid = guid;
                    newItem.itemid = itemid;
                    newItem.name = name;
                    newItem.status = status;
                    newItem.titleid = titleid;

                    // Item location
                    ItemList.Location itemLocation = new ItemList.Location();
                    itemLocation.subid = 0;
                    itemLocation.zone= zone;
                    itemLocation.heading = convertHeading(Heading);
                    itemLocation.posX = PosX;
                    itemLocation.posY = PosY;
                    itemLocation.posZ = PosZ;
                    newItem.position.Add(itemLocation);
                    myList.Add(newItem);

Could you help me with advices on how my class structure and lists should look like ?

Are there better ways to interate with the lists to get lastWaypoint of a GUID or verify wether an item exist or not ?

What else would you advise me in general ?

PS: If you have any questions or if there is something I missed to post please let me know and I will update it.

© Stack Overflow or respective owner

Related posts about c#

Related posts about class-design