Commit 34bc0d9c authored by Adam Leyshon's avatar Adam Leyshon
Browse files

Subscribing now requires the use of a server issued token to complete.

Server no longer trusts our Subscription start/end tick so don't send them.
Order request body is now compressed, should help with large orders.
We now send our current tick when updating colony metadata.
parent bf260fc7
......@@ -9,12 +9,15 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Release-Local|Any CPU = Release-Local|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Release|Any CPU.Build.0 = Release|Any CPU
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Release-Local|Any CPU.ActiveCfg = Release-Local|Any CPU
{1976D016-8918-48C9-A54E-4D9EF7F200B7}.Release-Local|Any CPU.Build.0 = Release-Local|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -96,7 +96,7 @@ namespace Glitterworld_Prime
Action actionConfirm = delegate
{
Find.WindowStack.Add(new DialogPrimeUpdateSubscription(_player, _trader,
_subscriptionMeta.SubscriptionCost));
_subscriptionMeta));
Close();
};
......
......@@ -20,25 +20,24 @@ namespace Glitterworld_Prime
#region Fields
private static readonly string _downloadMessage = "Subscribing...";
private readonly int _costOfSubscription;
private readonly GlitterWorld_MapComponent _mapComponent;
private readonly Pawn _player;
private readonly ITrader _trader;
private DownloadStatus _isDownloadComplete;
private bool _shouldClose;
private GlitterWorldSubscriptionMetadata _subscriptionMetadata;
#endregion
#region ctor
public DialogPrimeUpdateSubscription(Pawn playerNegotiator, ITrader traderPrime, int subscriptionCost)
internal DialogPrimeUpdateSubscription(Pawn playerNegotiator, ITrader traderPrime, GlitterWorldSubscriptionMetadata subData)
{
forcePause = true;
absorbInputAroundWindow = true;
_player = playerNegotiator;
_trader = traderPrime;
_mapComponent = Utilities.GetMapComponent(_player.Map);
_costOfSubscription = subscriptionCost;
_subscriptionMetadata = subData;
var t = new Thread(ApiAsync);
t.Start();
......@@ -101,9 +100,8 @@ namespace Glitterworld_Prime
private void ApiAsync()
{
var nextResub = Find.TickManager.TicksAbs + GenDate.TicksPerDay * GenDate.DaysPerQuadrum;
var gsm = new GlitterWorldSubscriptionMetadata(nextResub, _costOfSubscription);
var gsm = new GlitterWorldSubscriptionMetadata(_subscriptionMetadata.SubscriptionCost,
_subscriptionMetadata.Token);
if (GlitterWorldApi.SetSubscriptionData(_player.Map, gsm))
{
......@@ -120,7 +118,7 @@ namespace Glitterworld_Prime
private void RefundSilver()
{
// Add Silver
var thing = Utilities.CreateThing("Silver", _costOfSubscription);
var thing = Utilities.CreateThing("Silver", _subscriptionMetadata.SubscriptionCost);
TradeUtility.SpawnDropPod(DropCellFinder.TradeDropSpot(_player.Map), _player.Map, thing);
Messages.Message("GWPCantSubscribe".Translate(),
MessageTypeDefOf.NegativeEvent);
......
......@@ -197,8 +197,11 @@ namespace Glitterworld_Prime
var request = new RestRequest($"/v4/{MarketUrl}/orders/{colonyId}", Method.PUT);
request.AddJsonBody(orderRequest);
// Compress the list of things to request and send it as a file.
var compressed = SerializeAndCompress(request.JsonSerializer, orderRequest);
request.AddFileBytes("order", compressed, "order");
// Make request.
var response = ExecuteRequestNoDataExpected(client, request);
......@@ -351,6 +354,7 @@ namespace Glitterworld_Prime
Find.World.info.name, Utilities.GetCurrentMapName(map),
Utilities.GetUserType(),
Utilities.GetUserId(),
Find.TickManager.TicksAbs,
mapComponent.HasSpawned
);
......
......@@ -102,7 +102,7 @@ namespace Glitterworld_Prime.ApiStructs
}
public GlitterWorldColonyMetadata(string factionName, string planet, string baseName, string ownerType,
string ownerId, bool hasSpawned)
string ownerId, int currentTick , bool hasSpawned)
{
FactionName = factionName;
Planet = planet;
......@@ -110,6 +110,7 @@ namespace Glitterworld_Prime.ApiStructs
OwnerType = ownerType;
OwnerID = ownerId;
HasSpawned = hasSpawned;
LastGameTick = currentTick;
}
#endregion
......@@ -128,6 +129,7 @@ namespace Glitterworld_Prime.ApiStructs
public bool HasSpawned { get; set; }
public int LastGameTick { get; set; }
#endregion
}
......@@ -140,10 +142,10 @@ namespace Glitterworld_Prime.ApiStructs
{
}
public GlitterWorldSubscriptionMetadata(int subscriptionExpires, int cost)
public GlitterWorldSubscriptionMetadata(int cost, string token)
{
TickSubscriptionExpires = subscriptionExpires;
SubscriptionCost = cost;
Token = token;
}
#endregion
......@@ -154,6 +156,8 @@ namespace Glitterworld_Prime.ApiStructs
public int SubscriptionCost { get; set; }
public string Token { get; set; }
#endregion
}
......
......@@ -36,6 +36,16 @@
<PropertyGroup>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Local|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\Assemblies\</OutputPath>
<DefineConstants>TRACE;LOCALSERVER</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>.\0Harmony.dll</HintPath>
......
......@@ -140,18 +140,17 @@ namespace Glitterworld_Prime.Tools
foreach (var thing in things)
try
{
if (thing.category == ThingCategory.Item && thing.thingClass.Name != "MinifiedThing" &&
TradeUtility.EverPlayerSellable(thing))
if (thing.tradeability == Tradeability.None) continue;
if (thing.category == ThingCategory.Item && thing.thingClass.Name != "MinifiedThing")
thingData.AddRange(ComputeThingDef(thing, stuffCategories));
// Add support for Minifiable buildings
else if (thing.category == ThingCategory.Building && thing.Minifiable &&
thing.tradeability != Tradeability.None)
else if (thing.category == ThingCategory.Building && thing.Minifiable)
thingData.AddRange(ComputeThingDef(thing, stuffCategories));
}
catch (Exception ex)
{
LogWriter.WriteErrorMessage(
$"GlitterWorldPrime failed to dump ThingDef {thing.defName}.");
$"GlitterWorldPrime failed to process ThingDef {thing.defName} for trading, it won't be tradeable.");
LogWriter.WriteErrorMessage(ex.Message);
}
......@@ -160,15 +159,15 @@ namespace Glitterworld_Prime.Tools
internal static void DumpData()
{
var languageName = Verse.LanguageDatabase.activeLanguage.FriendlyNameEnglish;
var languageName = Verse.LanguageDatabase.activeLanguage.FriendlyNameEnglish.ToLowerInvariant();
var data = new List<GlitterWorldItemDumpData>(GetThingData());
data.ForEach(t =>
{
t.LanguageCode = languageName.ToLowerInvariant();
t.LocalizedName = t.LocalizedName.ToLowerInvariant();
t.LanguageCode = languageName;
t.LocalizedName = t.LocalizedName;
});
var path = Path.Combine(GenFilePaths.SaveDataFolderPath, $"{languageName}_thing_data.json");
var path = Path.Combine(GenFilePaths.SaveDataFolderPath, $"{GlitterWorldApi.MarketUrl}_thing_data_{languageName}.json");
var jsonString = RestSharp.SimpleJson.SerializeObject(data);
File.WriteAllText(path, jsonString);
Messages.Message($"{data.Count()} objects dumped to {path}", MessageTypeDefOf.TaskCompletion);
......
......@@ -248,44 +248,6 @@ namespace Glitterworld_Prime
return result;
}
private static IEnumerable<GlitterWorldRequestItem> GetThingData()
{
var stuffCategories = new Dictionary<StuffCategoryDef, List<ThingDef>>();
var thingData = new List<GlitterWorldRequestItem>();
var stuffCategoryDefs = DefDatabase<StuffCategoryDef>.AllDefsListForReading;
var things = DefDatabase<ThingDef>.AllDefsListForReading;
foreach (var stuffCategory in stuffCategoryDefs) stuffCategories.Add(stuffCategory, new List<ThingDef>());
// Scan Defs for Things that can be used as stuff.
foreach (var thing in things)
// If this can be used as Stuff and it has StuffCategories assigned
if (thing.IsStuff && thing.stuffProps.categories.Count > 0)
foreach (var category in thing.stuffProps.categories)
stuffCategories[category].Add(thing);
// Then scan for Defs that can be traded.
foreach (var thing in things)
try
{
if (thing.category == ThingCategory.Item && thing.thingClass.Name != "MinifiedThing" &&
TradeUtility.EverPlayerSellable(thing))
thingData.AddRange(ComputeThingDef(thing, stuffCategories));
// Add support for Minifiable buildings
else if (thing.category == ThingCategory.Building && thing.Minifiable &&
thing.tradeability != Tradeability.None)
thingData.AddRange(ComputeThingDef(thing, stuffCategories));
}
catch (Exception ex)
{
LogWriter.WriteErrorMessage(
$"GlitterWorldPrime failed to dump ThingDef {thing.defName}.");
LogWriter.WriteErrorMessage(ex.Message);
}
return thingData;
}
internal static Faction GetColonyFaction()
{
return Find.FactionManager.AllFactions.FirstOrDefault(f => f.IsPlayer);
......
......@@ -49,7 +49,6 @@ Task("CopyDataFolders")
Task("CopyDLLs")
.IsDependentOn("Compile")
.Does(() => {
CreateDirectory(mod_path+"/Assemblies");
CopyFiles("./Assemblies/*.dll", mod_path+"/Assemblies");
});
......@@ -81,6 +80,7 @@ Task("UpdateXML")
Task("Compile")
.Does(() => {
CreateDirectory("./Assemblies");
DeleteDirectory("./Assemblies", new DeleteDirectorySettings {
Recursive = true,
Force = true
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment