Skip to content

Commit

Permalink
Merge pull request #189 from ethanmoffat/record_types
Browse files Browse the repository at this point in the history
Update immutable types to use [Record] attribute. Remove boilerplate code in favor of third-party code generation.
  • Loading branch information
ethanmoffat authored Apr 28, 2022
2 parents 0f2b155 + 6cabf65 commit 92682e4
Show file tree
Hide file tree
Showing 168 changed files with 989 additions and 2,701 deletions.
6 changes: 3 additions & 3 deletions EOBot/Interpreter/BuiltInIdentifierConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private Task LoginToCharacterAsync(string charName)
return Readonly(mapStateObj);
}

private IVariable GetMapStateCharacter(ICharacter c)
private IVariable GetMapStateCharacter(Character c)
{
var charObj = new ObjectVariable();
charObj.SymbolTable[PredefinedIdentifiers.NAME] = Readonly(new StringVariable(c.Name));
Expand All @@ -269,7 +269,7 @@ private IVariable GetMapStateCharacter(ICharacter c)
return charObj;
}

private IVariable GetMapStateNPC(INPC npc)
private IVariable GetMapStateNPC(NPC npc)
{
var npcFile = DependencyMaster.TypeRegistry[_botIndex].Resolve<IPubFileProvider>().ENFFile;

Expand All @@ -282,7 +282,7 @@ private IVariable GetMapStateNPC(INPC npc)
return npcObj;
}

private IVariable GetMapStateItem(IItem item)
private IVariable GetMapStateItem(MapItem item)
{
var itemFile = DependencyMaster.TypeRegistry[_botIndex].Resolve<IPubFileProvider>().EIFFile;

Expand Down
12 changes: 6 additions & 6 deletions EOBot/TrainerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ protected override async Task DoWorkAsync(CancellationToken ct)
var charInventoryRepo = c.Resolve<ICharacterInventoryRepository>();
var walkValidator = c.Resolve<IWalkValidationActions>();

var healItems = new List<IInventoryItem>();
var healSpells = new List<IInventorySpell>();
var healItems = new List<InventoryItem>();
var healSpells = new List<InventorySpell>();

int attackCount = 0, cachedPlayerCount = 0;
bool time_to_die = false;
Expand Down Expand Up @@ -297,7 +297,7 @@ private async Task PickUpItems(IMapCellState cellState)
}
}

