items db + default items

This commit is contained in:
rfi
2023-11-15 08:01:16 +07:00
parent e850a18d54
commit 03128fcb84
7 changed files with 109 additions and 5 deletions

View File

@@ -0,0 +1,69 @@
using AscNet.Common.MsgPack;
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
{
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;
}
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; }
}
}

View File

@@ -212,7 +212,7 @@ namespace AscNet.Common.MsgPack
}
[MessagePackObject(true)]
public partial class ItemList
public partial class Item
{
public int Id { get; set; }
public long Count { get; set; }
@@ -319,7 +319,7 @@ namespace AscNet.Common.MsgPack
public PlayerData PlayerData { get; set; }
public List<TimeLimitCtrlConfigList> TimeLimitCtrlConfigList { get; set; } = new();
public List<SharePlatformConfigList> SharePlatformConfigList { get; set; } = new();
public List<ItemList> ItemList { get; set; } = new();
public List<Item> ItemList { get; set; } = new();
public Dictionary<int, List<ItemRecycleData>> ItemRecycleDict { get; set; } = new();
public List<LoginCharacterList> CharacterList { get; set; } = new();
public List<EquipList> EquipList { get; set; } = new();

View File

@@ -2,7 +2,7 @@
{
public static class Miscs
{
public static int ParseIntOr(string? s, int d)
public static int ParseIntOr(string? s, int d = 0)
{
if (int.TryParse(s, out var parsed))
{