Skip to content

Commit

Permalink
Merge pull request #630 from area363/merge/110-to-main
Browse files Browse the repository at this point in the history
Merge/110 to main
  • Loading branch information
area363 authored Feb 27, 2024
2 parents 3069abe + d4792e1 commit 331ea2e
Show file tree
Hide file tree
Showing 15 changed files with 4,367 additions and 182 deletions.
385 changes: 216 additions & 169 deletions NineChronicles.DataProvider.Executable/Commands/UserDataMigration.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace NineChronicles.DataProvider.Executable.Migrations
{
public partial class ActivateCollection : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ActivateCollections",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
AvatarAddress = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
CollectionId = table.Column<int>(type: "int", nullable: false),
ActionId = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
BlockIndex = table.Column<long>(type: "bigint", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn)
},
constraints: table =>
{
table.PrimaryKey("PK_ActivateCollections", x => x.Id);
table.ForeignKey(
name: "FK_ActivateCollections_Avatars_AvatarAddress",
column: x => x.AvatarAddress,
principalTable: "Avatars",
principalColumn: "Address",
onDelete: ReferentialAction.Cascade);
})
.Annotation("MySql:CharSet", "utf8mb4");

migrationBuilder.CreateTable(
name: "CollectionOptionModel",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
StatType = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
OperationType = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Value = table.Column<long>(type: "bigint", nullable: false),
ActivateCollectionId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CollectionOptionModel", x => x.Id);
table.ForeignKey(
name: "FK_CollectionOptionModel_ActivateCollections_ActivateCollection~",
column: x => x.ActivateCollectionId,
principalTable: "ActivateCollections",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
})
.Annotation("MySql:CharSet", "utf8mb4");

migrationBuilder.CreateIndex(
name: "IX_ActivateCollections_AvatarAddress",
table: "ActivateCollections",
column: "AvatarAddress");

migrationBuilder.CreateIndex(
name: "IX_CollectionOptionModel_ActivateCollectionId",
table: "CollectionOptionModel",
column: "ActivateCollectionId");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CollectionOptionModel");

migrationBuilder.DropTable(
name: "ActivateCollections");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("AbilityRanking");
});

modelBuilder.Entity("NineChronicles.DataProvider.Store.Models.ActivateCollectionModel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("ActionId")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("AvatarAddress")
.IsRequired()
.HasColumnType("varchar(255)");
b.Property<long>("BlockIndex")
.HasColumnType("bigint");
b.Property<int>("CollectionId")
.HasColumnType("int");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.HasIndex("AvatarAddress");
b.ToTable("ActivateCollections");
});

modelBuilder.Entity("NineChronicles.DataProvider.Store.Models.AgentModel", b =>
{
b.Property<string>("Address")
Expand Down Expand Up @@ -3135,6 +3166,49 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("WorldBossSeasonMigrationModels");
});

modelBuilder.Entity("NineChronicles.DataProvider.Store.Models.ActivateCollectionModel", b =>
{
b.HasOne("NineChronicles.DataProvider.Store.Models.AvatarModel", "Avatar")
.WithMany("ActivateCollections")
.HasForeignKey("AvatarAddress")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.OwnsMany("NineChronicles.DataProvider.Store.Models.CollectionOptionModel", "Options", b1 =>
{
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b1.Property<int>("ActivateCollectionId")
.HasColumnType("int");
b1.Property<string>("OperationType")
.IsRequired()
.HasColumnType("longtext");
b1.Property<string>("StatType")
.IsRequired()
.HasColumnType("longtext");
b1.Property<long>("Value")
.HasColumnType("bigint");
b1.HasKey("Id");
b1.HasIndex("ActivateCollectionId");
b1.ToTable("CollectionOptionModel");
b1.WithOwner()
.HasForeignKey("ActivateCollectionId");
});
b.Navigation("Avatar");
b.Navigation("Options");
});

modelBuilder.Entity("NineChronicles.DataProvider.Store.Models.AuraSummonFailModel", b =>
{
b.HasOne("NineChronicles.DataProvider.Store.Models.AgentModel", "Agent")
Expand Down Expand Up @@ -3679,6 +3753,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Navigation("Agent");
});

modelBuilder.Entity("NineChronicles.DataProvider.Store.Models.AvatarModel", b =>
{
b.Navigation("ActivateCollections");
});
#pragma warning restore 612, 618
}
}
Expand Down
2 changes: 1 addition & 1 deletion NineChronicles.DataProvider.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ IActionEvaluatorConfiguration GetActionEvaluatorConfiguration(IConfiguration con
ReorgInterval = headlessConfig.ReorgInterval,
TxLifeTime = TimeSpan.FromMinutes(headlessConfig.TxLifeTime),
MinerCount = headlessConfig.NoMiner ? 0 : 1,
NetworkType = headlessConfig.NetworkType,
Planet = headlessConfig.Planet,
};
NineChroniclesNodeService service =
NineChroniclesNodeService.Create(nineChroniclesProperties, context);
Expand Down
73 changes: 73 additions & 0 deletions NineChronicles.DataProvider.Tests/ActivateCollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Nekoyume.Model.Stat;
using NineChronicles.DataProvider.Store;
using NineChronicles.DataProvider.Store.Models;
using Xunit;

namespace NineChronicles.DataProvider.Tests;

