ApiHandler.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. StopGame
  29. }
  30. public ApiHandler()
  31. {
  32. }
  33. public void HandleMessage(JObject o)
  34. {
  35. Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
  36. switch (c)
  37. {
  38. case Command.Hi:
  39. Debug.WriteLine("Keep alive message received!");
  40. SendHi();
  41. break;
  42. case Command.PlayerJoined:
  43. Debug.WriteLine("Played joined");
  44. PlayerJoined(o[Command.PlayerJoined.ToString()].ToString());
  45. break;
  46. case Command.PlayerRemoved:
  47. Debug.WriteLine("Played removed");
  48. PlayerRemoved(o[Command.PlayerRemoved.ToString()].ToString());
  49. break;
  50. case Command.Picture:
  51. if (o["selected"].ToObject<bool>() == true)
  52. {
  53. App.Game.SetSelected(true);
  54. App.Navigate(typeof(PhotoView));
  55. }
  56. else
  57. App.Navigate(typeof(WaitingView), "Waiting on photo...");
  58. App.Game.MoveToWaiting();
  59. break;
  60. case Command.PictureUrl:
  61. if (!App.Game.Selected)
  62. App.Photo.SetPhoto(o[Command.PictureUrl.ToString()].ToString());
  63. double lat = (double)o["lat"];
  64. double lon = (double)o["lon"];
  65. BasicGeoposition bgps = new BasicGeoposition() { Latitude = lat, Longitude = lon };
  66. App.Game.MoveToStarted(bgps);
  67. break;
  68. case Command.GameEnded:
  69. string winner = o["winner"].ToString();
  70. if (winner == Settings.Username)
  71. winner = "You";
  72. Util.ShowToastNotification(winner + " won!", "Press Ready or Leave");
  73. foreach(var i in (JObject) o["players"])
  74. {
  75. Debug.WriteLine(i.Key);
  76. Debug.WriteLine(i.Value["points"]);
  77. string username = i.Key;
  78. double points = i.Value["points"].ToObject<Double>();
  79. double pointstotal = i.Value["pointstotal"].ToObject<Double>();
  80. App.Game.UpdatePlayer(username, pointstotal, points);
  81. }
  82. App.Game.StopMatch();
  83. break;
  84. case Command.StopGame:
  85. if (App.Game.Status != GameHandler.GameStatus.STOPPED)
  86. {
  87. Util.ShowToastNotification("Game stopped", "A player has left the game.");
  88. App.Game.StopGame();
  89. }
  90. break;
  91. default:
  92. //Do nothing
  93. break;
  94. }
  95. }
  96. private void PlayerJoined(string username)
  97. {
  98. //Event will be handled by the game manager
  99. App.Game.AddPlayer(username);
  100. }
  101. private void PlayerRemoved(string username)
  102. {
  103. //Event will be handled by the game manager
  104. App.Game.RemovePlayer(username);
  105. }
  106. public JObject Message(Command c, string msg)
  107. {
  108. return JObject.FromObject(new
  109. {
  110. command = c.ToString(),
  111. msg = msg
  112. });
  113. }
  114. public async Task SendHi()
  115. {
  116. JObject obj = JObject.FromObject(new { command = Command.Hi.ToString() });
  117. await App.Network.Write(obj.ToString(Formatting.None));
  118. }
  119. //API stuff
  120. public async Task<bool> JoinGame()
  121. {
  122. JObject obj = JObject.FromObject(new
  123. {
  124. command = Command.Name.ToString(),
  125. name = Settings.Username,
  126. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  127. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  128. });
  129. await App.Network.Write(obj.ToString(Formatting.None));
  130. return true;
  131. }
  132. public async Task<bool> LeaveGame()
  133. {
  134. JObject obj = JObject.FromObject(new
  135. {
  136. command = Command.PlayerRemoved.ToString(),
  137. name = Settings.Username
  138. });
  139. await App.Network.Write(obj.ToString(Formatting.None));
  140. return true;
  141. }
  142. public async Task<bool> SendPhoto(string url)
  143. {
  144. JObject obj = JObject.FromObject(new
  145. {
  146. command = Command.PictureUrl.ToString(),
  147. PictureUrl = url,
  148. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  149. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  150. });
  151. await App.Network.Write(obj.ToString(Formatting.None));
  152. return true;
  153. }
  154. public async Task<bool> DestinationReached()
  155. {
  156. JObject obj = JObject.FromObject(new
  157. {
  158. command = Command.DestinationReached.ToString(),
  159. username = Settings.Username
  160. });
  161. await App.Network.Write(obj.ToString(Formatting.None));
  162. return true;
  163. }
  164. public async Task<bool> Ready()
  165. {
  166. JObject obj = JObject.FromObject(new
  167. {
  168. command = Command.PlayerReady.ToString(),
  169. ready = true
  170. });
  171. await App.Network.Write(obj.ToString(Formatting.None));
  172. return true;
  173. }
  174. }
  175. }