the scoreboard work
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent fbcce2b5c8
commit 324c394d0a

@ -34,10 +34,10 @@ namespace QwirkleClassLibrary.Boards
} }
[DataMember] [DataMember]
public int Rows { get; set; } public double Rows { get; set; }
[DataMember] [DataMember]
public int Columns { get; set; } public double Columns { get; set; }
/// <summary> /// <summary>
/// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board. /// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board.

@ -5,12 +5,15 @@ using QwirkleClassLibrary.Tiles;
using QwirkleClassLibrary.Boards; using QwirkleClassLibrary.Boards;
using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Events;
using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Players;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using static System.Formats.Asn1.AsnWriter;
namespace QwirkleClassLibrary.Games namespace QwirkleClassLibrary.Games
{ {
[DataContract] [DataContract]
public class Game : IPlayer, IRules public class Game : IPlayer, IRules, INotifyPropertyChanged
{ {
[DataMember] [DataMember]
private TileBag? bag = null; private TileBag? bag = null;
@ -30,10 +33,15 @@ namespace QwirkleClassLibrary.Games
private readonly List<Player> players = []; private readonly List<Player> players = [];
[DataMember] [DataMember]
private readonly Dictionary<string, int> scoreBoard = []; private readonly Dictionary<string, int> scoreBoard = new Dictionary<string, int>();
public ReadOnlyDictionary<string, int> ScoreBoard => scoreBoard.AsReadOnly(); public ReadOnlyDictionary<string, int> ScoreBoard => scoreBoard.AsReadOnly();
private ObservableCollection<KeyValuePair<string, int>> observableScoreBoard = new ObservableCollection<KeyValuePair<string, int>>();
public ReadOnlyObservableCollection<KeyValuePair<string, int>> ObservableScoreBoard =>
new ReadOnlyObservableCollection<KeyValuePair<string, int>>(observableScoreBoard);
[DataMember] [DataMember]
private readonly List<Cell> cellUsed = []; private readonly List<Cell> cellUsed = [];
@ -61,6 +69,14 @@ namespace QwirkleClassLibrary.Games
public event EventHandler<SwapTilesNotifiedEventArgs>? SwapTilesNotified; public event EventHandler<SwapTilesNotifiedEventArgs>? SwapTilesNotified;
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnSwapTiles(SwapTilesNotifiedEventArgs args) protected virtual void OnSwapTiles(SwapTilesNotifiedEventArgs args)
=> SwapTilesNotified?.Invoke(this, args); => SwapTilesNotified?.Invoke(this, args);
@ -104,7 +120,7 @@ namespace QwirkleClassLibrary.Games
{ {
Player pl = CreatePlayer(tag); Player pl = CreatePlayer(tag);
players.Add(pl); players.Add(pl);
scoreBoard.Add(pl.NameTag, 0); SetScoreBoard(pl.NameTag, 0);
} }
OnPlayerNotified(new AddPlayerNotifiedEventArgs("Players were correctly added.")); OnPlayerNotified(new AddPlayerNotifiedEventArgs("Players were correctly added."));
@ -621,7 +637,9 @@ namespace QwirkleClassLibrary.Games
if (!scoreBoard.TryAdd(player.NameTag, score)) if (!scoreBoard.TryAdd(player.NameTag, score))
{ {
scoreBoard[player.NameTag] += score;
scoreBoard.TryGetValue(player.NameTag, out int scoreold);
SetScoreBoard(player.NameTag, score + scoreold);
} }
return score; return score;
@ -773,7 +791,8 @@ namespace QwirkleClassLibrary.Games
{ {
OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false; GameRunning = false;
scoreBoard[player.NameTag] += 6; scoreBoard.TryGetValue(player.NameTag, out int scoreold);
SetScoreBoard(player.NameTag, 6 + scoreold);
return true; return true;
} }
@ -789,5 +808,25 @@ namespace QwirkleClassLibrary.Games
board = CreateBoard(); board = CreateBoard();
GameRunning = false; GameRunning = false;
} }
public void SetScoreBoard(string name, int score)
{
if (scoreBoard.ContainsKey(name))
{
scoreBoard[name] = score;
}
else
{
scoreBoard.Add(name, score);
}
observableScoreBoard.Clear();
foreach (var item in scoreBoard)
{
observableScoreBoard.Add(item);
}
OnPropertyChanged(nameof(ObservableScoreBoard));
}
} }
} }

