From 260ed9ae78a20e2db4ffea1d9a385909e25fa04f Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Mon, 18 Jan 2016 22:35:55 +0100 Subject: [PATCH] Implement basic functionality and improved design --- YJMPD-UWP/App.xaml.cs | 3 + .../EventArgs/GamePlayersUpdatedEventArgs.cs | 20 ++ YJMPD-UWP/Helpers/Settings.cs | 37 +++- YJMPD-UWP/Helpers/Util.cs | 175 +++++------------- YJMPD-UWP/MainPage.xaml | 24 +-- YJMPD-UWP/MainPage.xaml.cs | 13 +- YJMPD-UWP/Model/CompassHandler.cs | 6 +- YJMPD-UWP/Model/GameHandler.cs | 40 ++-- YJMPD-UWP/Model/NetworkHandler.cs | 9 +- YJMPD-UWP/Model/Object/Statistics.cs | 31 ++++ YJMPD-UWP/Model/PhotoHandler.cs | 13 ++ YJMPD-UWP/Themes/DefaultStyles.xaml | 30 ++- YJMPD-UWP/ViewModels/AccountVM.cs | 10 - YJMPD-UWP/ViewModels/MainPageVM.cs | 28 ++- YJMPD-UWP/ViewModels/MatchVM.cs | 52 ++++++ YJMPD-UWP/ViewModels/SettingsVM.cs | 16 +- YJMPD-UWP/ViewModels/StatisticsVM.cs | 81 +++++++- YJMPD-UWP/Views/AboutView.xaml | 8 +- YJMPD-UWP/Views/AccountView.xaml | 13 -- YJMPD-UWP/Views/AccountView.xaml.cs | 17 -- YJMPD-UWP/Views/MatchView.xaml | 17 +- YJMPD-UWP/Views/MatchView.xaml.cs | 10 + YJMPD-UWP/Views/SettingsView.xaml | 11 +- YJMPD-UWP/Views/StatisticsView.xaml | 46 ++++- YJMPD-UWP/YJMPD-UWP.csproj | 13 +- 25 files changed, 475 insertions(+), 248 deletions(-) create mode 100644 YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs create mode 100644 YJMPD-UWP/Model/Object/Statistics.cs create mode 100644 YJMPD-UWP/Model/PhotoHandler.cs delete mode 100644 YJMPD-UWP/ViewModels/AccountVM.cs delete mode 100644 YJMPD-UWP/Views/AccountView.xaml delete mode 100644 YJMPD-UWP/Views/AccountView.xaml.cs diff --git a/YJMPD-UWP/App.xaml.cs b/YJMPD-UWP/App.xaml.cs index 2c3bef0..c070221 100644 --- a/YJMPD-UWP/App.xaml.cs +++ b/YJMPD-UWP/App.xaml.cs @@ -9,6 +9,7 @@ using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; +using YJMPD_UWP.Helpers; namespace YJMPD_UWP { @@ -114,11 +115,13 @@ namespace YJMPD_UWP public static bool Navigate(Type type) { + App.Geo.TryConnectIfNull(); return ContentFrame.Navigate(type); } public static bool Navigate(Type type, object param) { + App.Geo.TryConnectIfNull(); return ContentFrame.Navigate(type, param); } diff --git a/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs b/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs new file mode 100644 index 0000000..a031bad --- /dev/null +++ b/YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace YJMPD_UWP.Helpers.EventArgs +{ + public class GamePlayersUpdatedEventArgs : System.EventArgs + { + public string Username { get; private set; } + public double Points { get; private set; } + + public GamePlayersUpdatedEventArgs(string username, double points) + { + Username = username; + Points = points; + } + } +} diff --git a/YJMPD-UWP/Helpers/Settings.cs b/YJMPD-UWP/Helpers/Settings.cs index 83e5225..95f22a6 100644 --- a/YJMPD-UWP/Helpers/Settings.cs +++ b/YJMPD-UWP/Helpers/Settings.cs @@ -1,5 +1,8 @@ -using Windows.Foundation.Collections; +using System; +using System.Diagnostics; +using Windows.Foundation.Collections; using Windows.Storage; +using YJMPD_UWP.Model.Object; namespace YJMPD_UWP.Helpers { @@ -11,6 +14,38 @@ namespace YJMPD_UWP.Helpers { //Define default settings here Values["hostname"] = "imegumii.space"; + + if (Values["username"] == null) + Values["username"] = "Anon_" + Util.Random(5, Util.RandomType.ALPHANUMERIC); + + if (Values["statistics"] == null) + Values["statistics"] = Util.Serialize(new Statistics()); + + + st = Util.Deserialize(Values["statistics"] as string); + } + + + private static Statistics st; + public static Statistics Statistics + { + get + { + return st; + } + } + + + public static string Username + { + get + { + return Values["username"] as string; + } + set + { + Values["username"] = value; + } } } } diff --git a/YJMPD-UWP/Helpers/Util.cs b/YJMPD-UWP/Helpers/Util.cs index f7d4260..cad26b9 100644 --- a/YJMPD-UWP/Helpers/Util.cs +++ b/YJMPD-UWP/Helpers/Util.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -16,8 +17,41 @@ namespace YJMPD_UWP.Helpers { public enum DialogType { YESNO, OKCANCEL } + public enum RandomType { ALPHA, NUMERIC, ALPHANUMERIC } + private static string AlphaSource = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + private static string NumericSource = "0123456789"; + public static double Now { get { return (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } } + public static string Random(int length, RandomType type) + { + string str = ""; + + string source = ""; + switch(type) + { + case RandomType.ALPHA: + source = AlphaSource; + break; + case RandomType.NUMERIC: + source = NumericSource; + break; + default: + case RandomType.ALPHANUMERIC: + source = AlphaSource + NumericSource; + break; + } + + Random rand = new Random(); + + for(int i = 0; i(E o) + { + return JsonConvert.SerializeObject(o); + } + + public static E Deserialize(string s) + { + return JsonConvert.DeserializeObject(s); + } + + //GPS + public static async Task FindLocation(string location, Geopoint reference) { MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(location, reference); @@ -85,56 +131,6 @@ namespace YJMPD_UWP.Helpers return address; } - public static MapPolyline GetRouteLine(MapRoute m, Color color, int zindex, int thickness = 5) - { - var line = new MapPolyline - { - StrokeThickness = thickness, - StrokeColor = color, - StrokeDashed = false, - ZIndex = zindex - }; - - if (m != null) - line.Path = new Geopath(m.Path.Positions); - - return line; - } - - public static MapPolyline GetRouteLine(List positions, Color color, int zindex, int thickness = 5) - { - var line = new MapPolyline - { - StrokeThickness = thickness, - StrokeColor = color, - StrokeDashed = false, - ZIndex = zindex - }; - - line.Path = new Geopath(positions); - - return line; - } - - public static MapPolyline GetRouteLine(BasicGeoposition p1, BasicGeoposition p2, Color color, int zindex, int thickness = 5) - { - var line = new MapPolyline - { - StrokeThickness = thickness, - StrokeColor = color, - StrokeDashed = false, - ZIndex = zindex - }; - - List plist = new List(); - plist.Add(p1); - plist.Add(p2); - - line.Path = new Geopath(plist); - - return line; - } - public static void ShowToastNotification(string title, string text) { ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; @@ -179,82 +175,5 @@ namespace YJMPD_UWP.Helpers else return false; } - - public static string TranslatedManeuver(MapRouteManeuver maneuver, int distance) - { - string response = ""; - bool onstreet = false; - bool meters = true; - - distance = (int)Math.Round(distance / 5.0) * 5; - - switch (maneuver.Kind) - { - default: - response = "RouteSeeMap"; - meters = false; - break; - case MapRouteManeuverKind.End: - response = "RouteEnd"; - break; - case MapRouteManeuverKind.GoStraight: - response = "RouteGoStraight"; - onstreet = true; - break; - case MapRouteManeuverKind.None: - response = "RouteNone"; - meters = false; - break; - case MapRouteManeuverKind.Start: - response = "RouteStart"; - meters = false; - break; - case MapRouteManeuverKind.TurnHardLeft: - case MapRouteManeuverKind.TurnLeft: - response = "RouteLeft"; - onstreet = true; - break; - case MapRouteManeuverKind.TurnHardRight: - case MapRouteManeuverKind.TurnRight: - response = "RouteRight"; - onstreet = true; - break; - case MapRouteManeuverKind.TrafficCircleLeft: - response = "RouteTrafficCircleLeft"; - onstreet = true; - break; - case MapRouteManeuverKind.TrafficCircleRight: - response = "RouteTrafficCircleRight"; - onstreet = true; - break; - case MapRouteManeuverKind.TurnKeepLeft: - case MapRouteManeuverKind.TurnLightLeft: - response = "RouteKeepLeft"; - break; - case MapRouteManeuverKind.TurnKeepRight: - case MapRouteManeuverKind.TurnLightRight: - response = "RouteKeepRight"; - break; - case MapRouteManeuverKind.UTurnLeft: - case MapRouteManeuverKind.UTurnRight: - response = "RouteUTurn"; - break; - } - - if (maneuver.StreetName == "") - onstreet = false; - - if (distance < 10) - meters = false; - - - if (onstreet) - response += " " + "RouteOn" + " " + maneuver.StreetName; - - if (meters) - response = "RouteIn" + " " + distance + "m" + " " + response.ToLower(); - - return response; - } } } diff --git a/YJMPD-UWP/MainPage.xaml b/YJMPD-UWP/MainPage.xaml index 27fc007..7b6fb66 100644 --- a/YJMPD-UWP/MainPage.xaml +++ b/YJMPD-UWP/MainPage.xaml @@ -33,7 +33,7 @@ - - + + + + + + + diff --git a/YJMPD-UWP/ViewModels/AccountVM.cs b/YJMPD-UWP/ViewModels/AccountVM.cs deleted file mode 100644 index e2cb5da..0000000 --- a/YJMPD-UWP/ViewModels/AccountVM.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace YJMPD_UWP.ViewModels -{ - public class AccountVM : TemplateVM - { - public AccountVM() : base("Account") - { - - } - } -} diff --git a/YJMPD-UWP/ViewModels/MainPageVM.cs b/YJMPD-UWP/ViewModels/MainPageVM.cs index 84fb654..0993326 100644 --- a/YJMPD-UWP/ViewModels/MainPageVM.cs +++ b/YJMPD-UWP/ViewModels/MainPageVM.cs @@ -6,22 +6,40 @@ namespace YJMPD_UWP.ViewModels { public MainPageVM() : base("Loading") { - + App.Game.OnStatusUpdate += Game_OnStatusUpdate; + App.Game.OnPlayersUpdate += Game_OnPlayersUpdate; + } + + private void Game_OnPlayersUpdate(object sender, Helpers.EventArgs.GamePlayersUpdatedEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + NotifyPropertyChanged(nameof(Players)); + }); + } + + private void Game_OnStatusUpdate(object sender, Helpers.EventArgs.GameStatusUpdatedEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + NotifyPropertyChanged(nameof(GameState)); + NotifyPropertyChanged(nameof(GameVisible)); + }); } public string GameState { get { - return "N/A"; + return App.Game.Status.ToString(); } } - public string People + public string Players { get { - return "0/0"; + return App.Game.Players.Count + " players"; } } @@ -29,7 +47,7 @@ namespace YJMPD_UWP.ViewModels { get { - return true; + return App.Game.Status != Model.GameHandler.GameStatus.STOPPED; } } diff --git a/YJMPD-UWP/ViewModels/MatchVM.cs b/YJMPD-UWP/ViewModels/MatchVM.cs index 0c1f50c..763a5b0 100644 --- a/YJMPD-UWP/ViewModels/MatchVM.cs +++ b/YJMPD-UWP/ViewModels/MatchVM.cs @@ -4,7 +4,59 @@ { public MatchVM() : base("Match") { + App.Geo.OnStatusUpdate += Geo_OnStatusUpdate; + App.Network.OnStatusUpdate += Network_OnStatusUpdate; + App.Game.OnStatusUpdate += Game_OnStatusUpdate; + } + private void Game_OnStatusUpdate(object sender, Helpers.EventArgs.GameStatusUpdatedEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + NotifyPropertyChanged(nameof(StartMatch)); + NotifyPropertyChanged(nameof(StopMatch)); + }); + } + + private void Network_OnStatusUpdate(object sender, Helpers.EventArgs.NetworkStatusUpdatedEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + NotifyPropertyChanged(nameof(MatchAvailable)); + }); + } + + private void Geo_OnStatusUpdate(object sender, Helpers.EventArgs.PositionStatusUpdatedEventArgs e) + { + dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => + { + NotifyPropertyChanged(nameof(MatchAvailable)); + }); + } + + + public bool MatchAvailable + { + get + { + return App.Geo.Status == Windows.Devices.Geolocation.PositionStatus.Ready && App.Network.Status == Model.NetworkHandler.NetworkStatus.CONNECTED; + } + } + + public bool StartMatch + { + get + { + return App.Game.Status == Model.GameHandler.GameStatus.STOPPED; + } + } + + public bool StopMatch + { + get + { + return !StartMatch; + } } } } diff --git a/YJMPD-UWP/ViewModels/SettingsVM.cs b/YJMPD-UWP/ViewModels/SettingsVM.cs index f48d885..c6ff439 100644 --- a/YJMPD-UWP/ViewModels/SettingsVM.cs +++ b/YJMPD-UWP/ViewModels/SettingsVM.cs @@ -1,4 +1,6 @@ -namespace YJMPD_UWP.ViewModels +using YJMPD_UWP.Helpers; + +namespace YJMPD_UWP.ViewModels { public class SettingsVM : TemplateVM { @@ -6,5 +8,17 @@ { } + + public string Username + { + get + { + return Settings.Username; + } + set + { + Settings.Username = value; + } + } } } diff --git a/YJMPD-UWP/ViewModels/StatisticsVM.cs b/YJMPD-UWP/ViewModels/StatisticsVM.cs index 6ffab1b..444c455 100644 --- a/YJMPD-UWP/ViewModels/StatisticsVM.cs +++ b/YJMPD-UWP/ViewModels/StatisticsVM.cs @@ -1,10 +1,89 @@ -namespace YJMPD_UWP.ViewModels +using System.Linq; +using YJMPD_UWP.Helpers; +using YJMPD_UWP.Model.Object; + +namespace YJMPD_UWP.ViewModels { public class StatisticsVM : TemplateVM { + Statistics st; + public StatisticsVM() : base("Statistics") { + st = Settings.Statistics; + } + public string Information + { + get + { + return "This page holds all the statistics for " + Settings.Username; + } + } + + public string Distance + { + get + { + return st.Distance + "km"; + } + } + + public string LeaderCount + { + get + { + return st.Leader + ""; + } + } + + public string MatchesCount + { + get + { + return st.Matches + ""; + } + } + + public string PointsTotal + { + get + { + return st.Points.Sum() + ""; + } + } + + public string PointsAverage + { + get + { + if (st.Points.Count < 1) + return "0"; + else + return (st.Points.Sum() / st.Matches) + ""; + } + } + + public string PointsMax + { + get + { + if (st.Points.Count < 1) + return "0"; + else + return st.Points.Max() + ""; + } + } + + public string PointsMin + { + get + { + if (st.Points.Count < 1) + return "0"; + else + return st.Points.Min() + ""; + } } } } diff --git a/YJMPD-UWP/Views/AboutView.xaml b/YJMPD-UWP/Views/AboutView.xaml index 98b0ec6..26c32b9 100644 --- a/YJMPD-UWP/Views/AboutView.xaml +++ b/YJMPD-UWP/Views/AboutView.xaml @@ -7,7 +7,9 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> - - - + + + + + diff --git a/YJMPD-UWP/Views/AccountView.xaml b/YJMPD-UWP/Views/AccountView.xaml deleted file mode 100644 index d98e054..0000000 --- a/YJMPD-UWP/Views/AccountView.xaml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - diff --git a/YJMPD-UWP/Views/AccountView.xaml.cs b/YJMPD-UWP/Views/AccountView.xaml.cs deleted file mode 100644 index 3271f06..0000000 --- a/YJMPD-UWP/Views/AccountView.xaml.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Windows.UI.Xaml.Controls; -using YJMPD_UWP.ViewModels; - -namespace YJMPD_UWP.Views -{ - public sealed partial class AccountView : Page - { - AccountVM accountvm; - - public AccountView() - { - accountvm = new AccountVM(); - this.DataContext = accountvm; - this.InitializeComponent(); - } - } -} diff --git a/YJMPD-UWP/Views/MatchView.xaml b/YJMPD-UWP/Views/MatchView.xaml index 562eac2..f56f50f 100644 --- a/YJMPD-UWP/Views/MatchView.xaml +++ b/YJMPD-UWP/Views/MatchView.xaml @@ -3,11 +3,22 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:YJMPD_UWP.Views" + xmlns:convert="using:YJMPD_UWP.Helpers.Converter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> - - - + + + + + + + + +