ApiHandler.cs 6.1 KB

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