using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DorkNet.Server.Data;
namespace DorkNet.Server.Controllers.API.Role;
///
/// api.rec.net/api/role/* — role / privilege probes the watch consults
/// when deciding whether to surface dev-only UI (the in-watch debug
/// console button, hidden admin-tool tab, etc).
///
/// URL verified against
/// Cpp2IL_ISIL/IsilDump/Assembly-CSharp/RecNet/Accounts.txt:5168
/// ("role/developer/{0}") called from
/// RecNet.Accounts.GetAccountIsDeveloper. Response shape is a
/// bare JSON boolean — the watch wraps the call with
/// PromiseExtensions.ExpectPrimitiveResponse<bool>, so an
/// array or object response throws InvalidCastException and silently
/// disables the dev UI.
///
/// To make a player a developer (and unlock the watch's
/// debug-console button), set Players.IsDeveloper = 1 in the
/// SQLite DB or via the admin UI's player editor.
///
[ApiController]
public class RoleController(DorkNetDbContext db) : ControllerBase
{
// Service-routed call from Accounts.GetAccountIsDeveloper. The
// 2020 dump's SendRequest carries `Service = 0` (Auth) — verified
// at Cpp2IL_ISIL/.../RecNet/Accounts.txt:5183-5186 against the
// RecNet.Service enum in dump.cs:586523-586538 (Auth=0). Service.Auth
// maps to auth.{apex} in ConfigService's ServiceUrls, and
// the path the watch appends has NO `/api/` prefix — it's just
// role/developer/{id}. The previous /api/role/...-only
// route never matched, so every nameplate probe came back 404 and
// the watch logged "Failed to GetIsDeveloper for accountId X: HTTP
// Error 404" once per remote player per scene load.
[HttpGet("/role/developer/{accountId:long}")]
[HttpGet("/api/role/developer/{accountId:long}")]
[AllowAnonymous]
public async Task> IsDeveloper(long accountId)
{
// OR IsCommunityTeam into the answer so the watch unlocks the
// in-settings "Developer Display Mode" slider for community-team
// members too — the slider's positions render the overhead
// badge as "Community Team" / "Developer" (see
// Cpp2IL_ISIL/.../PlayerUI.txt:9085-9099). The 2020 watch never
// probes a separate community-team role, so collapsing both
// flags into this one endpoint is the only way to reach the
// badge UI without patching the binary.
var isDev = await db.Players
.Where(p => p.Id == accountId)
.Select(p => (bool?)(p.IsDeveloper || p.IsCommunityTeam || p.IsAdmin))
.FirstOrDefaultAsync();
return Ok(isDev ?? false);
}
/// Mirror endpoint for the admin role — same shape, in case
/// later builds added an /api/role/admin/{id} probe. The 2020
/// dump only references the developer one but admins also get all
/// dev privileges, so the result is unioned.
[HttpGet("/role/admin/{accountId:long}")]
[HttpGet("/api/role/admin/{accountId:long}")]
[AllowAnonymous]
public async Task> IsAdmin(long accountId)
{
var isAdmin = await db.Players
.Where(p => p.Id == accountId)
.Select(p => (bool?)p.IsAdmin)
.FirstOrDefaultAsync();
return Ok(isAdmin ?? false);
}
/// The March-2023 client probes this one in addition to
/// role/developer. Both calls funnel through the same helper
/// (OPMAPIOEIFG.ILHILCFCNFF, which formats "role/{0}/{1}"
/// and issues it with HTTPMethods.GET); the two call sites differ only
/// in the role literal they pass — "developer" at
/// RecNet.Runtime/OPMAPIOEIFG.txt:9402 and "moderator" at
/// :9471. The 2020 build never referenced the moderator probe, so it
/// used to 404 for every remote player on the 2023 client.
///
/// DorkNet has no dedicated moderator bit, so admins and community-team
/// members answer true — they are the accounts that actually hold
/// moderation powers here.
[HttpGet("/role/moderator/{accountId:long}")]
[HttpGet("/api/role/moderator/{accountId:long}")]
[AllowAnonymous]
public async Task> IsModerator(long accountId)
{
var isMod = await db.Players
.Where(p => p.Id == accountId)
.Select(p => (bool?)(p.IsAdmin || p.IsCommunityTeam))
.FirstOrDefaultAsync();
return Ok(isMod ?? false);
}
/// Future-proofing for a hypothetical
/// role/communityteam/{id} probe — not referenced by the 2020
/// build, but mirrors the developer shape so a patched watch can
/// distinguish the two roles without a server change.
[HttpGet("/role/communityteam/{accountId:long}")]
[HttpGet("/api/role/communityteam/{accountId:long}")]
[AllowAnonymous]
public async Task> IsCommunityTeam(long accountId)
{
var isCt = await db.Players
.Where(p => p.Id == accountId)
.Select(p => (bool?)p.IsCommunityTeam)
.FirstOrDefaultAsync();
return Ok(isCt ?? false);
}
}