using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Runtime.CompilerServices; using RadicadeServer.API.Notifications; using RadicadeServer.DB; using RadicadeServer.Extensions; using RadicadeServer.RecNet.Enums.Messages; using RadicadeServer.RecNet.Messages; using RadicadeServer.RecNet.Notifications; using RadicadeServer.RecNet.Relationships; namespace RadicadeServer.API.Relationships { public static class RelationshipsShared { internal static IDbContextFactory DbFactory = null!; internal static async Task GetOrCreate(RecDBContext db, long accountId, long playerId) { var rel = await db.Relationships.FirstOrDefaultAsync(x => x.AccountId == accountId && x.PlayerId == playerId); if (rel == null) { rel = new RealRelationship { AccountId = accountId, PlayerId = playerId }; await db.Relationships.AddAsync(rel); } await db.SaveChangesAsync(); return rel; } internal static void SetReciprocalStatus( RealRelationship mine, RealRelationship theirs, Func getter, Action setter, bool enabling) { if (enabling) { var theirStatus = getter(theirs); bool theyHaveIt = theirStatus == ReciprocalStatus.Local || theirStatus == ReciprocalStatus.Mutual; setter(mine, theyHaveIt ? ReciprocalStatus.Mutual : ReciprocalStatus.Local); setter(theirs, theyHaveIt ? ReciprocalStatus.Mutual : ReciprocalStatus.Remote); } else { var theirStatus = getter(theirs); setter(mine, ReciprocalStatus.None); setter(theirs, theirStatus == ReciprocalStatus.Mutual ? ReciprocalStatus.Remote : ReciprocalStatus.None); } } } }