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