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
89c4a3a6 sago007 2008-08-29 14:32 31
Stats::Stats()
89c4a3a6 sago007 2008-08-29 14:32 32
{
c53e6443 sago007 2012-04-17 11:04 33
	statMap.clear();
c53e6443 sago007 2012-04-17 11:04 34
	load();
89c4a3a6 sago007 2008-08-29 14:32 35
}
89c4a3a6 sago007 2008-08-29 14:32 36
89c4a3a6 sago007 2008-08-29 14:32 37
void Stats::load()
89c4a3a6 sago007 2008-08-29 14:32 38
{
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];
c53e6443 sago007 2012-04-17 11:04 43
	if(inFile)
c53e6443 sago007 2012-04-17 11:04 44
	{
c53e6443 sago007 2012-04-17 11:04 45
		while(!inFile.eof())
c53e6443 sago007 2012-04-17 11:04 46
		{
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
c53e6443 sago007 2012-04-17 11:04 49
			inFile.getline(value,MAX_VAR_LENGTH); //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
		inFile.close();
c53e6443 sago007 2012-04-17 11:04 53
	}
89c4a3a6 sago007 2008-08-29 14:32 54
}
89c4a3a6 sago007 2008-08-29 14:32 55
89c4a3a6 sago007 2008-08-29 14:32 56
Stats* Stats::getInstance()
89c4a3a6 sago007 2008-08-29 14:32 57
{
acd79baf sago007 2015-11-22 19:37 58
	if(Stats::instance==nullptr)
c53e6443 sago007 2012-04-17 11:04 59
	{
c53e6443 sago007 2012-04-17 11:04 60
		Stats::instance = new Stats();
c53e6443 sago007 2012-04-17 11:04 61
c53e6443 sago007 2012-04-17 11:04 62
	}
c53e6443 sago007 2012-04-17 11:04 63
	return Stats::instance;
89c4a3a6 sago007 2008-08-29 14:32 64
}
89c4a3a6 sago007 2008-08-29 14:32 65
89c4a3a6 sago007 2008-08-29 14:32 66
void Stats::save()
89c4a3a6 sago007 2008-08-29 14:32 67
{
c53e6443 sago007 2012-04-17 11:04 68
	string filename = getPathToSaveFiles()+"/statsFile";
c53e6443 sago007 2012-04-17 11:04 69
	ofstream outFile(filename.c_str(),ios::trunc);
c53e6443 sago007 2012-04-17 11:04 70
c53e6443 sago007 2012-04-17 11:04 71
	if(outFile)
c53e6443 sago007 2012-04-17 11:04 72
	{
c53e6443 sago007 2012-04-17 11:04 73
		//outFile << statMap.size() << endl;
c53e6443 sago007 2012-04-17 11:04 74
		map<string,unsigned int>::iterator iter;
c53e6443 sago007 2012-04-17 11:04 75
		for(iter = statMap.begin(); iter != statMap.end(); iter++)
c53e6443 sago007 2012-04-17 11:04 76
		{
c53e6443 sago007 2012-04-17 11:04 77
			outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 78
		}
c53e6443 sago007 2012-04-17 11:04 79
	}
89c4a3a6 sago007 2008-08-29 14:32 80
}
89c4a3a6 sago007 2008-08-29 14:32 81
af35c39d sago007 2013-11-16 16:37 82
unsigned int Stats::getNumberOf(const string &statName)
89c4a3a6 sago007 2008-08-29 14:32 83
{
c53e6443 sago007 2012-04-17 11:04 84
	if(exists(statName))
c53e6443 sago007 2012-04-17 11:04 85
	{
c53e6443 sago007 2012-04-17 11:04 86
		return statMap[statName];
c53e6443 sago007 2012-04-17 11:04 87
	}
acd79baf sago007 2015-11-22 19:37 88
	else {
c53e6443 sago007 2012-04-17 11:04 89
		return 0;
acd79baf sago007 2015-11-22 19:37 90
	}
89c4a3a6 sago007 2008-08-29 14:32 91
}
89c4a3a6 sago007 2008-08-29 14:32 92
af35c39d sago007 2013-11-16 16:37 93
void Stats::addOne(const string &statName)
89c4a3a6 sago007 2008-08-29 14:32 94
{
c53e6443 sago007 2012-04-17 11:04 95
	map<string,unsigned int>::iterator iter = statMap.find(statName);
c53e6443 sago007 2012-04-17 11:04 96
	if(iter == statMap.end())
c53e6443 sago007 2012-04-17 11:04 97
	{
c53e6443 sago007 2012-04-17 11:04 98
		statMap[statName] = 1;
c53e6443 sago007 2012-04-17 11:04 99
	}
c53e6443 sago007 2012-04-17 11:04 100
	else
c53e6443 sago007 2012-04-17 11:04 101
	{
c53e6443 sago007 2012-04-17 11:04 102
		iter->second++;
c53e6443 sago007 2012-04-17 11:04 103
	}
89c4a3a6 sago007 2008-08-29 14:32 104
}
89c4a3a6 sago007 2008-08-29 14:32 105
af35c39d sago007 2013-11-16 16:37 106
bool Stats::exists(const string &statName)
89c4a3a6 sago007 2008-08-29 14:32 107
{
c53e6443 sago007 2012-04-17 11:04 108
	//Using that 'find' returns an iterator to the end of the map if not found
c53e6443 sago007 2012-04-17 11:04 109
	return statMap.find(statName) != statMap.end();
89c4a3a6 sago007 2008-08-29 14:32 110
}
1970-01-01 00:00 111