Skip to content

Commit

Permalink
Added functions to HandbookService
Browse files Browse the repository at this point in the history
Added GetItemProperties overload and changed how props is deserialized
Removed code from HandbookTemplates model
Changed some ItemProperties to be properties and changed some arrays to lists
  • Loading branch information
nexus4880 committed Jan 21, 2025
1 parent 2bd4f14 commit ea344c6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 71 deletions.
4 changes: 2 additions & 2 deletions Fuyu.Backend.BSG/ItemTemplates/CompoundItemItemProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class Slot
public class SlotProperties
{
[DataMember(Name = "filters")]
public SlotPropertiesFilter[] Filters { get; set; }
public List<SlotPropertiesFilter> Filters { get; set; }
}

[DataContract]
Expand Down Expand Up @@ -129,7 +129,7 @@ public class Grid
public class GridProperties
{
[DataMember(Name = "filters")]
public GridPropertiesFilter[] Filters { get; set; }
public List<GridPropertiesFilter> Filters { get; set; }

[DataMember(Name = "cellsH")]
public int CellsHorizontal { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.BSG/ItemTemplates/WeaponItemProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public class WeaponItemProperties : CompoundItemItemProperties

[DataMember(Name = "AimSensitivity")]
[UnionMappings(JTokenType.Float, JTokenType.Array)]
public Union<float, float[][]> AimSensitivity = 1f;
public Union<float, float[][]> AimSensitivity { get; set; } = 1f;

[DataMember(Name = "DurabilityBurnRatio")]
public float DurabilityBurnRatio = 1f;
Expand Down
64 changes: 1 addition & 63 deletions Fuyu.Backend.BSG/Models/Trading/HandbookTemplates.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Fuyu.Common.Hashing;

namespace Fuyu.Backend.BSG.Models.Trading;

Expand All @@ -13,64 +11,4 @@ public class HandbookTemplates

[DataMember(Name = "Items")]
public List<HandbookItem> Items { get; set; }

public List<HandbookCategory> GetAllCategoriesOfType(HandbookCategory root)
{
var result = new List<HandbookCategory> { root };
var added = true;

while (added)
{
added = false;

for (var i = 0; i < Categories.Count; i++)
{
var category = Categories[i];

if (category.ParentId.HasValue &&
result.Exists(c => c.Id == category.ParentId.Value) &&
!result.Exists(c => c.Id == category.Id))
{
result.Add(category);
added = true;
}
}
}

return result;
}

public List<HandbookItem> GetAllItemsOfType(MongoId id)
{
var items = new List<HandbookItem>();

for (var i = 0; i < Items.Count; i++)
{
var item = Items[i];

if (item.ParentId == id)
{
items.Add(item);
}
}

return items;
}

public List<HandbookItem> GetAllItemsOfTypeAndSubcategories(MongoId id)
{
var items = new List<HandbookItem>();

for (var i = 0; i < Items.Count; i++)
{
var item = Items[i];

if (item.ParentId == id)
{
items.Add(item);
}
}

return items;
}
}
19 changes: 17 additions & 2 deletions Fuyu.Backend.BSG/Services/ItemFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Fuyu.Common.Hashing;
using Fuyu.Common.IO;
using Fuyu.Common.Serialization;
using Newtonsoft.Json;

namespace Fuyu.Backend.BSG.Services;

Expand Down Expand Up @@ -40,7 +41,21 @@ public void Load()
/// <returns>The <see cref="ItemProperties"/> class defined</returns>
public T GetItemProperties<T>(MongoId templateId) where T : ItemProperties
{
return ItemTemplates[templateId].Props.ToObject<T>();
return GetItemProperties<T>(ItemTemplates[templateId]);
}

/// <summary>
/// Gets an <see cref="ItemProperties"/> from a <see cref="ItemTemplate"/> Template
/// </summary>
/// <typeparam name="T">The <see cref="ItemProperties"/> class to return</typeparam>
/// <param name="template">The <see cref="ItemTemplate"/> Template to get the <see cref="ItemProperties"/> from</param>
/// <returns>The <see cref="ItemProperties"/> class defined</returns>
public T GetItemProperties<T>(ItemTemplate template) where T : ItemProperties
{
var reader = template.Props.CreateReader();
var serializer = JsonSerializer.Create(Json.jsonSerializerSettings);

return serializer.Deserialize<T>(reader);
}

public List<ItemInstance> CreateItem(ItemTemplate template, int? count = null, MongoId? id = null, string parentId = null,
Expand Down Expand Up @@ -70,7 +85,7 @@ public List<ItemInstance> CreateItem(ItemTemplate template, int? count = null, M
// Handle child items - these are created once per root item
if (compoundItemProperties.Slots != null)
{
foreach (var slot in compoundItemProperties.Slots.Where(s => s.Required && s.Properties.Filters.Length > 0))
foreach (var slot in compoundItemProperties.Slots.Where(s => s.Required && s.Properties.Filters.Count > 0))
{
if (!slot.Properties.Filters[0].Plate.HasValue)
{
Expand Down
73 changes: 73 additions & 0 deletions Fuyu.Backend.EFTMain/Services/HandbookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,77 @@ public HashSet<HandbookCategory> GetHandbookTree(List<HandbookCategory> categori

return price;
}

public List<HandbookCategory> GetAllCategoriesOfType(HandbookCategory root)
{
var handbook = _eftOrm.GetHandbook();
var result = new List<HandbookCategory> { root };
var added = true;

while (added)
{
added = false;

for (var i = 0; i < handbook.Categories.Count; i++)
{
var category = handbook.Categories[i];

if (category.ParentId.HasValue &&
result.Exists(c => c.Id == category.ParentId.Value) &&
!result.Exists(c => c.Id == category.Id))
{
result.Add(category);
added = true;
}
}
}

return result;
}

public List<HandbookItem> GetAllItemsOfType(MongoId id)
{
var handbook = _eftOrm.GetHandbook();
var rootCategory = handbook.Categories.Find(c => c.Id == id);
var categories = GetAllCategoriesOfType(rootCategory).Select(c => c.Id).ToList();

var itemIds = new List<MongoId>();
var added = true;

while (added)
{
added = false;

for (var i = 0; i < handbook.Items.Count; i++)
{
var item = handbook.Items[i];

if (!itemIds.Contains(item.Id) && categories.Contains(item.ParentId))
{
itemIds.Add(item.Id);
added = true;
}
}
}

return handbook.Items.Where(i => itemIds.Contains(i.Id)).ToList();
}

public List<HandbookItem> GetAllItemsOfTypeAndSubcategories(MongoId id)
{
var handbook = _eftOrm.GetHandbook();
var items = new List<HandbookItem>();

for (var i = 0; i < handbook.Items.Count; i++)
{
var item = handbook.Items[i];

if (item.ParentId == id)
{
items.Add(item);
}
}

return items;
}
}
6 changes: 3 additions & 3 deletions Fuyu.Common/Serialization/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ namespace Fuyu.Common.Serialization;

public static class Json
{
private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings
public static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new UnionContractResolver(),
Formatting = Formatting.None
};

public static T Parse<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json, _settings);
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
}

public static string Stringify(object o)
{
return JsonConvert.SerializeObject(o, _settings);
return JsonConvert.SerializeObject(o, jsonSerializerSettings);
}

public static T Clone<T>(object o)
Expand Down

0 comments on commit ea344c6

Please sign in to comment.