Skip to content

Commit

Permalink
Refactor friend/ignore list loading/saving. Implement friend list fil…
Browse files Browse the repository at this point in the history
…ter in OnlineListPanel.
  • Loading branch information
ethanmoffat committed Mar 21, 2022
1 parent 8548495 commit 27b5680
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 121 deletions.
3 changes: 3 additions & 0 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static class Constants
public const string LogFilePath = "log/debug.log";
public const string LogFileFmt = "log/{0}-debug.log";

public const string FriendListFile = "config/friends.ini";
public const string IgnoreListFile = "config/ignore.ini";

//Should be easily customizable between different clients (based on graphics)
//not a config option because this shouldn't be exposed at the user level
public static readonly int[] TrapSpikeGFXObjectIDs = {449, 450, 451, 452};
Expand Down
14 changes: 9 additions & 5 deletions EndlessClient/Dialogs/Factories/FriendIgnoreListDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using EndlessClient.GameExecution;
using EndlessClient.HUD.Controls;
using EndlessClient.Old;
using EndlessClient.Services;
using EndlessClient.UIControls;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Graphics;
using EOLib.Localization;
Expand All @@ -25,6 +27,7 @@ public class FriendIgnoreListDialogFactory : IFriendIgnoreListDialogFactory
private readonly IHudControlProvider _hudControlProvider;
private readonly ITextInputDialogFactory _textInputDialogFactory;
private readonly IEOMessageBoxFactory _eoMessageBoxFactory;
private readonly IFriendIgnoreListService _friendIgnoreListService;

