Skip to content

Commit

Permalink
Update collections to use immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Nov 15, 2023
1 parent b3e08be commit 8503c90
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Edelstein.Common.Constants;
using Edelstein.Protocol.Gameplay.Game.Combat;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Stats;
using Edelstein.Protocol.Gameplay.Game.Objects.User;

namespace Edelstein.Common.Gameplay.Game.Combat.Skills.Resistance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Edelstein.Common.Constants;
using Edelstein.Protocol.Gameplay.Game.Combat;
using Edelstein.Protocol.Gameplay.Game.Objects;
using Edelstein.Protocol.Gameplay.Game.Objects.AffectedArea;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Stats;
using Edelstein.Protocol.Gameplay.Game.Objects.Summoned;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Edelstein.Common.Gameplay.Game.Objects.User.Effects;
using System.Collections.Immutable;
using Edelstein.Common.Gameplay.Game.Objects.User.Effects;
using Edelstein.Protocol.Gameplay.Game.Conversations.Speakers.Facades;
using Edelstein.Protocol.Gameplay.Game.Objects.User;

Expand Down Expand Up @@ -37,5 +38,5 @@ public bool HasSlotFor(int templateID, short count = 1)
public bool HasSlotFor(IDictionary<int, short> templates)
=> _user.StageUser.Context.Managers.Inventory.HasSlotFor(_user.Character.Inventories, templates
.Select(kv => Tuple.Create(kv.Key, kv.Value))
.ToList());
.ToImmutableArray());
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketUserCharacterInfoReq
.Select(kv => kv.Value)
.Select(i => i.ID)
.Where(i => i / 10000 == 301)
.ToList() ?? new List<int>();
.ToImmutableArray() ?? ImmutableArray<int>.Empty;

packet.WriteInt(chairs.Count);
packet.WriteInt(chairs.Length);
foreach (var chair in chairs)
packet.WriteInt(chair);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Duey.Abstractions;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Protocol.Gameplay.Game.Objects;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Templates;
using Edelstein.Protocol.Gameplay.Models.Characters.Skills.Templates;
Expand Down Expand Up @@ -49,9 +50,9 @@ public MobTemplate(int id, IDataNode node, IDataNode info)
var elemCount = 0;
var elemAttrs = info.ResolveString("elemAttr") ?? string.Empty;

