ascnet/AscNet.Common/Database/Character.cs

315 lines
12 KiB
C#
Raw Normal View History

2023-10-18 14:45:13 +00:00
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
2023-12-10 02:22:33 +00:00
using AscNet.Table.V2.share.character;
using AscNet.Table.V2.share.character.skill;
2023-10-18 14:45:13 +00:00
using AscNet.Common.MsgPack;
using AscNet.Common.Util;
2023-11-26 14:08:59 +00:00
using Newtonsoft.Json;
2023-11-29 14:01:00 +00:00
using AscNet.Table.V2.share.equip;
using AscNet.Table.V2.share.character.quality;
2023-10-18 14:45:13 +00:00
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 Character
{
2023-11-29 14:01:00 +00:00
public static readonly List<CharacterLevelUpTemplate> characterLevelUpTemplates;
public static readonly List<EquipLevelUpTemplate> equipLevelUpTemplates;
2023-10-18 14:45:13 +00:00
public static readonly IMongoCollection<Character> collection = Common.db.GetCollection<Character>("characters");
2023-11-26 14:08:59 +00:00
static Character()
{
if (File.Exists("Data/CharacterLevelUpTemplate.json"))
2023-11-29 14:01:00 +00:00
characterLevelUpTemplates = JsonConvert.DeserializeObject<List<CharacterLevelUpTemplate>>(File.ReadAllText("Data/CharacterLevelUpTemplate.json")) ?? new();
2023-11-26 14:08:59 +00:00
else
2023-11-29 14:01:00 +00:00
characterLevelUpTemplates = new();
if (File.Exists("Data/EquipLevelUpTemplate.json"))
equipLevelUpTemplates = JsonConvert.DeserializeObject<List<EquipLevelUpTemplate>>(File.ReadAllText("Data/EquipLevelUpTemplate.json")) ?? new();
else
equipLevelUpTemplates = new();
2023-11-26 14:08:59 +00:00
}
2023-10-19 07:07:48 +00:00
private uint NextEquipId => Equips.MaxBy(x => x.Id)?.Id + 1 ?? 1;
2023-10-18 14:45:13 +00:00
public static Character FromUid(long uid)
{
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid) ?? Create(uid);
}
private static Character Create(long uid)
{
Character character = new()
{
Uid = uid,
2023-10-19 07:07:48 +00:00
Characters = new(),
Equips = new(),
Fashions = new()
2023-10-18 14:45:13 +00:00
};
2023-10-18 15:33:14 +00:00
// Lucia havers by default
2023-10-18 14:45:13 +00:00
character.AddCharacter(1021001);
collection.InsertOne(character);
return character;
}
/// <summary>
/// Don't forget to send Equip, Fashion, and the Character notify after using this!
/// </summary>
/// <param name="id"></param>
/// <exception cref="ServerCodeException"></exception>
public AddCharacterRet AddCharacter(uint id)
2023-10-18 14:45:13 +00:00
{
AddCharacterRet ret = new();
2023-12-10 02:22:33 +00:00
CharacterTable? character = TableReaderV2.Parse<CharacterTable>().Find(x => x.Id == id);
CharacterSkillTable? characterSkill = TableReaderV2.Parse<CharacterSkillTable>().Find(x => x.CharacterId == id);
CharacterQualityTable? characterQuality = TableReaderV2.Parse<CharacterQualityTable>().OrderBy(x => x.Quality).FirstOrDefault(x => x.CharacterId == id);
2023-10-19 07:07:48 +00:00
if (character is null || characterSkill is null || characterQuality is null)
{
// CharacterManagerGetCharacterDataNotFound
throw new ServerCodeException("Invalid character id!", 20009021);
}
2023-10-19 07:07:48 +00:00
if (Characters.FirstOrDefault(x => x.Id == character.Id) is not null)
{
// CharacterManagerCreateCharacterAlreadyExist
throw new ServerCodeException("Character already obtained!", 20009022);
}
2023-10-19 07:07:48 +00:00
2024-01-25 15:23:47 +00:00
CharacterData characterData = new()
2023-10-18 14:45:13 +00:00
{
Id = (uint)character.Id,
Level = 1,
Exp = 0,
Quality = characterQuality.Quality,
InitQuality = characterQuality.Quality,
2023-10-18 14:45:13 +00:00
Star = 0,
Grade = 1,
FashionId = (uint)character.DefaultNpcFashtionId,
CreateTime = DateTimeOffset.Now.ToUnixTimeSeconds(),
TrustLv = 1,
TrustExp = 0,
Ability = 0,
LiberateLv = 1,
CharacterHeadInfo = new()
{
HeadFashionId = (uint)character.DefaultNpcFashtionId,
HeadFashionType = 0
}
};
2023-12-10 02:22:33 +00:00
// TODO: Don't do the ToString, query skill properly pls.
2024-01-25 15:23:47 +00:00
characterData.SkillList.AddRange(characterSkill.SkillGroupId.Take(8).Select(x => new CharacterSkill()
2023-10-18 14:45:13 +00:00
{
Id = uint.Parse(x.ToString().Take(6).ToArray()),
Level = 1
}));
2023-12-10 02:22:33 +00:00
FashionList fashion = new()
2023-10-19 07:07:48 +00:00
{
Id = character.DefaultNpcFashtionId,
IsLock = false
};
Fashions.Add(fashion);
ret.Fashion = fashion;
2023-10-19 07:07:48 +00:00
if (character.EquipId > 0)
ret.Equip = AddEquip((uint)character.EquipId, character.Id);
2023-10-18 14:45:13 +00:00
Characters.Add(characterData);
ret.Character = characterData;
return ret;
2023-10-18 14:45:13 +00:00
}
2024-01-25 15:23:47 +00:00
public CharacterData? AddCharacterExp(int characterId, int exp, int maxLvl = 0)
2023-11-26 14:08:59 +00:00
{
2023-12-10 02:22:33 +00:00
var characterData = TableReaderV2.Parse<CharacterTable>().FirstOrDefault(x => x.Id == characterId);
2023-11-26 14:08:59 +00:00
var character = Characters.FirstOrDefault(x => x.Id == characterId);
if (character is not null && characterData is not null)
{
levelCheck:
2023-11-29 14:01:00 +00:00
CharacterLevelUpTemplate? levelUpTemplate = characterLevelUpTemplates.FirstOrDefault(x => x.Level == character.Level && x.Type == characterData.Type);
2023-11-26 14:08:59 +00:00
if (levelUpTemplate is not null)
{
if (levelUpTemplate.Exp > exp)
{
character.Exp += (uint)Math.Max(0, exp);
}
else if (maxLvl > 0 && character.Level == maxLvl)
{
character.Exp = (uint)Math.Max(0, levelUpTemplate.Exp);
}
else
{
character.Level++;
exp -= (int)(levelUpTemplate.Exp - character.Exp);
character.Exp = 0;
goto levelCheck;
}
}
}
return character;
}
2024-01-25 15:23:47 +00:00
public UpgradeCharacterSkillResult UpgradeCharacterSkillGroup(int skillGroupId, int count)
{
List<uint> affectedCharacters = new();
int totalCoinCost = 0;
int totalSkillPointCost = 0;
2023-12-10 02:22:33 +00:00
IEnumerable<int> affectedSkills = TableReaderV2.Parse<CharacterSkillGroupTable>().Where(x => x.Id == skillGroupId).SelectMany(x => x.SkillId);
foreach (var skillId in affectedSkills)
{
foreach (var character in Characters.Where(x => x.SkillList.Any(x => x.Id == skillId)))
{
var characterSkill = character.SkillList.First(x => x.Id == skillId);
int targetLevel = characterSkill.Level + count;
while (characterSkill.Level < targetLevel)
{
2023-12-10 02:22:33 +00:00
var skillUpgrade = TableReaderV2.Parse<CharacterSkillUpgradeTable>().Find(x => x.SkillId == skillId && x.Level == characterSkill.Level);
2023-12-10 02:22:33 +00:00
totalCoinCost += skillUpgrade?.UseCoin ?? 0;
totalSkillPointCost += skillUpgrade?.UseSkillPoint ?? 0;
characterSkill.Level++;
}
affectedCharacters.Add(character.Id);
}
}
2023-11-26 14:08:59 +00:00
return new UpgradeCharacterSkillResult()
{
AffectedCharacters = affectedCharacters,
CoinCost = totalCoinCost,
SkillPointCost = totalSkillPointCost
};
}
2023-12-10 02:01:51 +00:00
public EquipData AddEquip(uint equipId, int characterId = 0)
2023-10-19 07:07:48 +00:00
{
2023-12-10 02:01:51 +00:00
EquipData equipData = new()
2023-10-19 07:07:48 +00:00
{
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);
return equipData;
2023-10-19 07:07:48 +00:00
}
2023-12-10 02:01:51 +00:00
public EquipData? AddEquipExp(int equipId, int exp)
2023-11-29 14:01:00 +00:00
{
var equip = Equips.FirstOrDefault(x => x.Id == equipId);
EquipTable? equipData = TableReaderV2.Parse<EquipTable>().FirstOrDefault(x => x.Id == equip?.TemplateId);
EquipBreakThroughTable? equipBreakThroughTable = TableReaderV2.Parse<EquipBreakThroughTable>().FirstOrDefault(x => x.EquipId == equip?.TemplateId && x.Times == equip?.Breakthrough);
if (equip is not null && equipData is not null && equipBreakThroughTable is not null)
{
EquipLevelUpTemplate? levelUpTemplate = equipLevelUpTemplates.FirstOrDefault(x => x.TemplateId == equipBreakThroughTable.LevelUpTemplateId && x.Level == equip.Level);
if (levelUpTemplate is not null)
{
if (exp + equip.Exp < levelUpTemplate.Exp)
{
equip.Exp += Math.Max(0, exp);
}
else if (equip.Level < equipBreakThroughTable.LevelLimit)
{
equip.Level++;
exp -= levelUpTemplate.Exp - equip.Exp;
equip.Exp = 0;
return AddEquipExp(equipId, exp);
}
else
{
equip.Exp = levelUpTemplate.Exp;
}
}
}
return equip;
}
public void Save()
{
collection.ReplaceOne(Builders<Character>.Filter.Eq(x => x.Id, Id), this);
}
2023-10-18 14:45:13 +00:00
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("uid")]
[BsonRequired]
public long Uid { get; set; }
2023-10-19 07:07:48 +00:00
[BsonElement("characters")]
2023-10-18 14:45:13 +00:00
[BsonRequired]
2024-01-25 15:23:47 +00:00
public List<CharacterData> Characters { get; set; }
2023-10-19 07:07:48 +00:00
[BsonElement("equips")]
[BsonRequired]
2023-12-10 02:01:51 +00:00
public List<EquipData> Equips { get; set; }
2023-10-19 07:07:48 +00:00
[BsonElement("fashions")]
[BsonRequired]
public List<FashionList> Fashions { get; set; }
2023-10-18 14:45:13 +00:00
}
2023-11-26 14:08:59 +00:00
public struct UpgradeCharacterSkillResult
{
public int CoinCost { get; init; }
public int SkillPointCost { get; init; }
public List<uint> AffectedCharacters { get; init; }
}
2023-11-26 14:08:59 +00:00
public partial class CharacterLevelUpTemplate
{
[JsonProperty("Level")]
public int Level { get; set; }
[JsonProperty("Exp")]
public int Exp { get; set; }
[JsonProperty("AllExp")]
public int AllExp { get; set; }
[JsonProperty("Type")]
public int Type { get; set; }
}
2023-11-29 14:01:00 +00:00
public partial class EquipLevelUpTemplate
{
[JsonProperty("Level")]
public int Level { get; set; }
[JsonProperty("Exp")]
public int Exp { get; set; }
[JsonProperty("AllExp")]
public int AllExp { get; set; }
[JsonProperty("TemplateId")]
public int TemplateId { get; set; }
}
public struct AddCharacterRet
{
2024-01-25 15:23:47 +00:00
public CharacterData Character { get; set; }
2023-12-10 02:01:51 +00:00
public EquipData Equip { get; set; }
public FashionList Fashion { get; set; }
}
2023-10-18 14:45:13 +00:00
}