ApiHandler.cs 3.2 KB

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