summaryrefslogtreecommitdiff
path: root/src/master/stats.c
blob: b4ea5f8cf80aa4ecaeebfd24773ad8551b2c3794 (plain)
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
/*
stats.c

Statistics for tremmaster

Copyright (C) 2009 Darklegion Development

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef _WIN32

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <tdb.h>

#include "common.h"

#define MAX_DATA_SIZE 1024
#define CS_FILENAME   "clientStats.tdb"

/*
====================
RecordClientStat
====================
*/
void RecordClientStat( const char *address, const char *version, const char *renderer )
{
  TDB_CONTEXT *tctx = NULL;
  TDB_DATA    key, data;
  char        ipText[ 22 ];
  char        dataText[ MAX_DATA_SIZE ] = { 0 };
  char        *p;
  int         i;

  tctx = tdb_open( CS_FILENAME, 0, 0, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR );

  if( !tctx )
  {
		MsgPrint( MSG_DEBUG, "Couldn't open %s\n", CS_FILENAME );
    return;
  }

  strncpy( ipText, address, 22 );
  if( ( p = strrchr( ipText, ':' ) ) ) // Remove port
    *p = '\0';

  key.dptr = ipText;
  key.dsize = strlen( ipText );

  strncat( dataText, "\"", MAX_DATA_SIZE );
  strncat( dataText, version, MAX_DATA_SIZE );

  // Remove last three tokens (the date)
  for( i = 0; i < 3; i++ )
  {
    if( ( p = strrchr( dataText, ' ' ) ) )
      *p = '\0';
  }
  strncat( dataText, "\"", MAX_DATA_SIZE );

  strncat( dataText, " \"", MAX_DATA_SIZE );
  strncat( dataText, renderer, MAX_DATA_SIZE );
  strncat( dataText, "\"", MAX_DATA_SIZE );

  data.dptr = dataText;
  data.dsize = strlen( dataText );

  if( tdb_store( tctx, key, data, 0 ) < 0 )
		MsgPrint( MSG_DEBUG, "tdb_store failed\n" );

  tdb_close( tctx );
	MsgPrint( MSG_DEBUG, "Recorded client stat for %s\n", address );
}

#define GS_FILENAME   "gameStats.tdb"

/*
====================
RecordGameStat
====================
*/
void RecordGameStat( const char *address, const char *dataText )
{
  TDB_CONTEXT *tctx = NULL;
  TDB_DATA    key, data;
  char        keyText[ MAX_DATA_SIZE ] = { 0 };
  char        *p;
  time_t      tm = time( NULL );

  tctx = tdb_open( GS_FILENAME, 0, 0, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR );

  if( !tctx )
  {
		MsgPrint( MSG_DEBUG, "Couldn't open %s\n", GS_FILENAME );
    return;
  }

  strncpy( keyText, address, 22 );
  if( ( p = strrchr( keyText, ':' ) ) ) // Remove port
    *p = '\0';

  strncat( keyText, " ", MAX_DATA_SIZE );
  strncat( keyText, asctime( gmtime( &tm ) ), MAX_DATA_SIZE );

  key.dptr = keyText;
  key.dsize = strlen( keyText );

  data.dptr = (char *)dataText;
  data.dsize = strlen( dataText );

  if( tdb_store( tctx, key, data, 0 ) < 0 )
		MsgPrint( MSG_DEBUG, "tdb_store failed\n" );

  tdb_close( tctx );
	MsgPrint( MSG_NORMAL, "Recorded game stat from %s\n", address );
}

#endif