summaryrefslogtreecommitdiff
path: root/utils/tfstats/plrpersist.cpp
blob: 5a3b86ef9680a2fd0e6e3ca5252d9bb8b71b9548 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
//
//------------------------------------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#pragma warning (disable:4786)
#include "PlrPersist.h"
#include "TextFile.h"

#include <map>
#include <string>
using namespace std;

//------------------------------------------------------------------------------------------------------
// Function:	CPlrPersist::generate
// Purpose:	fills in the fields of this with the data in the given CPlayer object
// Input:	cp - the player object to get data from
//------------------------------------------------------------------------------------------------------
void CPlrPersist::generate(CPlayer& cp)
{
	kills=deaths=timeon=0;
	valid=true;
	WONID=cp.WONID;
	matches=1;
	
	lastplayed=cp.logofftime;

	//do perteam stuff
	CTimeIndexedList<int>::iterator teamiter=cp.teams.begin();
	for (teamiter;teamiter!=cp.teams.end();++teamiter)
	{
		int tdt=teamiter->data;
		kills+=cp.perteam[teamiter->data].kills;
		deaths+=cp.perteam[teamiter->data].deaths;
		timeon+=cp.perteam[teamiter->data].timeon;

		map<string,int>::iterator it;
		it=cp.perteam[teamiter->data].weaponKills.begin();
		for (it;it!=cp.perteam[teamiter->data].weaponKills.end();++it)
		{
			string name=it->first;
			int kills=it->second;
			weapmap[name]+=kills;
		}
	}

	CTimeIndexedList<player_class>::iterator clsit=cp.allclassesplayed.begin();
	for (clsit;clsit!=cp.allclassesplayed.end();++clsit)
	{
		string classname=plrClassNames[clsit->data];
		classmap[classname]+=cp.allclassesplayed.howLong(clsit->data);
	}
	
	CTimeIndexedList<string>::iterator nameiter;
	for (nameiter=cp.aliases.begin();nameiter!=cp.aliases.end();++nameiter)
	{
		nickmap[nameiter->data]+=cp.aliases.howLong(nameiter->data);
	}

	pair<time_t,time_t> startstop;
	startstop.first=cp.logontime;
	startstop.second=cp.logofftime;

	playtimes.push_back(startstop);
}

//------------------------------------------------------------------------------------------------------
// Function:	CPlrPersist::merge
// Purpose:	 merges the stats of another CPlrPersist object into this one.
//	This is the key operation of this class.  This is how player stats are kept up 
//	to date over time.  If the two data files have playtimes that overlap, they 
//	are not merged (unless the mergeOverlaps flag is true)
// Input:	other - the CPlrPersist object that we want to merge into this one
//				mergeOverlaps - if true, overlapping playtimes are ignored. 
//------------------------------------------------------------------------------------------------------
void CPlrPersist::merge(CPlrPersist& other,bool mergeOverlaps)
{
	if (!other.valid)
		return;	//don't modify
	if (WONID!=other.WONID)
	{
		g_pApp->warning("merging stats for two different WONIDs (%lu, %lu)",WONID,other.WONID);
		
	}
	else
	{
		//do playtimes first to see if overlaps occur
		list<pair<time_t,time_t> >::iterator itOther=other.playtimes.begin();
		for (itOther;itOther!=other.playtimes.end();++itOther)
		{
			list<pair<time_t,time_t> >::iterator overlap=timesOverlap(itOther->first,itOther->second);
			time_t overlapSecond=overlap->second;
			time_t overlapFirst=overlap->first;
			if (mergeOverlaps || overlap==playtimes.end())
				playtimes.push_back(*itOther);
			else
			{
				g_pApp->warning("not merging stats for WON ID# %lu, playtime ranges overlap\n\t((%lu-%lu) overlaps with (%lu-%lu))",WONID,itOther->first,itOther->second,overlap->first,overlap->second);
				return;
			}
		}
	}


	matches+=other.matches;
	kills+=other.kills;
	deaths+=other.deaths;
	timeon+=other.timeon;
	if (other.lastplayed > lastplayed)
		lastplayed=other.lastplayed;

	//do names
	map<string,int>::iterator it;
	it=other.nickmap.begin();
	for (it;it!=other.nickmap.end();++it)
	{
		string name=it->first;
		int time=it->second;
		nickmap[name]+=time;
	}
	
	//do weapons
	it=other.weapmap.begin();
	for (it;it!=other.weapmap.end();++it)
	{
		string name=it->first;
		int kills=it->second;
		weapmap[name]+=kills;
	}
	
	//do classes
	it=other.classmap.begin();
	for (it;it!=other.classmap.end();++it)
	{
		string name=it->first;
		int time=it->second;
		classmap[name]+=time;
	}

}

