ProjectDDD/Assets/_DDD/_Scripts/Restaurant/State/FlowStates/RestaurantManagementState.cs

290 lines
12 KiB
C#
Raw Normal View History

2025-07-27 19:32:41 +00:00
using System.Collections.Generic;
using System.Linq;
2025-07-27 19:32:41 +00:00
using Sirenix.OdinInspector;
using UnityEngine;
namespace DDD.Restaurant
2025-07-27 19:32:41 +00:00
{
public class RestaurantManagementState : ScriptableObject
2025-07-27 19:32:41 +00:00
{
private Dictionary<string, int> _todayFoodRecipeAndAmounts = new();
private Dictionary<string, int> _todayDrinkRecipeAndAmounts = new();
private List<string> _todayWorkers = new();
private Dictionary<string, HashSet<string>> _cookwareToRecipeMapping = new();
2025-08-17 12:51:51 +00:00
public IReadOnlyDictionary<string, int> TodayFoodRecipeAndAmounts => _todayFoodRecipeAndAmounts;
public IReadOnlyDictionary<string, int> TodayDrinkRecipeAndAmounts => _todayDrinkRecipeAndAmounts;
public IReadOnlyList<string> TodayWorkers => _todayWorkers;
public IReadOnlyDictionary<string, HashSet<string>> CookwareToRecipeMapping => _cookwareToRecipeMapping;
2025-08-17 12:51:51 +00:00
public int AddedTodayMenuCount => AddedTodayFoodCount + AddedTodayDrinkCount;
public int AddedTodayFoodCount => _todayFoodRecipeAndAmounts.Count;
public int AddedTodayDrinkCount => _todayDrinkRecipeAndAmounts.Count;
public int AddedTodayWorkerCount => _todayWorkers.Count;
public int AddedTodayCookwareCount => _cookwareToRecipeMapping.Count;
public int MatchedTodayMenuWithCookwareCount => _cookwareToRecipeMapping.Values.Count(recipeSet => recipeSet.Count > 0);
2025-08-21 09:15:51 +00:00
private RestaurantManagementData GetRestaurantManagementData() => RestaurantData.Instance.ManagementData;
2025-08-17 12:51:51 +00:00
public void InitializeReadyForRestaurant()
2025-07-27 19:32:41 +00:00
{
// TODO : Load from disk if possible (save data)
_todayFoodRecipeAndAmounts.Clear();
_todayDrinkRecipeAndAmounts.Clear();
_todayWorkers.Clear();
_cookwareToRecipeMapping.Clear();
2025-07-27 19:32:41 +00:00
}
2025-08-17 12:51:51 +00:00
/// <summary>
/// 체크리스트 항목들의 상태를 확인하는 메서드
/// </summary>
/// <returns>체크리스트 완료 여부 배열 (순서: 음식레시피, 조리도구, 매칭여부)</returns>
public bool[] GetChecklistStates()
{
bool hasFood = HasFoodRecipes();
bool hasCookware = HasCookwares();
bool hasMatching = HasMatchedMenuWithCookware();
return new bool[] { hasFood, hasCookware, hasMatching };
}
public bool HasFoodRecipes()
{
return _todayFoodRecipeAndAmounts.Count > 0;
2025-08-17 12:51:51 +00:00
}
public bool HasCookwares()
{
return _cookwareToRecipeMapping.Count > 0;
2025-08-17 12:51:51 +00:00
}
public bool HasMatchedMenuWithCookware()
{
return _cookwareToRecipeMapping.Values.Any(recipeSet => recipeSet.Count > 0);
}
public List<string> GetTodayFoodMenus()
{
return _todayFoodRecipeAndAmounts.Keys.ToList();
}
public int GetTodayFoodAvailableCount(string recipeId)
{
return _todayFoodRecipeAndAmounts.GetValueOrDefault(recipeId, 0);
2025-08-17 12:51:51 +00:00
}
public bool IsOpenable()
2025-07-27 19:32:41 +00:00
{
// TODO : 영업 가능한 상태인지 조건 추가 (최소 요리, 요리도구 배치 등)
bool isExistedCookware = CookwareToRecipeMapping.Count > 0;
bool isExistedMatchedMenu = _cookwareToRecipeMapping.Values.Any(recipeSet => recipeSet is { Count: > 0 });
2025-08-12 11:46:30 +00:00
2025-08-25 11:30:04 +00:00
var isOpenable = isExistedCookware && isExistedMatchedMenu;
return isOpenable;
}
2025-08-26 09:56:40 +00:00
public bool TryAddTodayMenu(ItemModel model)
{
string recipeId = model.Id;
2025-08-17 12:51:51 +00:00
if (model.ItemType != ItemType.Recipe) return false;
2025-08-28 11:00:22 +00:00
if (DataManager.Instance.GetDataAsset<RecipeDataAsset>().TryGetDataById(recipeId, out RecipeDataEntry recipeData) == false) return false;
2025-08-17 12:51:51 +00:00
2025-07-29 15:56:47 +00:00
bool added = false;
2025-08-17 12:51:51 +00:00
2025-07-28 11:33:04 +00:00
if (recipeData.RecipeType == RecipeType.FoodRecipe)
2025-07-27 19:32:41 +00:00
{
if (_todayFoodRecipeAndAmounts.Count >= GetRestaurantManagementData().MaxFoodCount || _todayFoodRecipeAndAmounts.ContainsKey(recipeId)) return false;
2025-07-29 15:56:47 +00:00
2025-08-28 11:00:22 +00:00
var foodData = DataManager.Instance.GetDataAsset<FoodDataAsset>().GetDataById(recipeData.RecipeResult);
2025-07-29 15:56:47 +00:00
var craftableCount = foodData.GetCraftableCount();
foodData.ConsumeAllCraftableIngredients();
2025-07-28 11:33:04 +00:00
_todayFoodRecipeAndAmounts[recipeId] = craftableCount;
2025-07-29 15:56:47 +00:00
added = true;
2025-07-28 11:33:04 +00:00
}
else if (recipeData.RecipeType == RecipeType.DrinkRecipe)
{
if (_todayDrinkRecipeAndAmounts.Count >= GetRestaurantManagementData().MaxDrinkCount || _todayDrinkRecipeAndAmounts.ContainsKey(recipeId)) return false;
2025-07-29 15:56:47 +00:00
2025-08-28 11:00:22 +00:00
var drinkData = DataManager.Instance.GetDataAsset<DrinkDataAsset>().GetDataById(recipeData.RecipeResult);
2025-07-29 15:56:47 +00:00
var craftableCount = drinkData.GetCraftableCount();
drinkData.ConsumeAllCraftableIngredients();
2025-07-28 11:33:04 +00:00
_todayDrinkRecipeAndAmounts[recipeId] = craftableCount;
2025-07-29 15:56:47 +00:00
added = true;
2025-07-27 19:32:41 +00:00
}
2025-07-29 15:56:47 +00:00
if (added)
{
var cookwareKey = GetRequiredCookwareKey(recipeId);
if (string.IsNullOrWhiteSpace(cookwareKey) == false && _cookwareToRecipeMapping.TryGetValue(cookwareKey, out var recipeSet))
{
recipeSet.Add(recipeId);
}
2025-08-17 12:51:51 +00:00
var dirtyEvt = GameEvents.SmartVariablesDirtyEvent;
dirtyEvt.DomainFlags = SmartVariablesDomain.RestaurantToday;
EventBus.Broadcast(dirtyEvt);
2025-07-29 15:56:47 +00:00
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
}
return added;
2025-07-27 19:32:41 +00:00
}
2025-08-26 09:56:40 +00:00
public bool TryRemoveTodayMenu(ItemModel model)
2025-07-27 19:32:41 +00:00
{
string recipeId = model.Id;
2025-08-17 12:51:51 +00:00
var removedEvt = RestaurantEvents.TodayMenuRemovedEvent;
2025-07-27 19:32:41 +00:00
2025-08-28 11:00:22 +00:00
if (DataManager.Instance.GetDataAsset<RecipeDataAsset>().TryGetDataById(recipeId, out RecipeDataEntry recipeData) == false) return false;
2025-07-28 11:33:04 +00:00
bool removed = false;
int refundCount = 0;
if (recipeData.RecipeType == RecipeType.FoodRecipe)
2025-07-27 19:32:41 +00:00
{
if (_todayFoodRecipeAndAmounts.TryGetValue(recipeId, out refundCount))
2025-07-28 11:33:04 +00:00
{
removed = _todayFoodRecipeAndAmounts.Remove(recipeId);
2025-08-17 12:51:51 +00:00
removedEvt.InventoryCategoryType = InventoryCategoryType.Food;
2025-07-28 11:33:04 +00:00
if (removed)
{
2025-08-28 11:00:22 +00:00
var foodData = DataManager.Instance.GetDataAsset<FoodDataAsset>().GetDataById(recipeData.RecipeResult);
2025-07-29 15:56:47 +00:00
foodData.RefundIngredients(refundCount);
2025-07-28 11:33:04 +00:00
}
}
2025-07-27 19:32:41 +00:00
}
2025-07-28 11:33:04 +00:00
else if (recipeData.RecipeType == RecipeType.DrinkRecipe)
2025-07-27 19:32:41 +00:00
{
if (_todayDrinkRecipeAndAmounts.TryGetValue(recipeId, out refundCount))
2025-07-28 11:33:04 +00:00
{
removed = _todayDrinkRecipeAndAmounts.Remove(recipeId);
2025-08-17 12:51:51 +00:00
removedEvt.InventoryCategoryType = InventoryCategoryType.Drink;
2025-07-28 11:33:04 +00:00
if (removed)
{
2025-08-28 11:00:22 +00:00
var drinkData = DataManager.Instance.GetDataAsset<DrinkDataAsset>().GetDataById(recipeData.RecipeResult);
2025-07-29 15:56:47 +00:00
drinkData.RefundIngredients(refundCount);
2025-07-28 11:33:04 +00:00
}
}
2025-07-27 19:32:41 +00:00
}
if (removed)
{
var cookwareKey = GetRequiredCookwareKey(recipeId);
if (string.IsNullOrWhiteSpace(cookwareKey) == false && _cookwareToRecipeMapping.TryGetValue(cookwareKey, out var recipeSet))
{
recipeSet.Remove(recipeId);
}
2025-07-28 11:33:04 +00:00
2025-08-17 12:51:51 +00:00
var dirtyEvt = GameEvents.SmartVariablesDirtyEvent;
dirtyEvt.DomainFlags = SmartVariablesDomain.RestaurantToday;
EventBus.Broadcast(dirtyEvt);
EventBus.Broadcast(removedEvt);
}
return removed;
2025-07-27 19:32:41 +00:00
}
2025-08-26 09:56:40 +00:00
public bool TryAddTodayCookware(ItemModel model)
2025-08-03 23:09:01 +00:00
{
var cookwareId = model.Id;
2025-08-17 12:51:51 +00:00
2025-08-28 11:00:22 +00:00
if (model.HasItem == false || DataManager.Instance.GetDataAsset<CookwareDataAsset>().ContainsData(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
if (_cookwareToRecipeMapping.Count >= GetRestaurantManagementData().MaxCookwareCount || _cookwareToRecipeMapping.ContainsKey(cookwareId)) return false;
2025-08-17 12:51:51 +00:00
_cookwareToRecipeMapping[cookwareId] = new HashSet<string>();
2025-08-17 12:51:51 +00:00
foreach (var recipeId in _todayFoodRecipeAndAmounts.Keys.Concat(_todayDrinkRecipeAndAmounts.Keys))
{
var required = GetRequiredCookwareKey(recipeId);
if (required == cookwareId)
{
_cookwareToRecipeMapping[cookwareId].Add(recipeId);
}
}
2025-08-17 12:51:51 +00:00
var dirtyEvt = GameEvents.SmartVariablesDirtyEvent;
dirtyEvt.DomainFlags = SmartVariablesDomain.RestaurantToday;
EventBus.Broadcast(dirtyEvt);
2025-08-03 23:09:01 +00:00
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
return true;
}
2025-08-17 12:51:51 +00:00
public bool IsContainTodayMenu(string recipeId) => _todayFoodRecipeAndAmounts.ContainsKey(recipeId) || _todayDrinkRecipeAndAmounts.ContainsKey(recipeId);
2025-08-03 23:09:01 +00:00
2025-08-26 09:56:40 +00:00
public bool TryRemoveTodayCookware(ItemModel model)
2025-08-03 23:09:01 +00:00
{
var cookwareId = model.Id;
2025-08-03 23:09:01 +00:00
2025-08-28 11:00:22 +00:00
if (DataManager.Instance.GetDataAsset<CookwareDataAsset>().ContainsData(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
if (_cookwareToRecipeMapping.Remove(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
var dirtyEvt = GameEvents.SmartVariablesDirtyEvent;
dirtyEvt.DomainFlags = SmartVariablesDomain.RestaurantToday;
EventBus.Broadcast(dirtyEvt);
var evt = RestaurantEvents.TodayMenuRemovedEvent;
evt.InventoryCategoryType = InventoryCategoryType.Cookware;
EventBus.Broadcast(evt);
2025-08-17 12:51:51 +00:00
2025-08-03 23:09:01 +00:00
return true;
}
2025-08-17 12:51:51 +00:00
private string GetRequiredCookwareKey(string recipeId)
{
2025-08-28 11:00:22 +00:00
if (DataManager.Instance.GetDataAsset<RecipeDataAsset>().TryGetDataById(recipeId, out var recipeData) == false) return null;
2025-08-03 23:09:01 +00:00
var resultKey = recipeData.RecipeResult;
return recipeData.RecipeType switch
{
2025-08-28 11:00:22 +00:00
RecipeType.FoodRecipe => DataManager.Instance.GetDataAsset<FoodDataAsset>().GetDataById(resultKey).CookwareKey,
RecipeType.DrinkRecipe => DataManager.Instance.GetDataAsset<DrinkDataAsset>().GetDataById(resultKey).CookwareKey,
_ => null
};
}
public bool IsCookwareMatched(string recipeId)
{
return _cookwareToRecipeMapping.Values.Any(recipeHashSets => recipeHashSets.Contains(recipeId));
}
public bool IsTodayMenuMatched(string cookwareId)
{
if (_cookwareToRecipeMapping.TryGetValue(cookwareId, out var recipeSet))
{
return recipeSet.Count > 0;
}
return false;
}
2025-08-27 10:38:46 +00:00
public bool HasAddedCookByCookwareKey(string cookwareKey)
{
return _cookwareToRecipeMapping.ContainsKey(cookwareKey) && _cookwareToRecipeMapping[cookwareKey].Count > 0;;
2025-08-27 10:38:46 +00:00
}
2025-08-29 10:01:27 +00:00
public bool TryCookingById(string recipeId)
{
if (_todayFoodRecipeIds.TryGetValue(recipeId, out var foodCount) && foodCount > 0)
{
_todayFoodRecipeIds[recipeId] -= 1;
return true;
}
if (_todayDrinkRecipeIds.TryGetValue(recipeId, out var drinkCount) && drinkCount > 0)
{
_todayDrinkRecipeIds[recipeId] -= 1;
return true;
}
return false;
}
2025-07-27 19:32:41 +00:00
}
}