-
Notifications
You must be signed in to change notification settings - Fork 0
/
updategamestats.php
187 lines (148 loc) · 5.89 KB
/
updategamestats.php
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
<?php
date_default_timezone_set('UTC');
include_once( 'paths.php' );
/* we now import functions from steamlug.org repo */
include_once( $includeLocation . '/includes/paths.php' );
include_once( $includeLocation . '/includes/creds.php' );
include_once( $includeLocation . '/includes/functions_steam.php' );
include_once( $includeLocation . '/includes/functions_db.php' );
include_once( $includeLocation . '/includes/functions_apps.php' );
/* this can run for a good few minutes… about 80 users per minute ?! */
set_time_limit( 5400 );
$date = date( "Y-m-d" );
print $date . ": Starting stats gathering: " . date("c") . "\n<br>";
$database = connectDB();
/* TODO: make this script look for the most recent date,
* compare to today → fail
* < 2 weeks since last check → fail
* == 2 weeks → gfi */
foreach( $database->query( "SELECT count(1) AS number FROM steamlug.memberstats WHERE `date`='" . $date . "' LIMIT 1" ) as $res ) {
if ( $res[ 'number' ] == "1" ) {
print $date . ": We have already captured stats for today! Ending script.";
exit;
}
}
if ( true ) {
/* pick heuristic to decide to pull from db */
// TODO: move this to _stats, make it callable to refresh our intl db without being
// part of the stats update
// request Steam give us their latest this of games
$appslist = array( );
foreach ( getSteamApps( ) as $app ) {
$appslist[ $app[ 'appid' ] ] = array ( "name" => $app[ 'name' ], "onlinux" => false, "owners" => 0, "playtime" => 0, "fortnight" => 0, "playersfortnight" => 0 );
}
// open our lovely SteamDB list, for a vague notion of what is on Linux
$jsonfile = $steamLinuxDB . '/GAMES.json';
if ( !file_exists( $jsonfile ) ) {
// TODO Do a better job to pass this error back
return false;
}
$json = file_get_contents( $jsonfile );
$data = json_decode( $json, true );
$onlinux = 0;
foreach ( $data as $appid => $app ) {
/* ’cause Steam sometimes does not expose known apps on Steam? */
if ( !array_key_exists( $appid, $appslist ) ) {
print $date . ": " . $appid . " is missing from the data back from Valve.\n<br>";
continue;
}
if ( is_array( $app ) ) {
if ( array_key_exists( 'Hidden', $app ) )
continue;
// apps that are here are either beta, or have a comment. But they are still Linux apps
$appslist[ $appid ][ 'onlinux' ] = true;
$onlinux++;
} else {
$appslist[ $appid ][ 'onlinux' ] = true;
$onlinux++;
}
}
storeAppsDB( $appslist );
} else {
$appslist = getSteamAppsDB( );
}
print $date . ": " . count($appslist) . " known apps, " . $onlinux . " marked for Linux.\n<br>";
$members = getGroupMembers();
print $date . ": " . print count($members) . " members.\n<br>";
/* pointless stats tracking GET! */
$appsmin = 2000;
$appsmax = 0;
$publicMembers = 0;
foreach ( $members as $member ) {
$memberGames = getMemberGames( $member );
if ( count( $memberGames ) > 0 ) {
$publicMembers++;
/* print $member . " has " . $memberGames[ 'game_count' ] . " apps.\n<br>"; */
if ( ($memberGames[ 'game_count' ] > 0) and array_key_exists( 'games', $memberGames ) ) {
if ( $memberGames[ 'game_count' ] > $appsmax )
$appsmax = $memberGames[ 'game_count' ];
if ( $memberGames[ 'game_count' ] < $appsmin )
$appsmin = $memberGames[ 'game_count' ];
foreach ( $memberGames[ 'games' ] as $app ) {
if ( array_key_exists( $app[ 'appid' ], $appslist ) ) {
$appslist[ $app[ 'appid' ] ][ 'owners' ]++;
$appslist[ $app[ 'appid' ] ][ 'playtime' ] += $app[ 'playtime_forever' ];
if ( array_key_exists( "playtime_2weeks", $app ) ) {
$appslist[ $app[ 'appid' ] ][ 'fortnight' ] += $app[ 'playtime_2weeks' ];
$appslist[ $app[ 'appid' ] ][ 'playersfortnight' ]++;
}
} else {
// panic?
print "Game " . $app[ 'appid' ] . " doesn’t exist in Valve’s app output? lol.\n<br>";
}
}
} else {
$appsmin = 0;
// eh? Faulty data from Steam?
print $member . " has zero games on their profile.\n<br>";
}
} else {
// User has a private profile. Do something with this knowledge?
print $member . " has a private profile.\n<br>";
}
flush( );
}
print $date . ": Completed stats gathering: " . date("c") . "\n<br>";
print $date . ": " . $publicMembers . " public member profiles of " . count($members ) . " members read on " . date( "c" ) . "\n<br>";
flush( );
$storestats = $database->prepare( "INSERT INTO appstats (date, appid, owners, playtime, fortnight, playersfortnight) VALUES (:date, :appid, :owners, :playtime, :fortnight, :playersfortnight)" );
$storegroupstats = $database->prepare( "INSERT INTO memberstats (date, countpublic, count, min, max) VALUES (:date, :pubcount, :count, :min, :max)" );
$storeapps = $database->prepare( "INSERT INTO apps (appid, name) VALUES (:appid, :name)
ON DUPLICATE KEY UPDATE appid=VALUES(appid), name=VALUES(name);" );
try {
$database->beginTransaction( );
foreach ( $appslist as $appid=>$app ) {
if ( $app[ 'owners' ] == 0 )
continue;
$storestats->execute( array(
'date' => $date,
'appid' => $appid,
'owners' => $app[ 'owners' ],
'playtime' => $app[ 'playtime' ],
'fortnight' => $app[ 'fortnight' ],
'playersfortnight' => $app[ 'playersfortnight' ] ) );
}
foreach ( $appslist as $appid => $app ) {
$storeapps->execute( array(
'appid' => $appid,
'name' => $app[ 'name' ] ) );
}
$storegroupstats->execute( array(
'date' => $date,
'pubcount' => $publicMembers,
'count' => count($members),
'min' => $appsmin,
'max' => $appsmax ) );
$database->commit( );
} catch ( Exception $e ) {
print $date . ": Oops, database failure: " . $e;
}
/* XXX where to write this?
$logger = fopen( 'stats.log', 'a' );
fwrite( $logger, $date . " Stored " . count($members) . " member profiles." );
fclose( $logger );
*/
print $date . ": Completed stats storing: " . date("c") . "\n<br>";
$completion = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
print $date . ": Process Time: {$completion}.";
print $date . ": Memory: " . memory_get_usage( ) . "\n<br>";