feat(network): Combine purchase request and response event streams

This commit is contained in:
karashiiro 2023-02-15 18:30:24 -08:00
parent ecf039a200
commit 0f3a63420b

View file

@ -36,14 +36,11 @@ internal class NetworkHandlers : IDisposable, IServiceType
private readonly IDisposable handleMarketBoardHistory; private readonly IDisposable handleMarketBoardHistory;
private readonly IDisposable handleMarketTaxRates; private readonly IDisposable handleMarketTaxRates;
private readonly IDisposable handleMarketBoardPurchaseHandler; private readonly IDisposable handleMarketBoardPurchaseHandler;
private readonly IDisposable handleMarketBoardPurchase;
private readonly IDisposable handleCfPop; private readonly IDisposable handleCfPop;
[ServiceManager.ServiceDependency] [ServiceManager.ServiceDependency]
private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get(); private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get();
private MarketBoardPurchaseHandler? marketBoardPurchaseHandler;
private bool disposing; private bool disposing;
[ServiceManager.ServiceConstructor] [ServiceManager.ServiceConstructor]
@ -60,7 +57,6 @@ internal class NetworkHandlers : IDisposable, IServiceType
this.handleMarketBoardHistory = this.HandleMarketBoardHistory(); this.handleMarketBoardHistory = this.HandleMarketBoardHistory();
this.handleMarketTaxRates = this.HandleMarketTaxRates(); this.handleMarketTaxRates = this.HandleMarketTaxRates();
this.handleMarketBoardPurchaseHandler = this.HandleMarketBoardPurchaseHandler(); this.handleMarketBoardPurchaseHandler = this.HandleMarketBoardPurchaseHandler();
this.handleMarketBoardPurchase = this.HandleMarketBoardPurchase();
this.handleCfPop = this.HandleCfPop(); this.handleCfPop = this.HandleCfPop();
gameNetwork.NetworkMessage += this.ObserveNetworkMessage; gameNetwork.NetworkMessage += this.ObserveNetworkMessage;
@ -94,7 +90,6 @@ internal class NetworkHandlers : IDisposable, IServiceType
this.handleMarketBoardHistory.Dispose(); this.handleMarketBoardHistory.Dispose();
this.handleMarketTaxRates.Dispose(); this.handleMarketTaxRates.Dispose();
this.handleMarketBoardPurchaseHandler.Dispose(); this.handleMarketBoardPurchaseHandler.Dispose();
this.handleMarketBoardPurchase.Dispose();
this.handleCfPop.Dispose(); this.handleCfPop.Dispose();
} }
@ -334,42 +329,31 @@ internal class NetworkHandlers : IDisposable, IServiceType
private IDisposable HandleMarketBoardPurchaseHandler() private IDisposable HandleMarketBoardPurchaseHandler()
{ {
return this.OnMarketBoardPurchaseHandler() return this.OnMarketBoardPurchaseHandler()
.Where(_ => this.configuration.IsMbCollect) .Where(this.ShouldUpload)
.Zip(this.OnMarketBoardPurchase().Where(this.ShouldUpload))
.Subscribe( .Subscribe(
handler => { this.marketBoardPurchaseHandler = handler; }, data =>
ex => Log.Error(ex, "Failed to handle Market Board purchase handler event"));
}
private IDisposable HandleMarketBoardPurchase()
{
return this.OnMarketBoardPurchase()
.Where(_ => this.configuration.IsMbCollect)
.Subscribe(
purchase =>
{ {
if (this.marketBoardPurchaseHandler == null) var (handler, purchase) = data;
return;
var sameQty = purchase.ItemQuantity == this.marketBoardPurchaseHandler.ItemQuantity; var sameQty = purchase.ItemQuantity == handler.ItemQuantity;
var itemMatch = purchase.CatalogId == this.marketBoardPurchaseHandler.CatalogId; var itemMatch = purchase.CatalogId == handler.CatalogId;
var itemMatchHq = purchase.CatalogId == this.marketBoardPurchaseHandler.CatalogId + 1_000_000; var itemMatchHq = purchase.CatalogId == handler.CatalogId + 1_000_000;
// Transaction succeeded // Transaction succeeded
if (sameQty && (itemMatch || itemMatchHq)) if (sameQty && (itemMatch || itemMatchHq))
{ {
Log.Verbose( Log.Verbose(
$"Bought {purchase.ItemQuantity}x {this.marketBoardPurchaseHandler.CatalogId} for {this.marketBoardPurchaseHandler.PricePerUnit * purchase.ItemQuantity} gils, listing id is {this.marketBoardPurchaseHandler.ListingId}"); "Bought {PurchaseItemQuantity}x {HandlerCatalogId} for {HandlerPricePerUnit} gils, listing id is {HandlerListingId}",
purchase.ItemQuantity,
var handler = handler.CatalogId,
this.marketBoardPurchaseHandler; // Capture the object so that we don't pass in a null one when the task starts. handler.PricePerUnit * purchase.ItemQuantity,
handler.ListingId);
Task.Run(() => this.uploader.UploadPurchase(handler)) Task.Run(() => this.uploader.UploadPurchase(handler))
.ContinueWith( .ContinueWith(
task => Log.Error(task.Exception, "Market Board purchase data upload failed."), task => Log.Error(task.Exception, "Market Board purchase data upload failed"),
TaskContinuationOptions.OnlyOnFaulted); TaskContinuationOptions.OnlyOnFaulted);
} }
this.marketBoardPurchaseHandler = null;
}, },
ex => Log.Error(ex, "Failed to handle Market Board purchase event")); ex => Log.Error(ex, "Failed to handle Market Board purchase event"));
} }