//------------------------------------------------------------------------------------------------------
// Function:	CPlrPersist::read
// Purpose:	 fills in the fields of this by reading data out of a file
// Input:	f - the file from which to read the data
//------------------------------------------------------------------------------------------------------
void CPlrPersist::read(CTextFile& f)
{
	if (!f.isValid())
	{
		kills=deaths=timeon=0; WONID=-1;
		valid=false;
		return;
	}

	if(WONID==-1)
	{
		//parse it out of f;
		string s=f.fileName();
		char buf[100];
		int startpos=s.find_last_of(g_pApp->os->pathSeperator());
		int endpos=s.find_last_of(".");
		if (endpos == -1)
			return;
		if (startpos==-1)
			startpos=0;
		
		s.copy(buf,(endpos-startpos),startpos);
		buf[endpos-startpos]=0;
		WONID=strtoul(buf,NULL,10);
		if (!WONID)
		{
			WONID=-1;
			valid=false;
			return;
		}
			

	}
	
	

	valid=false;
	
	if (!f.eof()) kills=f.readInt();	else return;
	if (!f.eof()) deaths=f.readInt(); else return;
	if (!f.eof()) timeon=f.readULong(); else return;
	if (!f.eof()) matches=f.readInt(); else return;
	if (!f.eof()) lastplayed=f.readULong(); else return;

	string next;

	if (!f.eof())
	{
		f.discard("names");
		next= f.peekNextString();
		while ( next!="endnames")
		{
			string name=f.readString();
			int timeon=f.readInt();
			nickmap[name]=timeon;
			next=f.peekNextString();
		}
		f.discard("endnames");
	} else return;
	
	if (!f.eof())
	{
		f.discard("weapons");
		next= f.peekNextString();
		while (next!="endweapons")
		{
			string name=f.readString();
			int kills=f.readInt();
			weapmap[name]=kills;
			next=f.peekNextString();
		}
		f.discard("endweapons");
	} else return;

	if (!f.eof())
	{
		f.discard("classes");
		next= f.peekNextString();
		while (next!="endclasses")
		{
			string name=f.readString();
			int timeused=f.readInt();
			classmap[name]=timeused;
			next=f.peekNextString();
		}
		f.discard("endclasses");
	} else return;

	if (!f.eof())
	{
		f.discard("playtimes");
		next= f.peekNextString();
		while (next!="endplaytimes")
		{
			pair<time_t,time_t> startstop;
			startstop.first=f.readULong();
			startstop.second=f.readULong();
			playtimes.push_back(startstop);
			next=f.peekNextString();
		}
		f.discard("endplaytimes");
	} else return;

	valid=true;
	
}

//------------------------------------------------------------------------------------------------------
// Function:	CPlrPersist::read
// Purpose:	 converts the WONID to a file name (<wonid>.tfs) and passes execution
//	off to the above read function.
// Input:	WONID - the WONID of the player whose datafile we want to read
//------------------------------------------------------------------------------------------------------
void CPlrPersist::read(unsigned long WONID)
{
	
	string file=g_pApp->playerDirectory;
	char buf[100];
	file+=g_pApp->os->ultoa(WONID,buf,10);
	file+=".tfs";

	this->WONID=WONID;

	CTextFile f(file.c_str());
	read(f);
}



