ApiHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. App.Game.SetSelected(true);
  46. App.Navigate(typeof(PhotoView));
  47. }
  48. else
  49. App.Navigate(typeof(WaitingView), "Waiting on photo...");
  50. App.Game.MoveToWaiting();
  51. break;
  52. case Command.PictureUrl:
  53. if (!App.Game.Selected)
  54. App.Photo.SetPhoto(o[Command.PictureUrl.ToString()].ToString());
  55. App.Game.MoveToStarted();
  56. break;
  57. default:
  58. //Do nothing
  59. break;
  60. }
  61. }
  62. private void PlayerJoined(string username)
  63. {
  64. //Event will be handled by the game manager
  65. App.Game.AddPlayer(username);
  66. }
  67. private void PlayerRemoved(string username)
  68. {
  69. //Event will be handled by the game manager
  70. App.Game.RemovePlayer(username);
  71. }
  72. public JObject Message(Command c, string msg)
  73. {
  74. return JObject.FromObject(new
  75. {
  76. command = c.ToString(),
  77. msg = msg
  78. });
  79. }
  80. //API stuff
  81. public async Task<bool> JoinGame()
  82. {
  83. JObject obj = JObject.FromObject(new
  84. {
  85. command = Command.Name.ToString(),
  86. name = Settings.Username,
  87. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  88. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  89. });
  90. await App.Network.Write(obj.ToString(Formatting.None));
  91. return true;
  92. }
  93. public async Task<bool> LeaveGame()
  94. {
  95. JObject obj = JObject.FromObject(new
  96. {
  97. command = Command.PlayerRemoved.ToString(),
  98. name = Settings.Username
  99. });
  100. await App.Network.Write(obj.ToString(Formatting.None));
  101. return true;
  102. }
  103. public async Task<bool> SendPhoto(string url)
  104. {
  105. JObject obj = JObject.FromObject(new
  106. {
  107. command = Command.PictureUrl.ToString(),
  108. pictureurl = url,
  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. }
  116. }