GameHandler.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Windows.Devices.Geolocation;
  7. using Windows.Devices.Geolocation.Geofencing;
  8. using Windows.Foundation;
  9. using Windows.Services.Maps;
  10. using Windows.UI.Xaml.Media.Imaging;
  11. using YJMPD_UWP.Helpers;
  12. using YJMPD_UWP.Helpers.EventArgs;
  13. using YJMPD_UWP.Model.Object;
  14. using YJMPD_UWP.Views;
  15. namespace YJMPD_UWP.Model
  16. {
  17. public class GameHandler
  18. {
  19. public delegate void OnStatusUpdateHandler(object sender, GameStatusUpdatedEventArgs e);
  20. public event OnStatusUpdateHandler OnStatusUpdate;
  21. public delegate void OnPlayersUpdateHandler(object sender, GamePlayersUpdatedEventArgs e);
  22. public event OnPlayersUpdateHandler OnPlayersUpdate;
  23. public delegate void OnDestinationEnteredHandler(object sender, EventArgs e);
  24. public event OnDestinationEnteredHandler OnDestinationEnter;
  25. public delegate void OnDestinationLeftHandler(object sender, EventArgs e);
  26. public event OnDestinationLeftHandler OnDestinationLeave;
  27. public enum GameStatus { STARTED, SEARCHING, WAITING, ENDED, STOPPED }
  28. public GameStatus Status { get; private set; }
  29. public List<Player> Players { get; private set; }
  30. public bool Selected { get; private set; }
  31. private void UpdateGameStatus(GameStatus status)
  32. {
  33. Status = status;
  34. if (Status == GameStatus.STARTED)
  35. App.Geo.KeepHistory();
  36. else
  37. App.Geo.ClearHistory();
  38. if (OnStatusUpdate == null) return;
  39. OnStatusUpdate(this, new GameStatusUpdatedEventArgs(status));
  40. }
  41. private void UpdateGamePlayers(Player player)
  42. {
  43. if (OnPlayersUpdate == null) return;
  44. OnPlayersUpdate(this, new GamePlayersUpdatedEventArgs(player));
  45. }
  46. private void UpdateDestinationEnter()
  47. {
  48. if (OnDestinationEnter == null) return;
  49. OnDestinationEnter(this, new EventArgs());
  50. }
  51. private void UpdateDestinationLeave()
  52. {
  53. if (OnDestinationLeave == null) return;
  54. OnDestinationLeave(this, new EventArgs());
  55. }
  56. public GameHandler()
  57. {
  58. Players = new List<Player>();
  59. Status = GameStatus.STOPPED;
  60. App.Photo.OnStatusUpdate += Photo_OnStatusUpdate;
  61. GeofenceMonitor.Current.GeofenceStateChanged += Current_GeofenceStateChanged;
  62. }
  63. private void Current_GeofenceStateChanged(GeofenceMonitor sender, object args)
  64. {
  65. if (Status != GameStatus.STARTED) return;
  66. var reports = sender.ReadReports();
  67. foreach (GeofenceStateChangeReport report in reports)
  68. {
  69. GeofenceState state = report.NewState;
  70. Geofence geofence = report.Geofence;
  71. if (geofence.Id != "destination") continue;
  72. else if (state == GeofenceState.Entered)
  73. {
  74. UpdateDestinationEnter();
  75. if (!Selected)
  76. App.Api.DestinationReached();
  77. }
  78. else if (state == GeofenceState.Exited)
  79. {
  80. UpdateDestinationLeave();
  81. if (Selected)
  82. Util.ShowToastNotification("Left Area", "Please return to your location!");
  83. }
  84. }
  85. }
  86. private void Photo_OnStatusUpdate(object sender, PhotoStatusUpdatedEventArgs e)
  87. {
  88. switch(e.Status)
  89. {
  90. case PhotoHandler.PhotoStatus.UPLOADING:
  91. App.Navigate(typeof(WaitingView), "Uploading...");
  92. break;
  93. case PhotoHandler.PhotoStatus.DONE:
  94. if(Selected)
  95. App.Api.SendPhoto(App.Photo.Photo);
  96. break;
  97. }
  98. }
  99. public void AddPlayer(string username)
  100. {
  101. Player p = new Player(username);
  102. Players.Add(p);
  103. UpdateGamePlayers(p);
  104. }
  105. public void SetSelected(bool b)
  106. {
  107. if (b)
  108. {
  109. Selected = true;
  110. Settings.Statistics.Selected += 1;
  111. }
  112. else
  113. Selected = false;
  114. }
  115. public void MoveToWaiting()
  116. {
  117. UpdateGameStatus(GameStatus.WAITING);
  118. }
  119. public void MoveToStarted(BasicGeoposition bgps)
  120. {
  121. App.Navigate(typeof(MatchView));
  122. GeofenceMonitor.Current.Geofences.Add(new Geofence("destination", new Geocircle(bgps, 50), MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited, false, TimeSpan.FromSeconds(1)));
  123. UpdateGameStatus(GameStatus.STARTED);
  124. }
  125. public void RemovePlayer(string username)
  126. {
  127. for(int i=Players.Count-1; i>=0; i--)
  128. {
  129. if (Players[i].Username == username)
  130. {
  131. UpdateGamePlayers(Players[i]);
  132. Players.RemoveAt(i);
  133. return;
  134. }
  135. }
  136. }
  137. public void Reset()
  138. {
  139. App.Photo.Reset();
  140. Selected = false;
  141. Players.Clear();
  142. GeofenceMonitor.Current.Geofences.Clear();
  143. UpdateGamePlayers(null);
  144. App.Navigate(typeof(GameView));
  145. }
  146. public void UpdatePlayer(string username, double pointstotal, double points)
  147. {
  148. foreach(Player p in Players)
  149. {
  150. if(p.Username == username)
  151. {
  152. p.Update(pointstotal, points);
  153. UpdateGamePlayers(p);
  154. return;
  155. }
  156. }
  157. }
  158. public Player GetPlayer(string username)
  159. {
  160. foreach(Player p in Players)
  161. {
  162. if(p.Username == username)
  163. return p;
  164. }
  165. return null;
  166. }
  167. //Ending
  168. public async Task<bool> StopMatch()
  169. {
  170. CalculateDistanceWalked();
  171. Settings.Statistics.Matches += 1;
  172. Settings.Statistics.AddPoints(GetPlayer(Settings.Username).Points);
  173. GeofenceMonitor.Current.Geofences.Clear();
  174. Selected = false;
  175. App.Navigate(typeof(ScoreView));
  176. UpdateGameStatus(GameStatus.ENDED);
  177. return true;
  178. }
  179. public async void CalculateDistanceWalked()
  180. {
  181. MapRoute r = await Util.FindWalkingRoute(App.Geo.History.Select(p => p.Coordinate.Point).ToList());
  182. Settings.Statistics.Distance += r.LengthInMeters;
  183. }
  184. //Starting and Stopping
  185. public async Task<bool> StartGame()
  186. {
  187. UpdateGameStatus(GameStatus.SEARCHING);
  188. return await App.Api.JoinGame();
  189. }
  190. public async Task<bool> StopGame()
  191. {
  192. App.Api.LeaveGame();
  193. Reset();
  194. UpdateGameStatus(GameStatus.STOPPED);
  195. return true;
  196. }
  197. public void BackToGame()
  198. {
  199. switch(Status)
  200. {
  201. default:
  202. case GameStatus.STOPPED:
  203. break;
  204. case GameStatus.SEARCHING:
  205. App.Navigate(typeof(GameView));
  206. break;
  207. case GameStatus.WAITING:
  208. if (Selected)
  209. App.Navigate(typeof(PhotoView));
  210. else
  211. App.Navigate(typeof(WaitingView));
  212. break;
  213. case GameStatus.STARTED:
  214. App.Navigate(typeof(GameView));
  215. break;
  216. case GameStatus.ENDED:
  217. App.Navigate(typeof(ScoreView));
  218. break;
  219. }
  220. }
  221. }
  222. }