Skip to content

Commit

Permalink
Changed data only classes to records, slight code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcidev committed Aug 10, 2024
1 parent cdb667e commit ca19694
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 77 deletions.
18 changes: 1 addition & 17 deletions Client.Logic/Data/Achievements/Achievement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,8 @@

namespace Client.Logic.Data.Achievements
{
public class Achievement
public record Achievement(UInt32 Id, UInt32 ParentId, DateTime? CompletionDate, IReadOnlyCollection<Criteria> Criterias)
{
public UInt32 Id { get; }

public UInt32 ParentId { get; }

public bool IsCompleted => CompletionDate.HasValue;

public DateTime? CompletionDate { get; }

public IEnumerable<Criteria> Criterias { get; }

public Achievement(UInt32 id, UInt32 parentId, DateTime? completionDate, List<Criteria> criterias)
{
Id = id;
ParentId = parentId;
CompletionDate = completionDate;
Criterias = criterias;
}
}
}
10 changes: 1 addition & 9 deletions Client.Logic/Data/Achievements/AchievementInfo.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@

namespace Client.Logic.Data.Achievements
{
public class AchievementInfo
public record AchievementInfo(string Name, string Image)
{
public string Name { get; }

public string Image { get; }

public AchievementInfo(string name, string image)
{
Name = name;
Image = image;
}
}
}
16 changes: 1 addition & 15 deletions Client.Logic/Data/Achievements/Criteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@

