ApiHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //Download picture from URL and display to participants
  55. break;
  56. default:
  57. //Do nothing
  58. break;
  59. }
  60. }
  61. private void PlayerJoined(string username)
  62. {
  63. //Event will be handled by the game manager
  64. App.Game.AddPlayer(username);
  65. }
  66. private void PlayerRemoved(string username)
  67. {
  68. //Event will be handled by the game manager
  69. App.Game.RemovePlayer(username);
  70. }
  71. public JObject Message(Command c, string msg)
  72. {
  73. return JObject.FromObject(new
  74. {
  75. command = c.ToString(),
  76. msg = msg
  77. });
  78. }
  79. //API stuff
  80. public async Task<bool> JoinGame()
  81. {
  82. JObject obj = JObject.FromObject(new
  83. {
  84. command = Command.Name.ToString(),
  85. name = Settings.Username,
  86. lon = App.Geo.Position.Coordinate.Point.Position.Longitude,
  87. lat = App.Geo.Position.Coordinate.Point.Position.Latitude
  88. });
  89. Debug.WriteLine(obj.ToString(Formatting.None));
  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. Debug.WriteLine(obj.ToString(Formatting.None));
  101. await App.Network.Write(obj.ToString(Formatting.None));
  102. return true;
  103. }
  104. }
  105. }