GameHandler.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. Selected = false;
  61. App.Photo.OnStatusUpdate += Photo_OnStatusUpdate;
  62. GeofenceMonitor.Current.GeofenceStateChanged += Current_GeofenceStateChanged;
  63. }
  64. private void Current_GeofenceStateChanged(GeofenceMonitor sender, object args)
  65. {
  66. if (Status != GameStatus.STARTED) return;
  67. var reports = sender.ReadReports();
  68. foreach (GeofenceStateChangeReport report in reports)
  69. {
  70. GeofenceState state = report.NewState;
  71. Geofence geofence = report.Geofence;
  72. if (geofence.Id != "destination") continue;
  73. else if (state == GeofenceState.Entered)
  74. {
  75. UpdateDestinationEnter();
  76. if (!Selected)
  77. App.Api.DestinationReached();
  78. }
  79. else if (state == GeofenceState.Exited)
  80. {
  81. UpdateDestinationLeave();
  82. if (Selected)
  83. Util.ShowToastNotification("Left Area", "Please return to your location!");
  84. }
  85. }
  86. }
  87. private void Photo_OnStatusUpdate(object sender, PhotoStatusUpdatedEventArgs e)
  88. {
  89. switch(e.Status)
  90. {
  91. case PhotoHandler.PhotoStatus.UPLOADING:
  92. App.Navigate(typeof(WaitingView), "Uploading...");
  93. break;
  94. case PhotoHandler.PhotoStatus.DONE:
  95. if(Selected)
  96. App.Api.SendPhoto(App.Photo.Photo);
  97. break;
  98. }
  99. }
  100. public void AddPlayer(string username)
  101. {
  102. Player p = new Player(username);
  103. Players.Add(p);
  104. UpdateGamePlayers(p);
  105. }
  106. public void SetSelected(bool b)
  107. {
  108. if (b)
  109. {
  110. Selected = true;
  111. Settings.Statistics.Selected += 1;
  112. }
  113. else
  114. Selected = false;
  115. }
  116. public void MoveToWaiting()
  117. {
  118. UpdateGameStatus(GameStatus.WAITING);
  119. }
  120. public void MoveToStarted(BasicGeoposition bgps)
  121. {
  122. App.Navigate(typeof(MatchView));
  123. GeofenceMonitor.Current.Geofences.Add(new Geofence("destination", new Geocircle(bgps, 50), MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited, false, TimeSpan.FromSeconds(1)));
  124. UpdateGameStatus(GameStatus.STARTED);
  125. }
  126. public void RemovePlayer(string username)
  127. {
  128. for(int i=Players.Count-1; i>=0; i--)
  129. {
  130. if (Players[i].Username == username)
  131. {
  132. UpdateGamePlayers(Players[i]);
  133. Players.RemoveAt(i);
  134. return;
  135. }
  136. }
  137. }
  138. public void Reset()
  139. {
  140. App.Photo.Reset();
  141. Selected = false;
  142. Players.Clear();
  143. GeofenceMonitor.Current.Geofences.Clear();
  144. UpdateGamePlayers(null);
  145. App.Navigate(typeof(GameView));
  146. }
  147. public void UpdatePlayer(string username, double pointstotal, double points)
  148. {
  149. foreach(Player p in Players)
  150. {
  151. if(p.Username == username)
  152. {
  153. p.Update(pointstotal, points);
  154. UpdateGamePlayers(p);
  155. return;
  156. }
  157. }
  158. }
  159. public Player GetPlayer(string username)
  160. {
  161. foreach(Player p in Players)
  162. {
  163. if(p.Username == username)
  164. return p;
  165. }
  166. return null;
  167. }
  168. //Ending
  169. public async Task<bool> StopMatch()
  170. {
  171. CalculateDistanceWalked();
  172. Settings.Statistics.Matches += 1;
  173. Settings.Statistics.AddPoints(GetPlayer(Settings.Username).Points);
  174. GeofenceMonitor.Current.Geofences.Clear();
  175. Selected = false;
  176. App.Navigate(typeof(ScoreView));
  177. UpdateGameStatus(GameStatus.ENDED);
  178. return true;
  179. }
  180. public async void CalculateDistanceWalked()
  181. {
  182. MapRoute r = await Util.FindWalkingRoute(App.Geo.History.Select(p => p.Coordinate.Point).ToList());
  183. Settings.Statistics.Distance += r.LengthInMeters;
  184. }
  185. //Starting and Stopping
  186. public async Task<bool> StartGame()
  187. {
  188. UpdateGameStatus(GameStatus.SEARCHING);
  189. return await App.Api.JoinGame();
  190. }
  191. public async Task<bool> StopGame()
  192. {
  193. App.Api.LeaveGame();
  194. Reset();
  195. UpdateGameStatus(GameStatus.STOPPED);
  196. return true;
  197. }
  198. public void BackToGame()
  199. {
  200. switch(Status)
  201. {
  202. default:
  203. case GameStatus.STOPPED:
  204. break;
  205. case GameStatus.SEARCHING:
  206. App.Navigate(typeof(GameView));
  207. break;
  208. case GameStatus.WAITING:
  209. if (Selected)
  210. App.Navigate(typeof(PhotoView));
  211. else
  212. App.Navigate(typeof(WaitingView));
  213. break;
  214. case GameStatus.STARTED:
  215. App.Navigate(typeof(GameView));
  216. break;
  217. case GameStatus.ENDED:
  218. App.Navigate(typeof(ScoreView));
  219. break;
  220. }
  221. }
  222. }
  223. }