score manager beta (c'est signé)
This commit is contained in:
		
							parent
							
								
									f32488458c
								
							
						
					
					
						commit
						d4df66ead7
					
				| @ -4,4 +4,6 @@ Questions que je (Thomas Rubini) voudrais poser | ||||
| - Est-ce que vouloir faire des structures optimisées (pas de redondance de mémoire) est une bonne chose, ou pas importa,t ? | ||||
| - Est-ce que traduire les chars A B et C (identifiants des types d'aliens) tirés de la config en valeurs d'enum est une bonne chose, pas important, ou contre-productif ? | ||||
| - Est-ce que mon implémentation du réseau est bonne ? | ||||
| - Est-on obligé d'utiliser size_t quand on sait que la taille du vecteur ne dépassera jamais concrètement la taille d'un int (cas précis : taille de 100 maximum, est-on obligé d'utiliser size_t de 8 bytes ?) | ||||
| - Est-on obligé d'utiliser size_t quand on sait que la taille du vecteur ne dépassera jamais concrètement la taille d'un int (cas précis : taille de 100 maximum, est-on obligé d'utiliser size_t de 8 bytes ?) | ||||
| - Est-ce mon implémentation du multithreading est bonne ? | ||||
| - Que pensez-vous de la sémantique de déplacement, plutot que la référence constante ? | ||||
| @ -34,6 +34,7 @@ private: | ||||
| 	// basic methods
 | ||||
| 	void display(); | ||||
| 	void updateColumns(); | ||||
| 	void handleScoreSaving(); | ||||
| 
 | ||||
| 	// managers
 | ||||
| 	void managePlayers(); | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| #ifndef GUARD_PIXELMANAGER_H | ||||
| #define GUARD_PIXELMANAGER_H | ||||
| 
 | ||||
| #include <string> | ||||
| #include "mingl/mingl.h" | ||||
| #include "mingl/shape/line.h" | ||||
| #include "mingl/shape/triangle.h" | ||||
| @ -8,6 +9,8 @@ | ||||
| #include "mingl/shape/circle.h" | ||||
| #include "mingl/gui/sprite.h" | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| class PixelManager{ | ||||
| public: | ||||
| 	MinGL& window; | ||||
| @ -29,7 +32,8 @@ public: | ||||
| 	unsigned getScreenWidth(); | ||||
| 	void startFrame(); | ||||
| 	void endFrame(); | ||||
| 	 | ||||
| 
 | ||||
| 	void askPlayerNameMenu(string& name); | ||||
| 
 | ||||
| }; | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										26
									
								
								headers/scoresManager.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								headers/scoresManager.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| #ifndef GUARD_SCORESMANAGER_H | ||||
| #define GUARD_SCORESMANAGER_H | ||||
| 
 | ||||
| #include <utility> | ||||
| #include <vector> | ||||
| #include <string> | ||||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| struct Score{ | ||||
| 	string name; | ||||
| 	unsigned points; | ||||
| 	Score() = default; | ||||
| 	Score(const string& name, unsigned points); | ||||
| }; | ||||
| 
 | ||||
| class ScoresManager { | ||||
| public: | ||||
| 	vector<Score> scores; | ||||
| 	void inputScore(const string& name, unsigned points); | ||||
| 	void readFile(); | ||||
| 	void writeFile(); | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										3
									
								
								scores.kus
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								scores.kus
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| 11083679872916328441 | ||||
| thomas2,2 | ||||
| thomas,1 | ||||
| @ -23,6 +23,16 @@ void Game::updateColumns(){ | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void Game::handleScoreSaving(){ | ||||
| 	cout << players[0].score << endl; // will remove
 | ||||
| 
 | ||||
| 	string pName; | ||||
| 	pm.askPlayerNameMenu(pName); | ||||
| 	if(playMode==PlayMode::TWO_LOCAL){ | ||||
| 
 | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void Game::managedGames() { | ||||
| 
 | ||||
| 	playMode = PlayMode::NONE; | ||||
| @ -32,7 +42,7 @@ void Game::managedGames() { | ||||
| 			playMode = initialMenuHandler(); | ||||
| 		}else{ | ||||
| 			playGame(); // will read the playMode
 | ||||
| 			cout << players[0].score << endl; // will remove
 | ||||
| 			handleScoreSaving(); | ||||
| 			cout << "END OF GAME" << endl; | ||||
| 			break; // TODO remove
 | ||||
| 			if(!deathMenuHandler()) playMode = PlayMode::NONE; // back to the main menu
 | ||||
|  | ||||
| @ -44,6 +44,10 @@ void PixelManager::drawPlayer(const nsGraphics::Vec2D& baseVector, unsigned widt | ||||
| 	window << nsShape::Triangle(nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width*2,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT*0.9)+baseVector, color); | ||||
| } | ||||
| 
 | ||||
| void PixelManager::askPlayerNameMenu(string& name){ | ||||
| 	name = "Thomas"; | ||||
| } | ||||
| 
 | ||||
| void PixelManager::drawMissile(const nsGraphics::Vec2D& baseVector, unsigned width, const nsGraphics::RGBAcolor& color){ | ||||
| 	window << nsShape::Rectangle(baseVector, baseVector + Position(width, width * PROJ_LENGTH_FACTOR), color); | ||||
| } | ||||
|  | ||||
							
								
								
									
										97
									
								
								src/scoresManager.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/scoresManager.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,97 @@ | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <sstream> | ||||
| #include <utility> | ||||
| #include <openssl/sha.h> | ||||
| #include "scoresManager.h" | ||||
| 
 | ||||
| // Our own format : kustom
 | ||||
| #define SCORE_FILE "scores.kus" | ||||
| 
 | ||||
| #define SECRET_KEY "WeAreAGroupOf3" | ||||
| 
 | ||||
| void readWholeFile(ifstream& ifs, string& str){ | ||||
| 	stringstream ss; | ||||
| 	ss << ifs.rdbuf(); | ||||
| 	str.assign(ss.str()); | ||||
| } | ||||
| 
 | ||||
| static std::hash<string> hasher; | ||||
| 
 | ||||
| bool verifyHash(size_t savedHash, string& content){ | ||||
| 	// non-cryptographic hash, but it is part of the std, and openssl is REALLY difficult
 | ||||
| 	// to use in C++ while keeping semantic, because there are no wrappers...
 | ||||
| 	int actualHash = hasher(content+SECRET_KEY); | ||||
| 	return actualHash==savedHash; | ||||
| } | ||||
| 
 | ||||
| void ScoresManager::readFile() { | ||||
| 	ifstream ifs(SCORE_FILE); | ||||
| 	if(!ifs.is_open())return; // file can not exist
 | ||||
| 
 | ||||
| 	/* the first line is the hash sum of (the following content + a secret key)
 | ||||
| 	 * We use it to generate a data signature to prevent users | ||||
| 	 * from tampering with the save file | ||||
| 	 */ | ||||
| 	size_t hash; | ||||
| 	ifs >> hash; | ||||
| 
 | ||||
| 	string content; | ||||
| 	readWholeFile(ifs, content); | ||||
| 
 | ||||
| 	scores.clear(); | ||||
| 	if(verifyHash(hash, content)){ | ||||
| 		stringstream ss(content); | ||||
| 
 | ||||
| 		string line; | ||||
| 		Score s; | ||||
| 		while(true){ | ||||
| 			getline(ss, line); | ||||
| 			if(ss.eof())break; | ||||
| 
 | ||||
| 			size_t index = line.find(','); | ||||
| 			s.name = line.substr(0, index); | ||||
| 			s.points = stoi(line.substr(index)); | ||||
| 		} | ||||
| 	}else{ | ||||
| 		cerr << "Integrity check of the save file failed. Has it been tampered ?" << endl; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ScoresManager::writeFile() { | ||||
| 	ofstream ofs(SCORE_FILE); | ||||
| 
 | ||||
| 	string str; | ||||
| 	cout << scores.size() << endl; | ||||
| 	for(Score& sc : scores){ | ||||
| 		str.append(sc.name); | ||||
| 		str.append(","); | ||||
| 		str.append(to_string(sc.points)); | ||||
| 		str.append("\n"); | ||||
| 	} | ||||
| 
 | ||||
| 	ofs << hasher(str) << endl << str; | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * Insertion sort, probably the most efficient here | ||||
|  */ | ||||
| void ScoresManager::inputScore(const string& name, unsigned points) { | ||||
| 	auto ite = scores.begin(); | ||||
| 	while(ite!=scores.end()){ | ||||
| 		if(points>ite->points) { | ||||
| 			scores.emplace(ite, name, points); | ||||
| 			break; | ||||
| 		} | ||||
| 		++ite; | ||||
| 	} | ||||
| 	if(ite==scores.end())scores.emplace(ite, name, points); | ||||
| 	if(scores.size()==6)scores.resize(5); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // Not sure if I should use move semantics
 | ||||
| Score::Score(const string& name, unsigned int points) { | ||||
| 	this->name = name; | ||||
| 	this->points = points; | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user