Skip to content

Commit

Permalink
Merge pull request #111 from Lacyway/dev
Browse files Browse the repository at this point in the history
Add singletons to static classes
  • Loading branch information
seionmoya authored Jan 7, 2025
2 parents 2000aad + 6e04a3c commit c2710a0
Show file tree
Hide file tree
Showing 77 changed files with 501 additions and 382 deletions.
27 changes: 19 additions & 8 deletions Fuyu.Backend.BSG/DTO/Services/ItemFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@
namespace Fuyu.Backend.BSG.DTO.Services
{
// TODO: split UPD factory and Item Factory
public static class ItemFactoryService
public class ItemFactoryService
{
public static Dictionary<MongoId, ItemTemplate> ItemTemplates { get; private set; }
public static ItemFactoryService Instance => instance.Value;
private static readonly Lazy<ItemFactoryService> instance = new(() => new ItemFactoryService());

public static void Load()
/// <summary>
/// The construction of this class is handled in the <see cref="instance"/> (<see cref="Lazy{T}"/>)
/// </summary>
private ItemFactoryService()
{

}

public Dictionary<MongoId, ItemTemplate> ItemTemplates { get; private set; }

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

public static ItemInstance CreateItem(MongoId tpl, MongoId? id = null)
public ItemInstance CreateItem(MongoId tpl, MongoId? id = null)
{
var template = ItemTemplates[tpl];
var itemId = id.GetValueOrDefault(new MongoId(true));
Expand All @@ -37,7 +48,7 @@ public static ItemInstance CreateItem(MongoId tpl, MongoId? id = null)
return item;
}

public static ItemUpdatable CreateItemUpdatable(ItemTemplate template)
public ItemUpdatable CreateItemUpdatable(ItemTemplate template)
{
ItemUpdatable upd = null;
var updProperties = typeof(ItemUpdatable).GetProperties();
Expand All @@ -59,7 +70,7 @@ public static ItemUpdatable CreateItemUpdatable(ItemTemplate template)
return upd;
}

public static object CreateItemComponent(ItemTemplate template, Type componentType, bool createDefault)
public object CreateItemComponent(ItemTemplate template, Type componentType, bool createDefault)
{
var isItemComponentInterface = typeof(IItemComponent).IsAssignableFrom(componentType);
if (!isItemComponentInterface)
Expand All @@ -84,12 +95,12 @@ public static object CreateItemComponent(ItemTemplate template, Type componentTy
return result;
}

public static ItemUpdatable CreateItemUpdatable(MongoId tpl)
public ItemUpdatable CreateItemUpdatable(MongoId tpl)
{
return CreateItemUpdatable(ItemTemplates[tpl]);
}

public static object CreateItemComponent(MongoId tpl, Type componentType, bool createDefault)
public object CreateItemComponent(MongoId tpl, Type componentType, bool createDefault)
{
return CreateItemComponent(ItemTemplates[tpl], componentType, createDefault);
}
Expand Down
14 changes: 7 additions & 7 deletions Fuyu.Backend.BSG/Models/Profiles/InventoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class InventoryInfo

public List<ItemInstance> RemoveItem(ItemInstance item)
{
return InventoryService.RemoveItem(this, item);
return InventoryService.Instance.RemoveItem(this, item);
}

public List<ItemInstance> RemoveItem(MongoId id)
Expand All @@ -59,7 +59,7 @@ public List<ItemInstance> RemoveItem(MongoId id)
throw new Exception($"Failed to find item with id {id} in inventory");
}

return InventoryService.RemoveItem(this, item);
return InventoryService.Instance.RemoveItem(this, item);
}

public List<ItemInstance> GetItemsByTemplate(MongoId tpl)
Expand All @@ -69,7 +69,7 @@ public List<ItemInstance> GetItemsByTemplate(MongoId tpl)

public ItemInstance GetStock(ItemInstance root)
{
var rootItemTemplate = ItemFactoryService.ItemTemplates[root.TemplateId];
var rootItemTemplate = ItemFactoryService.Instance.ItemTemplates[root.TemplateId];
if (!rootItemTemplate.Props.ContainsKey("FoldedSlot"))
{
return null;
Expand All @@ -83,7 +83,7 @@ public ItemInstance GetStock(ItemInstance root)

public Vector2 GetItemSize(ItemInstance root)
{
var rootTemplate = ItemFactoryService.ItemTemplates[root.TemplateId];
var rootTemplate = ItemFactoryService.Instance.ItemTemplates[root.TemplateId];
var rootProperties = rootTemplate.Props.ToObject<ItemProperties>();
var width = rootProperties.Width;
var height = rootProperties.Height;
Expand All @@ -100,7 +100,7 @@ public Vector2 GetItemSize(ItemInstance root)
var children = ItemService.GetItemAndChildren(Items, root).Skip(1);
foreach (var child in children)
{
var itemTemplate = ItemFactoryService.ItemTemplates[child.TemplateId];
var itemTemplate = ItemFactoryService.Instance.ItemTemplates[child.TemplateId];
var itemProperties = itemTemplate.Props.ToObject<ItemProperties>();
if (itemProperties.ExtraSizeForceAdd)
{
Expand Down Expand Up @@ -151,7 +151,7 @@ public LocationInGrid GetNextFreeSlot(int width, int height, out string gridName
return null;
}

var stashItemTemplate = ItemFactoryService.ItemTemplates[stashItem.TemplateId];
var stashItemTemplate = ItemFactoryService.Instance.ItemTemplates[stashItem.TemplateId];
var stashItemProps = stashItemTemplate.Props.ToObject<CompoundItemItemProperties>();

foreach (var grid in stashItemProps.Grids)
Expand All @@ -169,7 +169,7 @@ public LocationInGrid GetNextFreeSlot(int width, int height, out string gridName
continue;
}

var itemTemplate = ItemFactoryService.ItemTemplates[itemInThisGrid.TemplateId];
var itemTemplate = ItemFactoryService.Instance.ItemTemplates[itemInThisGrid.TemplateId];
var itemProps = itemTemplate.Props.ToObject<ItemProperties>();
var itemLocation = itemInThisGrid.Location.Value1;
var itemSize = GetItemSize(itemInThisGrid);
Expand Down
20 changes: 16 additions & 4 deletions Fuyu.Backend.BSG/Services/InventoryService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Fuyu.Backend.BSG.Models.Items;
using Fuyu.Backend.BSG.Models.Profiles;
using Fuyu.Common.Hashing;

namespace Fuyu.Backend.BSG.Services
{
public static class InventoryService
public class InventoryService
{
public static List<ItemInstance> RemoveItem(InventoryInfo inventory, ItemInstance item)
public static InventoryService Instance => instance.Value;
private static readonly Lazy<InventoryService> instance = new(() => new InventoryService());

/// <summary>
/// The construction of this class is handled in the <see cref="instance"/> (<see cref="Lazy{T}"/>)
/// </summary>
private InventoryService()
{

}

public List<ItemInstance> RemoveItem(InventoryInfo inventory, ItemInstance item)
{
var itemsToRemove = ItemService.GetItemAndChildren(inventory.Items, item);

Expand All @@ -19,7 +31,7 @@ public static List<ItemInstance> RemoveItem(InventoryInfo inventory, ItemInstanc
// NOTE:
// * order is really important here!
// -- seionmoya, 2024-10-24
public static void RegenerateIds(InventoryInfo inventory)
public void RegenerateIds(InventoryInfo inventory)
{
var mapping = new Dictionary<MongoId, MongoId>();

Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.Core/Controllers/AccountGamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AccountGamesController() : base("/account/games")
public override Task RunAsync(CoreHttpContext context)
{
var sessionId = context.GetSessionId();
var result = AccountService.GetGames(sessionId);
var result = AccountService.Instance.GetGames(sessionId);
var response = new AccountGamesResponse()
{
Games = result
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.Core/Controllers/AccountLoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public AccountLoginController() : base("/account/login")

public override Task RunAsync(CoreHttpContext context, AccountLoginRequest body)
{
var response = AccountService.LoginAccount(body.Username, body.Password);
var response = AccountService.Instance.LoginAccount(body.Username, body.Password);

return context.SendJsonAsync(Json.Stringify(response));
}
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.Core/Controllers/AccountLogoutController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public AccountLogoutController() : base("/account/logout")
public override Task RunAsync(CoreHttpContext context)
{
var sessionId = context.GetSessionId();
CoreOrm.RemoveSession(sessionId);
CoreOrm.Instance.RemoveSession(sessionId);

return context.SendJsonAsync("{}");
}
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.Core/Controllers/AccountRegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AccountRegisterController() : base("/account/register")

public override Task RunAsync(CoreHttpContext context, AccountRegisterRequest request)
{
var result = AccountService.RegisterAccount(request.Username, request.Password);
var result = AccountService.Instance.RegisterAccount(request.Username, request.Password);
var response = new AccountRegisterResponse()
{
Status = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AccountRegisterGameController() : base("/account/register/game")
public override Task RunAsync(CoreHttpContext context, AccountRegisterGameRequest request)
{
var sessionId = context.GetSessionId();
var result = AccountService.RegisterGame(sessionId, request.Game, request.Edition);
var result = AccountService.Instance.RegisterGame(sessionId, request.Game, request.Edition);

return context.SendJsonAsync(Json.Stringify(result));
}
Expand Down
23 changes: 15 additions & 8 deletions Fuyu.Backend.Core/CoreDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@
using Fuyu.Common.Collections;
using Fuyu.Common.IO;
using Fuyu.Common.Serialization;
using System;

namespace Fuyu.Backend.Core
{
// NOTE: The properties of this class should _NEVER_ be accessed from the
// outside. Use CoreOrm instead.
// -- seionmoya, 2024/09/06

public static class CoreDatabase
public class CoreDatabase
{
internal static readonly ThreadList<Account> Accounts;
public static CoreDatabase Instance => instance.Value;
private static readonly Lazy<CoreDatabase> instance = new(() => new CoreDatabase());

internal readonly ThreadList<Account> Accounts;

// sessid aid
internal static readonly ThreadDictionary<string, int> Sessions;
internal readonly ThreadDictionary<string, int> Sessions;

static CoreDatabase()
/// <summary>
/// The construction of this class is handled in the <see cref="instance"/> (<see cref="Lazy{T}"/>)
/// </summary>
private CoreDatabase()
{
Accounts = new ThreadList<Account>();
Sessions = new ThreadDictionary<string, int>();
}

public static void Load()
public void Load()
{
LoadAccounts();
LoadSessions();
}

private static void LoadAccounts()
private void LoadAccounts()
{
var path = "./Fuyu/Accounts/Core/";

Expand All @@ -43,13 +50,13 @@ private static void LoadAccounts()
{
var json = VFS.ReadTextFile(filepath);
var account = Json.Parse<Account>(json);
CoreOrm.SetOrAddAccount(account);
CoreOrm.Instance.SetOrAddAccount(account);

Terminal.WriteLine($"Loaded fuyu account {account.Id}");
}
}

private static void LoadSessions()
private void LoadSessions()
{
// intentionally empty
// sessions are created when users are logged in successfully
Expand Down
Loading

0 comments on commit c2710a0

Please sign in to comment.