From c291fe1e7e1235f9099fc5c4a93ddcf91dd1e09f Mon Sep 17 00:00:00 2001 From: McJones Date: Mon, 26 Jun 2017 16:44:46 +1000 Subject: [PATCH] added swift code into repo --- Package.swift | 10 +++ README.md | 18 +++++- Sources/SteamHelper.swift | 125 ++++++++++++++++++++++++++++++++++++++ Sources/main.swift | 24 ++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 Package.swift create mode 100644 Sources/SteamHelper.swift create mode 100644 Sources/main.swift diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..b5a1e7d --- /dev/null +++ b/Package.swift @@ -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) + ] +) diff --git a/README.md b/README.md index 0a0c8cd..f107e46 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file diff --git a/Sources/SteamHelper.swift b/Sources/SteamHelper.swift new file mode 100644 index 0000000..f5db27b --- /dev/null +++ b/Sources/SteamHelper.swift @@ -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 +} diff --git a/Sources/main.swift b/Sources/main.swift new file mode 100644 index 0000000..6d26ce9 --- /dev/null +++ b/Sources/main.swift @@ -0,0 +1,24 @@ +import Foundation + +let steamIDs = Array(CommandLine.arguments[1..= i } +}