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
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 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
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.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"
89c4a3a6 sago007 2008-08-29 14:32 26
53001fa2 sago007 2015-11-08 15:13 27
using namespace std;
53001fa2 sago007 2015-11-08 15:13 28
acd79baf sago007 2015-11-22 19:37 29
Stats* Stats::instance = nullptr;
89c4a3a6 sago007 2008-08-29 14:32 30
ecef5838 sago007 2015-11-24 20:01 31
Stats::Stats() {
c53e6443 sago007 2012-04-17 11:04 32
	statMap.clear();
c53e6443 sago007 2012-04-17 11:04 33
	load();
89c4a3a6 sago007 2008-08-29 14:32 34
}
89c4a3a6 sago007 2008-08-29 14:32 35
ecef5838 sago007 2015-11-24 20:01 36
void Stats::load() {
c53e6443 sago007 2012-04-17 11:04 37
	string filename = getPathToSaveFiles()+"/statsFile";
c53e6443 sago007 2012-04-17 11:04 38
	ifstream inFile(filename.c_str());
c53e6443 sago007 2012-04-17 11:04 39
	string key;
c53e6443 sago007 2012-04-17 11:04 40
	char value[MAX_VAR_LENGTH];
ecef5838 sago007 2015-11-24 20:01 41
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 42
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 43
			inFile >> key; // The key is first on line
c53e6443 sago007 2012-04-17 11:04 44
			inFile.get(); //Take the space
c53e6443 sago007 2012-04-17 11:04 45
			inFile.getline(value,MAX_VAR_LENGTH); //The rest of the line is the value.
c53e6443 sago007 2012-04-17 11:04 46
			statMap[key] = str2int(value);
c53e6443 sago007 2012-04-17 11:04 47
		}
c53e6443 sago007 2012-04-17 11:04 48
		inFile.close();
c53e6443 sago007 2012-04-17 11:04 49
	}
89c4a3a6 sago007 2008-08-29 14:32 50
}
89c4a3a6 sago007 2008-08-29 14:32 51
ecef5838 sago007 2015-11-24 20:01 52
Stats* Stats::getInstance() {
ecef5838 sago007 2015-11-24 20:01 53
	if (Stats::instance==nullptr) {
c53e6443 sago007 2012-04-17 11:04 54
		Stats::instance = new Stats();
c53e6443 sago007 2012-04-17 11:04 55
c53e6443 sago007 2012-04-17 11:04 56
	}
c53e6443 sago007 2012-04-17 11:04 57
	return Stats::instance;
89c4a3a6 sago007 2008-08-29 14:32 58
}
89c4a3a6 sago007 2008-08-29 14:32 59
ecef5838 sago007 2015-11-24 20:01 60
void Stats::save() {
c53e6443 sago007 2012-04-17 11:04 61
	string filename = getPathToSaveFiles()+"/statsFile";
c53e6443 sago007 2012-04-17 11:04 62
	ofstream outFile(filename.c_str(),ios::trunc);
c53e6443 sago007 2012-04-17 11:04 63
ecef5838 sago007 2015-11-24 20:01 64
	if (outFile) {
c53e6443 sago007 2012-04-17 11:04 65
		//outFile << statMap.size() << endl;
c53e6443 sago007 2012-04-17 11:04 66
		map<string,unsigned int>::iterator iter;
ecef5838 sago007 2015-11-24 20:01 67
		for (iter = statMap.begin(); iter != statMap.end(); iter++) {
c53e6443 sago007 2012-04-17 11:04 68
			outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 69
		}
c53e6443 sago007 2012-04-17 11:04 70
	}
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