GameVM.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using YJMPD_UWP.Model.Object;
  4. namespace YJMPD_UWP.ViewModels
  5. {
  6. public class GameVM : TemplateVM
  7. {
  8. public GameVM() : base("Game")
  9. {
  10. App.Geo.OnStatusUpdate += Geo_OnStatusUpdate;
  11. App.Network.OnStatusUpdate += Network_OnStatusUpdate;
  12. App.Game.OnStatusUpdate += Game_OnStatusUpdate;
  13. App.Game.OnPlayersUpdate += Game_OnPlayersUpdate;
  14. }
  15. private void Game_OnPlayersUpdate(object sender, Helpers.EventArgs.GamePlayersUpdatedEventArgs e)
  16. {
  17. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  18. {
  19. NotifyPropertyChanged(nameof(Players));
  20. NotifyPropertyChanged(nameof(PlayersCount));
  21. });
  22. }
  23. private void Game_OnStatusUpdate(object sender, Helpers.EventArgs.GameStatusUpdatedEventArgs e)
  24. {
  25. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  26. {
  27. NotifyPropertyChanged(nameof(StartGame));
  28. NotifyPropertyChanged(nameof(StopGame));
  29. });
  30. }
  31. private void Network_OnStatusUpdate(object sender, Helpers.EventArgs.NetworkStatusUpdatedEventArgs e)
  32. {
  33. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  34. {
  35. NotifyPropertyChanged(nameof(GameAvailable));
  36. NotifyPropertyChanged(nameof(ServerAvailable));
  37. NotifyPropertyChanged(nameof(ServerMessage));
  38. });
  39. }
  40. private void Geo_OnStatusUpdate(object sender, Helpers.EventArgs.PositionStatusUpdatedEventArgs e)
  41. {
  42. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  43. {
  44. NotifyPropertyChanged(nameof(GameAvailable));
  45. NotifyPropertyChanged(nameof(ServerAvailable));
  46. NotifyPropertyChanged(nameof(ServerMessage));
  47. });
  48. }
  49. public List<Player> Players
  50. {
  51. get
  52. {
  53. return new List<Player>(App.Game.Players);
  54. }
  55. }
  56. public string PlayersCount
  57. {
  58. get
  59. {
  60. if (App.Game.Players.Count == 1)
  61. return "There is currently " + App.Game.Players.Count + " player in the game.";
  62. else
  63. return "There are currently " + App.Game.Players.Count + " players in the game.";
  64. }
  65. }
  66. public bool GameAvailable
  67. {
  68. get
  69. {
  70. return App.Geo.Status == Windows.Devices.Geolocation.PositionStatus.Ready && App.Network.Status == Model.NetworkHandler.NetworkStatus.CONNECTED;
  71. }
  72. }
  73. public bool ServerAvailable
  74. {
  75. get
  76. {
  77. return !GameAvailable;
  78. }
  79. }
  80. public string ServerMessage
  81. {
  82. get
  83. {
  84. string str = "";
  85. switch (App.Network.Status)
  86. {
  87. case Model.NetworkHandler.NetworkStatus.DISCONNECTED:
  88. str = "Disconnected";
  89. break;
  90. case Model.NetworkHandler.NetworkStatus.CONNECTING:
  91. str = "Connecting to server...";
  92. break;
  93. }
  94. switch(App.Geo.Status)
  95. {
  96. case Windows.Devices.Geolocation.PositionStatus.Disabled:
  97. case Windows.Devices.Geolocation.PositionStatus.NotAvailable:
  98. case Windows.Devices.Geolocation.PositionStatus.NoData:
  99. str = "GPS not available";
  100. break;
  101. case Windows.Devices.Geolocation.PositionStatus.NotInitialized:
  102. case Windows.Devices.Geolocation.PositionStatus.Initializing:
  103. str = "Waiting on GPS...";
  104. break;
  105. }
  106. if (str == "")
  107. str = "Connected";
  108. return str;
  109. }
  110. }
  111. public bool StartGame
  112. {
  113. get
  114. {
  115. return App.Game.Status == Model.GameHandler.GameStatus.STOPPED;
  116. }
  117. }
  118. public bool StopGame
  119. {
  120. get
  121. {
  122. return !StartGame;
  123. }
  124. }
  125. }
  126. }