Files
2026-10-16 23:33:13 -07:00

106 lines
3.4 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using RadicadeServer.Extensions;
using RadicadeServer.RecNet.Progression.Challenges;
using RadicadeServer.RecNet.Progression.Objectives;
using RadicadeServer.RecNet.Storefront;
namespace RadicadeServer.RecNet.Progression
{
public class ProgressionJson
{
public MyProgress ObjectiveProgress { get; set; } = new();
public List<ChallengeProgress> ChallengeProgress { get; set; } = new();
public DateTime? LastXpEarnedAt { get; set; }
public int XpEarnedToday { get; set; }
public DateTime? XpEarnedTodayDate { get; set; }
public DateTime? XpDailyCapReachedAt { get; set; }
public long? RoomXpRoomId { get; set; }
public int RoomXpEarned { get; set; }
public DateTime? RoomXpLastGrantAt { get; set; }
public List<int> DeliveredLevelUpGiftLevels { get; set; } = new();
public List<GameReward> PendingGameRewards { get; set; } = new();
public RadicadeServer.Utils.RewardDayLedger? RewardDay { get; set; }
}
public class ProgressionData
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public long PlayerId { get; set; }
public int XP { get; set; } = 0;
public int Level { get; set; } = 1;
public string Progress { get; set; } = new ProgressionJson().ToJson();
public ChallengeProgress? GetChallengeProgress()
{
return Progress.FromJson<ProgressionJson>()?.ChallengeProgress.LastOrDefault();
}
public void SetChallengeProgress(ChallengeProgress progress)
{
var deserialized = Progress.FromJson<ProgressionJson>();
if (deserialized == null) return;
deserialized.ChallengeProgress.Add(progress);
Progress = deserialized.ToJson();
}
public List<ChallengeProgress>? GetChallengeProgresses()
{
return Progress.FromJson<ProgressionJson>()?.ChallengeProgress;
}
public MyProgress? GetObjectiveProgress()
{
return Progress.FromJson<ProgressionJson>()?.ObjectiveProgress;
}
public void AddObjectivesProgress(ObjectiveProgress progress)
{
var deserialized = Progress.FromJson<ProgressionJson>();
if (deserialized == null) return;
var existingProgress = deserialized.ObjectiveProgress.Objectives.Find(x => x.Group == progress.Group && x.Index == progress.Index);
if (existingProgress == null)
deserialized.ObjectiveProgress.Objectives.Add(progress);
else
existingProgress = progress;
Progress = deserialized.ToJson();
}
public ProgressionJson? GetProgressionJson()
{
return Progress.FromJson<ProgressionJson>();
}
public void RecordXpGrant(int amount)
{
if (amount <= 0) return;
var json = GetProgressionJson() ?? new ProgressionJson();
var today = DateTime.UtcNow.Date;
if (json.XpEarnedTodayDate != today)
{
json.XpEarnedTodayDate = today;
json.XpEarnedToday = 0;
}
json.XpEarnedToday += amount;
json.LastXpEarnedAt = DateTime.UtcNow;
Progress = json.ToJson();
}
}
}