Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
Added option for automatic friend requests to known INARA commanders (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWanderer committed Jan 7, 2019
1 parent 228110d commit 9941132
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 57 deletions.
9 changes: 5 additions & 4 deletions Controller/AbstractPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public abstract class AbstractPlugin<TEvent, TSettings> : IPlugin, IObserver<Log
where TEvent : class
{
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
private readonly ConcurrentQueue<TEvent> eventQueue = new ConcurrentQueue<TEvent>();
private readonly Timer flushTimer = new Timer();

protected AbstractPlugin(ISettingsProvider settingsProvider)
Expand Down Expand Up @@ -44,6 +43,8 @@ protected GlobalSettings GlobalSettings

protected TSettings Settings => new PluginSettingsFacade<TSettings>(PluginId, GlobalSettings).Settings;

protected ConcurrentQueue<TEvent> EventQueue { get; } = new ConcurrentQueue<TEvent>();

public abstract AbstractSettingsControl GetPluginSettingsControl(GlobalSettings settings);

public abstract void FlushEvents(ICollection<TEvent> events);
Expand All @@ -55,7 +56,7 @@ public void FlushQueue()
try
{
var events = new List<TEvent>();
while (eventQueue.TryDequeue(out var @event))
while (EventQueue.TryDequeue(out var @event))
events.Add(@event);
if (events.Count > 0)
FlushEvents(events);
Expand All @@ -70,10 +71,10 @@ public void FlushQueue()
}
}

public void OnNext(LogEvent @event)
public virtual void OnNext(LogEvent @event)
{
foreach (var e in EventConverter.Convert(@event))
eventQueue.Enqueue(e);
EventQueue.Enqueue(e);
}

public virtual void OnCompleted() => FlushQueue();
Expand Down
2 changes: 1 addition & 1 deletion DW.ELA.UnitTests/INARA/InaraEventConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class InaraEventConverterTests
private readonly IPlayerStateHistoryRecorder stateRecorder = new PlayerStateRecorder();
private readonly InaraEventConverter eventConverter;

public InaraEventConverterTests() => eventConverter = new InaraEventConverter(stateRecorder) { ManageFriends = true };
public InaraEventConverterTests() => eventConverter = new InaraEventConverter(stateRecorder);

[Test]
[Parallelizable]
Expand Down
43 changes: 21 additions & 22 deletions Plugin.Inara/InaraEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public InaraEventConverter(IPlayerStateHistoryRecorder playerStateRecorder)
this.playerStateRecorder = playerStateRecorder ?? throw new ArgumentNullException(nameof(playerStateRecorder));
}

public bool ManageFriends { get; set; } = false;

public IEnumerable<ApiEvent> Convert(LogEvent @event)
{
try
Expand Down Expand Up @@ -75,9 +73,6 @@ public IEnumerable<ApiEvent> Convert(LogEvent @event)

// Community goals
case CommunityGoal e: return ConvertEvent(e);

// Friends/squadrons
case Friends e: return ConvertEvent(e);
}
}
catch (Exception e)
Expand All @@ -87,23 +82,27 @@ public IEnumerable<ApiEvent> Convert(LogEvent @event)
return Enumerable.Empty<ApiEvent>();
}

