Skip to content

Commit c13ba02

Browse files
feat: (Dobbie) migrate StatGrowth to individual ranges per stat and move to EquipmentProperties (AscensionGameDev#2087)
* wip: (Day) Pre-migration stuff for stat range migration * chore: (Day) Move EquipmentProperties to separate table * chore: (Day) first code review * wip: pre-migration * chore: (Day) Renames; code review * wip: Migration * Added missing attributes * Add missing properties * reuse DescriptorId as the PK (since it's 1:0-or-1) - This avoids the not-null constraint failure * fix: regenerate migration * wip: (Day) Migration now populates tables properly * fix: make everything work for SQLite * fix: add MySQL migration * fix: correct reference to Microsoft.EntityFrameworkCore.Abstractions * fix: correct seed parameter * fix: cleanup mess * fix: Enum.GetValues<TEnum> and delete EquipmentProperties when set to 0 * fix: using LowRange (negative) on a non-negative nud kept overwriting the value to 0 on load * fix: remove package downgrade * fix: (Day) fix issue where stats wouldn't roll --------- Co-authored-by: Robbie Lodico <[email protected]>
1 parent df69141 commit c13ba02

File tree

22 files changed

+4324
-19
lines changed

22 files changed

+4324
-19
lines changed

Framework/Intersect.Framework/Intersect.Framework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="MessagePack.Annotations" Version="2.6.100-alpha" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.14" />
1112
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
1213
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
1314
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />

Framework/Intersect.Framework/Reflection/TypeExtensions.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
12
using System.Diagnostics;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Reflection;
45
using System.Runtime.CompilerServices;
6+
using Microsoft.EntityFrameworkCore;
57

68
namespace Intersect.Framework.Reflection;
79

810
public static partial class TypeExtensions
911
{
12+
public static string[] GetMappedColumnNames(this Type type)
13+
{
14+
return type.GetProperties()
15+
.Where(propertyInfo => propertyInfo.GetCustomAttribute<NotMappedAttribute>() == default)
16+
.GroupBy(
17+
propertyInfo =>
18+
{
19+
var foreignKeyAttribute = propertyInfo.GetCustomAttribute<ForeignKeyAttribute>();
20+
return foreignKeyAttribute == default ? propertyInfo.Name : foreignKeyAttribute.Name;
21+
}
22+
)
23+
.SelectMany(
24+
group =>
25+
{
26+
var items = group.ToArray();
27+
if (items.Length > 1)
28+
{
29+
return items.Where(propertyInfo => propertyInfo.PropertyType.IsValueType)
30+
.Select(propertyInfo => propertyInfo.Name);
31+
}
32+
33+
return items.SelectMany(
34+
propertyInfo =>
35+
{
36+
if (propertyInfo.PropertyType.IsValueType)
37+
{
38+
return new[] { propertyInfo.Name };
39+
}
40+
41+
if (!propertyInfo.PropertyType.IsClass ||
42+
propertyInfo.PropertyType.IsAbstract ||
43+
propertyInfo.PropertyType.GetCustomAttribute<OwnedAttribute>() == default)
44+
{
45+
return Array.Empty<string>();
46+
}
47+
48+
return propertyInfo.PropertyType.GetMappedColumnNames()
49+
.Select(name => string.Join('_', propertyInfo.Name, name));
50+
}
51+
);
52+
}
53+
)
54+
.ToArray();
55+
}
56+
1057
public static string QualifiedGenericName(this Type type) =>
1158
$"{type.Name}<{string.Join(", ", type.GenericTypeArguments.Select(parameterType => parameterType.Name))}>";
1259

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Intersect.Enums;
2+
using Intersect.GameObjects.Ranges;
3+
4+
// ReSharper disable InconsistentNaming
5+
6+
namespace Intersect.GameObjects;
7+
8+
public partial class EquipmentProperties
9+
{
10+
public ItemRange StatRange_Attack
11+
{
12+
get => StatRanges.TryGetValue(Stat.Attack, out var range) ? range : StatRange_Attack = new ItemRange();
13+
set => StatRanges[Stat.Attack] = value;
14+
}
15+
16+
public ItemRange StatRange_AbilityPower
17+
{
18+
get =>
19+
StatRanges.TryGetValue(Stat.AbilityPower, out var range) ? range : StatRange_AbilityPower = new ItemRange();
20+
set => StatRanges[Stat.AbilityPower] = value;
21+
}
22+
23+
public ItemRange StatRange_Defense
24+
{
25+
get => StatRanges.TryGetValue(Stat.Defense, out var range) ? range : StatRange_Defense = new ItemRange();
26+
set => StatRanges[Stat.Defense] = value;
27+
}
28+
29+
public ItemRange StatRange_MagicResist
30+
{
31+
get =>
32+
StatRanges.TryGetValue(Stat.MagicResist, out var range) ? range : StatRange_MagicResist = new ItemRange();
33+
set => StatRanges[Stat.MagicResist] = value;
34+
}
35+
36+
public ItemRange StatRange_Speed
37+
{
38+
get => StatRanges.TryGetValue(Stat.Speed, out var range) ? range : StatRange_Speed = new ItemRange();
39+
set => StatRanges[Stat.Speed] = value;
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
using System.Text.Json.Serialization;
4+
using Intersect.Enums;
5+
using Intersect.GameObjects.Ranges;
6+
using Newtonsoft.Json;
7+
8+
namespace Intersect.GameObjects;
9+
10+
public partial class EquipmentProperties
11+
{
12+
public EquipmentProperties()
13+
{
14+
}
15+
16+
public EquipmentProperties(ItemBase descriptor)
17+
{
18+
Descriptor = descriptor;
19+
}
20+
21+
[Key]
22+
public Guid DescriptorId { get; set; }
23+
24+
[ForeignKey(nameof(DescriptorId))]
25+
[System.Text.Json.Serialization.JsonIgnore]
26+
[Newtonsoft.Json.JsonIgnore]
27+
public ItemBase Descriptor { get; set; }
28+
29+
[NotMapped]
30+
public Dictionary<Stat, ItemRange> StatRanges { get; set; } = new();
31+
}

Intersect (Core)/GameObjects/ItemBase.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
using Intersect.Enums;
77
using Intersect.GameObjects.Conditions;
88
using Intersect.GameObjects.Events;
9+
using Intersect.GameObjects.Ranges;
910
using Intersect.Models;
1011
using Intersect.Utilities;
1112

1213
using Microsoft.EntityFrameworkCore;
1314

1415
using Newtonsoft.Json;
16+
using static Intersect.GameObjects.EquipmentProperties;
1517

1618
namespace Intersect.GameObjects
1719
{
@@ -233,8 +235,6 @@ public ProjectileBase Projectile
233235
/// </summary>
234236
public int MaxBankStack { get; set; } = 1000000;
235237

236-
public int StatGrowth { get; set; }
237-
238238
public int Tool { get; set; } = -1;
239239

240240
/// <summary>
@@ -337,6 +337,19 @@ public string EffectsJson
337337
set => Effects = JsonConvert.DeserializeObject<List<EffectData>>(value ?? "") ?? new List<EffectData>();
338338
}
339339

340+
public EquipmentProperties? EquipmentProperties { get; set; }
341+
342+
[NotMapped, JsonIgnore]
343+
public ItemRange[] StatRanges => EquipmentProperties?.StatRanges?.Values.ToArray();
344+
345+
public bool TryGetRangeFor(Stat stat, out ItemRange range)
346+
{
347+
range = null;
348+
_ = EquipmentProperties?.StatRanges?.TryGetValue(stat, out range);
349+
350+
return range != default;
351+
}
352+
340353
public int GetEffectPercentage(ItemEffect type)
341354
{
342355
return Effects.Find(effect => effect.Type == type)?.Percentage ?? 0;
@@ -388,6 +401,21 @@ public string GetPaperdollForGender(Gender gender) =>
388401
_ => FemalePaperdoll,
389402
};
390403

404+
public override void Load(string json, bool keepCreationTime = false)
405+
{
406+
base.Load(json, keepCreationTime);
407+
408+
// ReSharper disable once InvertIf
409+
if (EquipmentProperties != default)
410+
{
411+
EquipmentProperties.Descriptor = this;
412+
if (EquipmentProperties.DescriptorId != Id)
413+
{
414+
EquipmentProperties.DescriptorId = default;
415+
}
416+
}
417+
}
418+
391419
private void Initialize()
392420
{
393421
Name = "New Item";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace Intersect.GameObjects.Ranges;
4+
5+
/// <summary>
6+
/// ItemRange exists to generalize rollable stats on an item
7+
/// </summary>
8+
[Owned]
9+
public partial class ItemRange
10+
{
11+
public ItemRange()
12+
{
13+
}
14+
15+
public ItemRange(int lowRange, int highRange) : this()
16+
{
17+
LowRange = lowRange;
18+
HighRange = highRange;
19+
}
20+
21+
private Random GetRandomInstance(int? seed = default) => seed == default ? Random.Shared : new Random(seed.Value);
22+
23+
public int Roll(int? seed = default)
24+
{
25+
var random = GetRandomInstance(seed);
26+
return random.Next(LowRange, HighRange + 1);
27+
}
28+
29+
public int LowRange { get; set; }
30+
31+
public int HighRange { get; set; }
32+
}

Intersect (Core)/Intersect.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
</PackageReference>
8080
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
8181
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="3.0.442202" />
82-
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.11" />
8382
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="7.0.11" />
8483
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
8584
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />

Intersect.Client/Interface/Game/DescriptionWindows/ItemDescriptionWindow.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ protected void SetupEquipmentInfo()
329329
for (var i = 0; i < Enum.GetValues<Stat>().Length; i++)
330330
{
331331
// Do we have item properties, if so this is a finished item. Otherwise does this item not have growing stats?
332-
if (statModifiers != default || mItem.StatGrowth == 0)
332+
if (statModifiers != default || mItem.StatRanges?.Length == 0)
333333
{
334334
var flatStat = mItem.StatsGiven[i];
335335
if (statModifiers != default)
@@ -353,8 +353,9 @@ protected void SetupEquipmentInfo()
353353
// We do not have item properties and have growing stats! So don't display a finished stat but a range instead.
354354
else
355355
{
356-
var statLow = mItem.StatsGiven[i] - mItem.StatGrowth;
357-
var statHigh = mItem.StatsGiven[i] + mItem.StatGrowth;
356+
_ = mItem.TryGetRangeFor((Stat)i, out var range);
357+
var statLow = mItem.StatsGiven[i] + range.LowRange;
358+
var statHigh = mItem.StatsGiven[i] + range.HighRange;
358359

359360
if (mItem.PercentageStatsGiven[i] != 0)
360361
{
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
</root>

0 commit comments

Comments
 (0)