void CPlrPersist::write()
{
	string file=g_pApp->playerDirectory;
	char buf[100];
	file+=g_pApp->os->ultoa(WONID,buf,10);
	file+=".tfs";

	FILE* fout=fopen(file.c_str(),"wt");

	fprintf(fout,"%li //kills\n",kills);
	fprintf(fout,"%li //deaths\n",deaths);
	fprintf(fout,"%lu //timeon\n",timeon);
	fprintf(fout,"%li //matches played\n",matches);
	fprintf(fout,"%lu //last played\n",lastplayed);
	
	map<string,int>::iterator it;
	
	fprintf(fout,"names\n");
	it=nickmap.begin();
	for (it;it!=nickmap.end();++it)
	{
		string name=it->first;
		int time=it->second;
		fprintf(fout,"\t\"%s\" %li //has used the name \"%s\" for %02li:%02li:%02li\n",name.c_str(),time,name.c_str(),Util::time_t2hours(time),Util::time_t2mins(time),Util::time_t2secs(time));
	}
	fprintf(fout,"endnames\n");

	fprintf(fout,"weapons\n");
	it=weapmap.begin();
	for (it;it!=weapmap.end();++it)
	{
		string name=it->first;
		int kills=it->second;
		fprintf(fout,"\t\"%s\" %li //has killed %li people with \"%s\"\n",name.c_str(),kills,kills,name.c_str());
	}
	fprintf(fout,"endweapons\n");

	fprintf(fout,"classes\n");
	it=classmap.begin();
	for (it;it!=classmap.end();++it)
	{
		string name=it->first;
		int time=it->second;
		fprintf(fout,"\t\"%s\" %li //has played as a \"%s\" for %02li:%02li:%02li\n",name.c_str(),time,name.c_str(),Util::time_t2hours(time),Util::time_t2mins(time),Util::time_t2secs(time));
	}
	fprintf(fout,"endclasses\n");

	fprintf(fout,"playtimes\n");
	list<pair<time_t,time_t> >::iterator it2=playtimes.begin();
	for (it2;it2!=playtimes.end();++it2)
	{
		char buf[500];
		time_t t1=it2->first;
		time_t t2=it2->second;
		bool doesOverlap;
		
		list<pair<time_t,time_t> >::iterator overlap=timesOverlap(it2->first,it2->second,false);
		doesOverlap= overlap!=playtimes.end();
			

		fprintf(fout,"\t%lu %lu //played from %s.",it2->first,it2->second,Util::makeDurationString(it2->first,it2->second,buf," to "));
		if (doesOverlap)
			fprintf(fout,"Warning! overlaps with time range (%lu-%lu)",overlap->first,overlap->second);
		fprintf(fout,"\n");

	}
	fprintf(fout,"endplaytimes\n");


	fclose(fout);

}


list<pair<time_t,time_t> >::iterator CPlrPersist::timesOverlap(time_t start, time_t end,bool testself)
{
	list<pair<time_t,time_t> >::iterator it;
	it=playtimes.begin();
	for (it;it!=playtimes.end();++it)
	{
		time_t itFirst=it->first;
		time_t itSecond=it->second;
		if (start == it->first && end == it->second)
		{
			if (testself)
				break;
		}
		//if start is in current range
		else if (start >= it->first  && start <= it->second)
			break;

		//if end is in current range
		else if (end >= it->first  && end <= it->second)
			break;

		//if the start is before this range and end is after
		else if (start <= it->first && end >= it->second)
			break;
	}
	return it;
}

string CPlrPersist::faveString(map<string,int>& theMap)
{
	string retstr;
	time_t max=0;
	map<string,int>::iterator it=theMap.begin();
	for (it;it!=theMap.end();++it)
	{
		if (it->second > max)
		{
			max=it->second;
			retstr=it->first;
		}
	}
	return retstr;
}

string CPlrPersist::faveName()
{
	return faveString(nickmap);
}
string CPlrPersist::faveWeap()
{
	string s=faveString(weapmap);
	faveweapkills=weapmap[s];
	return s;
}
string CPlrPersist::faveClass()
{
	return faveString(classmap);
}

double CPlrPersist::rank()
{
	return ((double)((double)kills - (double)deaths) * 1000.0) / (double)timeon;
}