ascnet/AscNet.Common/Database/Inventory.cs

138 lines
4.5 KiB
C#
Raw Normal View History

2023-11-15 01:01:16 +00:00
using AscNet.Common.MsgPack;
2023-12-24 11:29:17 +00:00
using AscNet.Common.Util;
using AscNet.Table.V2.share.item;
2023-11-15 01:01:16 +00:00
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Newtonsoft.Json;
namespace AscNet.Common.Database
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class Inventory
{
#region CommonItems
public const int Coin = 1;
public const int PaidGem = 2;
public const int FreeGem = 3;
public const int ActionPoint = 4;
public const int HongKa = 5;
public const int TeamExp = 7;
public const int AndroidHongKa = 8;
public const int IosHongKa = 10;
public const int SkillPoint = 12;
public const int DailyActiveness = 13;
public const int WeeklyActiveness = 14;
public const int HostelElectric = 15;
public const int HostelMat = 16;
public const int OnlineBossTicket = 17;
public const int BountyTaskExp = 18;
public const int DormCoin = 30;
public const int FurnitureCoin = 31;
public const int DormEnterIcon = 36;
public const int BaseEquipCoin = 300;
public const int InfestorActionPoint = 50;
public const int InfestorMoney = 51;
public const int PokemonLevelUpItem = 56;
public const int PokemonStarUpItem = 57;
public const int PokemonLowStarUpItem = 58;
public const int PassportExp = 60;
#endregion
2023-11-15 01:01:16 +00:00
public static readonly IMongoCollection<Inventory> collection = Common.db.GetCollection<Inventory>("inventory");
public static Inventory FromUid(long uid)
{
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid) ?? Create(uid);
}
private static Inventory Create(long uid)
{
Inventory inventory = new()
{
Uid = uid,
Items = new()
};
List<ItemConfig>? defaultItems = JsonConvert.DeserializeObject<List<ItemConfig>>(File.ReadAllText("./Configs/default_items.json"));
if (defaultItems is not null)
{
inventory.Items.AddRange(defaultItems.Select(item => new Item()
{
Id = item.Id,
Count = item.Count,
RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds()
}));
}
collection.InsertOne(inventory);
return inventory;
}
2023-11-20 15:19:46 +00:00
public Item Do(int itemId, int amount)
{
Item? item = Items.FirstOrDefault(x => x.Id == itemId);
2023-12-24 11:29:17 +00:00
ItemTable? itemTable = TableReaderV2.Parse<ItemTable>().Find(x => x.Id == itemId);
if (item is not null && itemTable is not null)
{
2024-01-17 15:54:02 +00:00
if (item.Count + amount <= itemTable.MaxCount && item.Count + amount >= 0)
{
item.Count += amount;
item.RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds();
}
2024-01-17 15:54:02 +00:00
else if (item.Count + amount < 0)
{
2024-01-17 15:54:02 +00:00
item.Count = 0;
item.RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds();
}
else
{
2024-01-17 15:54:02 +00:00
item.Count = itemTable.MaxCount ?? item.Count + amount;
item.RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds();
}
}
else
{
2023-11-20 15:19:46 +00:00
item = new Item()
{
Id = itemId,
Count = amount,
RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds()
2023-11-20 15:19:46 +00:00
};
Items.Add(item);
}
2023-11-20 15:19:46 +00:00
return item;
}
2023-11-15 01:01:16 +00:00
public void Save()
{
collection.ReplaceOne(Builders<Inventory>.Filter.Eq(x => x.Id, Id), this);
}
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("uid")]
[BsonRequired]
public long Uid { get; set; }
[BsonElement("items")]
[BsonRequired]
public List<Item> Items { get; set; }
}
public partial class ItemConfig
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("Count")]
public long Count { get; set; }
}
}