Merge pull request #377 from karashiiro/feat/mb-purchases

This commit is contained in:
goaaats 2021-08-01 16:16:24 +02:00 committed by GitHub
commit 72094136f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 246 additions and 34 deletions

View file

@ -51,5 +51,8 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dalamud/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Gpose/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=lumina/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Materia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=serveropcode/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unsanitized/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Universalis/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unsanitized/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uploaders/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View file

@ -18,5 +18,11 @@ namespace Dalamud.Game.Network.MarketBoardUploaders
/// </summary>
/// <param name="taxRates">The tax rate data being uploaded.</param>
void UploadTax(MarketTaxRates taxRates);
/// <summary>
/// Upload information about a purchase this client has made.
/// </summary>
/// <param name="purchaseHandler">The purchase handler data associated with the sale.</param>
void UploadPurchase(MarketBoardPurchaseHandler purchaseHandler);
}
}

View file

@ -0,0 +1,40 @@
using Newtonsoft.Json;
namespace Dalamud.Game.Network.MarketBoardUploaders.Universalis
{
/// <summary>
/// Request payload for market board purchases.
/// </summary>
public class UniversalisItemListingDeleteRequest
{
/// <summary>
/// Gets or sets the object ID of the retainer associated with the sale.
/// </summary>
[JsonProperty("retainerID")]
public string RetainerId { get; set; }
/// <summary>
/// Gets or sets the object ID of the item listing.
/// </summary>
[JsonProperty("listingID")]
public string ListingId { get; set; }
/// <summary>
/// Gets or sets the quantity of the item that was purchased.
/// </summary>
[JsonProperty("quantity")]
public uint Quantity { get; set; }
/// <summary>
/// Gets or sets the unit price of the item.
/// </summary>
[JsonProperty("pricePerUnit")]
public uint PricePerUnit { get; set; }
/// <summary>
/// Gets or sets the uploader ID.
/// </summary>
[JsonProperty("uploaderID")]
public string UploaderId { get; set; }
}
}

View file

