Sign in to follow this  
sushi-one

[.net] hiscore "system"

Recommended Posts

Im making a simple hi-score system for a web game. Peoples scores need to be saved into a txt file of the format: "Name","EMail","Score" how do I start? (I am new to asp.net codeing)

Share this post


Link to post
Share on other sites
A txt file? You sure? Xml is pretty easily done in .Net

Anyway, look into Serialization. If you do a simple class with just your Name Email and Score as properities and overload the serialisation stuff, you should be on your way.

Share this post


Link to post
Share on other sites
It could be done easily in XML like this:

[Serializable]
public class ScoreEntry
{
private String name, email;
private int score;

public String Name
{
get { return(name); }
set { name = value; }
}

public String Email
{
get { return(email); }
set { email = value; }
}

public int Score
{
get { return(score); }
set { score = value; }
}
}

[Serializable]
public class ScoreCollection : ICollection
{
private ArrayList innerList = new ArrayList();

public ArrayList List
{
get { return(innerList); }
}

//Add all the ICollection stuff and your own Add, Remove functions
}

//Later on in another file...
//Also reference the dll of the same name below
using System.Runtime.Serialization.Formatters.Soap;

//In some function
ScoreCollection myScoreCollection = new ScoreCollection();
//Fill with some data...

//Output to a text file
SoapFormatter formatter = new SoapFormatter( new FileStream("hiscore.txt", FileMode.Create) );
formatter.Serialize(myScoreCollection);



This creates two classes: ScoreEntry and ScoreCollection. Both must be marked with the [Serializable] attribute. This means that any public fields or properties will be converted automatically to xml when using the SoapFormatter. If you have a custom class as a field or property, it must be marked Serializable too.

ScoreCollection is just a serializable collection of ScoreEntry objects. The SoapFormatter will serialize the ArrayList from the List property by serializing each of the ScoreEntry objects. You can use the Deserialize method of a SoapFormatter object to read scores back into a ScoreCollection object.

Another option is to use the BinaryFormatter class, as it saves space, but it is a proprietry format, and you are bound to your particular .NET version of the BinaryFormatter if you use it.

Share this post


Link to post
Share on other sites
Another thing to look out for is the fact that you're writing to disk (at least in the solutions given so far). The serverside code is executed, most of the time, under the ASPNET account. Many times this account has NO right to write to disk. This is intentionally: a hacker could possibly flood the disk and thereby crash the server.

So another way of storing is to put the data in a flat file (read: little overhead) database. Something like access or mysql.

Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this