@ -108,6 +108,8 @@ public partial class Gameboard : ContentPage
ColorBC3 = Colors.Transparent; ColorBC3 = Colors.Transparent;
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4)); OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 1) if (game.GetPlayingPlayerPosition() == 1)
@ -118,6 +120,8 @@ public partial class Gameboard : ContentPage
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC1)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2)); OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 2) if (game.GetPlayingPlayerPosition() == 2)
{ {
@ -125,8 +129,10 @@ public partial class Gameboard : ContentPage
ColorBC1 = Colors.Transparent; ColorBC1 = Colors.Transparent;
ColorBC2 = Colors.Transparent; ColorBC2 = Colors.Transparent;
ColorBC4 = Colors.Transparent; ColorBC4 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC3)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2)); OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
if (game.GetPlayingPlayerPosition() == 3) if (game.GetPlayingPlayerPosition() == 3)
{ {
@ -134,8 +140,10 @@ public partial class Gameboard : ContentPage
ColorBC1 = Colors.Transparent; ColorBC1 = Colors.Transparent;
ColorBC3 = Colors.Transparent; ColorBC3 = Colors.Transparent;
ColorBC2 = Colors.Transparent; ColorBC2 = Colors.Transparent;
OnPropertyChanged(nameof(ColorBC4)); OnPropertyChanged(nameof(ColorBC1));
OnPropertyChanged(nameof(ColorBC2));
OnPropertyChanged(nameof(ColorBC3)); OnPropertyChanged(nameof(ColorBC3));
OnPropertyChanged(nameof(ColorBC4));
} }
} }

@ -7,13 +7,11 @@
x:Name="root"> x:Name="root">
<ContentPage.Resources> <ContentPage.Resources>
<ResourceDictionary>
<x:Double x:Key="CellWidth">75</x:Double> <x:Double x:Key="CellWidth">75</x:Double>
<x:Double x:Key="CellHeight">75</x:Double> <x:Double x:Key="CellHeight">75</x:Double>
<x:Double x:Key="VerticalSpacing">1</x:Double> <x:Double x:Key="VerticalSpacing">1</x:Double>
<x:Double x:Key="HorizontalSpacing">1</x:Double> <x:Double x:Key="HorizontalSpacing">1</x:Double>
<toolkit:MultiMathExpressionConverter x:Key="multiMathExpressionConverter" /> <toolkit:MultiMathExpressionConverter x:Key="multiMathExpressionConverter" />
</ResourceDictionary>
</ContentPage.Resources> </ContentPage.Resources>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>

@ -15,7 +15,26 @@
</CollectionView.ItemsLayout> </CollectionView.ItemsLayout>
<CollectionView.ItemTemplate> <CollectionView.ItemTemplate>
<DataTemplate> <DataTemplate>
<controls:ScoreboardLine Nameplayer="{Binding Key}" Score="{Binding Value}"/> <StackLayout>
<Grid ColumnDefinitions="4*, auto, 2*"
RowDefinitions="50">
<Label
Grid.Column="0"
Text="{Binding Key}"
Style="{StaticResource ContentTab}"/>
<Rectangle
Style="{StaticResource RectangleTab}"
Grid.Column="1"/>
<Label
Grid.Column="2"
Text="{Binding Value}"
Style="{StaticResource ContentTab}"/>
</Grid>
<Rectangle/>
</StackLayout>
</DataTemplate> </DataTemplate>
</CollectionView.ItemTemplate> </CollectionView.ItemTemplate>
</CollectionView> </CollectionView>

@ -5,18 +5,16 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
namespace Qwirkle.Views; namespace Qwirkle.Views
{
public partial class Scoreboard : ContentView, INotifyPropertyChanged public partial class Scoreboard : ContentView, INotifyPropertyChanged
{ {
private Game game = ((App)Application.Current!).Game; private Game game = ((App)Application.Current!).Game;
private IOrderedEnumerable<KeyValuePair<string, int>> scoreboard; private ObservableCollection<KeyValuePair<string, int>> scoreboardList;
private ObservableCollection<KeyValuePair<string, int>>? scoreboardList;
public ObservableCollection<KeyValuePair<string, int>> ScoreboardList public ObservableCollection<KeyValuePair<string, int>> ScoreboardList
{ {
get => scoreboardList!; get => scoreboardList;
set set
{ {
if (scoreboardList != value) if (scoreboardList != value)
@ -26,21 +24,33 @@ public partial class Scoreboard : ContentView, INotifyPropertyChanged
} }
} }
} }
public Scoreboard() public Scoreboard()
{ {
InitializeComponent(); InitializeComponent();
BindingContext = this; BindingContext = this;
scoreboard = game.ScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key);
UpdateScoreboard();
game.PropertyChanged += OnScoreChanged;
}
private void UpdateScoreboard()
{
var scoreboard = game.ObservableScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key);
ScoreboardList = new ObservableCollection<KeyValuePair<string, int>>(scoreboard); ScoreboardList = new ObservableCollection<KeyValuePair<string, int>>(scoreboard);
}
private void OnScoreChanged(object sender, EventArgs e)
{
UpdateScoreboard();
} }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) protected void OnPropertyChanged(string propertyName)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
} }
}

@ -18,12 +18,6 @@ public partial class TileView : ContentView
public TileView() public TileView()
{ {
InitializeComponent(); InitializeComponent();
((App)Application.Current!).Game.Board.PropertyChanged += Board_PropertyChanged;
}
private void Board_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Console.WriteLine(Shape);
} }
public static readonly BindableProperty ColorProperty = public static readonly BindableProperty ColorProperty =

Loading…
Cancel
Save