ProjectDDD/Assets/_DDD/_Scripts/GameState/RestaurantManagementSo.cs

224 lines
8.8 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 System.Threading.Tasks;
using Sirenix.OdinInspector;
using UnityEngine;
namespace DDD
{
2025-07-27 19:51:11 +00:00
[CreateAssetMenu(fileName = "RestaurantManagementSo", menuName = "GameState/RestaurantManagementSo")]
public class RestaurantManagementSo : GameFlowTask
2025-07-27 19:32:41 +00:00
{
2025-07-30 07:46:11 +00:00
// TODO : 체크리스트 기능
2025-07-27 22:55:07 +00:00
public ItemSlotUi ItemSlotUiPrefab;
[Title("선택된 메뉴 상세 내용")]
public TasteHashTagSlotUi TasteHashTagSlotUiPrefab;
public Color FoodTasteOutlineColor = Color.magenta;
public Color DrinkTasteOutlineColor = Color.magenta;
2025-08-03 23:09:01 +00:00
[Title("오늘의 레스토랑 상태")]
2025-07-27 22:55:07 +00:00
public int MaxFoodCount = 8;
public int MaxDrinkCount = 6;
2025-08-03 23:09:01 +00:00
public int MaxCookwareCount = 6;
2025-07-27 22:55:07 +00:00
2025-08-03 23:09:01 +00:00
[Title("실시간 데이터")]
[ReadOnly, ShowInInspector] private Dictionary<string, int> _todayFoodRecipeIds = new();
[ReadOnly, ShowInInspector] private Dictionary<string, int> _todayDrinkRecipeIds = new();
[ReadOnly, ShowInInspector] private List<string> _todayWorkerIds = new();
[ReadOnly, ShowInInspector] private Dictionary<string, HashSet<string>> _cookwareToRecipeIds = new();
2025-07-27 19:32:41 +00:00
2025-08-03 23:09:01 +00:00
public IReadOnlyDictionary<string, int> TodayFoodRecipeIds => _todayFoodRecipeIds;
public IReadOnlyDictionary<string, int> TodayDrinkRecipeIds => _todayDrinkRecipeIds;
public IReadOnlyList<string> TodayWorkerIds => _todayWorkerIds;
public IReadOnlyDictionary<string, HashSet<string>> CookwareToRecipeIds => _cookwareToRecipeIds;
2025-07-27 19:32:41 +00:00
public override Task OnReadyNewFlow(GameFlowState newFlowState)
2025-08-07 04:16:25 +00:00
{
if (newFlowState == GameFlowState.ReadyForRestaurant)
{
InitializeReadyForRestaurant();
}
return Task.CompletedTask;
}
private void InitializeReadyForRestaurant()
2025-07-27 19:32:41 +00:00
{
2025-08-03 23:09:01 +00:00
_todayFoodRecipeIds.Clear();
_todayDrinkRecipeIds.Clear();
_todayWorkerIds.Clear();
_cookwareToRecipeIds.Clear();
2025-07-27 19:32:41 +00:00
}
public bool IsOpenable()
2025-07-27 19:32:41 +00:00
{
// TODO : 영업 가능한 상태인지 조건 추가 (최소 요리, 요리도구 배치 등)
bool isExistedCookware = CookwareToRecipeIds.Count > 0;
bool isExistedMatchedMenu = _cookwareToRecipeIds.Values.Any(recipeSet => recipeSet is { Count: > 0 });
2025-07-27 19:32:41 +00:00
return isExistedCookware && isExistedMatchedMenu;
}
2025-07-28 11:33:04 +00:00
public bool TryAddTodayMenu(ItemViewModel model)
{
string recipeId = model.Id;
if (model.ItemType != ItemType.Recipe) return false;
if (DataManager.Instance.GetDataSo<RecipeDataSo>().TryGetDataById(recipeId, out RecipeData recipeData) == false) return false;
2025-07-29 15:56:47 +00:00
bool added = false;
2025-07-28 11:33:04 +00:00
if (recipeData.RecipeType == RecipeType.FoodRecipe)
2025-07-27 19:32:41 +00:00
{
2025-08-03 23:09:01 +00:00
if (_todayFoodRecipeIds.Count >= MaxFoodCount || _todayFoodRecipeIds.ContainsKey(recipeId)) return false;
2025-07-29 15:56:47 +00:00
var foodData = DataManager.Instance.GetDataSo<FoodDataSo>().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
2025-08-03 23:09:01 +00:00
_todayFoodRecipeIds[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)
{
2025-08-03 23:09:01 +00:00
if (_todayDrinkRecipeIds.Count >= MaxDrinkCount || _todayDrinkRecipeIds.ContainsKey(recipeId)) return false;
2025-07-29 15:56:47 +00:00
var drinkData = DataManager.Instance.GetDataSo<DrinkDataSo>().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
2025-08-03 23:09:01 +00:00
_todayDrinkRecipeIds[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 && _cookwareToRecipeIds.TryGetValue(cookwareKey, out var recipeSet))
{
recipeSet.Add(recipeId);
}
2025-07-29 15:56:47 +00:00
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
}
return added;
2025-07-27 19:32:41 +00:00
}
public bool TryRemoveTodayMenu(ItemViewModel model)
2025-07-27 19:32:41 +00:00
{
string recipeId = model.Id;
2025-07-27 19:32:41 +00:00
var evt = RestaurantEvents.TodayMenuRemovedEvent;
if (DataManager.Instance.GetDataSo<RecipeDataSo>().TryGetDataById(recipeId, out RecipeData 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
{
2025-08-03 23:09:01 +00:00
if (_todayFoodRecipeIds.TryGetValue(recipeId, out refundCount))
2025-07-28 11:33:04 +00:00
{
2025-08-03 23:09:01 +00:00
removed = _todayFoodRecipeIds.Remove(recipeId);
evt.InventoryCategoryType = InventoryCategoryType.Food;
2025-07-28 11:33:04 +00:00
if (removed)
{
var foodData = DataManager.Instance.GetDataSo<FoodDataSo>().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
{
2025-08-03 23:09:01 +00:00
if (_todayDrinkRecipeIds.TryGetValue(recipeId, out refundCount))
2025-07-28 11:33:04 +00:00
{
2025-08-03 23:09:01 +00:00
removed = _todayDrinkRecipeIds.Remove(recipeId);
evt.InventoryCategoryType = InventoryCategoryType.Drink;
2025-07-28 11:33:04 +00:00
if (removed)
{
var drinkData = DataManager.Instance.GetDataSo<DrinkDataSo>().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 && _cookwareToRecipeIds.TryGetValue(cookwareKey, out var recipeSet))
{
recipeSet.Remove(recipeId);
}
2025-07-28 11:33:04 +00:00
EventBus.Broadcast(evt);
}
return removed;
2025-07-27 19:32:41 +00:00
}
public bool TryAddTodayCookware(ItemViewModel model)
2025-08-03 23:09:01 +00:00
{
var cookwareId = model.Id;
2025-08-03 23:09:01 +00:00
if (model.HasItem == false || DataManager.Instance.GetDataSo<CookwareDataSo>().ContainsData(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
if (_cookwareToRecipeIds.Count >= MaxCookwareCount || _cookwareToRecipeIds.ContainsKey(cookwareId)) return false;
_cookwareToRecipeIds[cookwareId] = new HashSet<string>();
foreach (var recipeId in _todayFoodRecipeIds.Keys.Concat(_todayDrinkRecipeIds.Keys))
{
var required = GetRequiredCookwareKey(recipeId);
if (required == cookwareId)
{
_cookwareToRecipeIds[cookwareId].Add(recipeId);
}
}
2025-08-03 23:09:01 +00:00
EventBus.Broadcast(RestaurantEvents.TodayMenuAddedEvent);
return true;
}
public bool IsContainTodayMenu(string recipeId)=> _todayFoodRecipeIds.ContainsKey(recipeId) || _todayDrinkRecipeIds.ContainsKey(recipeId);
2025-08-03 23:09:01 +00:00
public bool TryRemoveTodayCookware(ItemViewModel model)
2025-08-03 23:09:01 +00:00
{
var cookwareId = model.Id;
2025-08-03 23:09:01 +00:00
if (DataManager.Instance.GetDataSo<CookwareDataSo>().ContainsData(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
if (_cookwareToRecipeIds.Remove(cookwareId) == false) return false;
2025-08-03 23:09:01 +00:00
var evt = RestaurantEvents.TodayMenuRemovedEvent;
evt.InventoryCategoryType = InventoryCategoryType.Cookware;
EventBus.Broadcast(evt);
2025-08-03 23:09:01 +00:00
return true;
}
private string GetRequiredCookwareKey(string recipeId)
{
if (DataManager.Instance.GetDataSo<RecipeDataSo>().TryGetDataById(recipeId, out var recipeData) == false) return null;
2025-08-03 23:09:01 +00:00
var resultKey = recipeData.RecipeResult;
return recipeData.RecipeType switch
{
RecipeType.FoodRecipe => DataManager.Instance.GetDataSo<FoodDataSo>().GetDataById(resultKey).CookwareKey,
RecipeType.DrinkRecipe => DataManager.Instance.GetDataSo<DrinkDataSo>().GetDataById(resultKey).CookwareKey,
_ => null
};
}
public bool IsCookwareMatched(string recipeId)
{
return _cookwareToRecipeIds.Values.Any(recipeHashSets => recipeHashSets.Contains(recipeId));
}
2025-07-27 19:32:41 +00:00
}
}