ApiHandler.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. JArray players = (JArray)o["players"];
  69. foreach(JToken pl in players)
  70. {
  71. string username = o["username"].ToString();
  72. double points = (double)o["points"];
  73. double pointstotal = (double)o["pointstotal"];
  74. App.Game.UpdatePlayer(username, pointstotal, points);
  75. }
  76. App.Game.StopMatch();
  77. break;
  78. default:
  79. //Do nothing
  80. break;
  81. }
  82. }
  83. private void PlayerJoined(string username)
  84. {
  85. //Event will be handled by the game manager
  86. App.Game.AddPlayer(username);
  87. }
  88. private void PlayerRemoved(string username)
  89. {
  90. //Event will be handled by the game manager
  91. App.Game.RemovePlayer(username);
  92. }
  93. public JObject Message(Command c, string msg)
  94. {
  95. return JObject.FromObject(new
  96. {
  97. command = c.ToString(),
  98. msg = msg
  99. });
  100. }
  101. //API stuff
  102. public async Task<bool> JoinGame()
  103. {
  104. JObject obj = JObject.FromObject(new
  105. {
  106. command = Command.Name.ToString(),
  107. name = Settings.Username,
  108. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  109. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  110. });
  111. await App.Network.Write(obj.ToString(Formatting.None));
  112. return true;
  113. }
  114. public async Task<bool> LeaveGame()
  115. {
  116. JObject obj = JObject.FromObject(new
  117. {
  118. command = Command.PlayerRemoved.ToString(),
  119. name = Settings.Username
  120. });
  121. await App.Network.Write(obj.ToString(Formatting.None));
  122. return true;
  123. }
  124. public async Task<bool> SendPhoto(string url)
  125. {
  126. JObject obj = JObject.FromObject(new
  127. {
  128. command = Command.PictureUrl.ToString(),
  129. PictureUrl = url,
  130. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  131. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  132. });
  133. await App.Network.Write(obj.ToString(Formatting.None));
  134. return true;
  135. }
  136. public async Task<bool> DestinationReached()
  137. {
  138. JObject obj = JObject.FromObject(new
  139. {
  140. command = Command.DestinationReached.ToString(),
  141. username = Settings.Username
  142. });
  143. await App.Network.Write(obj.ToString(Formatting.None));
  144. return true;
  145. }
  146. public async Task<bool> Ready()
  147. {
  148. JObject obj = JObject.FromObject(new
  149. {
  150. command = Command.PlayerReady.ToString(),
  151. ready = true
  152. });
  153. await App.Network.Write(obj.ToString(Formatting.None));
  154. return true;
  155. }
  156. }
  157. }