-
Notifications
You must be signed in to change notification settings - Fork 11
/
updategamestats.php
193 lines (153 loc) · 5.74 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
188
189
190
191
192
193
<?php
date_default_timezone_set( 'UTC' );
include_once( 'includes/session.php' );
include_once( 'includes/functions_steam.php' );
include_once( 'includes/functions_db.php' );
include_once( 'includes/functions_apps.php' );
include_once( 'includes/functions_stats.php' );
/* this can run for a good few minutes… about 80 users per minute ?! */
set_time_limit( 10000 );
// are we logged in? no → leave
if ( ! login_check( ) ) {
header( 'Location: /' );
exit( );
} else {
$me = $_SESSION['u'];
}
// are we admin? no → leave
if ( in_array( $me, getAdmins( ) ) ) {
} else {
header( 'Location: /' );
exit( );
}
$date = date( 'Y-m-d' );
echo $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' ) {
echo $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 = $steamDBRepo . '/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 ) ) {
echo $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( );
}
echo $date . ': ' . count( $appslist ) . ' known apps, ' . $onlinux . " marked for Linux.\n<br>";
$members = getGroupMembers( );
echo $date . ': ' . 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++;
/* echo $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?
echo 'Game ' . $app[ 'appid' ] . " doesn’t exist in Valve’s app output? lol.\n<br>";
}
}
} else {
$appsmin = 0;
// eh? Faulty data from Steam?
echo $member . " has zero games on their profile.\n<br>";
}
} else {
// User has a private profile. Do something with this knowledge?
echo $member . " has a private profile.\n<br>";
}
flush( );
}
echo $date . ': Completed stats gathering: ' . date( 'c' ) . "\n<br>";
echo $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)' );
$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' ] ) );
}
storeMemberStats( $date, $publicMembers, count( $members ), $appsmin, $appsmax );
$database->commit( );
} catch ( Exception $e ) {
echo $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 );
*/
echo $date . ': Completed stats storing: ' . date( 'c' ) . "\n<br>";
$completion = microtime( true ) - $_SERVER[ 'REQUEST_TIME_FLOAT' ];
echo $date . ": Process Time: {$completion}.";
echo $date . ': Memory: ' . memory_get_usage( ) . "\n<br>";