Skip to content

Commit

Permalink
Removed duplicate constructors
Browse files Browse the repository at this point in the history
Moved the logic into extension methods for now
  • Loading branch information
grofit committed Oct 26, 2022
1 parent 9c685d3 commit 74db592
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 62 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.25.{build}
version: 0.26.{build}
branches:
only:
- master
Expand Down
3 changes: 2 additions & 1 deletion src/OpenRpg.Genres/Builders/CharacterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using OpenRpg.Core.Utils;
using OpenRpg.Genres.Characters;
using OpenRpg.Genres.Extensions;
using OpenRpg.Genres.Persistence.Characters;
using OpenRpg.Genres.Persistence.Classes;
using OpenRpg.Genres.Persistence.Items;
Expand Down Expand Up @@ -124,7 +125,7 @@ public ICharacter Build()

var persistedClass = new ClassData(_classId, _classLevels);
var persistedEquipmentData = _equipment
.ToDictionary(x => x.Key, x => new ItemData(x.Value));
.ToDictionary(x => x.Key, x => x.Value.ToDataModel());

var persistedEquipment = new EquipmentData(persistedEquipmentData);

Expand Down
34 changes: 27 additions & 7 deletions src/OpenRpg.Genres/Extensions/PersistenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using OpenRpg.Core.Classes;
using OpenRpg.Genres.Characters;
using OpenRpg.Genres.Persistence.Characters;
Expand All @@ -14,18 +15,37 @@ namespace OpenRpg.Genres.Extensions
public static class PersistenceExtensions
{
public static CharacterData ToDataModel(this ICharacter character)
{ return new CharacterData(character); }

{
return new CharacterData(character.UniqueId,
character.NameLocaleId, character.DescriptionLocaleId, character.GenderType,
character.Race.Id, character.Class.ToDataModel(), character.State,
character.Equipment.ToDataModel(), character.Variables);
}

public static ItemData ToDataModel(this IUniqueItem item)
{ return new ItemData(item); }
{
return new ItemData(item.UniqueId,
item.ItemTemplate.Id,
item.Modifications.Select(x => x.Id).ToArray(),
item.Variables);
}

public static InventoryData ToDataModel(this IInventory inventory)
{ return new InventoryData(inventory); }

{
var items = inventory.Items.Select(x => (x as IUniqueItem).ToDataModel()).ToArray();
return new InventoryData(items, inventory.Variables);
}

public static EquipmentData ToDataModel(this IEquipment equipment)
{ return new EquipmentData(equipment); }
{
var slots = equipment.Slots
.ToDictionary(x => x.Key,
x => (x.Value.SlottedItem as IUniqueItem).ToDataModel());

return new EquipmentData(slots, equipment.Variables);
}

public static ClassData ToDataModel(this IClass @class)
{ return new ClassData(@class); }
{ return new ClassData(@class.ClassTemplate.Id, @class.Level, @class.Variables); }
}
}
14 changes: 1 addition & 13 deletions src/OpenRpg.Genres/Persistence/Characters/CharacterData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using OpenRpg.Genres.Characters;
using OpenRpg.Genres.Extensions;
using OpenRpg.Genres.Persistence.Classes;
using OpenRpg.Genres.Persistence.Items.Equipment;

Expand Down Expand Up @@ -30,18 +31,5 @@ public CharacterData(Guid id, string nameLocaleId, string descriptionLocaleId, b
EquipmentData = equipmentData;
Variables = variables ?? new Dictionary<int, object>();
}

public CharacterData(ICharacter character)
{
Id = character.UniqueId;
NameLocaleId = character.NameLocaleId;
DescriptionLocaleId = character.DescriptionLocaleId;
StateVariables = character.State;
ClassData = new ClassData(character.Class);
GenderType = character.GenderType;
RaceTemplateId = character.Race.Id;
EquipmentData = new EquipmentData(character.Equipment);
Variables = character.Variables;
}
}
}
8 changes: 0 additions & 8 deletions src/OpenRpg.Genres/Persistence/Classes/ClassData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using OpenRpg.Core.Classes;

namespace OpenRpg.Genres.Persistence.Classes
{
Expand All @@ -15,12 +14,5 @@ public ClassData(int classTemplateId, int classLevel, IReadOnlyDictionary<int, o
ClassLevel = classLevel;
Variables = variables ?? new Dictionary<int, object>();
}

public ClassData(IClass @class)
{
ClassTemplateId = @class.ClassTemplate.Id;
ClassLevel = @class.Level;
Variables = @class.Variables;
}
}
}
12 changes: 0 additions & 12 deletions src/OpenRpg.Genres/Persistence/Items/Equipment/EquipmentData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using OpenRpg.Items;
using OpenRpg.Items.Equipment;

namespace OpenRpg.Genres.Persistence.Items.Equipment
{
Expand All @@ -15,14 +12,5 @@ public EquipmentData(IReadOnlyDictionary<int, ItemData> slots, IReadOnlyDictiona
Slots = slots;
Variables = variables ?? new Dictionary<int, object>();
}

public EquipmentData(IEquipment equipment)
{
Slots = equipment.Slots
.ToDictionary(x => x.Key,
x => new ItemData(x.Value.SlottedItem as IUniqueItem));

Variables = equipment.Variables;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using OpenRpg.Items;
using OpenRpg.Items.Inventory;

namespace OpenRpg.Genres.Persistence.Items.Inventory
{
Expand All @@ -15,11 +12,5 @@ public InventoryData(IReadOnlyCollection<ItemData> items, IReadOnlyDictionary<in
Items = items;
Variables = variables;
}

public InventoryData(IInventory inventory)
{
Items = inventory.Items.Select(x => new ItemData(x as IUniqueItem)).ToArray();
Variables = inventory.Variables;
}
}
}
12 changes: 1 addition & 11 deletions src/OpenRpg.Genres/Persistence/Items/ItemData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRpg.Items;

namespace OpenRpg.Genres.Persistence.Items
{
Expand All @@ -12,20 +10,12 @@ public class ItemData
public int[] ModificationTypes { get; }
public IReadOnlyDictionary<int, object> Variables { get; }

public ItemData(Guid id, int itemTemplateId, int[] modificationTypes, Dictionary<int, object> variables = null)
public ItemData(Guid id, int itemTemplateId, int[] modificationTypes, IReadOnlyDictionary<int, object> variables = null)
{
Id = id;
ItemTemplateId = itemTemplateId;
ModificationTypes = modificationTypes;
Variables = variables ?? new Dictionary<int, object>();
}

public ItemData(IUniqueItem item)
{
Id = item.UniqueId;
ItemTemplateId = item.ItemTemplate.Id;
ModificationTypes = item.Modifications.Select(x => x.Id).ToArray();
Variables = item.Variables;
}
}
}

0 comments on commit 74db592

Please sign in to comment.