Cache user social credits on profile

This commit is contained in:
2025-08-22 22:28:48 +08:00
parent d1c3610ec8
commit 8f3825e92c
4 changed files with 30 additions and 2 deletions

View File

@@ -135,9 +135,20 @@ public class AccountProfile : ModelBase, IIdentifiedResource
[Column(TypeName = "jsonb")] public VerificationMark? Verification { get; set; } [Column(TypeName = "jsonb")] public VerificationMark? Verification { get; set; }
[Column(TypeName = "jsonb")] public BadgeReferenceObject? ActiveBadge { get; set; } [Column(TypeName = "jsonb")] public BadgeReferenceObject? ActiveBadge { get; set; }
public int Experience { get; set; } = 0; public int Experience { get; set; }
[NotMapped] public int Level => Leveling.ExperiencePerLevel.Count(xp => Experience >= xp) - 1; [NotMapped] public int Level => Leveling.ExperiencePerLevel.Count(xp => Experience >= xp) - 1;
public double SocialCredits { get; set; }
[NotMapped]
public int SocialCreditsLevel => SocialCredits switch
{
< 100 => -1,
> 100 and < 200 => 0,
< 200 => 1,
_ => 2
};
[NotMapped] [NotMapped]
public double LevelingProgress => Level >= Leveling.ExperiencePerLevel.Count - 1 public double LevelingProgress => Level >= Leveling.ExperiencePerLevel.Count - 1
? 100 ? 100
@@ -168,6 +179,8 @@ public class AccountProfile : ModelBase, IIdentifiedResource
Experience = Experience, Experience = Experience,
Level = Level, Level = Level,
LevelingProgress = LevelingProgress, LevelingProgress = LevelingProgress,
SocialCredits = SocialCredits,
SocialCreditsLevel = SocialCreditsLevel,
Picture = Picture?.ToProtoValue(), Picture = Picture?.ToProtoValue(),
Background = Background?.ToProtoValue(), Background = Background?.ToProtoValue(),
AccountId = AccountId.ToString(), AccountId = AccountId.ToString(),
@@ -198,6 +211,7 @@ public class AccountProfile : ModelBase, IIdentifiedResource
Verification = proto.Verification is null ? null : VerificationMark.FromProtoValue(proto.Verification), Verification = proto.Verification is null ? null : VerificationMark.FromProtoValue(proto.Verification),
ActiveBadge = proto.ActiveBadge is null ? null : BadgeReferenceObject.FromProtoValue(proto.ActiveBadge), ActiveBadge = proto.ActiveBadge is null ? null : BadgeReferenceObject.FromProtoValue(proto.ActiveBadge),
Experience = proto.Experience, Experience = proto.Experience,
SocialCredits = proto.SocialCredits,
Picture = proto.Picture is null ? null : CloudFileReferenceObject.FromProtoValue(proto.Picture), Picture = proto.Picture is null ? null : CloudFileReferenceObject.FromProtoValue(proto.Picture),
Background = proto.Background is null ? null : CloudFileReferenceObject.FromProtoValue(proto.Background), Background = proto.Background is null ? null : CloudFileReferenceObject.FromProtoValue(proto.Background),
AccountId = Guid.Parse(proto.AccountId), AccountId = Guid.Parse(proto.AccountId),

View File

@@ -18,6 +18,13 @@ public class SocialCreditService(AppDatabase db, ICacheService cache)
}; };
db.SocialCreditRecords.Add(record); db.SocialCreditRecords.Add(record);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
await db.AccountProfiles
.Where(p => p.AccountId == accountId)
.ExecuteUpdateAsync(p => p.SetProperty(v => v.SocialCredits, v => v.SocialCredits + record.Delta));
await cache.RemoveAsync($"{CacheKeyPrefix}{accountId}");
return record; return record;
} }
@@ -32,6 +39,7 @@ public class SocialCreditService(AppDatabase db, ICacheService cache)
.Where(x => x.AccountId == accountId) .Where(x => x.AccountId == accountId)
.SumAsync(x => x.Delta); .SumAsync(x => x.Delta);
records += BaseSocialCredit; records += BaseSocialCredit;
await cache.SetAsync($"{CacheKeyPrefix}{accountId}", records); await cache.SetAsync($"{CacheKeyPrefix}{accountId}", records);
return records; return records;
} }

View File

@@ -462,6 +462,10 @@ namespace DysonNetwork.Pass.Migrations
.HasColumnType("character varying(1024)") .HasColumnType("character varying(1024)")
.HasColumnName("pronouns"); .HasColumnName("pronouns");
b.Property<double>("SocialCredits")
.HasColumnType("double precision")
.HasColumnName("social_credits");
b.Property<string>("TimeZone") b.Property<string>("TimeZone")
.HasMaxLength(1024) .HasMaxLength(1024)
.HasColumnType("character varying(1024)") .HasColumnType("character varying(1024)")

View File

@@ -75,6 +75,8 @@ message AccountProfile {
int32 experience = 14; int32 experience = 14;
int32 level = 15; int32 level = 15;
double leveling_progress = 16; double leveling_progress = 16;
double social_credits = 17;
int32 social_credits_level = 18;
CloudFile picture = 19; CloudFile picture = 19;
CloudFile background = 20; CloudFile background = 20;