Storing data

Started by
3 comments, last by Zahlman 14 years, 10 months ago
Hello, I'm currently working on a sports management game which means I need to store a lot of data. I'm trying to figure out the best way to do this, right now I have written my own functions to store the classes binary. I have also been checking out XML and SQLite but I have failed to come up with a good way to organize the data when storing it. For example using XML, how would you store a structure like this?
struct Stats
{
    int assists;
    int games;
    int goals;
    int penaltyMinutes;
};

struct Player
{
    int height;
    int id;
    int weight;
    std::string name;
    std::vector<Stats> stats;
};
The problem I have with the above structure is obviously storing the vector. Basically I have the same problem when using SQLite. I don't have any previous experience in this area and it is giving me a headache :) Any ideas are welcome.
Advertisement
Hi dabo,

I would recommend to create a logical XML structure for your data first, and only then figure out how to read it. In this case I'd suggest:

<Player height='2' id='0' weight='80' name='John Doe'>  <Stats>    <Stat assists='4' games='20' goals='3' penaltyMinutes='32' />    <Stat assists='2' games='3' goals='1' penaltyMinutes='12' />  </Stats><Player>


To read the actual xml, you might want to check out the TinyXML library.

If you decide to use SQLite instead; I'd create 2 tables to model this.
- A Player table, containing the fields height, id, weight and name
- A Stats table, containing the fields assists, games, goals, penaltyMinutes and quite importantly playerID.
Than to get all stats of a certain player you'd be able to do something along the lines of:
RunQuery( "SELECT * FROM Stats WHERE playerID='{0}'", player.id );


Furthermore, I don't really have much domain knowledge of sports, but the fact that a player needs an array of stats in this manner seems a little odd to me. But again, I'm not exactly sure what you're trying to model, so it might be perfectly fine.

Kinds regards,
Tristan

[Edited by - Tristan85 on May 29, 2009 6:13:20 AM]
Quote:Original post by dabo
Hello, I'm currently working on a sports management game which means I need to store a lot of data. I'm trying to figure out the best way to do this, right now I have written my own functions to store the classes binary.


What are your requirements? If storing as binary is enough, why look for something else?

You can also look into JSON for storage:

{   height : 6,  id     : 1,  weight : 52,  name   : 'John',  stats  : [    {      assists : 3,      games   : 2,      goals   : 1,      penalty : 11     },    {      assists : 2,      games   : 2,      goals   : 8,      penalty : 3    }  ]}


Check out projects like jsoncpp.
Thank you both for your replies.

Yes I would have no problems storing everything binary, but I'm just interested in hearing from others how they would do it.
Quote:Original post by Tristan85
If you decide to use SQLite instead; I'd create 2 tables to model this.
- A Player table, containing the fields height, id, weight and name
- A Stats table, containing the fields assists, games, goals, penaltyMinutes and quite importantly playerID.
Than to get all stats of a certain player you'd be able to do something along the lines of:
RunQuery( "SELECT * FROM Stats WHERE playerID='{0}'", player.id );



If you take this approach, you'll need some way to ensure the order of loaded stats, assuming it matters.

Quote:Furthermore, I don't really have much domain knowledge of sports, but the fact that a player needs an array of stats in this manner seems a little odd to me. But again, I'm not exactly sure what you're trying to model, so it might be perfectly fine.


I'm guessing that they're "career" stats, taken year-over-year. :)

In that case you could add a "year" column to Stats (and make the player-id/year combination a primary key, or something), and an ORDER BY clause to the select statement. But that still wouldn't tell you what year the player started in, and wouldn't handle any missing years. So in the corresponding C++ structures, you'd want to replace the vector with a map of int (the year) to Stats.

This topic is closed to new replies.

Advertisement