Отчистка кода

This commit is contained in:
2025-04-24 17:58:58 +04:00
parent 31ad99ade5
commit cc7e97b6ff
20 changed files with 1972 additions and 1562 deletions
+52 -51
View File
@@ -1,5 +1,4 @@
using LinqToDB.Tools;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
@@ -7,8 +6,8 @@ using Newtonsoft.Json.Serialization;
using Pf2eModel;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Common.I18n;
using Pf2eModel.Model.Enum;
using static LinqToDB.Common.Configuration;
namespace Pf2eViewer;
@@ -49,11 +48,10 @@ public partial class MainViewForm : Form
public int MaxPage => Convert.ToInt32(Math.Ceiling(1.0 * Count / PerPage));
private void UpdateTreeView()
{
EntityTreeView.Nodes.Clear();
EntityType type = (EntityType) TypesListBox.SelectedItem!;
EntityType type = (EntityType)TypesListBox.SelectedItem!;
Count = Context.Entities.Count(e => e.Type == type);
@@ -63,8 +61,8 @@ public partial class MainViewForm : Form
return;
}
EntityGridView.DataSource = Context.Entities
.Where(e => e.Type == (EntityType) TypesListBox.SelectedItem)
EntityGridView.DataSource = Context
.Entities.Where(e => e.Type == (EntityType)TypesListBox.SelectedItem)
.OrderBy(e => e.Id)
.Select(e => new EntityGridRow(e))
.Skip((Page - 1) * PerPage)
@@ -90,22 +88,27 @@ public partial class MainViewForm : Form
Page = 1;
}
private void FirstPageButton_Click(object sender, EventArgs e)
{
Page = 1;
}
private void PrevPageButton_Click(object sender, EventArgs e)
{
Page--;
}
private void NextPageButton_Click(object sender, EventArgs e)
{
Page++;
}
private void LastPageButton_Click(object sender, EventArgs e)
{
Page = MaxPage;
}
private void PageTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
@@ -113,14 +116,16 @@ public partial class MainViewForm : Form
e.Handled = true;
}
if (e.KeyChar == (char) Keys.Return)
if (e.KeyChar == (char)Keys.Return)
{
UpdateTreeView();
}
}
private void EntityGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
private void EntityGridView_RowStateChanged(
object sender,
DataGridViewRowStateChangedEventArgs e
)
{
int rowsCount = EntityGridView.SelectedRows.Count;
if (rowsCount is 0 or > 1)
@@ -130,7 +135,7 @@ public partial class MainViewForm : Form
DataGridViewRow row = EntityGridView.SelectedRows[0];
EntityGridRow data = (EntityGridRow) row.DataBoundItem;
EntityGridRow data = (EntityGridRow)row.DataBoundItem;
ViewEntity(data.Id);
}
@@ -139,18 +144,16 @@ public partial class MainViewForm : Form
{
EntityTreeView.Nodes.Clear();
Entity? entity = Context.Entities
.Where(e => e.Id == id && e.Type == (EntityType) TypesListBox.SelectedItem!)
.Include(e => e.Sources)
.ThenInclude(s => s.Source)
.Include(e => e.Aliases)
.Include(e => e.Keywords)
.Include(e => e.Externals)
.ThenInclude(x => x.Target)
.Include(e => e.Traits)
.ThenInclude(t => t.Entity)
.FirstOrDefault()
;
Entity? entity = Context
.Entities.Where(e => e.Id == id && e.Type == (EntityType)TypesListBox.SelectedItem!)
.Include(e => e.Sources)
.ThenInclude(s => s.Source)
.Include(e => e.Keywords)
.Include(e => e.Externals)
.ThenInclude(x => x.Target)
.Include(e => e.Traits)
.ThenInclude(t => t.Entity)
.FirstOrDefault();
if (entity == null)
{
@@ -158,7 +161,6 @@ public partial class MainViewForm : Form
return;
}
string json = SerializeEntity(entity);
JsonToTreeview(EntityTreeView, json, "Entity");
@@ -166,13 +168,12 @@ public partial class MainViewForm : Form
DescriptionTextBox.Text = entity.Description.Replace("\\r", "").Replace("\\n", "\\r\\n");
}
public string SerializeEntity(Entity entity)
{
JsonSerializerSettings settings = new()
{
Converters = [
Converters =
[
new EntityConverter(),
new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() },
],
@@ -192,51 +193,51 @@ public partial class MainViewForm : Form
switch (entity.Type)
{
case EntityType.Ancestry:
return Context.Ancestries
.Include(a => a.AncestryBoosts)
return Context
.Ancestries.Include(a => a.AncestryBoosts)
.FirstOrDefault(e => e.Id == entity.Id);
case EntityType.Archetype:
return Context.Archetypes
.Include(a => a.DedicationFeat)
return Context
.Archetypes.Include(a => a.DedicationFeat)
.ThenInclude(f => f.Entity)
.Include(a => a.Feats)
.ThenInclude(f => f.Entity)
.FirstOrDefault(a => a.Id == entity.Id);
case EntityType.Background:
return Context.Backgrounds
.Include(b => b.BackgroundBoosts)
return Context
.Backgrounds.Include(b => b.BackgroundBoosts)
.Include(b => b.BackgroundSkills)
.ThenInclude(t => t.Training)
.Include(b => b.BackgroundItems)
.FirstOrDefault(e => e.Id == entity.Id);
case EntityType.Class:
return Context.Classes
.Include(c => c.ClassKeyAbilities)
return Context
.Classes.Include(c => c.ClassKeyAbilities)
.Include(c => c.ClassTrainings)
.ThenInclude(t => t.Training)
.Include(c => c.ClassProgressions)
.FirstOrDefault(c => c.Id == entity.Id);
case EntityType.Feat:
return Context.Feats
.Include(f => f.Archetypes)
return Context
.Feats.Include(f => f.Archetypes)
.ThenInclude(a => a.Entity)
.FirstOrDefault(f => f.Id == entity.Id);
default:
Type type = EntityOfTypeAttribute.Resolve(entity);
return (IEntity?) Context.Find(type, entity.Id);
return (IEntity?)Context.Find(type, entity.Id);
}
}
public static void JsonToTreeview(TreeView treeView, string jsonString, string rootName)
{
object json = jsonString.StartsWith('[')
? JArray.Parse(jsonString)
: JObject.Parse(jsonString);
? JArray.Parse(jsonString)
: JObject.Parse(jsonString);
treeView.Nodes.Clear();
foreach (TreeNode node in Element2TreeNode(json, rootName).Nodes)
@@ -253,7 +254,7 @@ public partial class MainViewForm : Form
switch (element.GetType().Name)
{
case "JObject":
JObject jObject = (JObject) element;
JObject jObject = (JObject)element;
foreach (KeyValuePair<string, JToken> token in jObject)
{
subNodes.Add(Element2TreeNode(token.Value, token.Key));
@@ -261,7 +262,7 @@ public partial class MainViewForm : Form
break;
case "JArray":
JArray jArray = (JArray) element;
JArray jArray = (JArray)element;
int i = 0;
subNodes.AddRange(jArray.Select(token => Element2TreeNode(token, $"[{i++}]")));
@@ -272,22 +273,23 @@ public partial class MainViewForm : Form
break;
case "JValue":
JValue jValue = (JValue) element;
JValue jValue = (JValue)element;
text += $": ({jValue.Type}) {jValue}";
break;
default:
throw new Exception("clsJSON2Treeview can't interpret object:" + element.GetType().Name);
throw new Exception(
"clsJSON2Treeview can't interpret object:" + element.GetType().Name
);
}
TreeNode treeNode = new(text);
if (subNodes.Count > 0)
{
treeNode.Nodes.AddRange(subNodes
.OrderBy(node => node.Nodes.Count > 0 ? 1 : 0)
.ToArray()
);
treeNode.Nodes.AddRange(
subNodes.OrderBy(node => node.Nodes.Count > 0 ? 1 : 0).ToArray()
);
}
return treeNode;
@@ -296,12 +298,11 @@ public partial class MainViewForm : Form
private class EntityGridRow(Entity e)
{
public int Id { get; set; } = e.Id;
public string Name { get; set; } = e.Name.ToString();
public string Name { get; set; } = e.Name[LocaleType.English];
public EntityRarity Rarity { get; set; } = e.Rarity;
private readonly Entity _e = e;
public Entity GetEntity() => _e;
}
}