ApiHandler.cs 5.6 KB

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