ApiHandler.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace YJMPD_UWP.Model
  11. {
  12. public class ApiHandler
  13. {
  14. public delegate void OnGameFoundHandler(object sender, EventArgs e);
  15. public event OnGameFoundHandler OnGameFound;
  16. public enum Command
  17. {
  18. Hi,
  19. Name,
  20. Picture,
  21. GameFound,
  22. PlayerJoined
  23. }
  24. public ApiHandler()
  25. {
  26. }
  27. public void HandleMessage(JObject o)
  28. {
  29. Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
  30. switch(c)
  31. {
  32. case Command.GameFound:
  33. GameFound();
  34. break;
  35. case Command.PlayerJoined:
  36. PlayerJoined(o["msg"].ToString());
  37. break;
  38. default:
  39. //Do nothing
  40. break;
  41. }
  42. }
  43. private void PlayerJoined(string username)
  44. {
  45. throw new NotImplementedException();
  46. //Event will be handled by the game manager
  47. App.Game.AddPlayer(username);
  48. }
  49. private void GameFound()
  50. {
  51. throw new NotImplementedException();
  52. if (OnGameFound == null) return;
  53. OnGameFound(this, new EventArgs());
  54. }
  55. public JObject Message(Command c, string msg)
  56. {
  57. return JObject.FromObject(new
  58. {
  59. command = c.ToString(),
  60. msg = msg
  61. });
  62. }
  63. //API stuff
  64. public async Task<bool> SearchGame()
  65. {
  66. JObject obj = JObject.FromObject(new
  67. {
  68. command = Command.Name.ToString(),
  69. name = Settings.Username
  70. });
  71. Debug.WriteLine(obj.ToString(Formatting.None));
  72. await App.Network.Write(obj.ToString(Formatting.None));
  73. return true;
  74. }
  75. }
  76. }