Files
Campus-25/server/Server/2025-RecRoom-Server-E12354-main/api/dbs/SitePrefsDB.cs
T
2026-10-14 06:59:51 +02:00

60 lines
2.5 KiB
C#

using LiteDB;
namespace Campus_live_rewrite.api.dbs
{
/// <summary>
/// Website-only per-account preferences: the nickname a player gives a friend and which
/// friends are pinned to their home page. Neither exists in the game, so they live here
/// (Data/DBs/SitePrefs.db) instead of on the relationship row, keyed by the OWNER's account.
/// </summary>
public static class SitePrefsDB
{
private static readonly Lazy<LiteDatabase> _db = new(() =>
new LiteDatabase(Path.Combine(Environment.CurrentDirectory, "Data", "DBs", "SitePrefs.db")));
public class Prefs
{
[BsonId] public ulong AccountId { get; set; }
public Dictionary<string, string> Nicknames { get; set; } = new(); // other account id -> nickname
public List<ulong> Pinned { get; set; } = new();
}
private static ILiteCollection<Prefs> Col() => _db.Value.GetCollection<Prefs>("prefs");
public static Prefs Get(ulong accountId) => Col().FindById(accountId) ?? new Prefs { AccountId = accountId };
public static string? NicknameFor(ulong owner, ulong other) =>
Get(owner).Nicknames.TryGetValue(other.ToString(), out var n) && !string.IsNullOrWhiteSpace(n) ? n : null;
public static bool IsPinned(ulong owner, ulong other) => Get(owner).Pinned.Contains(other);
public static string? SetNickname(ulong owner, ulong other, string? nickname)
{
var p = Get(owner);
nickname = (nickname ?? "").Trim();
if (nickname.Length > 24) nickname = nickname[..24];
if (nickname.Length == 0) p.Nicknames.Remove(other.ToString()); else p.Nicknames[other.ToString()] = nickname;
Col().Upsert(p);
return nickname.Length == 0 ? null : nickname;
}
public static bool SetPinned(ulong owner, ulong other, bool on)
{
var p = Get(owner);
p.Pinned.Remove(other);
if (on) { p.Pinned.Insert(0, other); if (p.Pinned.Count > 12) p.Pinned = p.Pinned.Take(12).ToList(); }
Col().Upsert(p);
return on;
}
/// <summary>Drop a friend's nickname and pin when the friendship ends.</summary>
public static void Forget(ulong owner, ulong other)
{
var p = Col().FindById(owner);
if (p == null) return;
bool changed = p.Nicknames.Remove(other.ToString()) | p.Pinned.Remove(other);
if (changed) Col().Update(p);
}
}
}