@ -41,12 +41,14 @@ namespace Dalamud.Game.Network.Universalis.MarketBoardUploaders
Log.Verbose("Starting Universalis upload.");
var uploader = this.dalamud.ClientState.LocalContentId;
var listingsRequestObject = new UniversalisItemListingsUploadRequest();
listingsRequestObject.WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0;
listingsRequestObject.UploaderId = uploader.ToString();
listingsRequestObject.ItemId = request.CatalogId;
var listingsRequestObject = new UniversalisItemListingsUploadRequest
{
WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0,
UploaderId = uploader.ToString(),
ItemId = request.CatalogId,
Listings = new List<UniversalisItemListingsEntry>(),
};
listingsRequestObject.Listings = new List<UniversalisItemListingsEntry>();
foreach (var marketBoardItemListing in request.Listings)
{
var universalisListing = new UniversalisItemListingsEntry
@ -62,9 +64,9 @@ namespace Dalamud.Game.Network.Universalis.MarketBoardUploaders
PricePerUnit = marketBoardItemListing.PricePerUnit,
Quantity = marketBoardItemListing.ItemQuantity,
RetainerCity = marketBoardItemListing.RetainerCityId,
Materia = new List<UniversalisItemMateria>(),
};
universalisListing.Materia = new List<UniversalisItemMateria>();
foreach (var itemMateria in marketBoardItemListing.Materia)
{
universalisListing.Materia.Add(new UniversalisItemMateria
@ -78,15 +80,17 @@ namespace Dalamud.Game.Network.Universalis.MarketBoardUploaders
}
var upload = JsonConvert.SerializeObject(listingsRequestObject);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", upload);
Log.Verbose(upload);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", upload);
var historyRequestObject = new UniversalisHistoryUploadRequest();
historyRequestObject.WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0;
historyRequestObject.UploaderId = uploader.ToString();
historyRequestObject.ItemId = request.CatalogId;
var historyRequestObject = new UniversalisHistoryUploadRequest
{
WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0,
UploaderId = uploader.ToString(),
ItemId = request.CatalogId,
Entries = new List<UniversalisHistoryEntry>(),
};
historyRequestObject.Entries = new List<UniversalisHistoryEntry>();
foreach (var marketBoardHistoryListing in request.History)
{
historyRequestObject.Entries.Add(new UniversalisHistoryEntry
@ -103,8 +107,8 @@ namespace Dalamud.Game.Network.Universalis.MarketBoardUploaders
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var historyUpload = JsonConvert.SerializeObject(historyRequestObject);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", historyUpload);
Log.Verbose(historyUpload);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", historyUpload);
Log.Verbose("Universalis data upload for item#{0} completed.", request.CatalogId);
}
@ -114,27 +118,56 @@ namespace Dalamud.Game.Network.Universalis.MarketBoardUploaders
{
using var client = new WebClient();
var taxRatesRequest = new UniversalisTaxUploadRequest();
taxRatesRequest.WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0;
taxRatesRequest.UploaderId = this.dalamud.ClientState.LocalContentId.ToString();
taxRatesRequest.TaxData = new UniversalisTaxData
var taxRatesRequest = new UniversalisTaxUploadRequest
{
LimsaLominsa = taxRates.LimsaLominsaTax,
Gridania = taxRates.GridaniaTax,
Uldah = taxRates.UldahTax,
Ishgard = taxRates.IshgardTax,
Kugane = taxRates.KuganeTax,
Crystarium = taxRates.CrystariumTax,
WorldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0,
UploaderId = this.dalamud.ClientState.LocalContentId.ToString(),
TaxData = new UniversalisTaxData
{
LimsaLominsa = taxRates.LimsaLominsaTax,
Gridania = taxRates.GridaniaTax,
Uldah = taxRates.UldahTax,
Ishgard = taxRates.IshgardTax,
Kugane = taxRates.KuganeTax,
Crystarium = taxRates.CrystariumTax,
},
};
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var historyUpload = JsonConvert.SerializeObject(taxRatesRequest);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", historyUpload);
Log.Verbose(historyUpload);
client.UploadString(ApiBase + $"/upload/{ApiKey}", "POST", historyUpload);
Log.Verbose("Universalis tax upload completed.");
}
/// <inheritdoc/>
public void UploadPurchase(MarketBoardPurchaseHandler purchaseHandler)
{
using var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add(HttpRequestHeader.Authorization, ApiKey);
var itemId = purchaseHandler.CatalogId;
var worldId = this.dalamud.ClientState.LocalPlayer?.CurrentWorld.Id ?? 0;
var purchaseRequest = new UniversalisItemListingDeleteRequest
{
PricePerUnit = purchaseHandler.PricePerUnit,
Quantity = purchaseHandler.ItemQuantity,
ListingId = purchaseHandler.ListingId.ToString(),
RetainerId = purchaseHandler.RetainerId.ToString(),
UploaderId = this.dalamud.ClientState.LocalContentId.ToString(),
};
var requestPath = ApiBase + $"/api/{worldId}/{itemId}/delete";
var purchaseUpload = JsonConvert.SerializeObject(purchaseRequest);
Log.Verbose($"Making request to {requestPath}");
Log.Verbose(purchaseUpload);
client.UploadString(requestPath, "POST", purchaseUpload);
Log.Verbose("Universalis purchase upload completed.");
}
}
}

View file

@ -15,9 +15,9 @@ using Serilog;
namespace Dalamud.Game.Network
{
/// <summary>
/// This class handles network notifications and uploading Marketboard data.
/// This class handles network notifications and uploading market board data.
/// </summary>
public class NetworkHandlers
internal class NetworkHandlers
{
private readonly Dalamud dalamud;
@ -26,12 +26,14 @@ namespace Dalamud.Game.Network
private readonly bool optOutMbUploads;
private readonly IMarketBoardUploader uploader;
private MarketBoardPurchaseHandler marketBoardPurchaseHandler;
/// <summary>
/// Initializes a new instance of the <see cref="NetworkHandlers"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
/// <param name="optOutMbUploads">Whether the client should opt out of marketboard uploads.</param>
internal NetworkHandlers(Dalamud dalamud, bool optOutMbUploads)
/// <param name="optOutMbUploads">Whether the client should opt out of market board uploads.</param>
public NetworkHandlers(Dalamud dalamud, bool optOutMbUploads)
{
this.dalamud = dalamud;
this.optOutMbUploads = optOutMbUploads;
@ -48,12 +50,22 @@ namespace Dalamud.Game.Network
private void OnNetworkMessage(IntPtr dataPtr, ushort opCode, uint sourceActorId, uint targetActorId, NetworkMessageDirection direction)
{
if (direction != NetworkMessageDirection.ZoneDown)
return;
if (!this.dalamud.Data.IsDataReady)
return;
if (direction == NetworkMessageDirection.ZoneUp)
{
if (!this.optOutMbUploads)
{
if (opCode == this.dalamud.Data.ClientOpCodes["MarketBoardPurchaseHandler"])
{
this.marketBoardPurchaseHandler = MarketBoardPurchaseHandler.Read(dataPtr);
}
}
return;
}
if (opCode == this.dalamud.Data.ServerOpCodes["CfNotifyPop"])
{
var data = new byte[64];
@ -256,6 +268,26 @@ namespace Dalamud.Game.Network
Log.Error(ex, "Market Board data upload failed.");
}
}
if (opCode == this.dalamud.Data.ServerOpCodes["MarketBoardPurchase"])
{
if (this.marketBoardPurchaseHandler == null)
return;
var purchase = MarketBoardPurchase.Read(dataPtr);
// Transaction succeeded
if (purchase.ItemQuantity == this.marketBoardPurchaseHandler.ItemQuantity
&& (purchase.CatalogId == this.marketBoardPurchaseHandler.CatalogId
|| purchase.CatalogId == this.marketBoardPurchaseHandler.CatalogId + 1000000))
{ // HQ
Log.Information("Bought " + purchase.ItemQuantity + "x " + this.marketBoardPurchaseHandler.CatalogId + " for " + (this.marketBoardPurchaseHandler.PricePerUnit * purchase.ItemQuantity) + " gils, listing id is " + this.marketBoardPurchaseHandler.ListingId);
var handler = this.marketBoardPurchaseHandler; // Capture the object so that we don't pass in a null one when the task starts.
Task.Run(() => this.uploader.UploadPurchase(handler));
}
this.marketBoardPurchaseHandler = null;
}
}
}
}