namespace Client.Logic.Data.Achievements
{
public class Criteria
public record Criteria(UInt32 Id, bool IsMet, UInt32 Progress, UInt32 Requirement)
{
public UInt32 Id { get; }

public bool IsMet { get; }

public UInt32 Progress { get; }

public UInt32 Requirement { get; }

public Criteria(UInt32 id, bool isMet, UInt32 progress, UInt32 requirement)
{
Id = id;
IsMet = isMet;
Progress = progress;
Requirement = requirement;
}
}
}
4 changes: 2 additions & 2 deletions Client.Logic/Data/Cards/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Client.Logic.Data.Cards
{
public class Card
public abstract class Card
{
private byte health;
private byte mana;
Expand Down Expand Up @@ -110,7 +110,7 @@ public byte DefenseModified

public Spell Spell { get; set; }

public Card(UInt32 id, CreatureType type, byte health, byte damage, byte mana, byte defense, Spell spell)
protected Card(UInt32 id, CreatureType type, byte health, byte damage, byte mana, byte defense, Spell spell)
{
this.id = id;
Type = type;
Expand Down
2 changes: 1 addition & 1 deletion Client.Logic/Data/Cards/DefensiveCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class DefensiveCard : PlayableCard
public DefensiveCard(UInt64 guid, Card card, Player player) : base(guid, card, player) { }

// Can't attack => no targets
public override IEnumerable<UInt64> GetPossibleTargets(IEnumerable<PlayableCard> enemyCards, int currentCardIndex) => new List<UInt64>();
public override IEnumerable<UInt64> GetPossibleTargets(IEnumerable<PlayableCard> enemyCards, int currentCardIndex) => [];
}
}
15 changes: 7 additions & 8 deletions Client.Logic/Data/Cards/PlayableCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ protected PlayableCard(UInt64 guid, Card card, Player player) : base (card.Id, c
Player = player;
}

private static readonly Dictionary<CreatureType, Type> derrivedClasses = new()
{
{ CreatureType.Melee, typeof(MeleeCard) },
{ CreatureType.Ranged, typeof(RangedCard) },
{ CreatureType.Defensive, typeof(DefensiveCard) }
};

public static PlayableCard Create(UInt64 guid, Card card, Player player)
{
return Activator.CreateInstance(derrivedClasses[card.Type], guid, card, player) as PlayableCard;
return card.Type switch
{
CreatureType.Melee => new MeleeCard(guid, card, player),
CreatureType.Ranged => new RangedCard(guid, card, player),
CreatureType.Defensive => new DefensiveCard(guid, card, player),
_ => throw new ArgumentOutOfRangeException($"Invalid value {card.Type} for {nameof(CreatureType)}"),
};
}

public abstract IEnumerable<UInt64> GetPossibleTargets(IEnumerable<PlayableCard> enemyCards, int currentCardIndex);
Expand Down
2 changes: 1 addition & 1 deletion Client.Logic/Data/IDataHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IDataHolder
{
IEnumerable<SelectableCard> Cards { get; }

void LoadData(Dictionary<UInt32, SelectableCard> cards);
void LoadData(IReadOnlyDictionary<UInt32, SelectableCard> cards);

SelectableCard GetCard(UInt32 id);

Expand Down
4 changes: 2 additions & 2 deletions Client.Logic/Data/Spells/Spell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Client.Logic.Data.Spells
{
public class Spell
{
private readonly SpellEffect[] spellEffects;
private readonly IReadOnlyCollection<SpellEffect> spellEffects;

public UInt32 Id { get; }
public byte ManaCost { get; }
public SpellData SpellData { get; set; }
public string Info => SpellData != null ? $"{SpellData.Name}: {SpellData.Description} (Costs: {ManaCost}mana)" : "";

public Spell(UInt32 id, byte manaCost, SpellEffect[] effets)
public Spell(UInt32 id, byte manaCost, IReadOnlyCollection<SpellEffect> effets)
{
Id = id;
ManaCost = manaCost;
Expand Down
17 changes: 2 additions & 15 deletions Client.Logic/Data/Spells/SpellData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@

namespace Client.Logic.Data.Spells
{
public class SpellData
public record SpellData(UInt32 SpellId, string Name, string Description, string AuraText, string AuraImagePath)
{
public UInt32 SpellId { get; }
public string Name { get; }
public string Description { get; }
public string AuraText { get; }
public string AuraImagePath { get; }

public SpellData(UInt32 spellId, string name, string description, string auraText, string auraImagePath)
{
SpellId = spellId;
Name = name;
Description = description;
AuraText = auraText;
AuraImagePath = auraImagePath;
}

}
}
2 changes: 1 addition & 1 deletion Client.Logic/Data/Spells/SpellTargetSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Client.Logic.Data.Spells
{
public class SpellTargetSelector
public static class SpellTargetSelector
{
private static readonly Func<Player, Player, SpellAttributes, IEnumerable<UInt64>>[] targetSelectors =
[
Expand Down
12 changes: 6 additions & 6 deletions Client.UI/Sql/DataHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Client.UI.Sql
{
public class DataHolder : IDataHolder
{
private Dictionary<UInt32, SelectableCard> cards;
private readonly Dictionary<UInt32, SpellData> spellsData = new();
private readonly Dictionary<UInt32, AchievementInfo> achievements = new();
private readonly Dictionary<UInt32, string> criteriaDescriptions = new();
private IReadOnlyDictionary<UInt32, SelectableCard> cards;
private readonly Dictionary<UInt32, SpellData> spellsData = [];
private readonly Dictionary<UInt32, AchievementInfo> achievements = [];
private readonly Dictionary<UInt32, string> criteriaDescriptions = [];

public IEnumerable<SelectableCard> Cards => cards?.Values;

Expand All @@ -26,7 +26,7 @@ public DataHolder()
}

// Loads data from database
public void LoadData(Dictionary<UInt32, SelectableCard> cards)
public void LoadData(IReadOnlyDictionary<UInt32, SelectableCard> cards)
{
using var connection = GetConnection();
connection.Open();
Expand Down Expand Up @@ -59,7 +59,7 @@ private void LoadSpellsData(SQLiteConnection connection)
}

// Loads cards
private void LoadCards(SQLiteConnection connection, Dictionary<UInt32, SelectableCard> cards)
private void LoadCards(SQLiteConnection connection, IReadOnlyDictionary<UInt32, SelectableCard> cards)
{
using var cmd = connection.CreateCommand();
cmd.CommandText = "SELECT id, name, imagePath FROM Cards";
Expand Down

0 comments on commit ca19694

Please sign in to comment.