Files
2026-02-27 19:30:49 -08:00

62 lines
1.7 KiB
C#

using RecRoomArchive.Models.API.Players;
using System.Security.Cryptography;
using System.Text.Json;
namespace RecRoomArchive.Services
{
public class AccountService
{
public static ulong? AccountId { get; private set; }
public bool AccountExists()
{
return File.Exists("data/profile.json");
}
public bool CreateAccount(string? username = null)
{
if (string.IsNullOrEmpty(username))
{
username = GetRandomUsername();
}
Console.WriteLine($"Creating account for {username}");
var profile = new BaseProfile
{
Id = (ulong)RandomNumberGenerator.GetInt32(1000, 9999999),
Username = username,
DisplayName = username
};
File.WriteAllText("data/profile.json", JsonSerializer.Serialize(profile));
return File.Exists("data/profile.json");
}
public BaseProfile? GetSelfAccount()
{
return JsonSerializer.Deserialize<BaseProfile>(File.ReadAllText("data/profile.json"));
}
public ulong? GetSelfAccountId()
{
if (AccountId.HasValue)
return AccountId;
var profile = JsonSerializer.Deserialize<BaseProfile>(File.ReadAllText("data/profile.json"));
if (profile == null)
return null;
AccountId = profile.Id;
return AccountId;
}
private static string GetRandomUsername()
{
int randomFourDigits = RandomNumberGenerator.GetInt32(1000, 9999);
return $"RRA-User_{randomFourDigits}";
}
}
}