git repos / blockattack-game

blame: source/code/stats.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c3a11a57 sago007 2016-03-06 11:21 4
Copyright (C) 2005-2016 Poul Sander
89c4a3a6 sago007 2008-08-29 14:32 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
89c4a3a6 sago007 2008-08-29 14:32 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
89c4a3a6 sago007 2008-08-29 14:32 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
89c4a3a6 sago007 2008-08-29 14:32 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
c3a11a57 sago007 2016-03-06 11:21 20
http://www.blockattack.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "stats.h"
89c4a3a6 sago007 2008-08-29 14:32 25
#include "common.h"
1899da11 sago007 2015-12-30 19:04 26
#include "os.hpp"
c3a11a57 sago007 2016-03-06 11:21 27
#include "sago/SagoMisc.hpp"
c3a11a57 sago007 2016-03-06 11:21 28
#include <sstream>
89c4a3a6 sago007 2008-08-29 14:32 29
96eb7a16 sago007 2016-04-29 17:01 30
using std::string;
96eb7a16 sago007 2016-04-29 17:01 31
using std::stringstream;
96eb7a16 sago007 2016-04-29 17:01 32
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 33
using std::endl;
96eb7a16 sago007 2016-04-29 17:01 34
using std::map;
96eb7a16 sago007 2016-04-29 17:01 35
using std::vector;
53001fa2 sago007 2015-11-08 15:13 36
acd79baf sago007 2015-11-22 19:37 37
Stats* Stats::instance = nullptr;
89c4a3a6 sago007 2008-08-29 14:32 38
c7f2082f sago007 2016-03-13 11:48 39
const char* const statsFileName = "statsFile";
6c28b502 sago007 2016-02-13 11:38 40
ecef5838 sago007 2015-11-24 20:01 41
Stats::Stats() {
c53e6443 sago007 2012-04-17 11:04 42
	statMap.clear();
c53e6443 sago007 2012-04-17 11:04 43
	load();
89c4a3a6 sago007 2008-08-29 14:32 44
}
89c4a3a6 sago007 2008-08-29 14:32 45
ecef5838 sago007 2015-11-24 20:01 46
void Stats::load() {
30412ca6 sago007 2016-03-25 19:35 47
	string fileContent = sago::GetFileContent(statsFileName);
30412ca6 sago007 2016-03-25 19:35 48
	stringstream inFile(fileContent);
c53e6443 sago007 2012-04-17 11:04 49
	string key;
30412ca6 sago007 2016-03-25 19:35 50
	string value;
ecef5838 sago007 2015-11-24 20:01 51
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 52
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 53
			inFile >> key; // The key is first on line
c53e6443 sago007 2012-04-17 11:04 54
			inFile.get(); //Take the space
30412ca6 sago007 2016-03-25 19:35 55
			getline(inFile, value); //The rest of the line is the value.
c53e6443 sago007 2012-04-17 11:04 56
			statMap[key] = str2int(value);
c53e6443 sago007 2012-04-17 11:04 57
		}
c53e6443 sago007 2012-04-17 11:04 58
	}
89c4a3a6 sago007 2008-08-29 14:32 59
}
89c4a3a6 sago007 2008-08-29 14:32 60
ecef5838 sago007 2015-11-24 20:01 61
Stats* Stats::getInstance() {
ecef5838 sago007 2015-11-24 20:01 62
	if (Stats::instance==nullptr) {
c53e6443 sago007 2012-04-17 11:04 63
		Stats::instance = new Stats();
c53e6443 sago007 2012-04-17 11:04 64
c53e6443 sago007 2012-04-17 11:04 65
	}
c53e6443 sago007 2012-04-17 11:04 66
	return Stats::instance;
89c4a3a6 sago007 2008-08-29 14:32 67
}
89c4a3a6 sago007 2008-08-29 14:32 68
ecef5838 sago007 2015-11-24 20:01 69
void Stats::save() {
c3a11a57 sago007 2016-03-06 11:21 70
	std::stringstream outFile;
c3a11a57 sago007 2016-03-06 11:21 71
	map<string,unsigned int>::iterator iter;
c3a11a57 sago007 2016-03-06 11:21 72
	for (iter = statMap.begin(); iter != statMap.end(); iter++) {
c3a11a57 sago007 2016-03-06 11:21 73
		outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 74
	}
c3a11a57 sago007 2016-03-06 11:21 75
	sago::WriteFileContent(statsFileName, outFile.str());
89c4a3a6 sago007 2008-08-29 14:32 76
}
89c4a3a6 sago007 2008-08-29 14:32 77
ecef5838 sago007 2015-11-24 20:01 78
unsigned int Stats::getNumberOf(const string& statName) {
ecef5838 sago007 2015-11-24 20:01 79
	if (exists(statName)) {
c53e6443 sago007 2012-04-17 11:04 80
		return statMap[statName];
c53e6443 sago007 2012-04-17 11:04 81
	}
acd79baf sago007 2015-11-22 19:37 82
	else {
c53e6443 sago007 2012-04-17 11:04 83
		return 0;
acd79baf sago007 2015-11-22 19:37 84
	}
89c4a3a6 sago007 2008-08-29 14:32 85
}
89c4a3a6 sago007 2008-08-29 14:32 86
ecef5838 sago007 2015-11-24 20:01 87
void Stats::addOne(const string& statName) {
c53e6443 sago007 2012-04-17 11:04 88
	map<string,unsigned int>::iterator iter = statMap.find(statName);
ecef5838 sago007 2015-11-24 20:01 89
	if (iter == statMap.end()) {
c53e6443 sago007 2012-04-17 11:04 90
		statMap[statName] = 1;
c53e6443 sago007 2012-04-17 11:04 91
	}
ecef5838 sago007 2015-11-24 20:01 92
	else {
c53e6443 sago007 2012-04-17 11:04 93
		iter->second++;
c53e6443 sago007 2012-04-17 11:04 94
	}
89c4a3a6 sago007 2008-08-29 14:32 95
}
89c4a3a6 sago007 2008-08-29 14:32 96
ecef5838 sago007 2015-11-24 20:01 97
bool Stats::exists(const string& statName) {
c53e6443 sago007 2012-04-17 11:04 98
	//Using that 'find' returns an iterator to the end of the map if not found
c53e6443 sago007 2012-04-17 11:04 99
	return statMap.find(statName) != statMap.end();
89c4a3a6 sago007 2008-08-29 14:32 100
}
1970-01-01 00:00 101