HighScores.cpp 6.1 KB

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