Game save + Leaderboard save + Leaderboard load

old_branch_before_remy
Jules LASCRET 11 months ago
parent 0878871c5d
commit b555f8b849

@ -16,7 +16,7 @@ namespace QwirkleClassLibrary.Players
public class Leaderboard : INotifyPropertyChanged
{
[DataMember]
private readonly ObservableCollection<Score> leaderboard = new();
private ObservableCollection<Score> leaderboard = new();
public event PropertyChangedEventHandler? PropertyChanged;
@ -27,11 +27,6 @@ namespace QwirkleClassLibrary.Players
public ReadOnlyObservableCollection<Score> Lb => new(leaderboard);
/// <summary>
/// Returns the index of the player in the leaderboard, -1 if the player is not in the leaderboard
/// </summary>
@ -96,6 +91,7 @@ namespace QwirkleClassLibrary.Players
first = false;
}
leaderboard = new ObservableCollection<Score>(leaderboard.OrderByDescending(x => x.Points).ThenBy(x => x.Victories));
}
}
}

@ -24,7 +24,7 @@ namespace QwirkleClassLibrary.Tiles
/// <exception cref="ArgumentException">Throw an exception if the number of copies is negative (impossible) or superior to 3 (contradiction with the rules).</exception>
public TileBag(int nbSet)
{
/* if (nbSet < 0 || nbSet > 3)
if (nbSet < 0 || nbSet > 3)
{
throw new ArgumentException(nbSet.ToString());
}
@ -39,13 +39,7 @@ namespace QwirkleClassLibrary.Tiles
tiles.Add(t);
}
}
}*/
Tile t1 = new Tile(Shape.Club, Color.Yellow);
Tile t2 = new Tile(Shape.Round, Color.Orange);
tiles.Add(t1);
tiles.Add(t2);
}
Init();
}

@ -2,7 +2,9 @@
using Microsoft.Maui.Controls;
using Qwirkle.Pages;
using QwirkleClassLibrary.Games;
using QwirkleClassLibrary.Persistences;
using QwirkleClassLibrary.Players;
using Leaderboard = QwirkleClassLibrary.Players.Leaderboard;
namespace Qwirkle
{
@ -10,6 +12,7 @@ namespace Qwirkle
{
public App()
{
Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\Files"));
InitializeComponent();
MainPage = new AppShell();
@ -20,10 +23,12 @@ namespace Qwirkle
Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
Routing.RegisterRoute(nameof(Qwirkle.Pages.Leaderboard), typeof(Qwirkle.Pages.Leaderboard));
ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson();
Ld = leaderboardLoad.LoadLeaderboard();
}
public Game Game { get; set; } = new();
public QwirkleClassLibrary.Players.Leaderboard LD { get; set; } = new();
public Game Game { get; set; } = new();
public Leaderboard Ld { get; set; }
}
}

@ -6,7 +6,7 @@
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Padding="30, 0, 30, 30"
Spacing="25">
<Label
@ -26,6 +26,8 @@
InfoClicked="OnInfoClicked"
/>
<controls:ButtonShadow Text="Continue"/>
<controls:ButtonShadow Text="Leaderboard"
InfoClicked="OnCLeaderboardClicked"/>

@ -22,7 +22,6 @@ namespace Qwirkle
Navigation.PushAsync(new SetPlayers());
}
public void OnRulesClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Rules());

@ -9,6 +9,7 @@ using System.ComponentModel;
using Cell = QwirkleClassLibrary.Boards.Cell;
using Color = Microsoft.Maui.Graphics.Color;
using System.Drawing;
using QwirkleClassLibrary.Persistences;
using CommunityToolkit.Maui.Views;
namespace Qwirkle.Pages;
@ -112,6 +113,11 @@ public partial class Gameboard : ContentPage
game.DrawTiles(game.GetPlayingPlayer());
}
game.CheckGameOver(game.GetPlayingPlayer());
IGamePersistence gameIntermediateSave = new GamePersistenceXml();
gameIntermediateSave.SaveGame(game);
if (!game.CheckGameOver(game.GetPlayingPlayer()))
{
game.SetNextPlayer();
@ -119,7 +125,14 @@ public partial class Gameboard : ContentPage
}
else
{
((App)Application.Current!).LD.AddScoreInLead(game.ScoreBoard);
((App)Application.Current!).Ld.AddScoreInLead(game.ScoreBoard);
IGamePersistence gameEndSave = new GamePersistenceXml();
gameEndSave.SaveGame(game);
ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson();
leaderboardSave.SaveLeaderboard(((App)Application.Current!).Ld);
game.ClearGame();
}
game.NextPlayerNotified -= Game_NextPlayerNotified;

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Qwirkle.Pages.Leaderboard"
@ -11,16 +12,16 @@
<Grid Style="{StaticResource GridMain}">
<controls:GoBack></controls:GoBack>
<Label Text="Leaderboard"
Style="{StaticResource Title}"/>
Style="{StaticResource Title}" />
</Grid>
<Border Style="{StaticResource TabBorder}">
<Border.Shadow>
<Shadow/>
<Shadow />
</Border.Shadow>
<Border.StrokeShape>
<RoundRectangle CornerRadius="3"/>
<RoundRectangle CornerRadius="3" />
</Border.StrokeShape>
<StackLayout>
@ -28,37 +29,38 @@
RowDefinitions="50">
<Label
Text="Player tag"
Style="{StaticResource ContentTab}"/>
Style="{StaticResource ContentTab}" />
<Rectangle
Style="{StaticResource RectangleTab}"
Grid.Column="1"/>
Grid.Column="1" />
<Label
Grid.Column="2"
Text="Date"
Style="{StaticResource ContentTab}"/>
Style="{StaticResource ContentTab}" />
<Rectangle
Style="{StaticResource RectangleTab}"
Grid.Column="3"/>
Grid.Column="3" />
<Label
Grid.Column="4"
Text="Points"
Style="{StaticResource ContentTab}"/>
Style="{StaticResource ContentTab}" />
<Rectangle
Style="{StaticResource RectangleTab}"
Grid.Column="5"/>
Grid.Column="5" />
<Label
Grid.Column="6"
Style="{StaticResource ContentTab}"
Text="Victories"/>
Text="Victories" />
</Grid>
<CollectionView ItemsSource="{Binding Lb}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"/>
<GridItemsLayout Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<controls:LeaderboardLine PlayerName="{Binding PlayerName}" Date="{Binding Date}" Points="{Binding Points}" Victories="{Binding Victories}" />
<controls:LeaderboardLine PlayerName="{Binding PlayerName}" Date="{Binding Date}"
Points="{Binding Points}" Victories="{Binding Victories}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

@ -5,6 +5,6 @@ public partial class Leaderboard : ContentPage
public Leaderboard()
{
InitializeComponent();
BindingContext = ((App)Application.Current!).LD;
BindingContext = ((App)Application.Current!).Ld;
}
}

@ -50,8 +50,6 @@ public class TestLeaderboard
leaderboard.AddScoreInLead(game.ScoreBoard);
var game2 = new Game();
game.AddPlayerInGame(playerstest);
game.StartGame();
@ -61,7 +59,7 @@ public class TestLeaderboard
leaderboard.AddScoreInLead(game.ScoreBoard);
Assert.Equal(2, leaderboard.Lb[0].Victories);
Assert.Equal(2, leaderboard.Lb[1].Victories);
}

Loading…
Cancel
Save