BestScores
continuous-integration/drone/push Build is failing Details

pull/107/head
Rémi LAVERGNE 11 months ago
parent c4414e687e
commit ccdc672033

@ -31,7 +31,7 @@ namespace DataContractPersistence
/// <summary>
/// Load all the data from JSON file
/// </summary>
/// <returns>A tuple with the lists of players, games, maps and best scores</returns>
/// <returns>A tuple with the lists of players, games, maps and bestScores</returns>
public (ObservableCollection<Player>, ObservableCollection<Game>, ObservableCollection<Map>, ObservableCollection<BestScore>) LoadData()
{
var JsonSerializer = new DataContractJsonSerializer(typeof(DataToPersist));

@ -29,7 +29,7 @@ namespace DataContractPersistence
/// <summary>
/// Load all the data from XML file
/// </summary>
/// <returns>A tuple with the lists of players, games, maps and best scores</returns>
/// <returns>A tuple with the lists of players, games, maps and bestScores</returns>
public (ObservableCollection<Player>, ObservableCollection<Game>, ObservableCollection<Map>, ObservableCollection<BestScore>) LoadData()
{
var serializer = new DataContractSerializer(typeof(DataToPersist));

@ -19,7 +19,7 @@ namespace DataContractPersistence
/// List of maps with their boards
/// </summary>
public ObservableCollection<Map> Maps { get; set; }
/// <summary>
/// List of best scores
/// </summary>

@ -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,12 +11,23 @@ namespace Models.Game
/// <summary>
/// This class represents the best score of a player.
/// </summary>
[DataContract]
public class BestScore
{
/// <summary>
/// Name of the map.
/// </summary>
[DataMember]
public string MapName { get; private set; }
[DataMember]
public Player ThePlayer { get; private set; }
/// <summary>
/// Number of games played by the user (on a specific map).
/// </summary>
private int _gamesPlayed;
[DataMember]
public int GamesPlayed
{
get => _gamesPlayed;
@ -31,6 +43,7 @@ namespace Models.Game
/// Best score of the player (on a specific map).
/// </summary>
private int _score;
[DataMember]
public int Score
{
get => _score;
@ -47,8 +60,11 @@ namespace Models.Game
/// </summary>
/// <param name="gamesPlayed">Number of games played by the new user</param>
/// <param name="score">Best score</param>
public BestScore(int gamesPlayed, int score)
/// <param name="map">The name of the map</param>
public BestScore(string map, Player player, int gamesPlayed, int score)
{
MapName = map;
ThePlayer = player;
GamesPlayed = gamesPlayed;
Score = score;
}
@ -69,5 +85,29 @@ namespace Models.Game
{
Score = newScore;
}
/// <summary>
/// Redefine the equal operation between BestScore.
/// </summary>
/// <param name="obj">The object to compare with the current BestScore.</param>
/// <returns>true if the specified object is equal to the current BestScore; otherwise, false.</returns>
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);
}
/// <summary>
/// Returns the hash code for the current BestScore, in order for the Equals operation to work
/// </summary>
/// <returns>The hash code for the current BestScore.</returns>
public override int GetHashCode()
{
return MapName.GetHashCode() ^ ThePlayer.GetHashCode();
}
}
}

File diff suppressed because one or more lines are too long

@ -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,12 +18,7 @@ namespace Models.Game
void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// <summary>
/// The best score of the player on a specific map.
/// </summary>
[DataMember]
public Dictionary<Map, BestScore> BestScores { get; private set; }
/// <summary>
/// It is he pseudo of the player.
/// </summary>
@ -68,7 +64,6 @@ namespace Models.Game
Pseudo = pseudo;
ProfilePicture = profilePicture;
CreationDate = DateTime.Now.ToString("dd/MM/yyyy");
BestScores = new Dictionary<Map, BestScore>();
}
/// <summary>
@ -102,13 +97,5 @@ namespace Models.Game
{
LastPlayed = DateTime.Now.ToString("dd/MM/yyyy");
}
/// <summary>
/// Add a new best score to the player.
/// </summary>
public void AddBestScore(Map map, BestScore bestScore)
{
BestScores.Add(map, bestScore);
}
}
}

@ -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(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);
}
}

