DB for starting tutorial

This commit is contained in:
rfi
2023-10-19 14:07:48 +07:00
parent 1ddd574645
commit 8d808170a5
10 changed files with 239 additions and 10 deletions

View File

@@ -12,6 +12,8 @@ namespace AscNet.Common.Database
{
public static readonly IMongoCollection<Character> collection = Common.db.GetCollection<Character>("characters");
private uint NextEquipId => Equips.MaxBy(x => x.Id)?.Id + 1 ?? 1;
public static Character FromUid(long uid)
{
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid) ?? Create(uid);
@@ -22,7 +24,9 @@ namespace AscNet.Common.Database
Character character = new()
{
Uid = uid,
Characters = new()
Characters = new(),
Equips = new(),
Fashions = new()
};
// Lucia havers by default
character.AddCharacter(1021001);
@@ -36,8 +40,12 @@ namespace AscNet.Common.Database
{
CharacterTable? character = CharacterTableReader.Instance.FromId((int)id);
CharacterSkillTable? characterSkill = CharacterSkillTableReader.Instance.FromCharacterId((int)id);
if (character is null || characterSkill is null)
throw new ArgumentException("Invlid character id!", nameof(id));
throw new ArgumentException("Invalid character id!", nameof(id));
if (Characters.FirstOrDefault(x => x.Id == character.Id) is not null)
throw new ArgumentException("Character already obtained!", nameof(id));
NotifyCharacterDataList.NotifyCharacterDataListCharacterData characterData = new()
{
Id = (uint)character.Id,
@@ -64,10 +72,38 @@ namespace AscNet.Common.Database
Id = uint.Parse(x.ToString().Take(6).ToArray()),
Level = 1
}));
Fashions.Add(new()
{
Id = character.DefaultNpcFashtionId,
IsLock = false
});
if (character.EquipId > 0)
AddEquip((uint)character.EquipId, character.Id);
Characters.Add(characterData);
}
public void AddEquip(uint equipId, int characterId = 0)
{
NotifyEquipDataList.NotifyEquipDataListEquipData equipData = new()
{
Id = NextEquipId,
TemplateId = equipId,
CharacterId = characterId,
Level = 1,
Exp = 0,
Breakthrough = 0,
ResonanceInfo = new(),
UnconfirmedResonanceInfo = new(),
AwakeSlotList = new(),
IsLock = false,
CreateTime = (uint)DateTimeOffset.Now.ToUnixTimeSeconds(),
IsRecycle = false
};
Equips.Add(equipData);
}
[BsonId]
public ObjectId Id { get; set; }
@@ -75,8 +111,16 @@ namespace AscNet.Common.Database
[BsonRequired]
public long Uid { get; set; }
[BsonElement("uid")]
[BsonElement("characters")]
[BsonRequired]
public List<NotifyCharacterDataList.NotifyCharacterDataListCharacterData> Characters { get; set; }
[BsonElement("equips")]
[BsonRequired]
public List<NotifyEquipDataList.NotifyEquipDataListEquipData> Equips { get; set; }
[BsonElement("fashions")]
[BsonRequired]
public List<FashionList> Fashions { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
namespace AscNet.Common.Database
{
public static class DatabaseExtensions
{
public static BsonClassMap<T> SetDictionaryRepresentation<T, TMember>(this BsonClassMap<T> classMap, Expression<Func<T, TMember>> memberLambda, DictionaryRepresentation representation)
{
var memberMap = classMap.GetMemberMap(memberLambda);
var serializer = memberMap.GetSerializer();
if (serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationSerializer)
serializer = dictionaryRepresentationSerializer.WithDictionaryRepresentation(representation);
memberMap.SetSerializer(serializer);
return classMap;
}
}
}

View File

@@ -2,6 +2,8 @@
using MongoDB.Bson;
using MongoDB.Driver;
using AscNet.Common.MsgPack;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
namespace AscNet.Common.Database
{
@@ -10,7 +12,7 @@ namespace AscNet.Common.Database
{
public static readonly IMongoCollection<Player> collection = Common.db.GetCollection<Player>("players");
public static Player FromId(long id)
public static Player FromPlayerId(long id)
{
return collection.AsQueryable().FirstOrDefault(x => x.PlayerData.Id == id) ?? Create(id);
}
@@ -48,13 +50,45 @@ namespace AscNet.Common.Database
CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
LastLoginTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
Flags = 1
},
HeadPortraits = new(),
TeamGroups = new()
{
{1, new TeamGroupDatum()
{
TeamType = 1,
TeamId = 1,
CaptainPos = 1,
FirstFightPos = 1,
TeamData = new()
{
{1, 1021001},
{2, 0},
{3, 0}
},
TeamName = null
}}
}
};
player.AddHead(9000001);
player.AddHead(9000002);
player.AddHead(9000003);
collection.InsertOne(player);
return player;
}
public void AddHead(int id)
{
HeadPortraits.Add(new()
{
Id = id,
LeftCount = 1,
BeginTime = DateTimeOffset.Now.ToUnixTimeSeconds()
});
}
[BsonId]
public ObjectId Id { get; set; }
@@ -65,5 +99,14 @@ namespace AscNet.Common.Database
[BsonElement("player_data")]
[BsonRequired]
public PlayerData PlayerData { get; set; }
[BsonElement("head_portraits")]
[BsonRequired]
public List<HeadPortraitList> HeadPortraits { get; set; }
[BsonElement("team_groups")]
[BsonRequired]
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<int, TeamGroupDatum> TeamGroups { get; set; }
}
}

View File

@@ -0,0 +1,71 @@
using AscNet.Common.MsgPack;
using AscNet.Table.share.guide;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Driver;
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 Stage
{
public static readonly IMongoCollection<Stage> collection = Common.db.GetCollection<Stage>("stages");
public static Stage FromUid(long uid)
{
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid) ?? Create(uid);
}
private static Stage Create(long uid)
{
Stage stage = new()
{
Stages = new()
};
foreach (var guideFight in GuideFightTableReader.Instance.All)
{
stage.AddStage(new StageDatum()
{
StageId = guideFight.StageId,
StarsMark = 7,
Passed = true,
PassTimesToday = 0,
PassTimesTotal = 1,
BuyCount = 0,
Score = 0,
LastPassTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
RefreshTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
BestRecordTime = guideFight.DefaultRecordTime,
LastRecordTime = guideFight.DefaultRecordTime,
BestCardIds = new List<long>() { 1021001 },
LastCardIds = new List<long>() { 1021001 }
});
}
collection.InsertOne(stage);
return stage;
}
public void AddStage(StageDatum stageData)
{
Stages.Add(stageData.StageId, stageData);
}
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("uid")]
[BsonRequired]
public long Uid { get; set; }
[BsonElement("stages")]
[BsonRequired]
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<long, StageDatum> Stages { get; set; }
}
}