HighScores.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "HighScores.h"
  28. #include <Rocket/Core/TypeConverter.h>
  29. #include <stdio.h>
  30. HighScores* HighScores::instance = NULL;
  31. HighScores::HighScores() : Rocket::Controls::DataSource("high_scores")
  32. {
  33. ROCKET_ASSERT(instance == NULL);
  34. instance = this;
  35. for (int i = 0; i < NUM_SCORES; i++)
  36. {
  37. scores[i].score = -1;
  38. }
  39. LoadScores();
  40. }
  41. HighScores::~HighScores()
  42. {
  43. ROCKET_ASSERT(instance == this);
  44. SaveScores();
  45. instance = NULL;
  46. }
  47. void HighScores::Initialise()
  48. {
  49. new HighScores();
  50. }
  51. void HighScores::Shutdown()
  52. {
  53. delete instance;
  54. }
  55. void HighScores::GetRow(Rocket::Core::StringList& row, const Rocket::Core::String& table, int row_index, const Rocket::Core::StringList& columns)
  56. {
  57. if (table == "scores")
  58. {
  59. for (size_t i = 0; i < columns.size(); i++)
  60. {
  61. if (columns[i] == "name")
  62. {
  63. row.push_back(scores[row_index].name);
  64. }
  65. else if (columns[i] == "name_required")
  66. {
  67. row.push_back(Rocket::Core::String(4, "%d", scores[row_index].name_required));
  68. }
  69. else if (columns[i] == "score")
  70. {
  71. row.push_back(Rocket::Core::String(32, "%d", scores[row_index].score));
  72. }
  73. else if (columns[i] == "colour")
  74. {
  75. Rocket::Core::String colour_string;
  76. Rocket::Core::TypeConverter< Rocket::Core::Colourb, Rocket::Core::String >::Convert(scores[row_index].colour, colour_string);
  77. row.push_back(colour_string);
  78. }
  79. else if (columns[i] == "wave")
  80. {
  81. row.push_back(Rocket::Core::String(8, "%d", scores[row_index].wave));
  82. }
  83. }
  84. }
  85. }
  86. int HighScores::GetNumRows(const Rocket::Core::String& table)
  87. {
  88. if (table == "scores")
  89. {
  90. for (int i = 0; i < NUM_SCORES; i++)
  91. {
  92. if (scores[i].score == -1)
  93. {
  94. return i;
  95. }
  96. }
  97. return NUM_SCORES;
  98. }
  99. return 0;
  100. }
  101. int HighScores::GetHighScore()
  102. {
  103. if (instance->GetNumRows("scores") == 0)
  104. {
  105. return 0;
  106. }
  107. return instance->scores[0].score;
  108. }
  109. void HighScores::SubmitScore(const Rocket::Core::String& name, const Rocket::Core::Colourb& colour, int wave, int score)
  110. {
  111. instance->SubmitScore(name, colour, wave, score, false);
  112. }
  113. void HighScores::SubmitScore(const Rocket::Core::Colourb& colour, int wave, int score)
  114. {
  115. instance->SubmitScore("", colour, wave, score, true);
  116. }
  117. // Sets the name of the last player to submit their score.
  118. void HighScores::SubmitName(const Rocket::Core::String& name)
  119. {
  120. for (int i = 0; i < instance->GetNumRows("scores"); i++)
  121. {
  122. if (instance->scores[i].name_required)
  123. {
  124. instance->scores[i].name = name;
  125. instance->scores[i].name_required = false;
  126. instance->NotifyRowChange("scores", i, 1);
  127. }
  128. }
  129. }
  130. void HighScores::SubmitScore(const Rocket::Core::String& name, const Rocket::Core::Colourb& colour, int wave, int score, bool name_required)
  131. {
  132. for (int i = 0; i < NUM_SCORES; i++)
  133. {
  134. if (score > scores[i].score)
  135. {
  136. // If we've already got the maximum number of scores, then we have
  137. // to send a RowsRemoved message as we're going to delete the last
  138. // row from the data source.
  139. bool max_rows = scores[NUM_SCORES - 1].score != -1;
  140. // Push down all the other scores.
  141. for (int j = NUM_SCORES - 1; j > i; j--)
  142. {
  143. scores[j] = scores[j - 1];
  144. }
  145. // Insert our new score.
  146. scores[i].name = name;
  147. scores[i].colour = colour;
  148. scores[i].wave = wave;
  149. scores[i].score = score;
  150. scores[i].name_required = name_required;
  151. // Send the row removal message (if necessary).
  152. if (max_rows)
  153. {
  154. NotifyRowRemove("scores", NUM_SCORES - 1, 1);
  155. }
  156. // Then send the rows added message.
  157. NotifyRowAdd("scores", i, 1);
  158. return;
  159. }
  160. }
  161. }
  162. void HighScores::LoadScores()
  163. {
  164. // Open and read the high score file.
  165. FILE* scores_file = fopen("scores.txt", "rt");
  166. if (scores_file)
  167. {
  168. char buffer[1024];
  169. while (fgets(buffer, 1024, scores_file))
  170. {
  171. Rocket::Core::StringList score_parts;
  172. Rocket::Core::StringUtilities::ExpandString(score_parts, Rocket::Core::String(buffer), '\t');
  173. if (score_parts.size() == 4)
  174. {
  175. Rocket::Core::Colourb colour;
  176. int wave;
  177. int score;
  178. if (Rocket::Core::TypeConverter< Rocket::Core::String , Rocket::Core::Colourb >::Convert(score_parts[1], colour) &&
  179. Rocket::Core::TypeConverter< Rocket::Core::String, int >::Convert(score_parts[2], wave) &&
  180. Rocket::Core::TypeConverter< Rocket::Core::String, int >::Convert(score_parts[3], score))
  181. {
  182. SubmitScore(score_parts[0], colour, wave, score);
  183. }
  184. }
  185. }
  186. fclose(scores_file);
  187. }
  188. }
  189. void HighScores::SaveScores()
  190. {
  191. FILE* scores_file = fopen("scores.txt", "wt");
  192. if (scores_file)
  193. {
  194. for (int i = 0; i < GetNumRows("scores"); i++)
  195. {
  196. Rocket::Core::String colour_string;
  197. Rocket::Core::TypeConverter< Rocket::Core::Colourb, Rocket::Core::String >::Convert(scores[i].colour, colour_string);
  198. Rocket::Core::String score(1024, "%s\t%s\t%d\t%d\n", scores[i].name.CString(), colour_string.CString(), scores[i].wave, scores[i].score);
  199. fputs(score.CString(), scores_file);
  200. }
  201. fclose(scores_file);
  202. }
  203. }