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