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