/Source/ReachStats.TestConsole/Program.cs

# · C# · 225 lines · 168 code · 38 blank · 19 comment · 11 complexity · 5dcc938fc9599e7bd837627679b63647 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using HaloReachApi;
  7. using Service4u2.Common;
  8. using HaloReachApi.Responses;
  9. namespace HaloReachApi.TestConsole
  10. {
  11. class Program
  12. {
  13. private static string apiKey = "[YOURAPIKEY]";
  14. static void Main(string[] args)
  15. {
  16. // Remember to set your Halo API Key in the AppSettings, or modify the code to specify your api key in the service constructors.
  17. //TestGameMetadataService();
  18. //TestGameHistoryService();
  19. //TestGameDetailService();
  20. GameMetadataExample();
  21. PlayerDetailsExample();
  22. var k = Console.ReadKey();
  23. }
  24. private static void GameMetadataExample()
  25. {
  26. // Create the metadata service, passing in our api key.
  27. // NOTE: Alternatively, the default Constructor will look for a "HaloReachAPIKey" App Setting.
  28. var metadataService = new GameMetadataService(apiKey);
  29. bool completed = false;
  30. metadataService.GotError += (s, e) =>
  31. {
  32. Console.WriteLine("Metadata Service Error: {0}", e.Argument.Message);
  33. completed = true;
  34. };
  35. metadataService.GotResult += (s, e) =>
  36. {
  37. var response = e.Argument;
  38. Console.WriteLine("Metadata Service Response: {0}", response.reason);
  39. Console.WriteLine("\tCommendation Count: {0}", response.Data.AllCommendationsById.Count);
  40. Console.WriteLine("\tEnemy Count: {0}", response.Data.AllEnemiesById.Count);
  41. Console.WriteLine("\tMedal Count: {0}", response.Data.AllMedalsById.Count);
  42. Console.WriteLine("\tMaps Count: {0}", response.Data.AllMapsById.Count);
  43. Console.WriteLine("\tWeapons Count: {0}", response.Data.AllWeaponsById.Count);
  44. completed = true;
  45. };
  46. metadataService.GetGameDataAsync();
  47. // Waiting around for the service to return...
  48. var start = DateTime.Now;
  49. while (DateTime.Now.Subtract(start).TotalSeconds < 5)
  50. {
  51. if (completed)
  52. break;
  53. Console.WriteLine("Sleeping...");
  54. Thread.Sleep(1000);
  55. }
  56. }
  57. /// <summary>
  58. /// An example method for getting a players statistics and details.
  59. /// </summary>
  60. private static void PlayerDetailsExample()
  61. {
  62. // Using the default constructor will load your API Key from the "HaloReachAPIKey" App Setting.
  63. var playerDetailsService = new PlayerDetailsService(apiKey);
  64. bool completed = false;
  65. playerDetailsService.GotError += (s, e) =>
  66. {
  67. Console.WriteLine("Player Details Service Error: {0}", e.Argument.Message);
  68. completed = true;
  69. };
  70. playerDetailsService.GotResult += (s, e) =>
  71. {
  72. var response = e.Argument;
  73. if (response.Player == null)
  74. {
  75. Console.WriteLine("Player not found...");
  76. completed = true;
  77. return;
  78. }
  79. Console.WriteLine("Player Details: {0} ({1})", response.Player.gamertag, response.Player.service_tag);
  80. // Player model image (standard and Hi Res)
  81. Console.WriteLine("Player Model Url: http://bungie.net" + response.PlayerModelUrl);
  82. Console.WriteLine("Player Model Url (Hi-Res): http://bungie.net" + response.PlayerModelUrlHiRes);
  83. // Challenges information
  84. Console.WriteLine("Daily Challenges Completed: {0}", response.Player.daily_challenges_completed);
  85. Console.WriteLine("Weekly Challenges Completed: {0}", response.Player.weekly_challenges_completed);
  86. // Last game type played
  87. Console.WriteLine("Last Game Type Played: {0}", response.Player.LastGameVariantClassPlayed);
  88. if (e.Argument.StatisticsByMap != null)
  89. {
  90. foreach (var mapStat in e.Argument.StatisticsByMap)
  91. {
  92. // Statistics for last played maps.
  93. Console.WriteLine("Map Id: {0}", mapStat.MapId);
  94. Console.WriteLine("\tTotal Kills: {0}", mapStat.total_kills);
  95. Console.WriteLine("\tTotal Deaths: {0}", mapStat.total_deaths);
  96. Console.WriteLine("\tTotal Score: {0}", mapStat.total_score);
  97. Console.WriteLine("\tTotal Medals: {0}", mapStat.TotalMedals);
  98. }
  99. }
  100. completed = true;
  101. };
  102. // Ask for a gamertag to look up.
  103. Console.WriteLine("Enter a Gamertag: ");
  104. var gamerTag = Console.ReadLine();
  105. // Uncomment this to get details by map, but remember to comment out the NoStats call that is one line below.
  106. //playerDetailsService.GetPlayerDetailsWithStatsByMapAsync(gamerTag);
  107. playerDetailsService.GetPlayerDetailsNoStatsAsync(gamerTag);
  108. // Waiting around for the service to return... Takes about 10 seconds for player details calls
  109. var start = DateTime.Now;
  110. while (DateTime.Now.Subtract(start).TotalSeconds < 20)
  111. {
  112. if (completed)
  113. break;
  114. Console.WriteLine("Sleeping...");
  115. Thread.Sleep(1000);
  116. }
  117. }
  118. private static void TestGameDetailService()
  119. {
  120. TestServiceCommon<GameDetailsService, GameDetailsResponse>(new GameDetailsService(),
  121. serv =>
  122. {
  123. Console.Write("GameId: ");
  124. var gameId = Console.ReadLine();
  125. serv.GetGameDetailsAsync(gameId);
  126. },
  127. (s, e) =>
  128. {
  129. Console.WriteLine("Game Time: {0}", e.Argument.GameDetails.GameTimestamp);
  130. Console.WriteLine("Players: ");
  131. foreach (var pl in e.Argument.GameDetails.Players)
  132. {
  133. Console.WriteLine(pl.PlayerDetail == null ? "[null]" : pl.PlayerDetail.gamertag);
  134. }
  135. });
  136. }
  137. private static void TestGameMetadataService()
  138. {
  139. TestServiceCommon<GameMetadataService, GameMetadataResponse>(new GameMetadataService(),
  140. serv => serv.GetGameDataAsync(),
  141. (s, e) => Console.WriteLine("Metadata Service Response: {0}, {1}", e.Argument.status, e.Argument.Data.AllCommendationsById.Count));
  142. }
  143. private static void TestGameHistoryService()
  144. {
  145. TestServiceCommon<GameHistoryService, GameHistoryResponse>(new GameHistoryService(),
  146. serv =>
  147. {
  148. Console.Write("Gametag for history: ");
  149. var gamer = Console.ReadLine();
  150. serv.GetGameHistoryAsync(gamer);
  151. },
  152. (s, e) =>
  153. {
  154. foreach (var gm in e.Argument.RecentGames)
  155. {
  156. Console.WriteLine(String.Format("Game Id: {3}\r\n\tScore: {0}\r\n\tKills: {1}\r\n\tRating: {2}", gm.RequestedPlayerScore, gm.RequestedPlayerKills, gm.RequestedPlayerRating, gm.GameId));
  157. }
  158. });
  159. }
  160. private static void TestServiceCommon<TService, TResponse>(TService service, Action<TService> startAction, Action<object, EventArgs<TResponse>> resultAction)
  161. where TResponse : new()
  162. where TService : BaseReachApiJsonService<TResponse>
  163. {
  164. var serv = service;
  165. bool completed = false;
  166. serv.GotError += (s, e) =>
  167. {
  168. Console.WriteLine("Metadata Service Error: {0}", e.Argument.Message);
  169. completed = true;
  170. };
  171. serv.GotResult += (s, e) =>
  172. {
  173. resultAction(s, e);
  174. completed = true;
  175. };
  176. startAction(serv);
  177. var start = DateTime.Now;
  178. while (DateTime.Now.Subtract(start).TotalSeconds < 5)
  179. {
  180. if (completed)
  181. break;
  182. Console.WriteLine("Sleeping...");
  183. Thread.Sleep(1000);
  184. }
  185. }
  186. }
  187. }