Esempio n. 1
0
 public override void initData()
 {
     try
     {
         localData = Roboto.Settings.getPluginData <mod_steam_core_data>();
     }
     catch (InvalidDataException)
     {
         //Data doesnt exist, create, populate with sample data and register for saving
         localData = new mod_steam_core_data();
         sampleData();
         Roboto.Settings.registerData(localData);
     }
 }
        public static List <mod_steam_game> getRecentGames(string playerID)
        {
            mod_steam_core_data   localData = getLocalData();
            List <mod_steam_game> games     = new List <mod_steam_game>();
            NameValueCollection   pairs     = new NameValueCollection();

            pairs["steamid"] = playerID.ToString();
            JObject response = sendPOST(@"/IPlayerService/GetRecentlyPlayedGames/v0001/", pairs);

            foreach (JToken token in response.SelectTokens("response.games[*]"))//jo.Children()) //) records[*].data.importedPath"
            {
                //mangle this into a player object
                string gameName = "";
                string gameID   = "";
                try
                {
                    //get the message details
                    gameName = token.SelectToken("name").Value <string>();
                    gameID   = token.SelectToken("appid").Value <string>();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error parsing message " + e.ToString());
                }

                //try get the game from our game cache
                mod_steam_game game = localData.getGame(gameID);

                if (game == null)
                {
                    //otherwise add it
                    game = new mod_steam_game(gameID, gameName);
                }
                games.Add(game);
            }

            //make sure these are all in our local cache
            foreach (mod_steam_game g in games)
            {
                bool added = localData.tryAddGame(g);
                if (added)
                {
                    Console.WriteLine("Added game " + g.displayName + " to the list of games we know about");
                }
            }
            return(games);
        }
Esempio n. 3
0
        public void checkAchievements()
        {
            List <string>       announce  = new List <string>();
            mod_steam_core_data localData = Roboto.Settings.getPluginData <mod_steam_core_data>();

            //get a list of what the player has been playing
            try
            {
                List <mod_steam_game> playerGames = mod_steam_steamapi.getRecentGames(playerID);

                foreach (mod_steam_game g in playerGames)
                {
                    //get the local data object
                    mod_steam_game gameData = localData.getGame(g.gameID);

                    //get the achievement list for each game
                    try
                    {
                        List <string> gainedAchievements = mod_steam_steamapi.getAchievements(playerID, g.gameID);

                        //make a list of any that we havent recorded yet
                        List <string> newAchievements = new List <string>();
                        foreach (string achievementCode in gainedAchievements)
                        {
                            if (chievs.Where(x => x.chievName == achievementCode && x.appID == g.gameID).Count() == 0)
                            {
                                newAchievements.Add(achievementCode);
                            }
                        }

                        if (newAchievements.Count() > 0)
                        {
                            List <string> failedAchieves = new List <string>();
                            //try get the cached friendly text for each achievement, and add them to our player's stash.
                            foreach (string s in newAchievements)
                            {
                                chievs.Add(new mod_steam_chiev(s, g.gameID));
                                mod_steam_achievement chiev = gameData.getAchievement(s);
                                if (chiev == null)
                                {
                                    Console.WriteLine("Failed to get friendly data for " + s + " from cache, will refresh");
                                    failedAchieves.Add(s);
                                }
                                else
                                {
                                    announce.Add(chiev.ToString() + " in " + g.displayName);
                                }
                            }

                            //if we failed, refresh the cache and try again.
                            if (failedAchieves.Count() > 0)
                            {
                                gameData.refreshAchievs();
                            }
                            //add any that failed originally.
                            foreach (string s in failedAchieves)
                            {
                                mod_steam_achievement chiev = gameData.getAchievement(s);
                                if (chiev == null)
                                {
                                    Console.WriteLine("Failed to get friendly data for " + s + " even after refresh. Will add default text instead");
                                    announce.Add(s.Replace("_", " ") + " in " + g.displayName);
                                }
                                else
                                {
                                    announce.Add(chiev.ToString() + " in " + g.displayName);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        //probably failed to call the web service.
                        Console.WriteLine("Failed during update of player achievements for game" + e.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                //probably failed to call the web service.
                Console.WriteLine("Failed during update of player achievements " + e.ToString());
            }
            //send a message (first few per game)
            if (announce.Count() > 0)
            {
                string message = playerName + " got the following achievements:" + "\n\r";
                int    max     = 5;
                if (announce.Count < 5)
                {
                    max = announce.Count;
                }

                for (int i = 0; i < max; i++)
                {
                    message += "- " + announce[i] + "\n\r";
                }
                if (announce.Count > 5)
                {
                    message += "And (" + (announce.Count - 5).ToString() + ") others";
                }
                TelegramAPI.SendMessage(this.chatID, message, null, true, -1, true);
            }
        }
        //TODO - merge this with the telegramAPI class, probably.

        /// <summary>
        /// Sends a POST message, returns the reply object
        /// </summary>
        /// <returns></returns>
        private static JObject sendPOST(String methodURL, NameValueCollection pairs)
        {
            //get our URL and Key
            mod_steam_core_data localData = getLocalData();

            pairs["key"] = localData.steamAPIKey;
            string finalString = localData.steamCoreURL + methodURL;

            //sort out encodings and clients.
            Encoding  enc    = Encoding.GetEncoding(1252);
            WebClient client = new WebClient();


            //now move the params across
            bool first = true;

            foreach (string itemKey in pairs)
            {
                if (first)
                {
                    finalString += "?";
                    first        = false;
                }
                else
                {
                    finalString += "&";
                }
                finalString += Uri.EscapeDataString(itemKey) + "=" + Uri.EscapeDataString(pairs[itemKey]);
            }


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(finalString);

            request.Method      = "GET";
            request.ContentType = "application/json";
            Roboto.log.log("Calling Steam API:\n\r" + request.RequestUri.ToString(), logging.loglevel.low);
            try
            {
                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

                if (webResponse != null)
                {
                    StreamReader responseSR = new StreamReader(webResponse.GetResponseStream(), enc);
                    string       response   = responseSR.ReadToEnd();

                    JObject jo = JObject.Parse(response);

                    //success
                    //TODO - for Steam this is a "success" object that should return "true". Not the first item.
                    string path = jo.First.Path;

                    //if (path != "ok" || result != "True")
                    //{
                    //    Console.WriteLine("Error received sending message!");
                    //throw new WebException("Failure code from web service");

                    //}
                    Roboto.log.log("Message Success", logging.loglevel.low);
                    return(jo);
                }
            }
            catch (Exception e)
            {
                Roboto.log.log("Steam API Call failed " + e.ToString(), logging.loglevel.critical);
                throw new WebException("Error during method call", e);
            }

            return(null);
        }
        private static mod_steam_core_data getLocalData()
        {
            mod_steam_core_data localData = (mod_steam_core_data)Roboto.Settings.getPluginData <mod_steam_core_data>();

            return(localData);
        }