Skip to content

Commit

Permalink
added swift code into repo
Browse files Browse the repository at this point in the history
  • Loading branch information
McJones committed Jun 26, 2017
1 parent 3e21ee4 commit c291fe1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// swift-tools-version:3.1

import PackageDescription

let package = Package(
name: "SteamThing",
dependencies : [
.Package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", majorVersion:3)
]
)
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# SteamThing
A quick tool I made to see what games people have in common across their steam accounts

A tool I made to see what games people have in common across their steam accounts.
This came about because at LANs there is no easy way to know what games most people have until you get there, and I did this as something to do while home bored, so it might be a bit odd.

## Using it

You will need a Steam API Key before you can use SteamThing, you can get one from [Valve](http://steamcommunity.com/dev).
Once you have that you can plug it into the code at `Sources/SteamHelper.swift` and then you can build SteamThing.

SteamThing uses the Swift package manager so running `swift build` will handle everything.

Once it is built you can run it from the command line, by default the built SteamThing will be inside the `.build/debug` folder, so you can run this like:
```
.build/debug/SteamThing steamid1 steamid2 steamid3
```
This will spit out a list of games that the users with the steam ids steamid1, steamid2, steamid3 all own as well as games that the majority of players own.
To work out what your/others steam id are, the easiest way is to use the [Steam ID Finder](http://steamidfinder.com).
125 changes: 125 additions & 0 deletions Sources/SteamHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// SteamHelper.swift
// SteamThing
//
// Created by Tim Nugent on 26/6/17.
//
//

import Foundation
import SwiftyJSON

let apikey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

class SteamAPI
{
var games = [Int : Game]()
var playersInfo = [Player]()

func load(playerIDs: [String])
{
guard let players = self.getPlayerInfo(steamID: playerIDs) else
{
print("failed to load player info")
return
}
for player in players
{
//ok go through and run the get games script
// but modify it so that it returns how many people own that game
self.getGames(steamID: player.steamID)
}
self.playersInfo = players
}
private func getGames(steamID : String)
{
let urlString = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=\(apikey)&steamid=\(steamID)&include_appinfo=1&include_played_free_games=1&format=json"
if let url = URL(string: urlString)
{
do
{
let contents = try String(contentsOf: url)

if let jsonData = contents.data(using: .utf8)
{
let json = JSON(data: jsonData)
if let gamesList = json["response"]["games"].array
{
for game in gamesList
{
let name = game["name"].stringValue
let appid = game["appid"].intValue
let count = self.games[appid] != nil ? self.games[appid]!.count + 1 : 1

self.games[appid] = Game(name: name, appid: appid, count: count)
}
}
}
}
catch
{
print("Error grabbing games list for steam id \(steamID)")
}
}
}
private func getPlayerInfo(steamID : [String]) -> [Player]?
{
var players : [Player]?
let urlString = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=\(apikey)&steamids=\(steamID.joined(separator: ",")))"
if let url = URL(string: urlString)
{
do
{
let contents = try String(contentsOf: url)

if let jsonData = contents.data(using: .utf8)
{
let json = JSON(data: jsonData)
if let playerArray = json["response"]["players"].array
{
for player in playerArray
{
if let playerInfo = player.dictionary
{
let id = playerInfo["steamid"]?.stringValue
let name = playerInfo["personaname"]?.stringValue
let avatar = playerInfo["avatar"]?.stringValue

if players == nil
{
players = [Player]()
}
players?.append(Player(steamID: id!, name: name!, avatar: avatar!))
}
}
}
}
}
catch
{
print("Error grabbing user information for id \(steamID)")
}
}
return players
}
}

struct Game
{
let name : String
let appid : Int
var count : Int = 0
}
extension Game : Equatable
{
static func ==(lhs: Game, rhs: Game) -> Bool
{
return lhs.appid == rhs.appid
}
}
internal struct Player
{
let steamID : String
let name : String
let avatar : String
}
24 changes: 24 additions & 0 deletions Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

let steamIDs = Array(CommandLine.arguments[1..<CommandLine.arguments.count])

let steamHelper = SteamAPI()
steamHelper.load(playerIDs: steamIDs)

let playerNames = steamHelper.playersInfo.map { return $0.name }
print("\(playerNames.joined(separator: ", ")) all have the following games:")

var games = steamHelper.games.values.filter { $0.count == steamIDs.count }
games.sorted{$0.name < $1.name}.forEach{print($0.name)}

// going through all games that 2 or more peeps own
for i in (2...steamIDs.count - 1).reversed()
{
var newGames = steamHelper.games.values.filter { $0.count == i }.filter { !games.contains($0) }

print("\nThe following \(newGames.count) games are owned by \(i) players:")
newGames.sorted { $0.name < $1.name }.forEach { print($0.name) }

// the new list of already shown games
games = steamHelper.games.values.filter { $0.count >= i }
}

0 comments on commit c291fe1

Please sign in to comment.