File: database.hh

package info (click to toggle)
performous 1.1%2Bgit20181118-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,712 kB
  • sloc: cpp: 30,008; ansic: 2,751; sh: 801; xml: 464; python: 374; makefile: 22
file content (107 lines) | stat: -rw-r--r-- 3,324 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once

#include "color.hh"
#include "controllers.hh"
#include "fs.hh"
#include "hiscore.hh"
#include "players.hh"
#include "songitems.hh"
#include <string>
#include <ostream>

struct ScoreItem {
	int score;
	input::DevType type;
	std::string track;  ///< includes difficulty
	std::string track_simple; ///< no difficulty
	Color color;
	bool operator<(ScoreItem const& other) const { return score < other.score; }
};

/**Access to a database for performous which holds
  Player-, Hiscore-, Song-, Track- and (in future)
  Partydata.

  This is a facade for Players, Hiscore and SongItems.

  Will be initialized at the very beginning of
  the program.

  The current lists (Players and scores) are used
  to pass the information which players have won
  to the ScoreScreen and then to the players window.
 */
class Database {
public:
	/**Will try to load the database.
	  If it does not succeed the error will be ignored.
	  Only some information will be printed on stderr.
	  */
	Database(fs::path const& filename);
	/**Will try to save the database.
	  This will even be done if the loading failed.
	  It tries to create the directory above the file.
	  */
	~Database();

	/**Loads the whole database from xml.
	  @exception bad_cast may be thrown if xml element is not of correct type
	  @exception xmlpp exceptions may be thrown on any parse errors
	  @exception PlayersException if some conditions of players fail (e.g. no id)
	  @exception HiscoreException if some hiscore conditions fail (e.g. score too high)
	  @exception SongItemsExceptions if some songs conditions fail (e.g. no id)
	  @post filled database
	  */
	void load();
	/**Saves the whole database to xml.
	  Will write out everything to the file given in the constructor, @see file()
	*/
	void save();

	friend class ScreenHiscore;
	friend class ScreenPlayers;
	friend class ScreenSing;
	friend class ScoreWindow;
	friend class LayoutSinger;
	friend class NoteGraph;
	friend class Engine;
private: // will be bypassed by above friend declaration
	typedef std::list<Player> cur_players_t;
	typedef std::list<ScoreItem> cur_scores_t;

	//This fields are misused as additional parameters
	cur_players_t cur;
	cur_scores_t scores;

public: // methods for database management

	/**A facade for Players::addPlayer.*/
	void addPlayer(std::string const& name, std::string const& picture = "", int id = -1);
	/**A facade for SongItems::addSong.*/
	void addSong(std::shared_ptr<Song> s);
	/**A facade for Hiscore::addHiscore.
	 The ids will be looked up first by using the songs and current players data.
	 */
	void addHiscore(std::shared_ptr<Song> s);

public: // methods for database queries
	/**A facade for Hiscore::reachedHiscore.
	  Queries if the current player with current score has reached a new hiscore
	  for the song s.
	 */
	bool reachedHiscore(std::shared_ptr<Song> s) const;

	void queryOverallHiscore(std::ostream & os, std::string const& track = std::string()) const;
	void queryPerSongHiscore(std::ostream & os, std::shared_ptr<Song> s, std::string const& track = std::string()) const;
	void queryPerPlayerHiscore(std::ostream & os, std::string const& track = std::string()) const;

	bool hasHiscore(Song& s) const;
	bool noPlayers() const;

private:
	fs::path m_filename;

	Players m_players;
	Hiscore m_hiscores;
	SongItems m_songs;
};