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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "dod_playerstats.h"
#include "hud_macros.h"
const char *g_pszShortTeamNames[2] =
{
"US",
"Ger"
};
const char *g_pszClassNames[NUM_DOD_PLAYERCLASSES] =
{
"Rifleman",
"Assault",
"Support",
"Sniper",
"MG",
"Rocket"
};
const char *g_pszStatNames[DODSTAT_MAX] =
{
"iPlayTime",
"iRoundsWon",
"iRoundsLost",
"iKills",
"iDeaths",
"iCaptures",
"iBlocks",
"iBombsPlanted",
"iBombsDefused",
"iDominations",
"iRevenges",
"iShotsHit",
"iShotsFired",
"iHeadshots"
};
int iValidWeaponStatBitMask = ( (1<<DODSTAT_KILLS ) | (1<<DODSTAT_SHOTS_HIT) | (1<<DODSTAT_SHOTS_FIRED) | (1<<DODSTAT_HEADSHOTS) );
int iValidPlayerStatBitMask = 0xFFFF & ~( (1<<DODSTAT_SHOTS_HIT) | (1<<DODSTAT_SHOTS_FIRED) | (1<<DODSTAT_HEADSHOTS) );
//-----------------------------------------------------------------------------
// Purpose:
// Input : &msg -
//-----------------------------------------------------------------------------
void __MsgFunc_DODPlayerStatsUpdate( bf_read &msg )
{
g_DODPlayerStats.MsgFunc_DODPlayerStatsUpdate( msg );
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CDODPlayerStats::CDODPlayerStats()
{
m_flTimeNextForceUpload = 0;
}
//-----------------------------------------------------------------------------
// Purpose: called at init time after all systems are init'd. We have to
// do this in PostInit because the Steam app ID is not available earlier
//-----------------------------------------------------------------------------
void CDODPlayerStats::PostInit()
{
SetNextForceUploadTime();
ListenForGameEvent( "player_stats_updated" );
ListenForGameEvent( "user_data_downloaded" );
HOOK_MESSAGE( DODPlayerStatsUpdate );
}
//-----------------------------------------------------------------------------
// Purpose: called at level shutdown
//-----------------------------------------------------------------------------
void CDODPlayerStats::LevelShutdownPreEntity()
{
// upload user stats to Steam on every map change or when we quit
UploadStats();
}
//-----------------------------------------------------------------------------
// Purpose: called when the stats have changed in-game
//-----------------------------------------------------------------------------
void CDODPlayerStats::FireGameEvent( IGameEvent *event )
{
const char *pEventName = event->GetName();
if ( FStrEq( pEventName, "user_data_downloaded" ) )
{
// Store our stat data
Assert( steamapicontext->SteamUserStats() );
if ( !steamapicontext->SteamUserStats() )
return;
CGameID gameID( engine->GetAppID() );
// reset everything
Q_memset( &m_PlayerStats, 0, sizeof(m_PlayerStats) );
Q_memset( &m_WeaponStats, 0, sizeof(m_WeaponStats) );
// read playerclass stats
for ( int iTeam=0;iTeam<2;iTeam++ )
{
for ( int iClass=0;iClass<NUM_DOD_PLAYERCLASSES;iClass++ )
{
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( iValidPlayerStatBitMask & (1<<iStat) )
{
char szStatName[256];
int iData;
Q_snprintf( szStatName, ARRAYSIZE( szStatName ), "%s.%s.%s", g_pszShortTeamNames[iTeam], g_pszClassNames[iClass], g_pszStatNames[iStat] );
if ( steamapicontext->SteamUserStats()->GetStat( szStatName, &iData ) )
{
// use Steam's value
m_PlayerStats[iTeam][iClass].m_iStat[iStat] = iData;
}
}
}
}
}
// read weapon stats
for ( int iWeapon=WEAPON_NONE+1;iWeapon<WEAPON_MAX;iWeapon++ )
{
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( iValidWeaponStatBitMask & (1<<iStat) )
{
char szStatName[256];
int iData;
Q_snprintf( szStatName, ARRAYSIZE( szStatName ), "%s.%s", s_WeaponAliasInfo[iWeapon], g_pszStatNames[iStat] );
if ( steamapicontext->SteamUserStats()->GetStat( szStatName, &iData ) )
{
// use Steam's value
m_WeaponStats[iWeapon].m_iStat[iStat] = iData;
}
}
}
}
IGameEvent * event = gameeventmanager->CreateEvent( "player_stats_updated" );
if ( event )
{
event->SetBool( "forceupload", false );
gameeventmanager->FireEventClientSide( event );
}
}
}
void CDODPlayerStats::MsgFunc_DODPlayerStatsUpdate( bf_read &msg )
{
dod_stat_accumulator_t playerStats;
Q_memset( &playerStats, 0, sizeof(playerStats) );
// get the fixed-size information
int iClass = msg.ReadByte();
int iTeam = msg.ReadByte();
int iPlayerStatBits = msg.ReadLong();
Assert( iClass >= 0 && iClass <= 5 );
if ( iClass < 0 || iClass > 5 )
return;
// the bitfield indicates which stats are contained in the message. Set the stats appropriately.
int iStat = DODSTAT_FIRST;
while ( iPlayerStatBits > 0 )
{
if ( iPlayerStatBits & 1 )
{
playerStats.m_iStat[iStat] = msg.ReadLong();
}
iPlayerStatBits >>= 1;
iStat++;
}
int iNumWeapons = msg.ReadByte();
int i;
CUtlVector<weapon_stat_t> vecWeaponStats;
for ( i=0;i<iNumWeapons;i++ )
{
weapon_stat_t weaponStat;
Q_memset( &weaponStat, 0, sizeof(weaponStat) );
weaponStat.iWeaponID = msg.ReadByte();
vecWeaponStats.AddToTail( weaponStat );
}
for ( i=0;i<vecWeaponStats.Count();i++ )
{
int iWeaponStatMask = msg.ReadLong();
int iStat = DODSTAT_FIRST;
while ( iWeaponStatMask > 0 )
{
if ( iWeaponStatMask & 1 )
{
vecWeaponStats[i].stats.m_iStat[iStat] = msg.ReadLong();
}
iWeaponStatMask >>= 1;
iStat++;
}
}
// sanity check: the message should contain exactly the # of bytes we expect based on the bit field
Assert( !msg.IsOverflowed() );
Assert( 0 == msg.GetNumBytesLeft() );
// if byte count isn't correct, bail out and don't use this data, rather than risk polluting player stats with garbage
if ( msg.IsOverflowed() || ( 0 != msg.GetNumBytesLeft() ) )
return;
UpdateStats( iClass, iTeam, &playerStats, &vecWeaponStats );
}
//-----------------------------------------------------------------------------
// Purpose: Uploads stats for current Steam user to Steam
//-----------------------------------------------------------------------------
void CDODPlayerStats::UploadStats()
{
// only upload if Steam is running
if ( !steamapicontext->SteamUserStats() )
return;
CGameID gameID( engine->GetAppID() );
char szStatName[256];
// write playerclass stats
for ( int iTeam=0;iTeam<2;iTeam++ )
{
for ( int iClass=0;iClass<NUM_DOD_PLAYERCLASSES;iClass++ )
{
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( iValidPlayerStatBitMask & (1<<iStat) )
{
if ( m_PlayerStats[iTeam][iClass].m_bDirty[iStat] )
{
Q_snprintf( szStatName, ARRAYSIZE( szStatName ), "%s.%s.%s", g_pszShortTeamNames[iTeam], g_pszClassNames[iClass], g_pszStatNames[iStat] );
steamapicontext->SteamUserStats()->SetStat( szStatName, m_PlayerStats[iTeam][iClass].m_iStat[iStat] );
}
}
}
}
}
// write weapon stats
for ( int iWeapon=WEAPON_NONE+1;iWeapon<WEAPON_MAX;iWeapon++ )
{
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( m_WeaponStats[iWeapon].m_bDirty[iStat] )
{
if ( iValidWeaponStatBitMask & (1<<iStat) )
{
Q_snprintf( szStatName, ARRAYSIZE( szStatName ), "%s.%s", s_WeaponAliasInfo[iWeapon], g_pszStatNames[iStat] );
steamapicontext->SteamUserStats()->SetStat( szStatName, m_WeaponStats[iWeapon].m_iStat[iStat] );
}
}
}
}
SetNextForceUploadTime();
}
void CDODPlayerStats::UpdateStats( int iPlayerClass, int iTeam, dod_stat_accumulator_t *playerStats, CUtlVector<weapon_stat_t> *vecWeaponStats )
{
Assert( iPlayerClass >= 0 && iPlayerClass < NUM_DOD_PLAYERCLASSES );
if ( iPlayerClass < 0 || iPlayerClass >= NUM_DOD_PLAYERCLASSES )
return;
// translate the team index into [0,1]
int iTeamIndex = ( iTeam == TEAM_ALLIES ) ? 0 : 1;
// upload this class' data
// add this stat update to our stored stats
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( iValidPlayerStatBitMask & (1<<iStat) )
{
if ( playerStats->m_iStat[iStat] > 0 )
{
m_PlayerStats[iTeamIndex][iPlayerClass].m_iStat[iStat] += playerStats->m_iStat[iStat];
m_PlayerStats[iTeamIndex][iPlayerClass].m_bDirty[iStat] = true;
}
}
}
int iWeaponStatCount = vecWeaponStats->Count();
for ( int i=0;i<iWeaponStatCount;i++ )
{
int iWeaponID = vecWeaponStats->Element(i).iWeaponID;
for ( int iStat=0;iStat<DODSTAT_MAX;iStat++ )
{
if ( iValidWeaponStatBitMask & (1<<iStat) )
{
int iValue = vecWeaponStats->Element(i).stats.m_iStat[iStat];
if ( iValue > 0 )
{
m_WeaponStats[iWeaponID].m_iStat[iStat] += iValue;
m_WeaponStats[iWeaponID].m_bDirty[iStat] = true;
}
}
}
}
// if we haven't uploaded stats in a long time, upload them
if ( ( gpGlobals->curtime >= m_flTimeNextForceUpload ) )
{
UploadStats();
}
}
//-----------------------------------------------------------------------------
// Purpose: sets the next time to force a stats upload at
//-----------------------------------------------------------------------------
void CDODPlayerStats::SetNextForceUploadTime()
{
// pick a time a while from now (an hour +/- 15 mins) to upload stats if we haven't gotten a map change by then
m_flTimeNextForceUpload = gpGlobals->curtime + ( 60 * RandomInt( 45, 75 ) );
}
CDODPlayerStats g_DODPlayerStats;
|