View file

@ -0,0 +1,40 @@
using System;
using System.IO;
namespace Dalamud.Game.Network.Structures
{
/// <summary>
/// Represents market board purchase information. This message is received from the
/// server when a purchase is made at a market board.
/// </summary>
internal class MarketBoardPurchase
{
/// <summary>
/// Gets the item ID of the item that was purchased.
/// </summary>
public uint CatalogId { get; private set; }
/// <summary>
/// Gets the quantity of the item that was purchased.
/// </summary>
public uint ItemQuantity { get; private set; }
/// <summary>
/// Reads market board purchase information from the struct at the provided pointer.
/// </summary>
/// <param name="dataPtr">A pointer to a struct containing market board purchase information from the server.</param>
/// <returns>An object representing the data read.</returns>
public static unsafe MarketBoardPurchase Read(IntPtr dataPtr)
{
var output = new MarketBoardPurchase();
using var stream = new UnmanagedMemoryStream((byte*)dataPtr.ToPointer(), 1544);
using var reader = new BinaryReader(stream);
output.CatalogId = reader.ReadUInt32();
stream.Position += 4;
output.ItemQuantity = reader.ReadUInt32();
return output;
}
}
}

View file

@ -0,0 +1,57 @@
using System;
using System.IO;
namespace Dalamud.Game.Network.Structures
{
/// <summary>
/// Represents market board purchase information. This message is sent from the
/// client when a purchase is made at a market board.
/// </summary>
internal class MarketBoardPurchaseHandler
{
/// <summary>
/// Gets the object ID of the retainer associated with the sale.
/// </summary>
public ulong RetainerId { get; private set; }
/// <summary>
/// Gets the object ID of the item listing.
/// </summary>
public ulong ListingId { get; private set; }
/// <summary>
/// Gets the item ID of the item that was purchased.
/// </summary>
public uint CatalogId { get; private set; }
/// <summary>
/// Gets the quantity of the item that was purchased.
/// </summary>
public uint ItemQuantity { get; private set; }
/// <summary>
/// Gets the unit price of the item.
/// </summary>
public uint PricePerUnit { get; private set; }
/// <summary>
/// Reads market board purchase information from the struct at the provided pointer.
/// </summary>
/// <param name="dataPtr">A pointer to a struct containing market board purchase information from the client.</param>
/// <returns>An object representing the data read.</returns>
public static unsafe MarketBoardPurchaseHandler Read(IntPtr dataPtr)
{
var output = new MarketBoardPurchaseHandler();
using var stream = new UnmanagedMemoryStream((byte*)dataPtr.ToPointer(), 1544);
using var reader = new BinaryReader(stream);
output.RetainerId = reader.ReadUInt64();
output.ListingId = reader.ReadUInt64();
output.CatalogId = reader.ReadUInt32();
output.ItemQuantity = reader.ReadUInt32();
output.PricePerUnit = reader.ReadUInt32();
return output;
}
}
}

View file

@ -1,4 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=HeapView_002EDelegateAllocation/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VariableHidesOuterVariable/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
@ -20,6 +20,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>