public class ActivateCollectionTest : TestBase
{

[Fact]
public void RelationShip()
{
var avatarAddress = new PrivateKey().Address;
var provider = Services.BuildServiceProvider();
var store = provider.GetRequiredService<MySqlStore>();
store.StoreAgent(avatarAddress);
var now = DateTimeOffset.UtcNow;
store.StoreAvatar(avatarAddress, avatarAddress, "name", now, 1, null, null, 0);
var avatar = store.GetAvatar(avatarAddress, true);
var actionId = Guid.NewGuid().ToString();
for (int i = 0; i < 2; i++)
{
var modifiers = new List<StatModifier>
{
new StatModifier(StatType.HP, StatModifier.OperationType.Add, 1L),
new StatModifier(StatType.HP, StatModifier.OperationType.Percentage, 1L)
};
var options = new List<CollectionOptionModel>();
var collection = new ActivateCollectionModel
{
Avatar = avatar,
CollectionId = 1 + i,
BlockIndex = 1L + i,
ActionId = actionId,
Options = options,
};
foreach (var modifier in modifiers)
{
options.Add(new CollectionOptionModel
{
Value = modifier.Value,
OperationType = modifier.Operation.ToString(),
StatType = modifier.StatType.ToString(),
});
}
avatar.ActivateCollections.Add(collection);
}

store.UpdateAvatar(avatar);
Assert.Equal(2, Context.Avatars!.Include(avatarModel => avatarModel.ActivateCollections).First().ActivateCollections.Count);
Assert.Equal(2, Context.ActivateCollections.Count(p => p.ActionId == actionId));
}

public void Dispose()
{
CleanUp();
}

protected override IWorldState GetMockState()
{
return new MockWorldState();
}
}
7 changes: 4 additions & 3 deletions NineChronicles.DataProvider/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#nullable enable

namespace NineChronicles.DataProvider
{
#nullable enable

using Nekoyume;
using NineChronicles.Headless.Properties;

public class Configuration
Expand Down Expand Up @@ -95,6 +96,6 @@ public class Configuration
#nullable disable
public string MySqlConnectionString { get; set; }

public NetworkType NetworkType { get; set; } = NetworkType.Main;
public Planet Planet { get; set; } = Planet.Odin;
}
}
37 changes: 29 additions & 8 deletions NineChronicles.DataProvider/DataRendering/AvatarData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NineChronicles.DataProvider.DataRendering
using Nekoyume.Extensions;
using Nekoyume.Model.EnumType;
using Nekoyume.Model.Item;
using Nekoyume.Model.Stat;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
Expand All @@ -27,14 +28,22 @@ public static AvatarModel GetAvatarInfo(
DateTimeOffset blockTime)
{
AvatarState avatarState = outputStates.GetAvatarState(avatarAddress);
var collectionExist = outputStates.TryGetCollectionState(avatarAddress, out var collectionState);
var sheetTypes = new List<Type>
{
typeof(CharacterSheet),
typeof(CostumeStatSheet),
typeof(RuneListSheet),
typeof(RuneOptionSheet),
typeof(CollectionSheet),
};
if (collectionExist)
{
sheetTypes.Add(typeof(CollectionSheet));
}

var sheets = outputStates.GetSheets(
sheetTypes: new[]
{
typeof(CharacterSheet),
typeof(CostumeStatSheet),
typeof(RuneListSheet),
typeof(RuneOptionSheet),
});
sheetTypes: sheetTypes);

var itemSlotStateAddress = ItemSlotState.DeriveAddress(avatarAddress, BattleType.Adventure);
var itemSlotState = outputStates.TryGetLegacyState(itemSlotStateAddress, out List rawItemSlotState)
Expand Down Expand Up @@ -103,13 +112,25 @@ public static AvatarModel GetAvatarInfo(
avatarTitleId = avatarTitleCostume.Id;
}

var collectionModifiers = new List<StatModifier>();
if (collectionExist)
{
var collectionSheet = sheets.GetSheet<CollectionSheet>();
foreach (var id in collectionState.Ids)
{
var row = collectionSheet[id];
collectionModifiers.AddRange(row.StatModifiers);
}
}

var avatarCp = CPHelper.TotalCP(
equipmentList,
costumeList,
runeOptions,
avatarState.level,
characterRow,
costumeStatSheet);
costumeStatSheet,
collectionModifiers);
string avatarName = avatarState.name;

Log.Debug(
Expand Down
37 changes: 37 additions & 0 deletions NineChronicles.DataProvider/RenderSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,43 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
}
});

_actionRenderer.EveryRender<ActivateCollection>().Subscribe(ev =>
{
if (ev.Exception is null && ev.Action is { } activateCollection)
{
var outputState = new World(_blockChainStates.GetWorldState(ev.OutputState));
var collectionSheet = outputState.GetSheet<CollectionSheet>();
var avatar = MySqlStore.GetAvatar(activateCollection.AvatarAddress, true);
foreach (var (collectionId, materials) in activateCollection.CollectionData)
{
var row = collectionSheet[collectionId];
var options = new List<CollectionOptionModel>();
foreach (var modifier in row.StatModifiers)
{
var option = new CollectionOptionModel
{
StatType = modifier.StatType.ToString(),
OperationType = modifier.Operation.ToString(),
Value = modifier.Value,
};
options.Add(option);
}
var collectionModel = new ActivateCollectionModel
{
ActionId = activateCollection.Id.ToString(),
Avatar = avatar,
BlockIndex = ev.BlockIndex,
CollectionId = collectionId,
Options = options,
};
avatar.ActivateCollections.Add(collectionModel);
}
MySqlStore.UpdateAvatar(avatar);
}
});

return Task.CompletedTask;
}

Expand Down
Loading

0 comments on commit 331ea2e

Please sign in to comment.