SHARE
TWEET

Steam API Sample - by ollydbg

a guest Feb 16th, 2014 750 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Xml.Serialization;
  8.  
  9.  
  10. namespace SteamAPI
  11. {
  12.     #region Serializacion data models
  13.  
  14.     public class achievement
  15.     {
  16.         public string apiname;
  17.         public int achieved;
  18.     }
  19.  
  20.     public class playerstats
  21.     {
  22.         public string steamID;
  23.         public string gameName;
  24.         public List<achievement> achievements;
  25.     }
  26.  
  27.     #endregion
  28.  
  29.     #region events
  30.  
  31.     public class SteamArgs : EventArgs
  32.     {
  33.         public playerstats value { get; set; }
  34.     }
  35.  
  36.     #endregion
  37.  
  38.     public class Steam
  39.     {
  40.         #region fields
  41.  
  42.         // Url de la api
  43.         private string SteamAPIURL =
  44.     "http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid={0}&key={1}&steamid={2}&format=xml";
  45.        
  46.         // key para usar la api
  47.         private string steam_key = "<pon tu key de la api de Steam aqui>";
  48.  
  49.         // web client para obtener el XML
  50.         WebClient client = new WebClient();
  51.  
  52.         // variable pública que contiene toda la información de los logros
  53.         public playerstats SteamPlayerAchievementsStats;
  54.  
  55.         // Eventos
  56.         public delegate void SteamEventHandler(SteamArgs e);
  57.         public event EventHandler GetStatsCompleted;
  58.  
  59.         #endregion
  60.  
  61.         #region ctor
  62.  
  63.         public Steam()
  64.         {
  65.             this.client.Proxy = null;
  66.             this.client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
  67.         }
  68.  
  69.         #endregion
  70.  
  71.         #region Descarga (asíncrona) completada
  72.  
  73.         void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
  74.         {
  75.             try
  76.             {
  77.                 /* Nota: otra posibilidad en lugar de usar un XmlSerializer sería usar un XDocument de esta forma:                
  78.                    XDocument xdoc = XDocument.Parse(System.Text.Encoding.UTF8.GetString(e.Result), LoadOptions.None);
  79.                    (para usar XDocument hay que importar ---> using System.Xml.Linq;)
  80.                 */
  81.                  
  82.                 XmlSerializer serializer = new XmlSerializer(typeof(playerstats));
  83.                 SteamPlayerAchievementsStats = (playerstats)serializer.Deserialize(new MemoryStream(e.Result));
  84.  
  85.                 if (GetStatsCompleted != null)
  86.                     GetStatsCompleted(this, new SteamArgs() { value = SteamPlayerAchievementsStats });
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 if (GetStatsCompleted != null)
  91.                     GetStatsCompleted(this, new SteamArgs() { value = null });
  92.             }
  93.         }
  94.  
  95.         #endregion
  96.  
  97.         #region Propiedades públicas para consultar en el evento GetStatsCompleted
  98.  
  99.         public int? TotalGameAchievements()
  100.         {
  101.             if (SteamPlayerAchievementsStats != null)
  102.             {
  103.                 return SteamPlayerAchievementsStats.achievements.Count;
  104.             }
  105.  
  106.             return null;
  107.         }
  108.  
  109.         public int? TotalGameAchievementsCompleted()
  110.         {
  111.             if (SteamPlayerAchievementsStats != null)
  112.             {
  113.                 return SteamPlayerAchievementsStats.achievements.Count(c => c.achieved == 1);
  114.             }
  115.  
  116.             return null;
  117.         }
  118.  
  119.         #endregion
  120.  
  121.         #region Método público para la obtención de los datos
  122.  
  123.         /// <summary>
  124.         /// Obtiene los stats de un juego. Descarga la información de forma asíncrona y finalmente se
  125.         /// dispara el evento GetStatsCompleted donde se podrán consultar los resultados
  126.         /// </summary>
  127.         /// <param name="appid">ID del juego de steam<</param>
  128.         /// <param name="steamid">ID del usuario de steam</param>
  129.         public void GetStats(string appid, string steamid)
  130.         {
  131.             client.DownloadDataAsync(new Uri(string.Format(SteamAPIURL, appid, steam_key, steamid)));
  132.         }
  133.  
  134.         #endregion
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top