NOTE: I’m so sorry I don’t have a code parser on my blog at the moment. This means that the code doesn’t look very good. I’m working on an codeparser, but just can’t find a working one at the moment. So I desided to do the post, and then clean up the source code in it later. 

In a comment to my blog post about PopTheBubble on silverlight.ne, Jason Schluter asked me for the code to the hi-score in the game. So here goes.

The hi-score actually works pretty simple. The game has a Service reference to a WebService, which lives on the web server ofcause (remember the game lives in the users browser). When the Hi-score UserControl is loaded in the Silverlight application (the game), calls a method called “GetScore” to retrieve the top-50 scores/players. When you type in your name and click submit, another method is called on the WebService to save your score. The score is persisted in an xml file on the server.

HiScore.xaml:

    public partial class HiScore : UserControl
    {
        private int score;
        private ServiceReference1.BubbleHiScoreSoapClient scoreClient;

        public HiScore()
        {
            InitializeComponent();
            score = 0;

            //Hook up with WebService
            scoreClient = new PopTheBubble.ServiceReference1.BubbleHiScoreSoapClient();
            scoreClient.GetScoresCompleted += new EventHandler<PopTheBubble.ServiceReference1.GetScoresCompletedEventArgs>(scoreClient_GetScoresCompleted);
            scoreClient.SaveScoreCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(scoreClient_SaveScoreCompleted);
        }

        public void UpdateScore()
        {
            scoreClient.GetScoresAsync();
        }

        public int Score
        {
            get { return this.score; }
            set
            {
                hiScoreTextBlock.Text = “Your score: ” + value;
                this.score = value;
            }
        }

        protected void scoreClient_SaveScoreCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            UpdateScore();
        }

        protected void scoreClient_GetScoresCompleted(object sender, PopTheBubble.ServiceReference1.GetScoresCompletedEventArgs e)
        {
            hiscoreDataGrid.ItemsSource = e.Result;
        }

        private void submitScoreButton_Click(object sender, RoutedEventArgs e)
        {
            if (!playerNameTextbox.Text.ToLower().Equals(“type your name…”) && playerNameTextbox.Text != “”)
                scoreClient.SaveScoreAsync(playerNameTextbox.Text, score);
        }
    }

 

HiScore.asmx 

        [WebMethod]
        public void SaveScore(string name, int score)
        {
           //Saves the score into a xml file. Original code has been removed.
        }

        [WebMethod]
        public List<HiScoreEntry> GetScores()
        {
            //Get the top-50 hiscores from the xml file. Original code has been removed.
        }

As you can see it’s pretty simple, but not very secure. You don’t need to be a state-of-the-art hacker to figure out how to cheat here, but this was not my focus with this game. The code in the WebService is just normal C# code reading/writing from an xml file. Please post a comment if you have any questions.