From 7dea8032c23302b302e9da6f264c7189da28b9e6 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 21 Jan 2025 01:35:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=BA=97=E5=92=8C?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity/System/Store.cs | 135 ++++++++++++++++++ Library/Common/Event/UserActivityEventArgs.cs | 13 ++ .../Common/JsonConverter/GoodsConverter.cs | 64 +++++++++ .../Common/JsonConverter/StoreConverter.cs | 43 ++++++ Library/Constant/ResultEnum.cs | 7 + Library/Constant/StateEnum.cs | 16 ++- Service/JsonManager.cs | 2 +- 7 files changed, 275 insertions(+), 5 deletions(-) create mode 100644 Entity/System/Store.cs create mode 100644 Library/Common/Event/UserActivityEventArgs.cs create mode 100644 Library/Common/JsonConverter/GoodsConverter.cs create mode 100644 Library/Common/JsonConverter/StoreConverter.cs diff --git a/Entity/System/Store.cs b/Entity/System/Store.cs new file mode 100644 index 0000000..3b918d6 --- /dev/null +++ b/Entity/System/Store.cs @@ -0,0 +1,135 @@ +using System.Text; +using Milimoe.FunGame.Core.Interface.Entity; +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Entity +{ + public class Store : BaseEntity + { + public User User { get; set; } = General.UnknownUserInstance; + public DateTime? StartTime { get; set; } = null; + public DateTime? EndTime { get; set; } = null; + public Dictionary Goods { get; } = []; + + public Store(string name, User? user = null) + { + Name = name; + if (user != null) + { + User = user; + } + } + + public override string ToString() + { + StringBuilder builder = new(); + + builder.AppendLine($"☆★☆ {Name} ☆★☆"); + if (StartTime.HasValue && EndTime.HasValue) + { + builder.AppendLine($"营业时间:{StartTime.Value.ToString(General.GeneralDateTimeFormatChinese)}至{EndTime.Value.ToString(General.GeneralDateTimeFormatChinese)}"); + } + else if (StartTime.HasValue && !EndTime.HasValue) + { + builder.AppendLine($"开始营业时间:{StartTime.Value.ToString(General.GeneralDateTimeFormatChinese)}"); + } + else if (!StartTime.HasValue && EndTime.HasValue) + { + builder.AppendLine($"停止营业时间:{EndTime.Value.ToString(General.GeneralDateTimeFormatChinese)}"); + } + else + { + builder.AppendLine($"[ 24H ] 全年无休,永久开放"); + } + builder.AppendLine($"☆--- 商品列表 ---☆"); + foreach (Goods good in Goods.Values) + { + builder.AppendLine($"{good.Id}. {good.Name}"); + builder.AppendLine($"商品描述:{good.Description}"); + builder.AppendLine($"商品售价:{string.Join("、", good.Prices.Select(kv => $"{kv.Value} {kv.Key}"))}"); + builder.AppendLine($"包含物品:{string.Join("、", good.Items.Select(i => $"[{ItemSet.GetQualityTypeName(i.QualityType)}|{ItemSet.GetItemTypeName(i.ItemType)}] {i.Name}"))}"); + builder.AppendLine($"剩余库存:{good.Stock}"); + } + + return builder.ToString().Trim(); + } + + public void AddItem(Item item, int stock, string name = "", string description = "") + { + long id = Goods.Count > 0 ? Goods.Keys.Max() + 1 : 1; + if (name.Trim() == "") + { + name = item.Name; + } + if (description.Trim() == "") + { + description = item.Description; + } + Goods goods = new(id, item, stock, name, description); + if (item.Price > 0) + { + goods.SetPrice(General.GameplayEquilibriumConstant.InGameCurrency, item.Price); + } + Goods.Add(id, goods); + } + + public void AddItems(IEnumerable items, int stock) + { + foreach (Item item in items) + { + AddItem(item, stock); + } + } + + public override bool Equals(IBaseEntity? other) + { + return other is Store && other.GetIdName() == GetIdName(); + } + } + + public class Goods + { + public long Id { get; set; } + public List Items { get; } + public string Name { get; set; } + public string Description { get; set; } + public Dictionary Prices { get; } = []; + public int Stock { get; set; } + + public Goods(long id, Item item, int stock, string name, string description, Dictionary? prices = null) + { + Id = id; + Items = [item]; + Stock = stock; + Name = name; + Description = description; + if (prices != null) Prices = prices; + } + + public Goods(long id, List items, int stock, string name, string description, Dictionary? prices = null) + { + Id = id; + Items = items; + Stock = stock; + Name = name; + Description = description; + if (prices != null) Prices = prices; + } + + public void SetPrice(string needy, double price) + { + Prices[needy] = price; + } + + public bool GetPrice(string needy, out double price) + { + price = -1; + if (Prices.TryGetValue(needy, out double temp) && temp > 0) + { + price = temp; + return true; + } + return false; + } + } +} diff --git a/Library/Common/Event/UserActivityEventArgs.cs b/Library/Common/Event/UserActivityEventArgs.cs new file mode 100644 index 0000000..d8786a5 --- /dev/null +++ b/Library/Common/Event/UserActivityEventArgs.cs @@ -0,0 +1,13 @@ +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Library.Common.Event +{ + public class UserActivityEventArgs(long userId, ActivityState activityState, DateTime startTime, DateTime endTime) : GeneralEventArgs + { + public long UserId { get; } = userId; + public ActivityState ActivityState { get; } = activityState; + public DateTime StartTime { get; } = startTime; + public DateTime EndTime { get; } = endTime; + public bool AllowAccess { get; set; } = false; + } +} diff --git a/Library/Common/JsonConverter/GoodsConverter.cs b/Library/Common/JsonConverter/GoodsConverter.cs new file mode 100644 index 0000000..205d8f7 --- /dev/null +++ b/Library/Common/JsonConverter/GoodsConverter.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Architecture; + +namespace Milimoe.FunGame.Core.Library.Common.JsonConverter +{ + public class GoodsConverter : BaseEntityConverter + { + public override Goods NewInstance() + { + return new Goods(0, Factory.GetItem(), 0, "", ""); + } + + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Goods result, Dictionary convertingContext) + { + switch (propertyName) + { + case nameof(Goods.Id): + result.Id = reader.GetInt64(); + break; + case nameof(Goods.Items): + List items = NetworkUtility.JsonDeserialize>(ref reader, options) ?? []; + foreach (Item item in items) + { + result.Items.Add(item); + } + break; + case nameof(Goods.Stock): + result.Stock = reader.GetInt32(); + break; + case nameof(Goods.Name): + result.Name = reader.GetString() ?? ""; + break; + case nameof(Goods.Description): + result.Description = reader.GetString() ?? ""; + break; + case nameof(Goods.Prices): + Dictionary prices = NetworkUtility.JsonDeserialize>(ref reader, options) ?? []; + foreach (string needy in prices.Keys) + { + result.Prices[needy] = prices[needy]; + } + break; + } + } + + public override void Write(Utf8JsonWriter writer, Goods value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + + writer.WriteNumber(nameof(Goods.Id), value.Id); + writer.WritePropertyName(nameof(Goods.Items)); + JsonSerializer.Serialize(writer, value.Items, options); + writer.WriteNumber(nameof(Goods.Stock), value.Stock); + writer.WriteString(nameof(Goods.Name), value.Name); + writer.WriteString(nameof(Goods.Description), value.Description); + writer.WritePropertyName(nameof(Goods.Prices)); + JsonSerializer.Serialize(writer, value.Prices, options); + + writer.WriteEndObject(); + } + } +} diff --git a/Library/Common/JsonConverter/StoreConverter.cs b/Library/Common/JsonConverter/StoreConverter.cs new file mode 100644 index 0000000..18d5197 --- /dev/null +++ b/Library/Common/JsonConverter/StoreConverter.cs @@ -0,0 +1,43 @@ +using System.Text.Json; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Architecture; + +namespace Milimoe.FunGame.Core.Library.Common.JsonConverter +{ + public class StoreConverter : BaseEntityConverter + { + public override Store NewInstance() + { + return new Store("商店"); + } + + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Store result, Dictionary convertingContext) + { + switch (propertyName) + { + case nameof(Store.Name): + result.Name = reader.GetString() ?? ""; + break; + case nameof(Store.Goods): + Dictionary goods = NetworkUtility.JsonDeserialize>(ref reader, options) ?? []; + foreach (long id in goods.Keys) + { + result.Goods[id] = goods[id]; + } + break; + } + } + + public override void Write(Utf8JsonWriter writer, Store value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + + writer.WriteString(nameof(Store.Name), value.Name); + writer.WritePropertyName(nameof(Store.Goods)); + JsonSerializer.Serialize(writer, value.Goods, options); + + writer.WriteEndObject(); + } + } +} diff --git a/Library/Constant/ResultEnum.cs b/Library/Constant/ResultEnum.cs index c8fb21c..3a82c98 100644 --- a/Library/Constant/ResultEnum.cs +++ b/Library/Constant/ResultEnum.cs @@ -64,4 +64,11 @@ namespace Milimoe.FunGame.Core.Library.Constant Critical, Evaded } + + public enum RedeemResult + { + Success, + StockNotEnough, + PointsNotEnough + } } diff --git a/Library/Constant/StateEnum.cs b/Library/Constant/StateEnum.cs index ec26fd8..db67c8e 100644 --- a/Library/Constant/StateEnum.cs +++ b/Library/Constant/StateEnum.cs @@ -89,9 +89,17 @@ namespace Milimoe.FunGame.Core.Library.Constant public enum QuestState { - NotStarted = 0, - InProgress = 1, - Completed = 2, - Settled = 3 + NotStarted, + InProgress, + Completed, + Settled + } + + public enum ActivityState + { + Future, + Upcoming, + InProgress, + Ended } } diff --git a/Service/JsonManager.cs b/Service/JsonManager.cs index 6904cb4..1d9e161 100644 --- a/Service/JsonManager.cs +++ b/Service/JsonManager.cs @@ -21,7 +21,7 @@ namespace Milimoe.FunGame.Core.Service ReferenceHandler = ReferenceHandler.IgnoreCycles, Converters = { new DateTimeConverter(), new DataTableConverter(), new DataSetConverter(), new UserConverter(), new RoomConverter(), new CharacterConverter(), new MagicResistanceConverter(), new EquipSlotConverter(), new SkillConverter(), new EffectConverter(), new ItemConverter(), - new InventoryConverter(), new NormalAttackConverter(), new ClubConverter() + new InventoryConverter(), new NormalAttackConverter(), new ClubConverter(), new GoodsConverter(), new StoreConverter() } };