using Microsoft.Extensions.Options; using MongoDB.Driver; using Prospect.Server.Api.Config; using Prospect.Server.Api.Services.Database.Models; namespace Prospect.Server.Api.Services.Database; public class DbUserService : BaseDbService { public DbUserService(IOptions settings) : base(settings, nameof(PlayFabUser)) { } public async Task FindAsync(PlayFabUserAuthType type, string key) { return await Collection.Find(user => user.Auth.Any(auth => auth.Type == type && auth.Key == key)).FirstOrDefaultAsync(); } // Resolve a player by its PlayFab id (used to display squad member names). public async Task FindByIdAsync(string id) { return await Collection.Find(user => user.Id == id).FirstOrDefaultAsync(); } private async Task CreateAsync(PlayFabUserAuthType type, string key) { var user = new PlayFabUser { DisplayName = "Unknown", Auth = new List { new PlayFabUserAuth { Type = type, Key = key } } }; await Collection.InsertOneAsync(user); return user; } public async Task FindOrCreateAsync(PlayFabUserAuthType type, string key) { return await FindAsync(type, key) ?? await CreateAsync(type, key); } // Resolve several auth keys (e.g. a batch of Steam IDs) to the players that exist on // this server in a single query. Used by the friends list to keep only the imported // Steam friends who have actually played here. public async Task> FindManyAsync(PlayFabUserAuthType type, IReadOnlyCollection keys) { if (keys == null || keys.Count == 0) { return new List(); } var wanted = new HashSet(keys); return await Collection.Find(user => user.Auth.Any(auth => auth.Type == type && wanted.Contains(auth.Key))).ToListAsync(); } }