@ -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<Player> { new Player("test", "DefaultProfilePicture") };
var games = new ObservableCollection<Game> { new Game(_mockPersistence.Object) };
var maps = new ObservableCollection<Map> { new Map("test_name", "test_background.png") };
var bestScores = new ObservableCollection<BestScore> { 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<Player> { myPlayer };
var games = new ObservableCollection<Game> { myGame };
var maps = new ObservableCollection<Map> { myMap };
var bestScores = new ObservableCollection<BestScore> { new BestScore(myMap.Name, myPlayer, 1, 45) };
_mockPersistence.Setup(p => p.LoadData()).Returns((players, games, maps, bestScores));

@ -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))

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Trek_12.Views.Components.ContentLeaderBoard">
x:Class="Trek_12.Views.Components.ContentLeaderBoard"
x:Name="this">
<Grid ColumnDefinitions="auto,*,*,*,*"
RowDefinitions="*,*"
Margin="0,20">
@ -12,31 +14,31 @@
WidthRequest="60"
IsClippedToBounds="True"
HasShadow="True">
<Image Source="profile.jpg"
<Image Source="{Binding ProfilePicture, Source={x:Reference this}}"
Aspect="AspectFill"
Margin="-20"/>
</Frame>
<Label Text="Kiwi6026"
Grid.Column="1"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"
Margin="10,0"/>
<Label Text="nbParties"
Grid.Column="2"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<Label Text="map"
Grid.Column="3"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<Label Text="bestScore"
Grid.Column="4"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<Label Text="{Binding Pseudo, Source={x:Reference this}}"
Grid.Column="1"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"
Margin="10,0"/>
<Label Text="{Binding NbGames, Source={x:Reference this}}"
Grid.Column="2"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<Label Text="{Binding BestScore, Source={x:Reference this}}"
Grid.Column="3"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<Label Text="{Binding Map, Source={x:Reference this}}"
Grid.Column="4"
VerticalOptions="Center"
FontSize="Title"
TextColor="DarkSalmon"/>
<BoxView Color="DarkSalmon"
Grid.Row="1"
HeightRequest="1"

File diff suppressed because one or more lines are too long

@ -4,45 +4,57 @@
xmlns:views="clr-namespace:Trek_12.Views.Components"
x:Class="Trek_12.Views.PageLeaderBoard"
Title="PageLeaderBoard">
<Grid BackgroundColor="BlanchedAlmond"
RowDefinitions="auto,6*,*">
<Frame Grid.Row="0" BackgroundColor="Transparent" BorderColor="Transparent" Padding="0" Margin="15">
<Image Source="back_arrow.png"
Margin="0"
HeightRequest="50"
WidthRequest="50"
VerticalOptions="Center"
HorizontalOptions="Start"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="OnBackArrow_Tapped"/>
</Frame.GestureRecognizers>
</Frame>
<ContentPage.Content>
<Grid BackgroundColor="BlanchedAlmond"
RowDefinitions="auto,6*,*">
<Frame Grid.Row="0" BackgroundColor="Transparent" BorderColor="Transparent" Padding="0" Margin="15">
<Image Source="back_arrow.png"
Margin="0"
HeightRequest="50"
WidthRequest="50"
VerticalOptions="Center"
HorizontalOptions="Start"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="OnBackArrow_Tapped"/>
</Frame.GestureRecognizers>
</Frame>
<VerticalStackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label
Text="Leaderboard"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Title"/>
<BoxView
Color="DarkSalmon"
HeightRequest="1"
WidthRequest="125"/>
</VerticalStackLayout>
<ScrollView Grid.Row="1"
VerticalOptions="FillAndExpand"
VerticalScrollBarVisibility="Never"
Margin="0,10">
<VerticalStackLayout HorizontalOptions="Center">
<views:ContentLeaderBoard/>
<views:ContentLeaderBoard/>
<views:ContentLeaderBoard/>
<views:ContentLeaderBoard/>
<views:ContentLeaderBoard/>
<views:ContentLeaderBoard/>
<VerticalStackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label
Text="Leaderboard"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Title"/>
<BoxView
Color="DarkSalmon"
HeightRequest="1"
WidthRequest="125"/>
</VerticalStackLayout>
</ScrollView>
</Grid>
<ScrollView Grid.Row="1"
VerticalOptions="FillAndExpand"
VerticalScrollBarVisibility="Never"
Margin="0,10">
<VerticalStackLayout HorizontalOptions="Center">
<CollectionView ItemsSource="{Binding BestScores}"
ItemsLayout="VerticalList"
VerticalOptions="Center">
<CollectionView.ItemTemplate>
<DataTemplate>
<views:ContentLeaderBoard
Pseudo="{Binding ThePlayer.Pseudo}"
ProfilePicture="{Binding ThePlayer.ProfilePicture}"
NbGames="{Binding GamesPlayed}"
BestScore="{Binding Score}"
Map="{Binding MapName}"
/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ScrollView>
</Grid>
</ContentPage.Content>
</ContentPage>

@ -1,10 +1,16 @@
using Models.Game;
using System.Diagnostics;
namespace Trek_12.Views;
public partial class PageLeaderBoard : ContentPage
{
public Game LeaderboardManager => (App.Current as App).Manager;
public PageLeaderBoard()
{
InitializeComponent();
BindingContext = LeaderboardManager;
}
private async void OnBackArrow_Tapped(object sender, EventArgs e)

Loading…
Cancel
Save