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