private IEnumerable<ApiEvent> ConvertEvent(Friends e)
{
if (!ManageFriends)
yield break;
if (e.Status == "Lost")
{
yield return new ApiEvent("delCommanderFriend")
{
Timestamp = e.Timestamp,
EventData = new Dictionary<string, object>()
{
{ "commanderName", e.Name },
{ "gamePlatform", "PC" }
}
};
}
else if (e.Status == "Added" || e.Status == "Online")
/// <summary>
/// Separate entry point to convert friends added
/// </summary>
/// <param name="e">"Friends" journal event</param>
/// <returns>sequence of INARA API inputs</returns>
public IEnumerable<ApiEvent> ConvertFriendsEvent(Friends e)
{
// if (e.Status == "Lost")
// {
// yield return new ApiEvent("delCommanderFriend")
// {
// Timestamp = e.Timestamp,
// EventData = new Dictionary<string, object>()
// {
// { "commanderName", e.Name },
// { "gamePlatform", "PC" }
// }
// };
// }
// else
if (e.Status == "Added" || e.Status == "Online")
{
yield return new ApiEvent("addCommanderFriend")
{
Expand Down
12 changes: 11 additions & 1 deletion Plugin.Inara/InaraPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using DW.ELA.Controller;
using DW.ELA.Interfaces;
using DW.ELA.Interfaces.Events;
using DW.ELA.Interfaces.Settings;
using DW.ELA.Plugin.Inara.Model;
using DW.ELA.Utility;
Expand Down Expand Up @@ -44,10 +45,19 @@ public InaraPlugin(IPlayerStateHistoryRecorder playerStateRecorder, ISettingsPro

public override void ReloadSettings()
{
eventConverter.ManageFriends = Settings.ManageFriends;
FlushQueue();
}

public override void OnNext(LogEvent @event)
{
base.OnNext(@event);
if (Settings.ManageFriends && @event is Friends friends)
{
foreach (var e in eventConverter.ConvertFriendsEvent(friends))
EventQueue.Enqueue(e);
}
}

public override AbstractSettingsControl GetPluginSettingsControl(GlobalSettings settings) => new InaraSettingsControl() { GlobalSettings = settings };

public override void OnSettingsChanged(object o, EventArgs e) => ReloadSettings();
Expand Down
69 changes: 40 additions & 29 deletions Plugin.Inara/InaraSettingsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal class InaraSettingsControl : AbstractSettingsControl
private TextBox inaraApiKeyTextBox;
private Button testCredentialsButton;
private Label credentialsStatusLabel;
private Button button1;
private CheckBox apiKeyValidatedCheckbox;
private CheckBox autoManageFriendsCheckbox;
private LinkLabel apiKeyLabel;

public InaraSettingsControl()
Expand All @@ -27,12 +27,15 @@ public InaraSettingsControl()
InitializeComponent();
}

public override void SaveSettings() => Settings = new InaraSettings() { ApiKey = inaraApiKeyTextBox.Text, Verified = apiKeyValidatedCheckbox.Checked, ManageFriends = autoManageFriendsCheckbox.Checked };

private void InaraSettingsControl_Load(object sender, EventArgs e) => ReloadSettings();

private void ReloadSettings()
{
inaraApiKeyTextBox.Text = Settings.ApiKey;
apiKeyValidatedCheckbox.Checked = Settings.Verified;
autoManageFriendsCheckbox.Checked = Settings.ManageFriends;
}

internal InaraSettings Settings
Expand All @@ -43,89 +46,99 @@ internal InaraSettings Settings

private void InitializeComponent()
{
button1 = new Button();
inaraApiKeyTextBox = new TextBox();
testCredentialsButton = new Button();
credentialsStatusLabel = new Label();
apiKeyValidatedCheckbox = new CheckBox();
apiKeyLabel = new LinkLabel();
autoManageFriendsCheckbox = new CheckBox();
SuspendLayout();

// button1
button1.Location = new Point(0, 0);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 0;
button1.Text = "button1";
button1.UseVisualStyleBackColor = true;

//
// inaraApiKeyTextBox
inaraApiKeyTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left
| AnchorStyles.Right;
inaraApiKeyTextBox.Location = new Point(91, 3);
//
inaraApiKeyTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
inaraApiKeyTextBox.Location = new Point(114, 3);
inaraApiKeyTextBox.Name = "inaraApiKeyTextBox";
inaraApiKeyTextBox.Size = new Size(224, 20);
inaraApiKeyTextBox.Size = new Size(244, 22);
inaraApiKeyTextBox.TabIndex = 1;
inaraApiKeyTextBox.Text = "Inara API Key";
inaraApiKeyTextBox.TextAlign = HorizontalAlignment.Center;

//
// testCredentialsButton
//
testCredentialsButton.Location = new Point(3, 27);
testCredentialsButton.Name = "testCredentialsButton";
testCredentialsButton.Size = new Size(122, 23);
testCredentialsButton.TabIndex = 2;
testCredentialsButton.Text = "Validate";
testCredentialsButton.UseVisualStyleBackColor = true;
testCredentialsButton.Click += new EventHandler(testCredentialsButton_Click);

//
// credentialsStatusLabel
//
credentialsStatusLabel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
credentialsStatusLabel.AutoSize = true;
credentialsStatusLabel.Location = new Point(3, 55);
credentialsStatusLabel.Location = new Point(271, 55);
credentialsStatusLabel.Name = "credentialsStatusLabel";
credentialsStatusLabel.Size = new Size(69, 13);
credentialsStatusLabel.Size = new Size(87, 17);
credentialsStatusLabel.TabIndex = 3;
credentialsStatusLabel.Text = "Not checked";

//
// apiKeyValidatedCheckbox
//
apiKeyValidatedCheckbox.AutoSize = true;
apiKeyValidatedCheckbox.Enabled = false;
apiKeyValidatedCheckbox.Location = new Point(131, 31);
apiKeyValidatedCheckbox.Name = "apiKeyValidatedCheckbox";
apiKeyValidatedCheckbox.Size = new Size(184, 17);
apiKeyValidatedCheckbox.Size = new Size(233, 21);
apiKeyValidatedCheckbox.TabIndex = 7;
apiKeyValidatedCheckbox.Text = "CMDR Name / API Key validated";
apiKeyValidatedCheckbox.UseVisualStyleBackColor = true;
apiKeyValidatedCheckbox.CheckedChanged += new EventHandler(apiKeyValidatedCheckbox_CheckedChanged);

//
// apiKeyLabel
//
apiKeyLabel.AutoSize = true;
apiKeyLabel.Location = new Point(3, 6);
apiKeyLabel.Name = "apiKeyLabel";
apiKeyLabel.Size = new Size(82, 13);
apiKeyLabel.Size = new Size(105, 17);
apiKeyLabel.TabIndex = 8;
apiKeyLabel.TabStop = true;
apiKeyLabel.Text = "INARA Api Key:";
apiKeyLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(apiKeyLabel_LinkClicked);

//
// autoManageFriendsCheckbox
//
autoManageFriendsCheckbox.AutoSize = true;
autoManageFriendsCheckbox.Location = new Point(3, 80);
autoManageFriendsCheckbox.Name = "autoManageFriendsCheckbox";
autoManageFriendsCheckbox.Size = new Size(188, 21);
autoManageFriendsCheckbox.TabIndex = 9;
autoManageFriendsCheckbox.Text = "Add friends automatically";
autoManageFriendsCheckbox.UseVisualStyleBackColor = true;
//
// InaraSettingsControl
//
Controls.Add(autoManageFriendsCheckbox);
Controls.Add(apiKeyLabel);
Controls.Add(apiKeyValidatedCheckbox);
Controls.Add(credentialsStatusLabel);
Controls.Add(testCredentialsButton);
Controls.Add(inaraApiKeyTextBox);
Name = "InaraSettingsControl";
Size = new Size(318, 148);
Size = new Size(361, 155);
ResumeLayout(false);
PerformLayout();

}

private async void testCredentialsButton_Click(object sender, EventArgs e)
{
try
{
testCredentialsButton.Enabled = false;
var apiKey = inaraApiKeyTextBox.Text;
var cmdrName = GlobalSettings.CommanderName;
string apiKey = inaraApiKeyTextBox.Text;
string cmdrName = GlobalSettings.CommanderName;
var apiFacade = new InaraApiFacade(InaraPlugin.RestClient, apiKey, cmdrName);
var @event = new ApiEvent("getCommanderProfile") { EventData = new { searchName = cmdrName }, Timestamp = DateTime.Now };
var result = await apiFacade.ApiCall(@event);
Expand All @@ -144,8 +157,6 @@ private async void testCredentialsButton_Click(object sender, EventArgs e)
}
}

public override void SaveSettings() => Settings = new InaraSettings() { ApiKey = inaraApiKeyTextBox.Text, Verified = apiKeyValidatedCheckbox.Checked };

private void apiKeyValidatedCheckbox_CheckedChanged(object sender, EventArgs e) => inaraApiKeyTextBox.BackColor = apiKeyValidatedCheckbox.Checked ? Color.LightGreen : Color.LightSalmon;

private void apiKeyLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) => Process.Start("https://inara.cz/settings-api/");
Expand Down

0 comments on commit 9941132

Please sign in to comment.