summaryrefslogtreecommitdiff
path: root/utils/tfstats/tfstatsapplication.cpp
blob: f7f441e08d2b2cdbefee0cf1f32902dcd1f3e783 (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#pragma warning (disable:4786)
//=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose:  Implementation of the TFStatsApplication class. 
//
// $Workfile:     $
// $Date:         $
//
//------------------------------------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "TFStatsApplication.h"
#include "TFStatsReport.h"
#include "LogEvent.h"
#include "ScoreBoard.h"
#include "WhoKilledWho.h"
#include "memdbg.h"
#include "awards.h"
#include "MatchResults.h"
#include "DialogueReadout.h"
#include "cvars.h"
#include "html.h"
#include "TextFile.h"
#include "CustomAward.h"
#include "PlayerSpecifics.h"
#include "util.h"
#include "AllPlayersStats.h"
#include <errno.h>



CTFStatsApplication* g_pApp; //global!

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::printUsage
// Purpose:	 Prints out how to use the program
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::printUsage()
{
	
	printf("TFStats version %li.%li\n",majorVer,minorVer);
	printf("\nUSAGE:\n");
#ifdef WIN32
	printf("TFstatsRT.exe <log file name> <optional switches>\n");
#else
	printf("TFstats_l <log file name> <optional switches>\n");
#endif
	printf("the available switches are detailed in the documentation. (TFStats.htm): \n");
	printf("---\n");
	printf("After TFStats is done, several new HTML files will be in the current directory,\n");
	printf("point your browser to the index.html in this directory and it will do the rest.\n");
#ifdef WIN32
	printf("(you should just be able to do \"start index.html\")\n");
#endif
	printf("TFStats uses Regex++:\nRegex++ � 1998-9 Dr John Maddock.\n");
	
}

#include "CustomAwardList.h"

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::DoAwards
// Purpose:	 This reports all of the awards
// Input:	MatchResultsPage - the page on which to write the awards' HTML
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::DoAwards(CHTMLFile& MatchResultsPage)
{
	int chresult=os->chdir(g_pApp->outputDirectory.c_str());

	MatchResultsPage.p();
	MatchResultsPage.write("<img src=\"%s/awards.gif\">\n",g_pApp->supportHTTPPath.c_str());
	MatchResultsPage.div("awards");
	
	//do custom awards first, they're usually the important ones for the match
	CCustomAwardList* pCust;
	os->chdir(ruleDirectory.c_str());
	pCust=CCustomAwardList::readCustomAwards(g_pMatchInfo->mapName());
	os->chdir(outputDirectory.c_str());
	if(pCust)
	{
		
		CCustomAwardIterator it;
		for (it=pCust->begin();it!=pCust->end();++it)
		{
			(*it)->report(MatchResultsPage);
		}
	
		delete pCust;
		MatchResultsPage.hr(450,true);
	}
	os->chdir(outputDirectory.c_str());
	
	//do scout award here.
	CSurvivalistAward csrva; csrva.report(MatchResultsPage);

	//sniper award
	CSharpshooterAward cssa; cssa.report(MatchResultsPage);
	
	//soldier award
	CRocketryAward cra; cra.report(MatchResultsPage);
	
	//demoman awards
	CGrenadierAward cga; cga.report(MatchResultsPage);
	CDemolitionsAward cda; cda.report(MatchResultsPage);
	
	//medic awards
	CCureAward cca;cca.report(MatchResultsPage);
	CBiologicalWarfareAward cbwa; cbwa.report(MatchResultsPage);
	
	//HW award
	CAssaultCannonAward caca;caca.report(MatchResultsPage);
	
	//pyro award
	CFlamethrowerAward cfa;cfa.report(MatchResultsPage);
	
	//spy award
	CKnifeAward cka;cka.report(MatchResultsPage);

	//engineer awards
	CBestSentryAward cbsa; cbsa.report(MatchResultsPage); 
	CSentryRebuildAward cba2;cba2.report(MatchResultsPage);
	
	
	MatchResultsPage.hr(450,true);
	
	//non class specific
	CKamikazeAward ckami;ckami.report(MatchResultsPage);
	CTalkativeAward cta;cta.report(MatchResultsPage);
	CTeamKillAward ctka; ctka.report(MatchResultsPage); 
	

	

	MatchResultsPage.enddiv();
}

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::DoMatchResults
// Purpose:	This creates the MatchResults page of the report (the main page)
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::DoMatchResults()
{
	int chresult=os->chdir(g_pApp->outputDirectory.c_str());
	CHTMLFile MatchResultsPage("MatchResults.html","results");
	CMatchResults cmr;
	cmr.report(MatchResultsPage);
	DoAwards(MatchResultsPage);
	
	CScoreboard cs;
	cs.report(MatchResultsPage);

	
	CWhoKilledWho cwkw; cwkw.report(MatchResultsPage);

}	

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::parseCmdLineArg
// Purpose:	This parses variables and values out of passed in commandline args
// Input:	in - the full command line argument
//				var - the output buffer in which to place the name of the variable
//				val - the output buffer in which to place the value of the variable
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::parseCmdLineArg(const char* in, char* var, char* val)
{
	char* pEq=strchr(in,'=');
	if (!pEq || pEq==in)
	{
		var[0]=val[0]=0;
	}
	else
	{	
		*pEq=0;
		strcpy(var,in);
		strcpy(val,pEq+1);
		Util::str2lowercase(var,var);
		Util::str2lowercase(val,val);
		*pEq='=';
	}

}

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::ParseCommandLine
// Purpose:	 this parses the commandline into the cmdLineSwitches map. Also 
//	recognizes certain switches and sets variables and creates directories 
//	accordingly to their values
// Input:	argc - count of arguments from commandline
//				argv[] - the arguments
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::ParseCommandLine(int argc,const char* argv[])
{
	char var[100];
	char val[100];
	
	//first find if we want to display startup info
	bool displayStartupInfo=false;
	for(int i=2;i<argc;i++)
	{
		parseCmdLineArg(argv[i],var,val);
		if (!var[0])
			fatalError("Malformed switch,  required format is <variable>=<value> with no spaces");
		
		cmdLineSwitches[var]=val;
		if (stricmp(var,"displaystartupinfo")==0)
			if (stricmp(val,"yes")==0)
			{
				displayStartupInfo=true;
				break;
			}
	}
	


	if (displayStartupInfo)
	{
		printf("%21s %-21s\n","Command line","Switches");
		printf("-------------------------------------------\n");
	}
	for(i=2;i<argc;i++)
	{
		parseCmdLineArg(argv[i],var,val);
		if (!var[0])
			fatalError("Malformed switch,  required format is <variable>=<value> with no spaces");
	
		if (displayStartupInfo)
			printf("%20s = %-20s\n",var,val);
		cmdLineSwitches[var]=val;
	}


	
	outputDirectory=os->removeDirSlash(cmdLineSwitches["outputdir"]);
	makeAndSaveDirectory(outputDirectory);
	
	ruleDirectory=os->removeDirSlash(cmdLineSwitches["ruledir"]);
	makeAndSaveDirectory(ruleDirectory);
	
	if (cmdLineSwitches["eliminateoldplayers"]=="yes")
	{
		eliminateOldPlayers=true;
		elimDays=atoi(cmdLineSwitches["oldplayercutoff"].c_str());
	}
	else
	{
		eliminateOldPlayers=false;
		elimDays=0;
	}

	
	if (cmdLineSwitches["usesupportdir"]=="yes")
	{
		supportDirectory=os->removeDirSlash(cmdLineSwitches["supportdir"]);
		supportHTTPPath=os->removeDirSlash(cmdLineSwitches["supporthttppath"]);
	}
	else
	{
		supportDirectory=g_pApp->outputDirectory+g_pApp->os->pathSeperator();
		supportDirectory+="support";
		supportHTTPPath="support";
	}
	makeAndSaveDirectory(supportDirectory);
	
	
	if (cmdLineSwitches["persistplayerstats"]=="yes")
	{
		playerDirectory=os->removeDirSlash(cmdLineSwitches["playerdir"]);
		playerHTTPPath=os->removeDirSlash(cmdLineSwitches["playerhttppath"]);
	}
	else
	{
		playerDirectory=g_pApp->outputDirectory+g_pApp->os->pathSeperator();
		playerDirectory+="players";
		playerHTTPPath="players";
	}
	makeAndSaveDirectory(playerDirectory);
	makeDirectory(playerDirectory+"support");
}


//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::main
// Purpose:	 The REAL main of the program
// Input:	argc - count of arguments from commandline
//				argv[] - the arguments
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::main(int argc,const char* argv[])
{
	if (argc<=1){printUsage();return;}
	g_pApp=this;
	
	//TODO: move this into OS interface
	TFStats_setNewHandler();
	
	inputDirectory=".";
	makeAndSaveDirectory(inputDirectory);
	
	//this call also sets up various directories to be used
	ParseCommandLine(argc,argv);		
	
	Util::initFriendlyWeaponNameTable();

	//LogFile is read in here.
	//MUST do this before we chdir to the output directory
	os->chdir(inputDirectory.c_str());
	CEventList* plogfile=NULL;
	plogfile=CEventList::readEventList(argv[1]);
	if (!plogfile)
		fatalError("No valid data found in logfile %s\n",argv[1]);
		
	
	os->chdir(outputDirectory.c_str());
	//make match information object
	g_pMatchInfo= new CMatchInfo(plogfile);
	
	CTFStatsReport matchreport;
	
	
	
	matchreport.genImages();
	
	
	matchreport.genJavaScript();
	matchreport.genStyleSheet();
	
	if (cmdLineSwitches["persistplayerstats"]=="yes")
		matchreport.genAllPlayersStyleSheet();

	matchreport.genIndex();
	
	os->chdir(outputDirectory.c_str());
	matchreport.genTopFrame();
	matchreport.genNavFrame();
	
	
	DoMatchResults();

	
	CCVarList ccvl;
	ccvl.makeHTMLPage("cvars.html","Server Settings");
	
	CDialogueReadout cdr;
	cdr.makeHTMLPage("dialogue.html","Dialogue");

	CPlayerSpecifics cps;
	cps.makeHTMLPage("players.html","Players");

	
	if (cmdLineSwitches["persistplayerstats"]=="yes")
	{
		if(!g_pMatchInfo->isLanGame())
		{
			g_pApp->os->chdir(g_pApp->playerDirectory.c_str());
			CAllPlayersStats caps;
			caps.makeHTMLPage("allplayers.html","All Players");
		}
		else
		{
			g_pApp->warning("Lan Game detected, player stats will not be saved");
		}
	}
	printf("TFStats completed successfully\n\n\n");
}

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::makeAndSaveDirectory
// Purpose:	 Creates a directory and writes the full directory path back into the
//	passed in string.  In effect, this turns a potentially relative path into an
//	absolute path, and ensures the directory exists.
// Input:	dir - the string that contains the pathname, and upon return will contain
//	the absolute pathname
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::makeAndSaveDirectory(string& dir)
{
	char startpath[500];
	char fullpath[500];
	os->getcwd(startpath,500);
	if (!os->makeHier(dir))
	{
		fatalError("Failed to make directory \"%s\", aborting. (Reason: %s)",dir.c_str(),strerror(errno));
	}
	os->chdir(dir.c_str());
	os->getcwd(fullpath,500);
	dir=fullpath;
	os->addDirSlash(dir);
	os->chdir(startpath);
}


//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::makeDirectory
// Purpose:	 Creates a directory.
// Input:	dir - the directory to create
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::makeDirectory(string& dir)
{
	char startpath[500];
	os->getcwd(startpath,500);
	
	if (!os->makeHier(dir))
	{
		fatalError("Failed to make directory \"%s\". (Reason: %s)",dir.c_str(),strerror(errno));
	}
	os->chdir(startpath);
}


//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::fatalError
// Purpose:	prints an error message and exits
// Input:	fmt - the format string
//				... - optional arguments
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::fatalError(char* fmt,...)
{
	va_list v;
	va_start(v,fmt);
	fprintf(stderr,"Fatal Error: ");
	vfprintf(stderr,fmt,v);
	fprintf(stderr,"\n***Aborting. \n");
	exit(-1);
}

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::warning
// Purpose:	prints a warning message and returns. (program doesn't exit)
// Input:	fmt - format string
//				... - optional arguments
//------------------------------------------------------------------------------------------------------
void CTFStatsApplication::warning(char* fmt,...)
{
	va_list v;
	va_start(v,fmt);
	fprintf(stderr,"Warning: ");	
	vfprintf(stderr,fmt,v);
	fprintf(stderr,"\n");
}

//------------------------------------------------------------------------------------------------------
// Function:	CTFStatsApplication::getCutoffSeconds
// Purpose:	Turns the number of cutoff days into seconds. Cutoff days are how
//	many days players can be absent from the server and still have their stats
// reported in the all-player stats
// Output:	time_t the number of cutoff seconds
//------------------------------------------------------------------------------------------------------
time_t CTFStatsApplication::getCutoffSeconds()	
{
	return 60*60*24*elimDays;
}