ApiHandler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 YJMPD_UWP.Helpers;
  10. using YJMPD_UWP.Views;
  11. namespace YJMPD_UWP.Model
  12. {
  13. public class ApiHandler
  14. {
  15. public enum Command
  16. {
  17. Hi,
  18. Name,
  19. Picture,
  20. Msg,
  21. PlayerJoined,
  22. PlayerRemoved,
  23. PictureUrl
  24. }
  25. public ApiHandler()
  26. {
  27. }
  28. public void HandleMessage(JObject o)
  29. {
  30. Debug.WriteLine(o.ToString());
  31. Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
  32. switch (c)
  33. {
  34. case Command.PlayerJoined:
  35. Debug.WriteLine("Played joined");
  36. PlayerJoined(o[Command.PlayerJoined.ToString()].ToString());
  37. break;
  38. case Command.PlayerRemoved:
  39. Debug.WriteLine("Played removed");
  40. PlayerRemoved(o[Command.PlayerRemoved.ToString()].ToString());
  41. break;
  42. case Command.Picture:
  43. if (o["selected"].ToObject<bool>() == true)
  44. {
  45. Debug.WriteLine("Selected player taking picture");
  46. App.Game.SetSelected(true);
  47. App.Navigate(typeof(PhotoView));
  48. }
  49. else
  50. App.Navigate(typeof(WaitingView));
  51. App.Game.MoveToWaiting();
  52. break;
  53. case Command.PictureUrl:
  54. if (!App.Game.Selected)
  55. App.Photo.UpdatePhotoTaken(o[Command.PictureUrl.ToString()].ToString());
  56. App.Game.MoveToStarted();
  57. break;
  58. default:
  59. //Do nothing
  60. break;
  61. }
  62. }
  63. private void PlayerJoined(string username)
  64. {
  65. //Event will be handled by the game manager
  66. App.Game.AddPlayer(username);
  67. }
  68. private void PlayerRemoved(string username)
  69. {
  70. //Event will be handled by the game manager
  71. App.Game.RemovePlayer(username);
  72. }
  73. public JObject Message(Command c, string msg)
  74. {
  75. return JObject.FromObject(new
  76. {
  77. command = c.ToString(),
  78. msg = msg
  79. });
  80. }
  81. //API stuff
  82. public async Task<bool> JoinGame()
  83. {
  84. JObject obj = JObject.FromObject(new
  85. {
  86. command = Command.Name.ToString(),
  87. name = Settings.Username,
  88. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  89. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  90. });
  91. await App.Network.Write(obj.ToString(Formatting.None));
  92. return true;
  93. }
  94. public async Task<bool> LeaveGame()
  95. {
  96. JObject obj = JObject.FromObject(new
  97. {
  98. command = Command.PlayerRemoved.ToString(),
  99. name = Settings.Username
  100. });
  101. await App.Network.Write(obj.ToString(Formatting.None));
  102. return true;
  103. }
  104. public async Task<bool> SendPicture(string url)
  105. {
  106. JObject obj = JObject.FromObject(new
  107. {
  108. command = Command.PictureUrl.ToString(),
  109. pictureurl = url
  110. });
  111. await App.Network.Write(obj.ToString(Formatting.None));
  112. return true;
  113. }
  114. }
  115. }