public FriendIgnoreListDialogFactory(IGameStateProvider gameStateProvider,
INativeGraphicsManager nativeGraphicsManager,
Expand All @@ -33,7 +36,8 @@ public FriendIgnoreListDialogFactory(IGameStateProvider gameStateProvider,
ICharacterProvider characterProvider,
IHudControlProvider hudControlProvider,
ITextInputDialogFactory textInputDialogFactory,
IEOMessageBoxFactory eoMessageBoxFactory)
IEOMessageBoxFactory eoMessageBoxFactory,
IFriendIgnoreListService friendIgnoreListService)
{
_gameStateProvider = gameStateProvider;
_nativeGraphicsManager = nativeGraphicsManager;
Expand All @@ -43,12 +47,12 @@ public FriendIgnoreListDialogFactory(IGameStateProvider gameStateProvider,
_hudControlProvider = hudControlProvider;
_textInputDialogFactory = textInputDialogFactory;
_eoMessageBoxFactory = eoMessageBoxFactory;
_friendIgnoreListService = friendIgnoreListService;
}

public ScrollingListDialog Create(bool isFriendList)
{
// todo: refactor InteractList
var textFileLines = isFriendList ? InteractList.LoadAllFriend() : InteractList.LoadAllIgnore();
var textFileLines = _friendIgnoreListService.LoadList(isFriendList ? Constants.FriendListFile : Constants.IgnoreListFile);

var dialog = new ScrollingListDialog(_gameStateProvider, _nativeGraphicsManager, _dialogButtonService)
{
Expand All @@ -67,9 +71,9 @@ public ScrollingListDialog Create(bool isFriendList)
dialog.DialogClosing += (_, _) =>
{
if (isFriendList)
InteractList.WriteFriendList(dialog.NamesList);
_friendIgnoreListService.SaveFriends(Constants.FriendListFile, dialog.NamesList);
else
InteractList.WriteIgnoreList(dialog.NamesList);
_friendIgnoreListService.SaveIgnored(Constants.IgnoreListFile, dialog.NamesList);
};

return dialog;
Expand Down
8 changes: 6 additions & 2 deletions EndlessClient/HUD/Panels/HudPanelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EndlessClient.ControlSets;
using EndlessClient.Dialogs.Factories;
using EndlessClient.Rendering.Chat;
using EndlessClient.Services;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Domain.Chat;
Expand All @@ -28,6 +29,7 @@ public class HudPanelFactory : IHudPanelFactory
private readonly IExperienceTableProvider _experienceTableProvider;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly ITrainingController _trainingController;
private readonly IFriendIgnoreListService _friendIgnoreListService;

public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
IContentManagerProvider contentManagerProvider,
Expand All @@ -38,7 +40,8 @@ public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
ICharacterInventoryProvider characterInventoryProvider,
IExperienceTableProvider experienceTableProvider,
IEOMessageBoxFactory messageBoxFactory,
ITrainingController trainingController)
ITrainingController trainingController,
IFriendIgnoreListService friendIgnoreListService)
{
_nativeGraphicsManager = nativeGraphicsManager;
_contentManagerProvider = contentManagerProvider;
Expand All @@ -50,6 +53,7 @@ public HudPanelFactory(INativeGraphicsManager nativeGraphicsManager,
_experienceTableProvider = experienceTableProvider;
_messageBoxFactory = messageBoxFactory;
_trainingController = trainingController;
_friendIgnoreListService = friendIgnoreListService;
}

public NewsPanel CreateNewsPanel()
Expand Down Expand Up @@ -101,7 +105,7 @@ public StatsPanel CreateStatsPanel()
public OnlineListPanel CreateOnlineListPanel()
{
var chatFont = _contentManagerProvider.Content.Load<SpriteFont>(Constants.FontSize08);
return new OnlineListPanel(_nativeGraphicsManager, _hudControlProvider, chatFont) { DrawOrder = HUD_CONTROL_LAYER };
return new OnlineListPanel(_nativeGraphicsManager, _hudControlProvider, _friendIgnoreListService, chatFont) { DrawOrder = HUD_CONTROL_LAYER };
}

public PartyPanel CreatePartyPanel()
Expand Down
12 changes: 8 additions & 4 deletions EndlessClient/HUD/Panels/OnlineListPanel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EndlessClient.ControlSets;
using EndlessClient.HUD.Controls;
using EndlessClient.Services;
using EndlessClient.UIControls;
using EOLib;
using EOLib.Domain.Online;
Expand All @@ -9,6 +10,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Optional.Unsafe;
using System;
using System.Collections.Generic;
using System.Linq;
using XNAControls;
Expand All @@ -31,6 +33,7 @@ private enum Filter

private readonly INativeGraphicsManager _nativeGraphicsManager;
private readonly IHudControlProvider _hudControlProvider;
private readonly IFriendIgnoreListService _friendIgnoreListService;
private readonly SpriteFont _chatFont;

private readonly List<OnlinePlayerInfo> _onlineList;
Expand All @@ -43,13 +46,16 @@ private enum Filter

private Filter _filter;
private List<OnlinePlayerInfo> _filteredList;
private IReadOnlyList<string> _friendList;

public OnlineListPanel(INativeGraphicsManager nativeGraphicsManager,
IHudControlProvider hudControlProvider,
IFriendIgnoreListService friendIgnoreListService,
SpriteFont chatFont)
{
_nativeGraphicsManager = nativeGraphicsManager;
_hudControlProvider = hudControlProvider;
_friendIgnoreListService = friendIgnoreListService;
_chatFont = chatFont;
_onlineList = new List<OnlinePlayerInfo>();

Expand Down Expand Up @@ -106,8 +112,7 @@ public void UpdateOnlinePlayers(IReadOnlyList<OnlinePlayerInfo> onlinePlayers)
_scrollBar.UpdateDimensions(_onlineList.Count);
_scrollBar.ScrollToTop();

// todo: friend/ignore lists
//m_friendList = InteractList.LoadAllFriend();
_friendList = _friendIgnoreListService.LoadList(Constants.FriendListFile);
}

protected override void OnUpdateControl(GameTime gameTime)
Expand All @@ -124,8 +129,7 @@ protected override void OnUpdateControl(GameTime gameTime)

switch (_filter)
{
// todo: friend/ignore lists
case Filter.Friends: _filteredList.Clear(); break;
case Filter.Friends: _filteredList = _onlineList.Where(x => _friendList.Contains(x.Name, StringComparer.InvariantCultureIgnoreCase)).ToList(); break;
case Filter.Admins: _filteredList = _onlineList.Where(IsAdminIcon).ToList(); break;
// todo: implement for party/guild
case Filter.Party: _filteredList.Clear(); break;
Expand Down
98 changes: 0 additions & 98 deletions EndlessClient/Old/InteractList.cs

This file was deleted.

26 changes: 14 additions & 12 deletions EndlessClient/Rendering/ContextMenuRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ private Action<object, EventArgs> _getActionFromMenuAction(MenuAction menuAction
case MenuAction.Invite: return _eventInviteToParty;
case MenuAction.Trade: return _eventTrade;
case MenuAction.Whisper: return _eventPrivateMessage;
case MenuAction.Friend: return _eventAddFriend;
case MenuAction.Ignore: return _eventAddIgnore;
//case MenuAction.Friend: return _eventAddFriend;
//case MenuAction.Ignore: return _eventAddIgnore;
default: throw new ArgumentOutOfRangeException(nameof(menuAction));
}
}
Expand Down Expand Up @@ -265,20 +265,22 @@ private void _eventTrade(object arg1, EventArgs arg2)
((EOGame)Game).Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, EOResourceID.STATUS_LABEL_TRADE_REQUESTED_TO_TRADE);
}
}

