Browse Source

Added players in match list

Kenneth van Ewijk 10 năm trước cách đây
mục cha
commit
4d323d1c44

+ 4 - 5
YJMPD-UWP/Helpers/EventArgs/GamePlayersUpdatedEventArgs.cs

@@ -3,18 +3,17 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using YJMPD_UWP.Model.Object;
 
 namespace YJMPD_UWP.Helpers.EventArgs
 {
     public class GamePlayersUpdatedEventArgs : System.EventArgs
     {
-        public string Username { get; private set; }
-        public double Points { get; private set; }
+        public Player Player { get; private set; }
 
-        public GamePlayersUpdatedEventArgs(string username, double points)
+        public GamePlayersUpdatedEventArgs(Player player)
         {
-            Username = username;
-            Points = points;
+            Player = player;
         }
     }
 }

+ 41 - 12
YJMPD-UWP/Model/GameHandler.cs

@@ -1,6 +1,8 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Threading.Tasks;
 using YJMPD_UWP.Helpers.EventArgs;
+using YJMPD_UWP.Model.Object;
 
 namespace YJMPD_UWP.Model
 {
@@ -15,7 +17,7 @@ namespace YJMPD_UWP.Model
         public enum GameStatus { STARTED, SEARCHING, WAITING, ENDED, STOPPED }
         public GameStatus Status { get; private set; }
 
-        public Dictionary<string, double> Players { get; private set; }
+        public List<Player> Players { get; private set; }
 
         private void UpdateGameStatus(GameStatus status)
         {
@@ -25,28 +27,43 @@ namespace YJMPD_UWP.Model
 
             OnStatusUpdate(this, new GameStatusUpdatedEventArgs(status));
         }
-        private void UpdateGamePlayers(string username, double points)
+        private void UpdateGamePlayers(Player player)
         {
             if (OnPlayersUpdate == null) return;
 
-            OnPlayersUpdate(this, new GamePlayersUpdatedEventArgs(username, points));
+            OnPlayersUpdate(this, new GamePlayersUpdatedEventArgs(player));
         }
 
         public GameHandler()
         {
-            Players = new Dictionary<string, double>();
+            Players = new List<Player>();
             Status = GameStatus.STOPPED;
         }
 
-        public void AddPlayer(string username, double points)
+        public void AddPlayer(string username)
         {
-            Players.Add(username, points);
-            UpdateGamePlayers(username, points);
+            Player p = new Player(username);
+            Players.Add(p);
+            UpdateGamePlayers(p);
         }
-        public void UpdatePlayer(string username, double points)
+
+        public void Reset()
+        {
+            Players.Clear();
+            UpdateGamePlayers(null);
+        }
+
+        public void UpdatePlayer(string username, double pointstotal, double points)
         {
-            Players.Remove(username);
-            AddPlayer(username, points);
+            foreach(Player p in Players)
+            {
+                if(p.Username == username)
+                {
+                    p.Update(pointstotal, points);
+                    UpdateGamePlayers(p);
+                    return;
+                }
+            }
         }
 
 
@@ -83,13 +100,25 @@ namespace YJMPD_UWP.Model
 
             //Do stuff
 
+            UpdateGameStatus(GameStatus.SEARCHING);
+
+            await Task.Delay(TimeSpan.FromSeconds(2));
+            AddPlayer("Kenneth");
+            await Task.Delay(TimeSpan.FromSeconds(1));
+            AddPlayer("Yorick");
+
+            UpdateGameStatus(GameStatus.WAITING);
+
+            await Task.Delay(TimeSpan.FromSeconds(3));
+
             UpdateGameStatus(GameStatus.STARTED);
+
             return true;
         }
 
         private async Task<bool> StopGame()
         {
-
+            Reset();
             //Do stuff
 
             UpdateGameStatus(GameStatus.STOPPED);

+ 40 - 0
YJMPD-UWP/Model/Object/Player.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace YJMPD_UWP.Model.Object
+{
+    public class Player
+    {
+        public string Username { get; private set; }
+        public double PointsTotal { get; private set; }
+        public double Points { get; private set; }
+
+        public Player(string username)
+        {
+            Username = username;
+            Points = 0;
+            PointsTotal = 0;
+        }
+
+        public Player(string username, double pointstotal)
+        {
+            Username = username;
+            PointsTotal = pointstotal;
+            Points = 0;
+        }
+
+        public void Reset()
+        {
+            Points = 0;
+        }
+
+        public void Update(double pointstotal, double points)
+        {
+            PointsTotal = pointstotal;
+            Points = points;
+        }
+    }
+}

+ 30 - 1
YJMPD-UWP/ViewModels/MatchVM.cs

@@ -1,4 +1,7 @@
-namespace YJMPD_UWP.ViewModels
+using System.Collections.Generic;
+using YJMPD_UWP.Model.Object;
+
+namespace YJMPD_UWP.ViewModels
 {
     public class MatchVM : TemplateVM
     {
@@ -7,6 +10,16 @@
             App.Geo.OnStatusUpdate += Geo_OnStatusUpdate;
             App.Network.OnStatusUpdate += Network_OnStatusUpdate;
             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));
+                NotifyPropertyChanged(nameof(PlayersCount));
+            });
         }
 
         private void Game_OnStatusUpdate(object sender, Helpers.EventArgs.GameStatusUpdatedEventArgs e)
