-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilFunctions.php
More file actions
154 lines (135 loc) · 4.99 KB
/
Copy pathutilFunctions.php
File metadata and controls
154 lines (135 loc) · 4.99 KB
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
<?php
require_once("connection.php");
/**
* Recursive function that returns the players Steam ID, even if the custom URL is provided.
* @param $username
* @return mixed
*/
function steamidResolve($username, $apiKey)
{
if (is_numeric($username)) {
$url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$apiKey.'&steamids=' . $username;
$data = curl_connect($url);
return json_decode($data);
} else {
$url = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='.$apiKey.'&vanityurl=' . $username;
$data = curl_connect($url);
$json_output = json_decode($data);
$username = $json_output->response->steamid;
return steamidResolve($username);
}
}
/**
* Helper function for curl connect.
* @param $url
* @return mixed
*/
function curl_connect($url)
{
$ch = curl_init();
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, $url);
// Disable SSL verification
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//Set method to GET
curl_setopt($ch, CURLOPT_HTTPGET, true);
//Execute the request.
$data = curl_exec($ch);
//Close the cURL handle.
curl_close($ch);
return $data;
}
/**
* Calculates the players steam score based on values given to each achievement based on global completion percentages.
* @param $steamId
* @param $appId
* @return int $steamScore
*/
function steamScore($steamId, $appId, $apiKey)
{
$url = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=' . $appId;
$json_percent = curl_connect($url);
$percent_out = json_decode($json_percent);
$url2 = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?key='.$apiKey.'&steamid=' . $steamId . '&appid=' . $appId;
$json_achieved = curl_connect($url2);
$achieved_out = json_decode($json_achieved);
$steamScore = 0;
//if(!empty($percent_out->game->availablegamestats)) {
//$steamScore = 5;
foreach ($percent_out->achievementpercentages->achievements as $percent) {
// comaparing name of achievement to name of completed achievement
$apiName = $percent->name;
//$achResult = hasAchieved($dB, $apiName);
foreach ($achieved_out->playerstats->achievements as $names) {
$achNames = $names->name;
if ($achNames == $apiName) {
if ($percent->percent >= 50) {
$steamScore += 5;
} elseif ($percent->percent < 50 && $percent->percent >= 25) {
$steamScore += 10;
} elseif ($percent->percent < 25 && $percent->percent >= 10) {
$steamScore += 15;
} elseif ($percent->percent < 10 && $percent->percent >= 2) {
$steamScore += 20;
} else {
$steamScore += 25;
}
}
}
//}
}
return $steamScore;
}
/**
* Currently an unused function that puts all achievements completed into an array.
* @param $steamId
* @param $appId
* @return array
*/
function hasAchievedToo($steamId, $appId, $apiKey)
{
$url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?key='.$apiKey.'&steamid=' . $steamId . '&appid=' . $appId;
$json_achieved = curl_connect($url);
$achieved_out = json_decode($json_achieved);
$achieved = array();
foreach ($achieved_out->playerstats->achievements as $vals) {
array_push($achieved, $vals->name);
}
return $achieved;
}
/**
* Finds if user has achieved an achievement or not.
* @param $dB
* @param $apiName
* @param $steamId
* @return bool
*/
function hasAchieved($dB, $apiName, $steamId)
{
$achId = mysqli_query($dB, "SELECT achieved FROM user_achievement
JOIN users ON user_achievement.user_id = users.user_id
JOIN achievement ON user_achievement.achievement_id = achievement.achievement_id
WHERE achievement.apiname LIKE '$apiName' AND user_achievement.user_id LIKE '$steamId';")->fetch_object()->achieved;
if ($achId == 1) {
return true;
} else {
return false;
}
}
/**
* Currently an unused function that adds up each individual Steam Score from each row in the game_score column of the
* database.
* @param $dB
* @param $steamId
* @return mixed
*/
function scoreSum($dB, $steamId)
{
$result = mysqli_query($dB, "SELECT SUM(game_score) AS score_sum FROM user_game WHERE user_game.user_id LIKE $steamId");
$row = mysqli_fetch_assoc($result);
$scoreSum = $row['score_sum'];
return $scoreSum;
}