Skip to content

Commit

Permalink
Store UnlockCombinationSlot action
Browse files Browse the repository at this point in the history
  • Loading branch information
U-lis committed Sep 12, 2024
1 parent e09f835 commit b1d58eb
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace NineChronicles.DataProvider.DataRendering.Crafting
{
using System;
using System.Collections.Generic;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Action;
using Nekoyume.Module;
using Nekoyume.TableData;
using NineChronicles.DataProvider.Store.Models.Crafting;

public static class UnlockCombinationSlotData
{
private const int GoldenDustId = 600201;
private const int RubyDustId = 600202;

public static UnlockCombinationSlotModel GetUnlockCombinationSlotInfo(
IWorld prevStates,
Address signer,
UnlockCombinationSlot action,
long blockIndex,
DateTimeOffset blockTime
)
{
var cost = prevStates.GetSheet<UnlockCombinationSlotCostSheet>()[action.SlotIndex];
var materialCost = new List<string>();
if (cost.GoldenDustPrice > 0)
{
materialCost.Add($"{GoldenDustId}:{cost.GoldenDustPrice}");
}

if (cost.RubyDustPrice > 0)
{
materialCost.Add($"{RubyDustId}:{cost.RubyDustPrice}");
}

return new UnlockCombinationSlotModel
{
Id = Guid.NewGuid().ToString(),
AgentAddress = signer.ToString(),
AvatarAddress = action.AvatarAddress.ToString(),
SlotIndex = action.SlotIndex,
NcgCost = (decimal)cost.NcgPrice,
CrystalCost = cost.CrystalPrice,
MaterialCosts = string.Join(",", materialCost),
BlockIndex = blockIndex,
Date = DateOnly.FromDateTime(blockTime.DateTime),
TimeStamp = blockTime,
};
}
}
}
3 changes: 3 additions & 0 deletions NineChronicles.DataProvider/RenderSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
// Crafting
_actionRenderer.EveryRender<RapidCombination>().Subscribe(SubscribeRapidCombination);
_actionRenderer.EveryRender<CustomEquipmentCraft>().Subscribe(SubscribeCustomEquipmentCraft);
_actionRenderer.EveryRender<UnlockCombinationSlot>().Subscribe(SubscribeUnlockCombinationSlot);

// Grinding
_actionRenderer.EveryRender<Grinding>().Subscribe(SubscribeGrinding);
Expand Down Expand Up @@ -1685,6 +1686,8 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)

partial void SubscribeCustomEquipmentCraft(ActionEvaluation<CustomEquipmentCraft> evt);

partial void SubscribeUnlockCombinationSlot(ActionEvaluation<UnlockCombinationSlot> evt);

//// Grinding
partial void SubscribeGrinding(ActionEvaluation<Grinding> ev);
/* Partial Methods */
Expand Down
35 changes: 35 additions & 0 deletions NineChronicles.DataProvider/Store/MySql/CraftingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,40 @@ public partial List<CustomEquipmentCraftCountModel> GetCustomEquipmentCraftCount
? ctx.CustomEquipmentCraftCount.ToList()
: ctx.CustomEquipmentCraftCount.Where(c => c.ItemSubType == itemSubType).ToList();
}

// UnlockCombinationSlot
public async partial Task StoreUnlockCombinationSlotList(
List<UnlockCombinationSlotModel> unlockCombinationSlotList
)
{
NineChroniclesContext? ctx = null;
try
{
ctx = await _dbContextFactory.CreateDbContextAsync();

foreach (var ucs in unlockCombinationSlotList)
{
if (!await ctx.UnlockCombinationSlot.AnyAsync(u => u.Id == ucs.Id))
{
await ctx.UnlockCombinationSlot.AddAsync(ucs);
}
}

await ctx.SaveChangesAsync();
Log.Debug($"[UnlockCombinationSlot] {unlockCombinationSlotList.Count} UnlockCombinationSlot saved.");
}
catch (Exception e)
{
Log.Debug(e.Message);
Log.Debug(e.StackTrace);
}
finally
{
if (ctx is not null)
{
await ctx.DisposeAsync();
}
}
}
}
}
2 changes: 2 additions & 0 deletions NineChronicles.DataProvider/Store/MySqlStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,8 @@ public void StoreRuneSummonFailList(List<RuneSummonFailModel> runeSummonFailList

public partial Task StoreGrindList(List<GrindingModel> grindingList);

public partial Task StoreUnlockCombinationSlotList(List<UnlockCombinationSlotModel> unlockCombinationSlotList);

public List<RaiderModel> GetRaiderList()
{
using NineChroniclesContext ctx = _dbContextFactory.CreateDbContext();
Expand Down
43 changes: 43 additions & 0 deletions NineChronicles.DataProvider/Subscriber/CraftingRenderSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class RenderSubscriber
{
private List<RapidCombinationModel> _rapidCombinationList = new ();
private List<CustomEquipmentCraftModel> _customEquipmentCraftList = new ();
private List<UnlockCombinationSlotModel> _unlockCombinationSlotList = new ();

// Store
private void StoreCraftingData()
Expand All @@ -42,6 +43,14 @@ private void StoreCraftingData()
await MySqlStore.StoreCustomEquipmentCraftList(_customEquipmentCraftList);
}));

// UnlockCombinationSlot
Log.Debug("[Crafting] Store UnlockCombinationSlot list");
tasks.Add(Task.Run(async () =>
{
Log.Debug($"[UnlockCombinationSlot] {_unlockCombinationSlotList.Count}");
await MySqlStore.StoreUnlockCombinationSlotList(_unlockCombinationSlotList);
}));

Task.WaitAll(tasks.ToArray());
}
catch (Exception e)
Expand All @@ -56,6 +65,7 @@ private void ClearCraftingList()
Log.Debug("[Crafting] Clear crafting related action data");
_rapidCombinationList.Clear();
_customEquipmentCraftList.Clear();
_unlockCombinationSlotList.Clear();
}

// Subscribe
Expand Down Expand Up @@ -159,5 +169,38 @@ partial void SubscribeCustomEquipmentCraft(ActionEvaluation<CustomEquipmentCraft
);
}
}

partial void SubscribeUnlockCombinationSlot(ActionEvaluation<UnlockCombinationSlot> evt)
{
try
{
if (evt.Exception is null && evt.Action is { } unlockCombinationSlot)
{
var start = DateTimeOffset.UtcNow;
_unlockCombinationSlotList.Add(UnlockCombinationSlotData.GetUnlockCombinationSlotInfo(
new World(_blockChainStates.GetWorldState(evt.PreviousState)),
evt.Signer,
evt.Action,
evt.BlockIndex,
_blockTimeOffset
));

Log.Debug(
"[DataProvider] Stored RapidCombination action in block #{index}. Time Taken: {time} ms.",
evt.BlockIndex,
(DateTimeOffset.UtcNow - start).Milliseconds
);
}
}
catch (Exception e)
{
Log.Error(
e,
"[DataProvider] RenderSubscriber Error: {ErrorMessage}, StackTrace: {StackTrace}",
e.Message,
e.StackTrace
);
}
}
}
}

0 comments on commit b1d58eb

Please sign in to comment.