HighScoreManager.hxx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. #ifndef SNAKE_HIGHSCOREMANAGER_HXX
  3. #define SNAKE_HIGHSCOREMANAGER_HXX
  4. #include <boost/noncopyable.hpp>
  5. #include <boost/serialization/string.hpp>
  6. #include <boost/serialization/vector.hpp>
  7. #include <boost/serialization/version.hpp>
  8. #include <optional>
  9. #include <string>
  10. #include <vector>
  11. struct Score final {
  12. std::string player_name_;
  13. unsigned points_;
  14. template<typename Archive>
  15. void serialize(Archive& archive, unsigned const version)
  16. {
  17. (void) version;
  18. archive & player_name_;
  19. archive & points_;
  20. }
  21. };
  22. BOOST_CLASS_VERSION(Score, 0);
  23. struct HighScores final {
  24. std::vector<Score> scores_;
  25. template<typename Archive>
  26. void serialize(Archive& archive, unsigned const version)
  27. {
  28. (void) version;
  29. archive & scores_;
  30. }
  31. };
  32. BOOST_CLASS_VERSION(HighScores, 0);
  33. class HighScoreManager final : private boost::noncopyable {
  34. public:
  35. static int constexpr MAX_SCORES = 10;
  36. void set_new_score(unsigned score);
  37. [[nodiscard]] bool has_new_score() const;
  38. void provide_name_for_new_score(std::string const& name);
  39. static HighScoreManager& instance();
  40. private:
  41. HighScoreManager();
  42. void load();
  43. void save() const;
  44. std::string data_dir_;
  45. HighScores high_score_;
  46. std::optional<unsigned> new_score_;
  47. };
  48. #endif // SNAKE_HIGHSCOREMANAGER_HXX