Skip to content

Commit

Permalink
Fixed things
Browse files Browse the repository at this point in the history
  • Loading branch information
nexus4880 committed Jan 26, 2025
1 parent 47dc5fa commit 6221d1e
Show file tree
Hide file tree
Showing 21 changed files with 521 additions and 293 deletions.
19 changes: 19 additions & 0 deletions Fuyu.Backend.BSG/ItemFactoryDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Fuyu.Backend.BSG.ItemTemplates;
using Fuyu.Common.Collections;
using Fuyu.Common.Hashing;

namespace Fuyu.Backend.BSG;

public class ItemFactoryDatabase
{
public static ItemFactoryDatabase Instance => instance.Value;
private static readonly Lazy<ItemFactoryDatabase> instance = new(() => new ItemFactoryDatabase());

internal readonly ThreadDictionary<MongoId, ItemTemplate> itemTemplates;

private ItemFactoryDatabase()
{
itemTemplates = new ThreadDictionary<MongoId, ItemTemplate>();
}
}
30 changes: 30 additions & 0 deletions Fuyu.Backend.BSG/ItemFactoryLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using Fuyu.Backend.BSG.ItemTemplates;
using Fuyu.Backend.BSG.Models.Responses;
using Fuyu.Common.Hashing;
using Fuyu.Common.IO;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.BSG;

public class ItemFactoryLoader
{
public static ItemFactoryLoader Instance => instance.Value;
private static readonly Lazy<ItemFactoryLoader> instance = new(() => new ItemFactoryLoader());

private readonly ItemFactoryOrm _itemFactoryOrm;

public ItemFactoryLoader()
{
_itemFactoryOrm = ItemFactoryOrm.Instance;
}

public void Load()
{
var itemsText = Resx.GetText("eft", "database.client.items.json");
var itemTemplates = Json.Parse<ResponseBody<Dictionary<MongoId, ItemTemplate>>>(itemsText).data;

_itemFactoryOrm.SetItemTemplates(itemTemplates);
}
}
37 changes: 37 additions & 0 deletions Fuyu.Backend.BSG/ItemFactoryOrm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using Fuyu.Backend.BSG.ItemTemplates;
using Fuyu.Common.Hashing;

namespace Fuyu.Backend.BSG;

public class ItemFactoryOrm
{
public static ItemFactoryOrm Instance => instance.Value;
private static readonly Lazy<ItemFactoryOrm> instance = new(() => new ItemFactoryOrm());

private readonly ItemFactoryDatabase _itemFactoryDatabase;

public ItemFactoryOrm()
{
_itemFactoryDatabase = ItemFactoryDatabase.Instance;
}

public void SetItemTemplates(Dictionary<MongoId, ItemTemplate> itemTemplates)
{
foreach (var (key, value) in itemTemplates)
{
_itemFactoryDatabase.itemTemplates.Set(key, value);
}
}

public Dictionary<MongoId, ItemTemplate> GetItemTemplates()
{
return _itemFactoryDatabase.itemTemplates.ToDictionary();
}

public ItemTemplate GetItemTemplate(MongoId id)
{
return _itemFactoryDatabase.itemTemplates.TryGet(id, out var itemTemplate) ? itemTemplate : null;
}
}
40 changes: 39 additions & 1 deletion Fuyu.Backend.BSG/Models/Items/ItemInstance.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using Fuyu.Backend.BSG.ItemTemplates;
using Fuyu.Backend.BSG.Services;
using Fuyu.Common.Collections;
using Fuyu.Common.Hashing;
Expand Down Expand Up @@ -67,4 +69,40 @@ public T GetOrCreateUpdatable<T>() where T : class

return value;
}
}

public void InitializeMatrices(IList<Grid> grids, IList<ItemInstance> children)
{
foreach (var grid in grids)
{
var width = grid.Properties.CellsHorizontal;
var height = grid.Properties.CellsVertical;
var matrix = new bool[width, height];

foreach (var itemInGrid in children.Where(i => i.SlotId == grid.Name))
{
if (!itemInGrid.Location.IsValue1)
{
throw new Exception("!itemInGrid.Location.IsValue1");
}

var itemsInGrid = ItemService.Instance.GetItemAndChildren(children.ToList(), itemInGrid);
(int itemWidth, int itemHeight) = ItemService.Instance.CalculateItemSize(itemsInGrid, itemInGrid.Location.Value1.r);

for (var dx = 0; dx < itemWidth; dx++)
{
for (var dy = 0; dy < itemHeight; dy++)
{
var x = itemInGrid.Location.Value1.x + dx;
var y = itemInGrid.Location.Value1.y + dy;

matrix[x, y] = true;
}
}
}

Matrices[grid.Name] = matrix;
}
}

public Dictionary<string, bool[,]> Matrices { get; } = [];
}
Loading

0 comments on commit 6221d1e

Please sign in to comment.