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