ApiHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. App.Game.StopGame();
  87. break;
  88. default:
  89. //Do nothing
  90. break;
  91. }
  92. }
  93. private void PlayerJoined(string username)
  94. {
  95. //Event will be handled by the game manager
  96. App.Game.AddPlayer(username);
  97. }
  98. private void PlayerRemoved(string username)
  99. {
  100. //Event will be handled by the game manager
  101. App.Game.RemovePlayer(username);
  102. }
  103. public JObject Message(Command c, string msg)
  104. {
  105. return JObject.FromObject(new
  106. {
  107. command = c.ToString(),
  108. msg = msg
  109. });
  110. }
  111. public async Task SendHi()
  112. {
  113. JObject obj = JObject.FromObject(new { command = Command.Hi.ToString() });
  114. await App.Network.Write(obj.ToString(Formatting.None));
  115. }
  116. //API stuff
  117. public async Task<bool> JoinGame()
  118. {
  119. JObject obj = JObject.FromObject(new
  120. {
  121. command = Command.Name.ToString(),
  122. name = Settings.Username,
  123. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  124. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  125. });
  126. await App.Network.Write(obj.ToString(Formatting.None));
  127. return true;
  128. }
  129. public async Task<bool> LeaveGame()
  130. {
  131. JObject obj = JObject.FromObject(new
  132. {
  133. command = Command.PlayerRemoved.ToString(),
  134. name = Settings.Username
  135. });
  136. await App.Network.Write(obj.ToString(Formatting.None));
  137. return true;
  138. }
  139. public async Task<bool> SendPhoto(string url)
  140. {
  141. JObject obj = JObject.FromObject(new
  142. {
  143. command = Command.PictureUrl.ToString(),
  144. PictureUrl = url,
  145. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  146. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  147. });
  148. await App.Network.Write(obj.ToString(Formatting.None));
  149. return true;
  150. }
  151. public async Task<bool> DestinationReached()
  152. {
  153. JObject obj = JObject.FromObject(new
  154. {
  155. command = Command.DestinationReached.ToString(),
  156. username = Settings.Username
  157. });
  158. await App.Network.Write(obj.ToString(Formatting.None));
  159. return true;
  160. }
  161. public async Task<bool> Ready()
  162. {
  163. JObject obj = JObject.FromObject(new
  164. {
  165. command = Command.PlayerReady.ToString(),
  166. ready = true
  167. });
  168. await App.Network.Write(obj.ToString(Formatting.None));
  169. return true;
  170. }
  171. }
  172. }