foreach (var group in elemAttrs.GroupBy(_ => elemCount++ / 2).ToList())
foreach (var group in elemAttrs.GroupBy(_ => elemCount++ / 2).ToImmutableArray())
{
var groupList = group.ToList();
var groupList = group.ToImmutableArray();
var elem = groupList[0] switch
{
'P' => Element.Physical,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Protocol.Gameplay.Game.Quests.Templates;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Common.Gameplay.Game.Spatial;
using Edelstein.Common.Utilities.Spatial;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Protocol.Gameplay.Shop.Commodities;
using Edelstein.Protocol.Utilities.Templates;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Edelstein.Common.Constants;
using System.Collections.Immutable;
using Edelstein.Common.Constants;
using Edelstein.Common.Utilities.Packets;
using Edelstein.Protocol.Gameplay.Models.Characters;
using Edelstein.Protocol.Gameplay.Models.Characters.Skills;
Expand Down Expand Up @@ -59,13 +60,13 @@ public void ResetByJobLevel(int jobLevel)
{
foreach (var kv in _character.Skills.Records
.Where(kv => JobConstants.GetJobLevel(jobLevel) == jobLevel)
.ToList())
.ToImmutableArray())
Set(kv.Key, 0);
}

public void ResetAll()
{
foreach (var kv in _character.Skills.Records.ToList())
foreach (var kv in _character.Skills.Records.ToImmutableArray())
Set(kv.Key, 0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Protocol.Gameplay.Models.Characters.Skills.Templates;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Immutable;
using Duey.Abstractions;
using Edelstein.Protocol.Gameplay.Models.Inventories.Templates.Sets;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ public override void Gather()
var inventoryCopy = _inventory.Items
.Where(kv => kv.Key > 0)
.OrderBy(kv => kv.Key)
.ToList();
.ToImmutableArray();
short position = 1;

inventoryCopy.ForEach(kv => RemoveSlot(kv.Key));
inventoryCopy.ForEach(kv => SetSlot(position++, kv.Value));
foreach (var kv in inventoryCopy)
RemoveSlot(kv.Key);
foreach (var kv in inventoryCopy)
SetSlot(position++, kv.Value);
}

public override void Sort()
Expand All @@ -160,15 +162,17 @@ public override void Sort()
.Where(kv => kv.Key > 0)
.OrderBy(kv => kv.Value.ID)
.ThenByDescending(kv => kv.Value is IItemSlotBundle bundle ? bundle.Number : 1)
.ToList();
.ToImmutableArray();

inventoryCopy.ForEach(kv => RemoveSlot(kv.Key));
inventoryCopy.ForEach(kv => Add(kv.Value));
foreach (var kv in inventoryCopy)
RemoveSlot(kv.Key);
foreach (var kv in inventoryCopy)
Add(kv.Value);
}
public override void Clear() =>
_inventory.Items
.Where(kv => kv.Key > 0)
.ToList()
.ToImmutableList()
.ForEach(kv => RemoveSlot(kv.Key));

public override short Add(int templateID) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Edelstein.Protocol.Services.Social;
using System.Collections.Immutable;
using Edelstein.Protocol.Services.Social;
using Edelstein.Protocol.Utilities.Packets;

namespace Edelstein.Common.Gameplay.Social;
Expand All @@ -7,7 +8,7 @@ public static class PartyExtensions
{
public static void WritePartyInfo(this IPacketWriter writer, IPartyMembership party)
{
var members = party.Members.Values.ToList();
var members = party.Members.Values.ToImmutableList();

for (var i = 0; i < 6; i++)
writer.WriteInt(members.ElementAtOrDefault(i)?.CharacterID ?? 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Buffers;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels;
using Edelstein.Common.Utilities.Packets;
using Edelstein.Protocol.Network;
using Edelstein.Protocol.Network.Transports;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Buffers;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels;
using Edelstein.Common.Utilities.Packets;
using Edelstein.Protocol.Network;
using Edelstein.Protocol.Network.Transports;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Frozen;
using Edelstein.Protocol.Utilities.Repositories;

namespace Edelstein.Common.Utilities.Repositories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using Edelstein.Protocol.Utilities.Tickers;
using Edelstein.Protocol.Utilities.Tickers;
using Microsoft.Extensions.Logging;

namespace Edelstein.Common.Utilities.Tickers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Collections.Immutable;
using System.Diagnostics;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Utilities.Templates;
using Gma.DataStructures.StringSearch;
Expand Down Expand Up @@ -72,7 +73,7 @@ protected override async Task Execute(IFieldUser user, TArgs args)
var results = _trie
.Retrieve(args.Search.ToLower())
.DistinctBy(d => d.ID)
.ToList();
.ToImmutableArray();
var elapsed = stopwatch.Elapsed;

if (args.Search.All(char.IsDigit) && !results.Any())
Expand All @@ -90,15 +91,15 @@ protected override async Task Execute(IFieldUser user, TArgs args)
{
var maxPerPage = 6;
var minPage = 1;
var maxPage = (int)Math.Ceiling((double)results.Count / maxPerPage);
var maxPage = (int)Math.Ceiling((double)results.Length / maxPerPage);
var currentPage = 1;

while (true)
{
var items = results
.Skip(maxPerPage * (currentPage - 1))
.Take(maxPerPage)
.ToList();
.ToImmutableArray();
var menu = items.ToDictionary(
r => r.ID,
r => $"{r.DisplayString} ({r.ID})"
Expand All @@ -107,7 +108,7 @@ protected override async Task Execute(IFieldUser user, TArgs args)
if (currentPage < maxPage) menu.Add(-10, "#rNext page#k");
if (currentPage > minPage) menu.Add(-20, "#rPrevious page#k");

var selection = target.AskMenu($"Found {results.Count} results for '{args.Search}' in {elapsed.TotalMilliseconds:F2}ms (page {currentPage} of {maxPage})", menu);
var selection = target.AskMenu($"Found {results.Length} results for '{args.Search}' in {elapsed.TotalMilliseconds:F2}ms (page {currentPage} of {maxPage})", menu);

if (selection == -10) { currentPage++; continue; }
if (selection == -20) { currentPage--; continue; }
Expand Down
10 changes: 6 additions & 4 deletions src/plugin/Edelstein.Plugin.Rue/Commands/Admin/EquipCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using System.Collections.Immutable;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Inventories;
using Edelstein.Protocol.Gameplay.Models.Inventories.Items;

Expand All @@ -11,11 +12,12 @@ public class EquipCommand : AbstractCommand

public override async Task Execute(IFieldUser user, string[] args)
{
var equipped = user.Character.Inventories[ItemInventoryType.Equip]?.Items.ToList() ?? new List<KeyValuePair<short, IItemSlot>>();
if (equipped.Count == 0) return;
var equipped = user.Character.Inventories[ItemInventoryType.Equip]?.Items
.ToImmutableArray() ?? ImmutableArray<KeyValuePair<short, IItemSlot>>.Empty;
if (equipped.Length == 0) return;

var slot = await user.Prompt(target => target.AskMenu("Which equipment would you like to modify?", equipped
.ToDictionary(
.ToImmutableDictionary(
i => (int)i.Key,
i => $"{i.Value.ID}"
)), -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ITemplateManager<IFieldStringTemplate> strings
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), $"{s.StreetName}: {s.MapName}")));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.MapName, $"{s.StreetName}: {s.MapName}")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ITemplateManager<IItemStringTemplate> strings
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name)));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, s.Name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ITemplateManager<IMobStringTemplate> strings
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name)));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, s.Name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ITemplateManager<INPCStringTemplate> strings
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name)));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, $"{s.Name}{(!string.IsNullOrWhiteSpace(s.Func) ? $": {s.Func}" : "")}")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ITemplateManager<IQuestTemplate> templates
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name)));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, s.Name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ITemplateManager<ISkillStringTemplate> strings
protected override async Task<IEnumerable<TemplateCommandIndex>> Indices()
{
var result = new List<TemplateCommandIndex>();
var strings = (await _strings.RetrieveAll()).ToList();
var strings = await _strings.RetrieveAll();

result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.ID.ToString(), s.Name)));
result.AddRange(strings.Select(s => new TemplateCommandIndex(s.ID, s.Name, s.Name)));
Expand Down

0 comments on commit 8503c90

Please sign in to comment.