@@ -35,6 +48,22 @@
         }
         
 
+        public List<Player> Players
+        {
+            get
+            {
+                return new List<Player>(App.Game.Players);
+            }
+        }
+
+        public string PlayersCount
+        {
+            get
+            {
+                return "There are currently " + App.Game.Players.Count + " players in the match.";
+            }
+        }
+
         public bool MatchAvailable
         {
             get

+ 19 - 2
YJMPD-UWP/Views/MatchView.xaml

@@ -16,8 +16,25 @@
         <StackPanel Style="{StaticResource Base}" >
             <StackPanel>
                 <TextBlock Text="Start a match" Style="{StaticResource Header}" />
-                <Button Content="Start" Style="{StaticResource Green}" Tapped="StartMatchButton_Tapped" IsEnabled="{Binding MatchAvailable}" Visibility="{Binding StartMatch, Converter={StaticResource BoolToVisConverter}}" />
-                <Button Content="Stop" Style="{StaticResource Red}" Tapped="StopMatchButton_Tapped" Visibility="{Binding StopMatch, Converter={StaticResource BoolToVisConverter}}" />
+                <Button Content="Start match" Style="{StaticResource Green}" Tapped="StartMatchButton_Tapped" IsEnabled="{Binding MatchAvailable}" Visibility="{Binding StartMatch, Converter={StaticResource BoolToVisConverter}}" />
+                <Button Content="Leave match" Style="{StaticResource Red}" Tapped="StopMatchButton_Tapped" Visibility="{Binding StopMatch, Converter={StaticResource BoolToVisConverter}}" />
+            </StackPanel>
+
+            <StackPanel Visibility="{Binding StopMatch, Converter={StaticResource BoolToVisConverter}}" Margin="0,10,0,0">
+                <TextBlock Text="Current players in match" Style="{StaticResource Header}" />
+
+                <TextBlock Text="{Binding PlayersCount}" />
+
+                <ListView x:Name="PlayersList" ItemsSource="{Binding Players}" SelectionMode="None" IsTapEnabled="False">
+                    <ListView.ItemTemplate>
+                        <DataTemplate x:Name="ListViewDataTemplate">
+                            <StackPanel Orientation="Horizontal">
+                                <TextBlock Text="{Binding Username}" />
+                            </StackPanel>
+                        </DataTemplate>
+                    </ListView.ItemTemplate>
+                </ListView>
+                
             </StackPanel>
         </StackPanel>
     </ScrollViewer>

+ 1 - 0
YJMPD-UWP/YJMPD-UWP.csproj

@@ -112,6 +112,7 @@
     <Compile Include="Model\GameHandler.cs" />
     <Compile Include="Model\GeoHandler.cs" />
     <Compile Include="Model\NetworkHandler.cs" />
+    <Compile Include="Model\Object\Player.cs" />
     <Compile Include="Model\Object\Statistics.cs" />
     <Compile Include="Model\PhotoHandler.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />