Skip to content

Commit

Permalink
Merge branch 'release/1.7.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko committed Jul 16, 2024
2 parents 2d3f6af + 6b52bc0 commit d6f9b76
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 9 deletions.
61 changes: 56 additions & 5 deletions MarketBoardPlugin/GUI/MarketBoardWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace MarketBoardPlugin.GUI
using System.Threading.Tasks;
using Dalamud.Game.Text;
using Dalamud.Interface;
using Dalamud.Interface.Colors;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.Style;
using Dalamud.Interface.Textures;
using Dalamud.Interface.Textures.TextureWraps;
using Dalamud.Interface.Windowing;
Expand Down Expand Up @@ -109,6 +111,10 @@ public class MarketBoardWindow : Window, IDisposable

private CancellationTokenSource? currentRefreshCancellationTokenSource;

private bool isUniversalisUp = false;

private readonly CancellationTokenSource statusCheckCancellationTokenSource = new CancellationTokenSource();

/// <summary>
/// Initializes a new instance of the <see cref="MarketBoardWindow"/> class.
/// </summary>
Expand Down Expand Up @@ -174,6 +180,8 @@ public MarketBoardWindow(MBPlugin plugin)
this.worldList.Add(("Chaos", "Chaos"));
this.worldList.Add(("Moogle", "Moogle"));
#endif

this.StartUniversalisStatusCheckTask(this.statusCheckCancellationTokenSource.Token);
}

/// <summary>
Expand Down Expand Up @@ -822,15 +830,42 @@ void SelectClassJob(ClassJob classJob)
}

ImGui.SetCursorPosY(ImGui.GetWindowContentRegionMax().Y - ImGui.GetFrameHeight());
if (ImGui.Button("Data provided by Universalis"))

if (this.isUniversalisUp)
{
var universalisUrl = "https://universalis.app";
if (this.selectedItem != null)
var buttonColor = 0x002ba040u;

ImGui.PushStyleColor(ImGuiCol.Button, 0xFF000000 | buttonColor);
ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0xDD000000 | buttonColor);
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, 0xAA000000 | buttonColor);

if (ImGui.Button("Data provided by Universalis"))
{
universalisUrl += $"/market/{this.selectedItem.RowId}";
var universalisUrl = "https://universalis.app";
if (this.selectedItem != null)
{
universalisUrl += $"/market/{this.selectedItem.RowId}";
}

Utilities.OpenBrowser(universalisUrl);
}

Utilities.OpenBrowser(universalisUrl);
ImGui.PopStyleColor(3);
}
else
{
var buttonColor = 0x005345e6u;

ImGui.PushStyleColor(ImGuiCol.Button, 0xFF000000 | buttonColor);
ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0xDD000000 | buttonColor);
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, 0xAA000000 | buttonColor);

if (ImGui.Button("Universalis API seems down"))
{
Utilities.OpenBrowser("https://status.universalis.app");
}

ImGui.PopStyleColor(3);
}

ImGui.SameLine(ImGui.GetContentRegionAvail().X - (120 * scale));
Expand Down Expand Up @@ -897,6 +932,8 @@ protected virtual void Dispose(bool disposing)
this.plugin.GameGui.HoveredItemChanged -= this.HandleHoveredItemChange;
this.defaultFontHandle?.Dispose();
this.titleFontHandle?.Dispose();
this.statusCheckCancellationTokenSource.Cancel();
this.statusCheckCancellationTokenSource.Dispose();
}

this.isDisposed = true;
Expand Down Expand Up @@ -1159,5 +1196,19 @@ private void RefreshMarketData()
},
this.currentRefreshCancellationTokenSource.Token);
}

private void StartUniversalisStatusCheckTask(CancellationToken cancellationToken)
{
Task.Run(
async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
this.isUniversalisUp = await this.plugin.UniversalisClient.CheckStatus(cancellationToken).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromMinutes(10), cancellationToken).ConfigureAwait(false);
}
},
cancellationToken);
}
}
}
60 changes: 60 additions & 0 deletions MarketBoardPlugin/Helpers/UniversalisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MarketBoardPlugin.Helpers
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -94,6 +95,65 @@ await this.client.GetStreamAsync(new Uri($"{worldName}/{itemId}?entries={history
}
}

/// <summary>
/// Retrieves the collection of data centers.
/// </summary>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A collection of <see cref="DataCenter"/> objects containing the data centers.</returns>
public async Task<ICollection<DataCenter>> GetDataCenters(CancellationToken cancellationToken)
{
try
{
using var content = await this.resiliencePipeline.ExecuteAsync(
async (ct) =>
await this.client.GetStreamAsync(new Uri("data-centers", UriKind.Relative), ct).ConfigureAwait(false),
cancellationToken)
.ConfigureAwait(false);

cancellationToken.ThrowIfCancellationRequested();

return await JsonSerializer
.DeserializeAsync<ICollection<DataCenter>>(content, cancellationToken: cancellationToken)
.ConfigureAwait(false) ?? throw new InvalidOperationException("Failed to parse data centers.");
}
catch (HttpRequestException ex)
{
this.plugin.Log.Warning(ex, "Failed to fetch data centers.");
throw;
}
catch (JsonException ex)
{
this.plugin.Log.Warning(ex, "Failed to parse data centers.");
throw;
}
}

/// <summary>
/// Checks if the Universalis API is up and running.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns><c>true</c> if the Universalis API is up; otherwise, <c>false</c>.</returns>
public async Task<bool> CheckStatus(CancellationToken cancellationToken)
{
try
{
await this.GetDataCenters(cancellationToken).ConfigureAwait(false);
}
catch (HttpRequestException ex)
{
this.plugin.Log.Warning(ex, "Universalis seems down.");
return false;
}
catch (JsonException ex)
{
this.plugin.Log.Warning(ex, "Universalis seems down.");
return false;
}

this.plugin.Log.Verbose("Universalis seems up.");
return true;
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down
8 changes: 4 additions & 4 deletions MarketBoardPlugin/MarketBoardPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<DocumentationFile>bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).xml</DocumentationFile>
<DebugType>PdbOnly</DebugType>
<GenerateFullPaths>true</GenerateFullPaths>
<AssemblyVersion>1.7.1</AssemblyVersion>
<FileVersion>1.7.1</FileVersion>
<Version>1.7.1</Version>
<AssemblyVersion>1.7.2</AssemblyVersion>
<FileVersion>1.7.2</FileVersion>
<Version>1.7.2</Version>
<Company>Florian Maunier</Company>
<Description>Market board plugin for Dalamud.</Description>
<Copyright>Copyright (c) Florian Maunier. All rights reserved.</Copyright>
Expand Down Expand Up @@ -49,4 +49,4 @@
<ProjectReference Include="..\OtterGui\OtterGui.csproj" />
</ItemGroup>

</Project>
</Project>
34 changes: 34 additions & 0 deletions MarketBoardPlugin/Models/Universalis/DataCenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="DataCenter.cs" company="Florian Maunier">
// Copyright (c) Florian Maunier. All rights reserved.
// </copyright>

namespace MarketBoardPlugin.Models.Universalis
{
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

/// <summary>
/// A model representing a data center from Universalis.
/// </summary>
public partial class DataCenter
{
/// <summary>
/// Gets or sets the name of the data center.
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the region of the data center.
/// </summary>
[JsonPropertyName("region")]
public string Region { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the list of worlds in the data center.
/// </summary>
[JsonPropertyName("worlds")]
public IReadOnlyCollection<uint> Worlds { get; set; } = [];
}
}

0 comments on commit d6f9b76

Please sign in to comment.