private async Task PickUpItem(IItem item)
private async Task PickUpItem(MapItem item)
{
await TrySend(() =>
{
Expand All @@ -311,14 +311,14 @@ await TrySend(() =>
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
}

private async Task JunkItem(IItem item)
private async Task JunkItem(MapItem item)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.JunkItem, $"{item.Amount,7} - {_itemData.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _itemActions.JunkItem(item.ItemID, item.Amount));
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
}

private async Task CastHealSpell(IEnumerable<IInventorySpell> healSpells)
private async Task CastHealSpell(IEnumerable<InventorySpell> healSpells)
{
var spellToUse = _spellData
.Where(x => healSpells.Any(y => y.ID == x.ID) && x.Target != SpellTarget.Group)
Expand All @@ -335,7 +335,7 @@ private async Task CastHealSpell(IEnumerable<IInventorySpell> healSpells)
await Task.Delay((int)Math.Round(spellToUse.CastTime / 2.0 * 950)); // ?
}

private async Task UseHealItem(IEnumerable<IInventoryItem> healItems)
private async Task UseHealItem(IEnumerable<InventoryItem> healItems)
{
var itemToUse = _itemData
.Where(x => healItems.Any(y => y.ItemID == x.ID))
Expand Down
178 changes: 22 additions & 156 deletions EOLib/Domain/Character/Character.cs
Original file line number Diff line number Diff line change
@@ -1,177 +1,43 @@
using System.Collections.Generic;
using Amadevus.RecordGenerator;
using EOLib.Domain.Spells;

namespace EOLib.Domain.Character
{
public class Character : ICharacter
[Record]
public sealed partial class Character : ISpellTargetable
{
public int ID { get; private set; }

public int Index => ID;

public string Name { get; private set; }

public string Title { get; private set; }

public string GuildName { get; private set; }

public string GuildRank { get; private set; }

public string GuildTag { get; private set; }

public byte ClassID { get; private set; }

public AdminLevel AdminLevel { get; private set; }

public ICharacterRenderProperties RenderProperties { get; private set; }

public ICharacterStats Stats { get; private set; }

public int MapID { get; private set; }

public bool NoWall { get; private set; }

public Character()
private static readonly Character _default = new Builder
{
RenderProperties = new CharacterRenderProperties();
Stats = new CharacterStats();
}
Stats = new CharacterStats(),
RenderProperties = new CharacterRenderProperties.Builder().ToImmutable()
}.ToImmutable();

public ICharacter WithID(int id)
{
var character = MakeCopy(this);
character.ID = id;
return character;
}
public static Character Default => _default;

public ICharacter WithName(string name)
{
var character = MakeCopy(this);
character.Name = name;
return character;
}
public int ID { get; }

public ICharacter WithTitle(string title)
{
var character = MakeCopy(this);
character.Title = title;
return character;
}
public int Index => ID;

public ICharacter WithGuildName(string guildName)
{
var character = MakeCopy(this);
character.GuildName = guildName;
return character;
}
public string Name { get; }

public ICharacter WithGuildRank(string guildRank)
{
var character = MakeCopy(this);
character.GuildRank = guildRank;
return character;
}
public string Title { get; }

public ICharacter WithGuildTag(string guildTag)
{
var character = MakeCopy(this);
character.GuildTag = guildTag;
return character;
}
public string GuildName { get; }

public ICharacter WithClassID(byte newClassID)
{
var character = MakeCopy(this);
character.ClassID = newClassID;
return character;
}
public string GuildRank { get; }

public ICharacter WithAdminLevel(AdminLevel level)
{
var character = MakeCopy(this);
character.AdminLevel = level;
return character;
}
public string GuildTag { get; }

public ICharacter WithRenderProperties(ICharacterRenderProperties renderProperties)
{
var character = MakeCopy(this);
character.RenderProperties = renderProperties;
return character;
}
public byte ClassID { get; }

public ICharacter WithStats(ICharacterStats stats)
{
var character = MakeCopy(this);
character.Stats = stats;
return character;
}
public AdminLevel AdminLevel { get; }

public ICharacter WithMapID(int mapID)
{
var character = MakeCopy(this);
character.MapID = mapID;
return character;
}
public CharacterRenderProperties RenderProperties { get; }

public ICharacter WithNoWall(bool noWall)
{
var character = MakeCopy(this);
character.NoWall = noWall;
return character;
}
public CharacterStats Stats { get; }

private static Character MakeCopy(ICharacter source)
{
return new Character
{
ID = source.ID,
Name = source.Name,
Title = source.Title,
GuildName = source.GuildName,
GuildRank = source.GuildRank,
GuildTag = source.GuildTag,
ClassID = source.ClassID,
AdminLevel = source.AdminLevel,
RenderProperties = source.RenderProperties,
Stats = source.Stats,
MapID = source.MapID,
NoWall = source.NoWall
};
}
public int MapID { get; }

public override bool Equals(object obj)
{
return obj is Character character &&
ID == character.ID &&
Name == character.Name &&
Title == character.Title &&
GuildName == character.GuildName &&
GuildRank == character.GuildRank &&
GuildTag == character.GuildTag &&
ClassID == character.ClassID &&
AdminLevel == character.AdminLevel &&
RenderProperties.Equals(character.RenderProperties) &&
Stats.Equals(character.Stats) &&
MapID == character.MapID &&
NoWall == character.NoWall;
}

public override int GetHashCode()
{
int hashCode = 170256730;
hashCode = hashCode * -1521134295 + ID.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Title);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(GuildName);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(GuildRank);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(GuildTag);
hashCode = hashCode * -1521134295 + ClassID.GetHashCode();
hashCode = hashCode * -1521134295 + AdminLevel.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<ICharacterRenderProperties>.Default.GetHashCode(RenderProperties);
hashCode = hashCode * -1521134295 + EqualityComparer<ICharacterStats>.Default.GetHashCode(Stats);
hashCode = hashCode * -1521134295 + MapID.GetHashCode();
hashCode = hashCode * -1521134295 + NoWall.GetHashCode();
return hashCode;
}
public bool NoWall { get; }
}
}
4 changes: 2 additions & 2 deletions EOLib/Domain/Character/CharacterActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public void CastSpell(int spellId, ISpellTargetable target)
}
else
{
var spellTargetType = target is INPC
var spellTargetType = target is NPC.NPC
? SpellTargetType.NPC
: target is ICharacter
: target is Character
? SpellTargetType.Player
: throw new InvalidOperationException("Invalid spell target specified, must be player or character");
builder = builder.AddChar((byte)spellTargetType);
Expand Down
8 changes: 4 additions & 4 deletions EOLib/Domain/Character/CharacterCreateData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class CharacterCreateData : ICharacterCreateData
{
public CharacterReply Response { get; }

private readonly List<ICharacter> _characters;
public IReadOnlyList<ICharacter> Characters => _characters;
private readonly List<Character> _characters;
public IReadOnlyList<Character> Characters => _characters;

public CharacterCreateData(CharacterReply response, List<ICharacter> characters)
public CharacterCreateData(CharacterReply response, List<Character> characters)
{
Response = response;
_characters = characters;
Expand All @@ -21,6 +21,6 @@ public interface ICharacterCreateData : ITranslatedData
{
CharacterReply Response { get; }

IReadOnlyList<ICharacter> Characters { get; }
IReadOnlyList<Character> Characters { get; }
}
}
20 changes: 10 additions & 10 deletions EOLib/Domain/Character/CharacterInventoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ namespace EOLib.Domain.Character
{
public interface ICharacterInventoryRepository
{
HashSet<IInventoryItem> ItemInventory { get; set; }
HashSet<InventoryItem> ItemInventory { get; set; }

HashSet<IInventorySpell> SpellInventory { get; set; }
HashSet<InventorySpell> SpellInventory { get; set; }
}

public interface ICharacterInventoryProvider
{
IReadOnlyCollection<IInventoryItem> ItemInventory { get; }
IReadOnlyCollection<InventoryItem> ItemInventory { get; }

IReadOnlyCollection<IInventorySpell> SpellInventory { get; }
IReadOnlyCollection<InventorySpell> SpellInventory { get; }
}

[AutoMappedType(IsSingleton = true)]
public class CharacterInventoryRepository : ICharacterInventoryRepository, ICharacterInventoryProvider
{
public HashSet<IInventoryItem> ItemInventory { get; set; }
public HashSet<IInventorySpell> SpellInventory { get; set; }
public HashSet<InventoryItem> ItemInventory { get; set; }
public HashSet<InventorySpell> SpellInventory { get; set; }

IReadOnlyCollection<IInventoryItem> ICharacterInventoryProvider.ItemInventory => ItemInventory;
IReadOnlyCollection<IInventorySpell> ICharacterInventoryProvider.SpellInventory => SpellInventory;
IReadOnlyCollection<InventoryItem> ICharacterInventoryProvider.ItemInventory => ItemInventory;
IReadOnlyCollection<InventorySpell> ICharacterInventoryProvider.SpellInventory => SpellInventory;

public CharacterInventoryRepository()
{
ItemInventory = new HashSet<IInventoryItem>();
SpellInventory = new HashSet<IInventorySpell>();
ItemInventory = new HashSet<InventoryItem>();
SpellInventory = new HashSet<InventorySpell>();
}
}
}
Loading

0 comments on commit 92682e4

Please sign in to comment.