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
53001fa2 sago007 2015-11-08 15:13 30
using namespace std;
53001fa2 sago007 2015-11-08 15:13 31
acd79baf sago007 2015-11-22 19:37 32
Stats* Stats::instance = nullptr;
89c4a3a6 sago007 2008-08-29 14:32 33
c7f2082f sago007 2016-03-13 11:48 34
const char* const statsFileName = "statsFile";
6c28b502 sago007 2016-02-13 11:38 35
ecef5838 sago007 2015-11-24 20:01 36
Stats::Stats() {
c53e6443 sago007 2012-04-17 11:04 37
	statMap.clear();
c53e6443 sago007 2012-04-17 11:04 38
	load();
89c4a3a6 sago007 2008-08-29 14:32 39
}
89c4a3a6 sago007 2008-08-29 14:32 40
ecef5838 sago007 2015-11-24 20:01 41
void Stats::load() {
30412ca6 sago007 2016-03-25 19:35 42
	string fileContent = sago::GetFileContent(statsFileName);
30412ca6 sago007 2016-03-25 19:35 43
	stringstream inFile(fileContent);
c53e6443 sago007 2012-04-17 11:04 44
	string key;
30412ca6 sago007 2016-03-25 19:35 45
	string value;
ecef5838 sago007 2015-11-24 20:01 46
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 47
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 48
			inFile >> key; // The key is first on line
c53e6443 sago007 2012-04-17 11:04 49
			inFile.get(); //Take the space
30412ca6 sago007 2016-03-25 19:35 50
			getline(inFile, value); //The rest of the line is the value.
c53e6443 sago007 2012-04-17 11:04 51
			statMap[key] = str2int(value);
c53e6443 sago007 2012-04-17 11:04 52
		}
c53e6443 sago007 2012-04-17 11:04 53
	}
89c4a3a6 sago007 2008-08-29 14:32 54
}
89c4a3a6 sago007 2008-08-29 14:32 55
ecef5838 sago007 2015-11-24 20:01 56
Stats* Stats::getInstance() {
ecef5838 sago007 2015-11-24 20:01 57
	if (Stats::instance==nullptr) {
c53e6443 sago007 2012-04-17 11:04 58
		Stats::instance = new Stats();
c53e6443 sago007 2012-04-17 11:04 59
c53e6443 sago007 2012-04-17 11:04 60
	}
c53e6443 sago007 2012-04-17 11:04 61
	return Stats::instance;
89c4a3a6 sago007 2008-08-29 14:32 62
}
89c4a3a6 sago007 2008-08-29 14:32 63
ecef5838 sago007 2015-11-24 20:01 64
void Stats::save() {
c3a11a57 sago007 2016-03-06 11:21 65
	std::stringstream outFile;
c3a11a57 sago007 2016-03-06 11:21 66
	map<string,unsigned int>::iterator iter;
c3a11a57 sago007 2016-03-06 11:21 67
	for (iter = statMap.begin(); iter != statMap.end(); iter++) {
c3a11a57 sago007 2016-03-06 11:21 68
		outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 69
	}
c3a11a57 sago007 2016-03-06 11:21 70
	sago::WriteFileContent(statsFileName, outFile.str());
89c4a3a6 sago007 2008-08-29 14:32 71
}
89c4a3a6 sago007 2008-08-29 14:32 72
ecef5838 sago007 2015-11-24 20:01 73
unsigned int Stats::getNumberOf(const string& statName) {
ecef5838 sago007 2015-11-24 20:01 74
	if (exists(statName)) {
c53e6443 sago007 2012-04-17 11:04 75
		return statMap[statName];
c53e6443 sago007 2012-04-17 11:04 76
	}
acd79baf sago007 2015-11-22 19:37 77
	else {
c53e6443 sago007 2012-04-17 11:04 78
		return 0;
acd79baf sago007 2015-11-22 19:37 79
	}
89c4a3a6 sago007 2008-08-29 14:32 80
}
89c4a3a6 sago007 2008-08-29 14:32 81
ecef5838 sago007 2015-11-24 20:01 82
void Stats::addOne(const string& statName) {
c53e6443 sago007 2012-04-17 11:04 83
	map<string,unsigned int>::iterator iter = statMap.find(statName);
ecef5838 sago007 2015-11-24 20:01 84
	if (iter == statMap.end()) {
c53e6443 sago007 2012-04-17 11:04 85
		statMap[statName] = 1;
c53e6443 sago007 2012-04-17 11:04 86
	}
ecef5838 sago007 2015-11-24 20:01 87
	else {
c53e6443 sago007 2012-04-17 11:04 88
		iter->second++;
c53e6443 sago007 2012-04-17 11:04 89
	}
89c4a3a6 sago007 2008-08-29 14:32 90
}
89c4a3a6 sago007 2008-08-29 14:32 91
ecef5838 sago007 2015-11-24 20:01 92
bool Stats::exists(const string& statName) {
c53e6443 sago007 2012-04-17 11:04 93
	//Using that 'find' returns an iterator to the end of the map if not found
c53e6443 sago007 2012-04-17 11:04 94
	return statMap.find(statName) != statMap.end();
89c4a3a6 sago007 2008-08-29 14:32 95
}
1970-01-01 00:00 96