1 /*
2  * Copyright (c) 2006 - 2008
3  * Wandering Monster Studios Limited
4  *
5  * Any use of this program is governed by the terms of Wandering Monster
6  * Studios Limited's Licence Agreement included with this program, a copy
7  * of which can be obtained by contacting Wandering Monster Studios
8  * Limited at info@wanderingmonster.co.nz.
9  *
10  */
11 
12 #include "HighScores.h"
13 #include <Rocket/Core/StringUtilities.h>
14 #include <Rocket/Core/TypeConverter.h>
15 #include <Rocket/Core.h>
16 #include <stdio.h>
17 
18 HighScores* HighScores::instance = NULL;
19 
HighScores()20 HighScores::HighScores()
21 {
22 	ROCKET_ASSERT(instance == NULL);
23 	instance = this;
24 
25 	for (int i = 0; i < NUM_SCORES; i++)
26 	{
27 		scores[i].score = -1;
28 	}
29 
30 	LoadScores();
31 }
32 
~HighScores()33 HighScores::~HighScores()
34 {
35 	ROCKET_ASSERT(instance == this);
36 	instance = NULL;
37 }
38 
Initialise()39 void HighScores::Initialise()
40 {
41 	new HighScores();
42 }
43 
Shutdown()44 void HighScores::Shutdown()
45 {
46 	delete instance;
47 }
48 
SubmitScore(const Rocket::Core::String & name,const Rocket::Core::Colourb & colour,int wave,int score)49 void HighScores::SubmitScore(const Rocket::Core::String& name, const Rocket::Core::Colourb& colour, int wave, int score)
50 {
51 	for (size_t i = 0; i < NUM_SCORES; i++)
52 	{
53 		if (score > scores[i].score)
54 		{
55 			// Push down all the other scores.
56 			for (size_t j = NUM_SCORES - 1; j > i; j--)
57 			{
58 				scores[j] = scores[j - 1];
59 			}
60 
61 			// Insert our new score.
62 			scores[i].name = name;
63 			scores[i].colour = colour;
64 			scores[i].wave = wave;
65 			scores[i].score = score;
66 
67 			return;
68 		}
69 	}
70 }
71 
LoadScores()72 void HighScores::LoadScores()
73 {
74 	// Open and read the high score file.
75 	Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
76 	Rocket::Core::FileHandle scores_file = file_interface->Open("high_score.txt");
77 
78 	if (scores_file)
79 	{
80 		file_interface->Seek(scores_file, 0, SEEK_END);
81 		size_t scores_length = file_interface->Tell(scores_file);
82 		file_interface->Seek(scores_file, 0, SEEK_SET);
83 
84 		if (scores_length > 0)
85 		{
86 			char* buffer = new char[scores_length + 1];
87 			file_interface->Read(buffer, scores_length, scores_file);
88 			file_interface->Close(scores_file);
89 			buffer[scores_length] = 0;
90 
91 			Rocket::Core::StringList score_lines;
92 			Rocket::Core::StringUtilities::ExpandString(score_lines, buffer, '\n');
93 			delete[] buffer;
94 
95 			for (size_t i = 0; i < score_lines.size(); i++)
96 			{
97 				Rocket::Core::StringList score_parts;
98 				Rocket::Core::StringUtilities::ExpandString(score_parts, score_lines[i], '\t');
99 				if (score_parts.size() == 4)
100 				{
101 					Rocket::Core::Colourb colour;
102 					int wave;
103 					int score;
104 
105 					if (Rocket::Core::TypeConverter< Rocket::Core::String , Rocket::Core::Colourb >::Convert(score_parts[1], colour) &&
106 						Rocket::Core::TypeConverter< Rocket::Core::String, int >::Convert(score_parts[2], wave) &&
107 						Rocket::Core::TypeConverter< Rocket::Core::String, int >::Convert(score_parts[3], score))
108 					{
109 						SubmitScore(score_parts[0], colour, wave, score);
110 					}
111 				}
112 			}
113 		}
114 	}
115 }
116