ApiHandler.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Windows.Devices.Geolocation;
  10. using YJMPD_UWP.Helpers;
  11. using YJMPD_UWP.Views;
  12. namespace YJMPD_UWP.Model
  13. {
  14. public class ApiHandler
  15. {
  16. public enum Command
  17. {
  18. Hi,
  19. Name,
  20. Picture,
  21. Msg,
  22. PlayerJoined,
  23. PlayerRemoved,
  24. PictureUrl,
  25. DestinationReached,
  26. GameEnded,
  27. PlayerReady
  28. }
  29. public ApiHandler()
  30. {
  31. }
  32. public void HandleMessage(JObject o)
  33. {
  34. Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
  35. switch (c)
  36. {
  37. case Command.Hi:
  38. Debug.WriteLine("Keep alive message received!");
  39. SendHi();
  40. break;
  41. case Command.PlayerJoined:
  42. Debug.WriteLine("Played joined");
  43. PlayerJoined(o[Command.PlayerJoined.ToString()].ToString());
  44. break;
  45. case Command.PlayerRemoved:
  46. Debug.WriteLine("Played removed");
  47. PlayerRemoved(o[Command.PlayerRemoved.ToString()].ToString());
  48. break;
  49. case Command.Picture:
  50. if (o["selected"].ToObject<bool>() == true)
  51. {
  52. App.Game.SetSelected(true);
  53. App.Navigate(typeof(PhotoView));
  54. }
  55. else
  56. App.Navigate(typeof(WaitingView), "Waiting on photo...");
  57. App.Game.MoveToWaiting();
  58. break;
  59. case Command.PictureUrl:
  60. if (!App.Game.Selected)
  61. App.Photo.SetPhoto(o[Command.PictureUrl.ToString()].ToString());
  62. double lat = (double)o["lat"];
  63. double lon = (double)o["lon"];
  64. BasicGeoposition bgps = new BasicGeoposition() { Latitude = lat, Longitude = lon };
  65. App.Game.MoveToStarted(bgps);
  66. break;
  67. case Command.GameEnded:
  68. string winner = o["winner"].ToString();
  69. if (winner == Settings.Username)
  70. winner = "You";
  71. Util.ShowToastNotification(winner + " won!", "Press Ready or Leave");
  72. foreach(var i in (JObject) o["players"])
  73. {
  74. Debug.WriteLine(i.Key);
  75. Debug.WriteLine(i.Value["points"]);
  76. string username = i.Key;
  77. double points = i.Value["points"].ToObject<Double>();
  78. double pointstotal = i.Value["pointstotal"].ToObject<Double>();
  79. App.Game.UpdatePlayer(username, pointstotal, points);
  80. }
  81. App.Game.StopMatch();
  82. break;
  83. default:
  84. //Do nothing
  85. break;
  86. }
  87. }
  88. private void PlayerJoined(string username)
  89. {
  90. //Event will be handled by the game manager
  91. App.Game.AddPlayer(username);
  92. }
  93. private void PlayerRemoved(string username)
  94. {
  95. //Event will be handled by the game manager
  96. App.Game.RemovePlayer(username);
  97. }
  98. public JObject Message(Command c, string msg)
  99. {
  100. return JObject.FromObject(new
  101. {
  102. command = c.ToString(),
  103. msg = msg
  104. });
  105. }
  106. public async Task SendHi()
  107. {
  108. JObject obj = JObject.FromObject(new { command = Command.Hi.ToString() });
  109. await App.Network.Write(obj.ToString(Formatting.None));
  110. }
  111. //API stuff
  112. public async Task<bool> JoinGame()
  113. {
  114. JObject obj = JObject.FromObject(new
  115. {
  116. command = Command.Name.ToString(),
  117. name = Settings.Username,
  118. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  119. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  120. });
  121. await App.Network.Write(obj.ToString(Formatting.None));
  122. return true;
  123. }
  124. public async Task<bool> LeaveGame()
  125. {
  126. JObject obj = JObject.FromObject(new
  127. {
  128. command = Command.PlayerRemoved.ToString(),
  129. name = Settings.Username
  130. });
  131. await App.Network.Write(obj.ToString(Formatting.None));
  132. return true;
  133. }
  134. public async Task<bool> SendPhoto(string url)
  135. {
  136. JObject obj = JObject.FromObject(new
  137. {
  138. command = Command.PictureUrl.ToString(),
  139. PictureUrl = url,
  140. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  141. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  142. });
  143. await App.Network.Write(obj.ToString(Formatting.None));
  144. return true;
  145. }
  146. public async Task<bool> DestinationReached()
  147. {
  148. JObject obj = JObject.FromObject(new
  149. {
  150. command = Command.DestinationReached.ToString(),
  151. username = Settings.Username
  152. });
  153. await App.Network.Write(obj.ToString(Formatting.None));
  154. return true;
  155. }
  156. public async Task<bool> Ready()
  157. {
  158. JObject obj = JObject.FromObject(new
  159. {
  160. command = Command.PlayerReady.ToString(),
  161. ready = true
  162. });
  163. await App.Network.Write(obj.ToString(Formatting.None));
  164. return true;
  165. }
  166. }
  167. }