Skip to content

Instantly share code, notes, and snippets.

@nexosisops
Created January 10, 2018 13:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nexosisops/0880381e47befb96a8e86c38986a43e1 to your computer and use it in GitHub Desktop.
Save nexosisops/0880381e47befb96a8e86c38986a43e1 to your computer and use it in GitHub Desktop.
Function Invoke-GetCsGoMetricsBySteamId {
Param(
$steamId
)
try {
"Processing Steam ID $steamId" | Write-Verbose
if ($env:steamApiKey -eq $null) {
'You must set the $env:steamApiKey variable before running this command:' | Write-Host
'[Environment]::SetEnvironmentVariable("steamApiKey", "{put your key here}", "User")' | Write-Host
'If you run the SetEnvironmentVariable command above you may need to restart the PowerShell environment so it picks up the new environment variable.' | Write-Host
throw 'No Steam API Key found.'
}
$script:getPlayerSummaries = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/'
$script:getUserStatsForGame = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/'
$script:getOwnedGames = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001'
$script:getPlayerBans = 'http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/'
$script:csgoAppId = 730
# Get Player Bans
$script:playerBans = Invoke-RestMethod -Method Get -Uri "$($script:getPlayerBans)?key=$env:steamApiKey&steamIds=$steamId"
# Get Player Summary
"Retrieving Player Summary for $steamId" | Write-Verbose
$script:playerSummary = Invoke-RestMethod -Method Get -Uri "$($script:getPlayerSummaries)?key=$env:steamApiKey&steamids=$steamId"
"Player: '$($script:playerSummary.response.players[0].personaname)' CommunityVisibilityState: $($script:playerSummary.response.players[0].CommunityVisibilityState)" | Write-Verbose
if ($script:playerSummary.response.players[0].CommunityVisibilityState -eq $null -or $script:playerSummary.response.players[0].CommunityVisibilityState -ne 3) {
"Player '$($script:playerSummary.response.players[0].personaname)' with Steam ID $steamId not found or Profile is not public. Skipping." | Write-Host
Return
}
# Get Games Owned
"Get Games Owned for User $steamId" | Write-Verbose
$script:gamesOwned = Invoke-RestMethod -Method Get -Uri "$($getOwnedGames)?key=$env:steamApiKey&steamid=$steamId&format=json"
"User $steamId owns $script:gamesOwned.response.game_count games." | Write-Verbose
if (($script:gamesOwned.response.games | Where appid -eq $csgoAppId) -eq $null) {
"User $steamId does NOT own GS:GO. Skipping..." | Write-Output
Return
}
# Get Users Stats For Game
"Get User Stats for User $steamId" | Write-Verbose
$script:playerStatsForCsgo = Invoke-RestMethod -Method Get -Uri "$($script:getUserStatsForGame)?appid=$csgoAppId&key=$env:steamApiKey&steamid=$steamID"
# Convert Array of hash to hash
$script:csgoStats = @{};
"Converting JSON Array of Hashes to a hashtable." | Write-Verbose
foreach ($stat in $script:playerStatsForCsgo.playerstats.stats) {
$script:csgoStats.Add($stat.name, [int]$stat.value)
}
$script:csgoStats.Add("steamId", $steamId)
$script:csgoStats.Add("total_games_owned", $script:gamesOwned.response.game_count)
$script:csgoStats.Add("VACBanned", [int]$script:playerBans.players[0].VACBanned)
"Metrics successfully retrived." | Write-Verbose
($script:csgoStats | Out-String).Trim() | Write-Verbose
$script:csgoStats
} catch {
"Error fetching Steam ID: $steamId : Exception:" | Write-Host
$_.Exception| Write-Host
}
'--------------------------------------' | Write-Verbose
}
Function Invoke-CalculateCsGoStatsForSteamId {
Param(
$steamId
)
try {
"Processing Steam ID $steamId" | Write-Verbose
if ($env:steamApiKey -eq $null) {
'You must set the $env:steamApiKey variable before running this command:' | Write-Host
'[Environment]::SetEnvironmentVariable("steamApiKey", "{put your key here}", "User")' | Write-Host
'If you run the SetEnvironmentVariable command above you may need to restart the PowerShell environment so it picks up the new environment variable.' | Write-Host
throw 'No Steam API Key found.'
}
$script:getPlayerSummaries = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/'
$script:getUserStatsForGame = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/'
$script:getOwnedGames = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001'
$script:getPlayerBans = 'http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/'
$script:csgoAppId = 730
# Get Player Bans
$script:playerBans = Invoke-RestMethod -Method Get -Uri "$($script:getPlayerBans)?key=$env:steamApiKey&steamIds=$steamId"
# Get Player Summary
"Retrieving Player Summary for $steamId" | Write-Verbose
$script:playerSummary = Invoke-RestMethod -Method Get -Uri "$($script:getPlayerSummaries)?key=$env:steamApiKey&steamids=$steamId"
"Player: '$($script:playerSummary.response.players[0].personaname)' CommunityVisibilityState: $($script:playerSummary.response.players[0].CommunityVisibilityState)" | Write-Verbose
if ($script:playerSummary.response.players[0].CommunityVisibilityState -eq $null -or $script:playerSummary.response.players[0].CommunityVisibilityState -ne 3) {
"Player '$($script:playerSummary.response.players[0].personaname)' with Steam ID $steamId not found or Profile is not public. Skipping." | Write-Host
Return
}
# Get Games Owned
"Get Games Owned for User $steamId" | Write-Verbose
$script:gamesOwned = Invoke-RestMethod -Method Get -Uri "$($getOwnedGames)?key=$env:steamApiKey&steamid=$steamId&format=json"
"User $steamId owns $script:gamesOwned.response.game_count games." | Write-Verbose
if (($script:gamesOwned.response.games | Where appid -eq $csgoAppId) -eq $null) {
"User $steamId does NOT own GS:GO. Skipping..." | Write-Host
Return
}
# Get Users Stats For Game
"Get User Stats for User $steamId" | Write-Verbose
$script:playerStatsForCsgo = Invoke-RestMethod -Method Get -Uri "$($script:getUserStatsForGame)?appid=$csgoAppId&key=$env:steamApiKey&steamid=$steamID"
# Convert Array of hash to hash
$script:csgoStats = @{};
"Converting JSON Array of Hashes to a hashtable." | Write-Verbose
foreach ($stat in $script:playerStatsForCsgo.playerstats.stats) {
$script:csgoStats.Add($stat.name, [int]$stat.value)
}
"Calculating statistics from metrics." | Write-Verbose
$script:calculatedPlayerStats = @{
steamId = $steamId
total_games_owned = $script:gamesOwned.response.game_count
#total_time_played = $script:csgoStats.total_time_played
win_ratio = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_wins -divisor $script:csgoStats.total_rounds_played)
total_accuracy = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_shots_hit -divisor $script:csgoStats.total_shots_fired)
kill_to_death_ratio = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_kills -divisor $script:csgoStats.total_deaths)
total_wins_per_hour = (((Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_wins -divisor $script:csgoStats.total_time_played)) / 60) / 60 # unit is in seconds, convert to hours
#weapons_donated_per_hour = (((Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_weapons_donated -divisor $script:csgoStats.total_time_played)) / 60) / 60 # unit is in seconds, convert to hours
mvp_per_round = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_mvps -divisor $script:csgoStats.total_rounds_played)
total_headshots_per_round = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_kills_headshot -divisor $script:csgoStats.total_rounds_played)
accuracy_ssg08 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_ssg08 -divisor $script:csgoStats.total_shots_ssg08)
accuracy_awp = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_awp -divisor $script:csgoStats.total_shots_awp)
accuracy_deagle = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_deagle -divisor $script:csgoStats.total_shots_deagle)
accuracy_aug = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_aug -divisor $script:csgoStats.total_shots_aug)
accuracy_scar20 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_scar20 -divisor $script:csgoStats.total_shots_scar20)
accuracy_m4a1 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_m4a1 -divisor $script:csgoStats.total_shots_m4a1)
accuracy_ak47 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_ak47 -divisor $script:csgoStats.total_shots_ak47)
accuracy_bizon = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_bizon -divisor $script:csgoStats.total_shots_bizon)
accuracy_elite = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_elite -divisor $script:csgoStats.total_shots_elite)
accuracy_famas = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_famas -divisor $script:csgoStats.total_shots_famas)
accuracy_fiveseven = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_fiveseven -divisor $script:csgoStats.total_shots_fiveseven)
accuracy_g3sg1 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_g3sg1 -divisor $script:csgoStats.total_shots_g3sg1)
accuracy_galilar = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_galilar -divisor $script:csgoStats.total_shots_galilar)
accuracy_glock = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_glock -divisor $script:csgoStats.total_shots_glock)
accuracy_hkp2000 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_hkp2000 -divisor $script:csgoStats.total_shots_hkp2000)
accuracy_m249 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_m249 -divisor $script:csgoStats.total_shots_m249)
accuracy_mac10 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_mac10 -divisor $script:csgoStats.total_shots_mac10)
accuracy_mag7 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_mag7 -divisor $script:csgoStats.total_shots_mag7)
accuracy_mp7 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_mp7 -divisor $script:csgoStats.total_shots_mp7)
accuracy_mp9 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_mp9 -divisor $script:csgoStats.total_shots_mp9)
accuracy_negev = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_negev -divisor $script:csgoStats.total_shots_negev)
accuracy_nova = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_nova -divisor $script:csgoStats.total_shots_nova)
accuracy_p250 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_p250 -divisor $script:csgoStats.total_shots_p250)
accuracy_p90 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_p90 -divisor $script:csgoStats.total_shots_p90)
accuracy_sawedoff = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_sawedoff -divisor $script:csgoStats.total_shots_sawedoff)
accuracy_sg556 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_sg556 -divisor $script:csgoStats.total_shots_sg556)
accuracy_tec9 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_tec9 -divisor $script:csgoStats.total_shots_tec9)
accuracy_ump45 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_ump45 -divisor $script:csgoStats.total_shots_ump45)
accuracy_xm1014 = (Invoke-DivideReturnNullOnError -dividend $script:csgoStats.total_hits_xm1014 -divisor $script:csgoStats.total_shots_xm1014)
VACBannedActual = [int]$script:playerBans.players[0].VACBanned
}
"Computations complete." | Write-Verbose
($script:calculatedPlayerStats | Out-String).Trim() | Write-Verbose
$script:calculatedPlayerStats
} catch {
"Error fetching Steam ID: $steamId : Exception: $_" | Write-Host
}
'--------------------------------------' | Write-Verbose
}
function Invoke-DivideReturnNullOnError {
Param(
$dividend,
$divisor
)
# don't bother to divide by zero return null
if ($divisor -eq 0) { return $null }
try {
return $dividend / $divisor
} catch {
# Any other unforeseen exception, return $null
"Error while trying to divide: $dividend / $divisor" | Write-Verbose
return $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment