add leaderboard
continuous-integration/drone/push Build is failing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent 6c2b4076f9
commit 8f0c610e7d

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
namespace QwirkleClassLibrary.Players
{
public class Leaderboard
{
public ReadOnlyCollection<Score> LB => leaderb.AsReadOnly();
private readonly List<Score> leaderb = new();
public Leaderboard() { }
public int IsPlayerIn(Player player)
{
for (int i = 0; i < leaderb.Count; i++)
{
if (player.NameTag == leaderb[i].PlayerName)
{
return i;
}
}
return -1;
}
public void AddScoreInLead(ReadOnlyDictionary<Player, int> ScoreBoard)
{
DateTime now = DateTime.Today;
bool first = true;
var sb = ScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key.NameTag);
foreach (KeyValuePair<Player, int> pair in sb)
{
int i = IsPlayerIn(pair.Key);
if (i != -1)
{
leaderb[i].Date = now;
if (first)
{
leaderb[i].Victories++;
}
leaderb[i].Points = pair.Value;
}
else
{
int v = 0;
if (first)
{
v = 1;
}
Score score = new Score(pair.Key.NameTag, now, pair.Value, v);
leaderb.Add(score);
}
first = false;
}
}
}
}

@ -1,27 +1,32 @@
using System;
using QwirkleClassLibrary.Boards;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QwirkleClassLibrary.Players
{
public struct Score
public class Score
{
private int PlayerScore { get; set; }
public string PlayerTag { get; }
public string PlayerName { get; private set; }
public DateTime Date { get; set; }
public int Points { get; set; }
public int Victories { get; set; }
public Score(Player? p)
public Score(string playerName, DateTime date, int points, int victories)
{
if (p == null)
{
throw new ArgumentNullException(nameof(p), "player cannot be null");
}
else
{
PlayerScore = 0;
PlayerTag = p.NameTag;
}
this.PlayerName = playerName;
this.Date = date;
this.Points = points;
this.Victories = victories;
}
public override string ToString()
{
return PlayerName + " Date last game :" + Date.ToString() + " Points :" + Points + " Win : " + Victories;
}
}
}

@ -258,6 +258,16 @@ static void ShowScoreBoard(Game g)
}
}
static void ShowLeaderboard(Leaderboard leaderboard)
{
WriteLine(" --------------------- THE LEADERBOARD : ---------------------");
for (int i=0; i<leaderboard.LB.Count; i++)
{
WriteLine("[" + i + "] " + leaderboard.LB[i].ToString());
}
}
static void MainMenu(Game game)
{
game.GiveTilesToPlayers();
@ -292,7 +302,7 @@ static void MainMenu(Game game)
static void MainGame()
{
Leaderboard leaderboard = new Leaderboard();
Console.ForegroundColor = ConsoleColor.DarkGray;
WriteLine(" ===================== WELCOME TO QWIRKLE ! =====================");
Console.ResetColor();
@ -335,7 +345,7 @@ static void MainGame()
MainMenu(game);
break;
case 2:
//ShowLeaderboard();
ShowLeaderboard(leaderboard);
break;
case 3:
return;

Loading…
Cancel
Save