diff --git a/source/Trek-12/DataContractPersistence/DataContractJson.cs b/source/Trek-12/DataContractPersistence/DataContractJson.cs index 2307f82..d53d6fb 100644 --- a/source/Trek-12/DataContractPersistence/DataContractJson.cs +++ b/source/Trek-12/DataContractPersistence/DataContractJson.cs @@ -31,7 +31,7 @@ namespace DataContractPersistence /// /// Load all the data from JSON file /// - /// A tuple with the lists of players, games, maps and best scores + /// A tuple with the lists of players, games, maps and bestScores public (ObservableCollection, ObservableCollection, ObservableCollection, ObservableCollection) LoadData() { var JsonSerializer = new DataContractJsonSerializer(typeof(DataToPersist)); diff --git a/source/Trek-12/DataContractPersistence/DataContractXml.cs b/source/Trek-12/DataContractPersistence/DataContractXml.cs index 8255ecc..9ffa396 100644 --- a/source/Trek-12/DataContractPersistence/DataContractXml.cs +++ b/source/Trek-12/DataContractPersistence/DataContractXml.cs @@ -29,7 +29,7 @@ namespace DataContractPersistence /// /// Load all the data from XML file /// - /// A tuple with the lists of players, games, maps and best scores + /// A tuple with the lists of players, games, maps and bestScores public (ObservableCollection, ObservableCollection, ObservableCollection, ObservableCollection) LoadData() { var serializer = new DataContractSerializer(typeof(DataToPersist)); diff --git a/source/Trek-12/DataContractPersistence/DataToPersist.cs b/source/Trek-12/DataContractPersistence/DataToPersist.cs index b78b204..9c72c68 100644 --- a/source/Trek-12/DataContractPersistence/DataToPersist.cs +++ b/source/Trek-12/DataContractPersistence/DataToPersist.cs @@ -19,7 +19,7 @@ namespace DataContractPersistence /// List of maps with their boards /// public ObservableCollection Maps { get; set; } - + /// /// List of best scores /// diff --git a/source/Trek-12/Models/Game/BestScore.cs b/source/Trek-12/Models/Game/BestScore.cs index c2c5ca4..e61a5c8 100644 --- a/source/Trek-12/Models/Game/BestScore.cs +++ b/source/Trek-12/Models/Game/BestScore.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; @@ -10,45 +11,64 @@ namespace Models.Game /// /// This class represents the best score of a player. /// + [DataContract] public class BestScore { /// - /// Initialize a new instance of the BestScore class. + /// Name of the map. /// - /// Number of games played by the new user - /// Best score - public BestScore(int gamesPlayed, int score) - { - GamesPlayed = gamesPlayed; - Score = score; - if (GamesPlayed < 0) - GamesPlayed = 0; - if (Score < 0) - Score = 0; - } + [DataMember] + public string MapName { get; private set; } - /// - /// Number of games played by the user. - /// - public int GamesPlayed { get; private set; } + [DataMember] + public Player ThePlayer { get; private set; } /// - /// Best score of the player. + /// Number of games played by the user (on a specific map). /// - private int _score; - public int Score - { - get + private int _gamesPlayed; + [DataMember] + public int GamesPlayed + { + get => _gamesPlayed; + private set { - return _score; + if (value >= 0) + _gamesPlayed = value; + else _gamesPlayed = 0; } + } + + /// + /// Best score of the player (on a specific map). + /// + private int _score; + [DataMember] + public int Score + { + get => _score; private set { - if (value > _score) + if (value >= 0) _score = value; + else _score = 0; } } + /// + /// Initialize a new instance of the BestScore class. + /// + /// Number of games played by the new user + /// Best score + /// The name of the map + public BestScore(string map, Player player, int gamesPlayed, int score) + { + MapName = map; + ThePlayer = player; + GamesPlayed = gamesPlayed; + Score = score; + } + /// /// Increment the number of games played by the user. /// @@ -63,7 +83,31 @@ namespace Models.Game /// New best score public void UpdateScore(int newScore) { - Score = newScore; + Score += newScore; + } + + /// + /// Redefine the equal operation between BestScore. + /// + /// The object to compare with the current BestScore. + /// true if the specified object is equal to the current BestScore; otherwise, false. + public override bool Equals(object? obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + BestScore c = (BestScore)obj; + return (MapName == c.MapName && ThePlayer == c.ThePlayer); + } + + /// + /// Returns the hash code for the current BestScore, in order for the Equals operation to work + /// + /// The hash code for the current BestScore. + public override int GetHashCode() + { + return MapName.GetHashCode() ^ ThePlayer.GetHashCode(); } } } diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index e0f1059..6f10636 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; +using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.Serialization; @@ -23,12 +24,11 @@ namespace Models.Game [DataContract] public class Game : INotifyPropertyChanged { - /* Persistence */ + /* Persistence Interface */ public IPersistence PersistenceManager { get; set; } /* List for the game and persistence */ private ObservableCollection _players; - public ObservableCollection Players { get => _players; @@ -40,7 +40,6 @@ namespace Models.Game } private ObservableCollection _games; - public ObservableCollection Games { get => _games; @@ -52,7 +51,6 @@ namespace Models.Game } private ObservableCollection _maps; - public ObservableCollection Maps { get => _maps; @@ -64,7 +62,6 @@ namespace Models.Game } private ObservableCollection _bestScores; - public ObservableCollection BestScores { get => _bestScores; @@ -74,7 +71,7 @@ namespace Models.Game OnPropertyChanged(nameof(BestScores)); } } - + private bool _isRunning; [DataMember] public bool IsRunning @@ -131,9 +128,20 @@ namespace Models.Game Maps.Add(map); } - public void AddBestScore(BestScore bestScore) + public void AddBestScore(int finalScore) { - BestScores.Add(bestScore); + BestScore bs = new BestScore(UsedMap.Name, CurrentPlayer, 1, finalScore); + foreach (var score in BestScores) + { + if (!bs.Equals(score)) continue; + + score.IncrGamesPlayed(); + score.UpdateScore(finalScore); + return; + } + + BestScores.Add(bs); + BestScores.OrderByDescending(p => p.Score); } public bool RemovePlayer(string playerName) @@ -144,6 +152,7 @@ namespace Models.Game return false; } Players.Remove(player); + CheckAndRemoveBestScoresDependencies(player.Pseudo); return true; } @@ -153,6 +162,7 @@ namespace Models.Game { if (index.Pseudo == pseudo) { + CheckAndChangeBestScoresDependencies(index.Pseudo, newpseudo); index.Pseudo = newpseudo; return true; } @@ -161,6 +171,32 @@ namespace Models.Game return false; } + public void CheckAndRemoveBestScoresDependencies(string playerName) + { + List bs = new List(); + foreach (var bestScore in BestScores) + { + if (!bestScore.ThePlayer.Pseudo.Equals(playerName)) continue; + + bs.Add(bestScore); + } + + foreach (var score in bs) + { + BestScores.Remove(score); + } + } + + public void CheckAndChangeBestScoresDependencies(string playerName, string newPlayerName) + { + foreach (var bestScore in BestScores) + { + if (!bestScore.ThePlayer.Pseudo.Equals(playerName)) continue; + + bestScore.ThePlayer.Pseudo = newPlayerName; + } + } + public void LoadData() { var data = PersistenceManager.LoadData(); diff --git a/source/Trek-12/Models/Game/Player.cs b/source/Trek-12/Models/Game/Player.cs index cb8f095..0acad39 100644 --- a/source/Trek-12/Models/Game/Player.cs +++ b/source/Trek-12/Models/Game/Player.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using Models.Interfaces; @@ -17,6 +18,7 @@ namespace Models.Game void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + /// /// It is he pseudo of the player. /// @@ -87,5 +89,13 @@ namespace Models.Game { return Pseudo.GetHashCode(); } + + /// + /// Actualize the last time the player played. + /// + public void UpdateLastPlayed() + { + LastPlayed = DateTime.Now.ToString("dd/MM/yyyy"); + } } } \ No newline at end of file diff --git a/source/Trek-12/Tests/BestScoreTests.cs b/source/Trek-12/Tests/BestScoreTests.cs index 951989c..f52a4cd 100644 --- a/source/Trek-12/Tests/BestScoreTests.cs +++ b/source/Trek-12/Tests/BestScoreTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; using Models.Game; @@ -12,7 +13,11 @@ namespace Tests [Fact] public void Constructor_WithNegativeInputs_SetsPropertiesToZero() { - var bestScore = new BestScore(-5, -10); + var myMap = new Map("Dunai", "Dunai.png"); + var myPlayer = new Player("John", "pp.png"); + + var bestScore = new BestScore(myMap.Name, myPlayer, -5, -10); + Assert.Equal(0, bestScore.GamesPlayed); Assert.Equal(0, bestScore.Score); } @@ -20,7 +25,11 @@ namespace Tests [Fact] public void Constructor_WithPositiveInputs_SetsPropertiesCorrectly() { - var bestScore = new BestScore(5, 10); + var myMap = new Map("Dunai", "Dunai.png"); + var myPlayer = new Player("John", "pp.png"); + + var bestScore = new BestScore(myMap.Name, myPlayer, 5, 10); + Assert.Equal(5, bestScore.GamesPlayed); Assert.Equal(10, bestScore.Score); } @@ -28,24 +37,36 @@ namespace Tests [Fact] public void IncrGamesPlayed_IncrementsGamesPlayed() { - var bestScore = new BestScore(0, 0); + var myMap = new Map("Dunai", "Dunai.png"); + var myPlayer = new Player("John", "pp.png"); + + var bestScore = new BestScore(myMap.Name, myPlayer, 0, 0); bestScore.IncrGamesPlayed(); + Assert.Equal(1, bestScore.GamesPlayed); } [Fact] public void ScoreSetter_WithLowerValue_DoesNotChangeScore() { - var bestScore = new BestScore(0, 10); + var myMap = new Map("Dunai", "Dunai.png"); + var myPlayer = new Player("John", "pp.png"); + + var bestScore = new BestScore(myMap.Name, myPlayer, 0, 10); bestScore.UpdateScore(5); - Assert.Equal(10, bestScore.Score); + + Assert.Equal(5, bestScore.Score); } [Fact] public void ScoreSetter_WithHigherValue_ChangesScore() { - var bestScore = new BestScore(0, 5); + var myMap = new Map("Dunai", "Dunai.png"); + var myPlayer = new Player("John", "pp.png"); + + var bestScore = new BestScore(myMap.Name, myPlayer, 0, 5); bestScore.UpdateScore(10); + Assert.Equal(10, bestScore.Score); } } diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs index 890890f..8dd3004 100644 --- a/source/Trek-12/Tests/GameTests.cs +++ b/source/Trek-12/Tests/GameTests.cs @@ -78,7 +78,7 @@ public class GameTests [Fact] public void AddBestScore_ShouldAddBestScoreToList() { - var bestScore = new BestScore(3, 125); + var bestScore = new BestScore("test", new Player("John", "Picture.png"), 3, 127); _game.AddBestScore(bestScore); @@ -88,10 +88,14 @@ public class GameTests [Fact] public void LoadData_ShouldLoadDataFromPersistence() { - var players = new ObservableCollection { new Player("test", "DefaultProfilePicture") }; - var games = new ObservableCollection { new Game(_mockPersistence.Object) }; - var maps = new ObservableCollection { new Map("test_name", "test_background.png") }; - var bestScores = new ObservableCollection { new BestScore(1, 26) }; + var myGame = new Game(_mockPersistence.Object); + var myPlayer = new Player("test", "DefaultProfilePicture"); + var myMap = new Map("test_name", "test_background.png"); + + var players = new ObservableCollection { myPlayer }; + var games = new ObservableCollection { myGame }; + var maps = new ObservableCollection { myMap }; + var bestScores = new ObservableCollection { new BestScore(myMap.Name, myPlayer, 1, 45) }; _mockPersistence.Setup(p => p.LoadData()).Returns((players, games, maps, bestScores)); diff --git a/source/Trek-12/Trek-12/App.xaml.cs b/source/Trek-12/Trek-12/App.xaml.cs index b906f7f..e640454 100644 --- a/source/Trek-12/Trek-12/App.xaml.cs +++ b/source/Trek-12/Trek-12/App.xaml.cs @@ -26,7 +26,7 @@ namespace Trek_12 Directory.CreateDirectory(FilePath); } - //File.Delete(Path.Combine(FilePath, FileName)); + File.Delete(Path.Combine(FilePath, FileName)); string fullPath = Path.Combine(FilePath, FileName); if (File.Exists(fullPath)) diff --git a/source/Trek-12/Trek-12/Views/Components/ContentLeaderBoard.xaml b/source/Trek-12/Trek-12/Views/Components/ContentLeaderBoard.xaml index 639b6f3..3ec0052 100644 --- a/source/Trek-12/Trek-12/Views/Components/ContentLeaderBoard.xaml +++ b/source/Trek-12/Trek-12/Views/Components/ContentLeaderBoard.xaml @@ -1,7 +1,9 @@ + x:Class="Trek_12.Views.Components.ContentLeaderBoard" + x:Name="this"> + @@ -12,31 +14,31 @@ WidthRequest="60" IsClippedToBounds="True" HasShadow="True"> - -