mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-21 03:29:36 +08:00
添加商店和商品列表
This commit is contained in:
parent
f0b3d0a549
commit
7dea8032c2
135
Entity/System/Store.cs
Normal file
135
Entity/System/Store.cs
Normal file
@ -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<long, Goods> 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<Item> 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<Item> Items { get; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public Dictionary<string, double> Prices { get; } = [];
|
||||
public int Stock { get; set; }
|
||||
|
||||
public Goods(long id, Item item, int stock, string name, string description, Dictionary<string, double>? prices = null)
|
||||
{
|
||||
Id = id;
|
||||
Items = [item];
|
||||
Stock = stock;
|
||||
Name = name;
|
||||
Description = description;
|
||||
if (prices != null) Prices = prices;
|
||||
}
|
||||
|
||||
public Goods(long id, List<Item> items, int stock, string name, string description, Dictionary<string, double>? 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;
|
||||
}
|
||||
}
|
||||
}
|
13
Library/Common/Event/UserActivityEventArgs.cs
Normal file
13
Library/Common/Event/UserActivityEventArgs.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
64
Library/Common/JsonConverter/GoodsConverter.cs
Normal file
64
Library/Common/JsonConverter/GoodsConverter.cs
Normal file
@ -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<Goods>
|
||||
{
|
||||
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<string, object> convertingContext)
|
||||
{
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(Goods.Id):
|
||||
result.Id = reader.GetInt64();
|
||||
break;
|
||||
case nameof(Goods.Items):
|
||||
List<Item> items = NetworkUtility.JsonDeserialize<List<Item>>(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<string, double> prices = NetworkUtility.JsonDeserialize<Dictionary<string, double>>(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();
|
||||
}
|
||||
}
|
||||
}
|
43
Library/Common/JsonConverter/StoreConverter.cs
Normal file
43
Library/Common/JsonConverter/StoreConverter.cs
Normal file
@ -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<Store>
|
||||
{
|
||||
public override Store NewInstance()
|
||||
{
|
||||
return new Store("商店");
|
||||
}
|
||||
|
||||
public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Store result, Dictionary<string, object> convertingContext)
|
||||
{
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(Store.Name):
|
||||
result.Name = reader.GetString() ?? "";
|
||||
break;
|
||||
case nameof(Store.Goods):
|
||||
Dictionary<long, Goods> goods = NetworkUtility.JsonDeserialize<Dictionary<long, Goods>>(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();
|
||||
}
|
||||
}
|
||||
}
|
@ -64,4 +64,11 @@ namespace Milimoe.FunGame.Core.Library.Constant
|
||||
Critical,
|
||||
Evaded
|
||||
}
|
||||
|
||||
public enum RedeemResult
|
||||
{
|
||||
Success,
|
||||
StockNotEnough,
|
||||
PointsNotEnough
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user