private void _eventPrivateMessage(object arg1, EventArgs arg2)
{
EOGame.Instance.Hud.SetChatText("!" + m_rend.Character.Name);
}
private void _eventAddFriend(object arg1, EventArgs arg2)
{
EOGame.Instance.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, m_rend.Character.Name, EOResourceID.STATUS_LABEL_WILL_BE_YOUR_FRIEND);
InteractList.WriteNewFriend(m_rend.Character.Name);
}
private void _eventAddIgnore(object arg1, EventArgs arg2)
{
EOGame.Instance.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, m_rend.Character.Name, EOResourceID.STATUS_LABEL_WILL_BE_IGNORED);
InteractList.WriteNewIgnore(m_rend.Character.Name);
}

//private void _eventAddFriend(object arg1, EventArgs arg2)
//{
// EOGame.Instance.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, m_rend.Character.Name, EOResourceID.STATUS_LABEL_WILL_BE_YOUR_FRIEND);
// InteractList.WriteNewFriend(m_rend.Character.Name);
//}
//private void _eventAddIgnore(object arg1, EventArgs arg2)
//{
// EOGame.Instance.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, m_rend.Character.Name, EOResourceID.STATUS_LABEL_WILL_BE_IGNORED);
// InteractList.WriteNewIgnore(m_rend.Character.Name);
//}

/* DISPOSABLE PATTERN */
public new void Dispose()
Expand Down
67 changes: 67 additions & 0 deletions EndlessClient/Services/FriendIgnoreListService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using AutomaticTypeMapper;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace EndlessClient.Services
{
[AutoMappedType]
public class FriendIgnoreListService : IFriendIgnoreListService
{
public IReadOnlyList<string> LoadList(string path)
{
return Load(path);
}

public void SaveFriends(string path, IReadOnlyList<string> contents)
{
Save(isIgnore: false, path, contents);
}

public void SaveIgnored(string path, IReadOnlyList<string> contents)
{
Save(isIgnore: true, path, contents);
}

private static List<string> Load(string fileName)
{
List<string> allLines;
try
{
allLines = new List<string>(File.ReadAllLines(fileName));
}
catch (IOException)
{
allLines = new List<string>();
}

allLines.RemoveAll(s => s.StartsWith("#") || string.IsNullOrWhiteSpace(s));

return allLines.Select(Capitalize).Distinct().ToList();
}

private static void Save(bool isIgnore, string fileName, IEnumerable<string> lines)
{
using (var sw = new StreamWriter(fileName))
{
string friendOrIgnore = isIgnore ? "ignore" : "friend";
sw.WriteLine($"# Endless Online 0.28 [ {friendOrIgnore} list ]\n");
sw.WriteLine($"# List of {friendOrIgnore}{(isIgnore ? "d" : "")} characters, use a new line for each name\n\n");

foreach (string s in lines)
sw.WriteLine(Capitalize(s));
}
}

private static string Capitalize(string input) => char.ToUpper(input[0]) + input.Substring(1).ToLower();
}

public interface IFriendIgnoreListService
{
IReadOnlyList<string> LoadList(string path);

void SaveFriends(string path, IReadOnlyList<string> contents);

void SaveIgnored(string path, IReadOnlyList<string> contents);
}
}

0 comments on commit 27b5680

Please sign in to comment.