2
0
mirror of synced 2025-01-27 08:46:04 +00:00

106 lines
3.3 KiB
C#
Raw Normal View History

2024-08-20 11:44:15 +02:00
using CodexPlugin.Hooks;
using Logging;
2023-09-20 08:45:55 +02:00
using Utils;
namespace CodexPlugin
{
public interface IMarketplaceAccess
{
2024-03-20 11:11:41 +01:00
string MakeStorageAvailable(StorageAvailability availability);
2024-09-23 10:52:12 +02:00
StorageAvailability[] GetAvailabilities();
2024-05-30 11:33:16 +02:00
IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase);
2023-09-20 08:45:55 +02:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly ILog log;
private readonly CodexAccess codexAccess;
2024-08-20 11:44:15 +02:00
private readonly ICodexNodeHooks hooks;
2023-09-20 08:45:55 +02:00
2024-08-20 11:44:15 +02:00
public MarketplaceAccess(ILog log, CodexAccess codexAccess, ICodexNodeHooks hooks)
2023-09-20 08:45:55 +02:00
{
this.log = log;
this.codexAccess = codexAccess;
2024-08-20 11:44:15 +02:00
this.hooks = hooks;
2023-09-20 08:45:55 +02:00
}
2024-05-30 11:33:16 +02:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 08:45:55 +02:00
{
2024-03-20 11:11:41 +01:00
purchase.Log(log);
2023-11-23 14:10:59 +01:00
2024-03-26 12:23:38 +01:00
var response = codexAccess.RequestStorage(purchase);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
if (string.IsNullOrEmpty(response) ||
2024-07-02 10:08:13 +02:00
response == "Unable to encode manifest" ||
2024-03-26 12:23:38 +01:00
response == "Purchasing not available" ||
2023-11-23 14:10:59 +01:00
response == "Expiry required" ||
response == "Expiry needs to be in future" ||
response == "Expiry has to be before the request's end (now + duration)")
2023-09-20 08:45:55 +02:00
{
throw new InvalidOperationException(response);
}
Log($"Storage requested successfully. PurchaseId: '{response}'.");
2024-08-20 11:44:15 +02:00
var contract = new StoragePurchaseContract(log, codexAccess, response, purchase, hooks);
contract.WaitForStorageContractSubmitted();
2024-08-20 11:44:15 +02:00
hooks.OnStorageContractSubmitted(contract);
return contract;
2023-09-20 08:45:55 +02:00
}
2024-03-20 11:11:41 +01:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-09-20 08:45:55 +02:00
{
2024-03-20 11:11:41 +01:00
availability.Log(log);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
var response = codexAccess.SalesAvailability(availability);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
Log($"Storage successfully made available. Id: {response.Id}");
2024-08-20 11:44:15 +02:00
hooks.OnStorageAvailabilityCreated(response);
2023-09-20 08:45:55 +02:00
2024-03-26 12:23:38 +01:00
return response.Id;
2023-09-20 08:45:55 +02:00
}
2024-09-23 10:52:12 +02:00
public StorageAvailability[] GetAvailabilities()
{
var result = codexAccess.GetAvailabilities();
Log($"Got {result.Length} availabilities:");
foreach (var a in result) a.Log(log);
return result;
}
2023-09-20 08:45:55 +02:00
private void Log(string msg)
{
log.Log($"{codexAccess.Container.Containers.Single().Name} {msg}");
2023-09-20 08:45:55 +02:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2024-03-20 11:11:41 +01:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-11-23 14:10:59 +01:00
{
Unavailable();
throw new NotImplementedException();
}
2024-05-30 11:33:16 +02:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 08:45:55 +02:00
{
Unavailable();
2024-03-20 11:11:41 +01:00
throw new NotImplementedException();
2023-09-20 08:45:55 +02:00
}
2024-09-23 10:52:12 +02:00
public StorageAvailability[] GetAvailabilities()
{
Unavailable();
throw new NotImplementedException();
}
2023-09-20 08:45:55 +02:00
private void Unavailable()
{
2023-09-20 10:51:47 +02:00
FrameworkAssert.Fail("Incorrect test setup: Marketplace was not enabled for this group of Codex nodes. Add 'EnableMarketplace(...)' after 'SetupCodexNodes()' to enable it.");
2023-09-20 08:45:55 +02:00
throw new InvalidOperationException();
}
}
}