HighScoreManager.hxx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. [[nodiscard]] unsigned get_new_score() const;
  39. void provide_name_for_new_score(std::string const& name);
  40. static HighScoreManager& instance();
  41. [[nodiscard]] std::vector<Score> const& get_scores() const;
  42. private:
  43. HighScoreManager();
  44. void load();
  45. void save() const;
  46. std::string data_dir_;
  47. HighScores high_score_;
  48. std::optional<unsigned> new_score_;
  49. };
  50. #endif // SNAKE_HIGHSCOREMANAGER_HXX