using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; namespace IsleBot; public class DbManager : DbContext { public DbSet Players { get; set; } public DbSet Cards { get; set; } public DbSet Matches { get; set; } public string dbpath { get; } = "database.db"; public DbManager() { var fullPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + dbpath; dbpath = fullPath; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlite($"Data Source={dbpath}"); #region Players Functions public List GetAllPlayers() => Players.ToList(); public User? GetPlayerById(ulong id) => Players.Find(id); public void AddPlayer(User player) { Players.Add(player); SaveChanges(); } public void RemovePlayer(User player) { Players.Remove(player); SaveChanges(); } public void UpdatePlayer(User player) { Players.Update(player); SaveChanges(); } #endregion #region Cards Functions public List GetAllCards() => Cards.ToList(); public Card? GetCardById(int id) => Cards.Find(id); public void AddCard(Card card) { Cards.Add(card); SaveChanges(); } public void RemoveCard(Card card) { Cards.Remove(card); SaveChanges(); } public void UpdateCard(Card card) { Cards.Update(card); SaveChanges(); } public List GetCardsByPlayerId(ulong id) => Cards.Where(card => card.Owner.Id == id).ToList(); public List GetCardsByPlayer(User player) => Cards.Where(card => card.Owner == player).ToList(); public List GetCardsByPlayerId(ulong id, int limit) => Cards.Where(card => card.Owner.Id == id).Take(limit).ToList(); public List GetCardsByPlayer(User player, int limit) => Cards.Where(card => card.Owner == player).Take(limit).ToList(); public List GetCardsByPlayerId(ulong id, int limit, int offset) => Cards.Where(card => card.Owner.Id == id).Skip(offset).Take(limit).ToList(); public List GetCardsByPlayer(User player, int limit, int offset) => Cards.Where(card => card.Owner == player).Skip(offset).Take(limit).ToList(); //probably gonna add extra options filtered by card class or something like that in the future #endregion #region Matches Functions public IEnumerable GetAllMatches() => Matches; public Match? GetMatchById(int id) => Matches.Find(id); public void AddMatch(Match match) { Matches.Add(match); SaveChanges(); } public void RemoveMatch(Match match) { Matches.Remove(match); SaveChanges(); } public void UpdateMatch(Match match) { Matches.Update(match); SaveChanges(); } //By player public List GetMatchesByPlayerId(ulong id) => Matches.Where(match => match.PlayerA.Id == id || match.PlayerB.Id == id).ToList(); public List GetMatchesByPlayer(User player) => Matches.Where(match => match.PlayerA == player || match.PlayerB == player).ToList(); public List GetMatchesByPlayerId(ulong id, int limit) => Matches.Where(match => match.PlayerA.Id == id || match.PlayerB.Id == id).Take(limit).ToList(); public List GetMatchesByPlayer(User player, int limit) => Matches.Where(match => match.PlayerA == player || match.PlayerB == player).Take(limit).ToList(); public List GetMatchesByPlayerId(ulong id, int limit, int offset) => Matches.Where(match => match.PlayerA.Id == id || match.PlayerB.Id == id).Skip(offset).Take(limit).ToList(); public List GetMatchesByPlayer(User player, int limit, int offset) => Matches.Where(match => match.PlayerA == player || match.PlayerB == player).Skip(offset).Take(limit).ToList(); //By status public List GetMatchesByStatus(EStatus status) => Matches.Where(match => match.Status == status).ToList(); public List GetMatchesByStatus(EStatus status, int limit) => Matches.Where(match => match.Status == status).Take(limit).ToList(); public List GetMatchesByStatus(EStatus status, int limit, int offset) => Matches.Where(match => match.Status == status).Skip(offset).Take(limit).ToList(); //By card public List GetMatchesByCardId(int id)=> Matches.Where(match => match.PlayerACard.Id == id || match.PlayerBCard.Id == id).ToList(); public List GetMatchesByCard(Card card) => Matches.Where(match => match.PlayerACard == card || match.PlayerBCard == card).ToList(); public List GetMatchesByCardId(int id, int limit) => Matches.Where(match => match.PlayerACard.Id == id || match.PlayerBCard.Id == id).Take(limit).ToList(); public List GetMatchesByCard(Card card, int limit) => Matches.Where(match => match.PlayerACard == card || match.PlayerBCard == card).Take(limit).ToList(); public List GetMatchesByCardId(int id, int limit, int offset) => Matches.Where(match => match.PlayerACard.Id == id || match.PlayerBCard.Id == id).Skip(offset).Take(limit).ToList(); public List GetMatchesByCard(Card card, int limit, int offset) => Matches.Where(match => match.PlayerACard == card || match.PlayerBCard == card).Skip(offset).Take(limit).ToList(); //By player and status public List GetMatchesByStatusAndPlayerId(EStatus status, ulong id) => Matches.Where(match => match.Status == status && (match.PlayerA.Id == id || match.PlayerB.Id == id)).ToList(); public List GetMatchesByStatusAndPlayer(EStatus status, User player) => Matches.Where(match => match.Status == status && (match.PlayerA == player || match.PlayerB == player)).ToList(); public List GetMatchesByStatusAndPlayerId(EStatus status, ulong id, int limit) => Matches.Where(match => match.Status == status && (match.PlayerA.Id == id || match.PlayerB.Id == id)).Take(limit).ToList(); public List GetMatchesByStatusAndPlayer(EStatus status, User player, int limit) => Matches.Where(match => match.Status == status && (match.PlayerA == player || match.PlayerB == player)).Take(limit).ToList(); public List GetMatchesByStatusAndPlayerId(EStatus status, ulong id, int limit, int offset) => Matches.Where(match => match.Status == status && (match.PlayerA.Id == id || match.PlayerB.Id == id)).Skip(offset).Take(limit).ToList(); public List GetMatchesByStatusAndPlayer(EStatus status, User player, int limit, int offset) => Matches.Where(match => match.Status == status && (match.PlayerA == player || match.PlayerB == player)).Skip(offset).Take(limit).ToList(); //By card and status public List GetMatchesByCardIdAndStatus(int id, EStatus status) => Matches.Where(match => match.Status == status && (match.PlayerACard.Id == id || match.PlayerBCard.Id == id)).ToList(); public List GetMatchesByCardAndStatus(Card card, EStatus status) => Matches.Where(match => match.Status == status && (match.PlayerACard == card || match.PlayerBCard == card)).ToList(); public List GetMatchesByCardIdAndStatus(int id, EStatus status, int limit) => Matches.Where(match => match.Status == status && (match.PlayerACard.Id == id || match.PlayerBCard.Id == id)).Take(limit).ToList(); public List GetMatchesByCardAndStatus(Card card, EStatus status, int limit) => Matches.Where(match => match.Status == status && (match.PlayerACard == card || match.PlayerBCard == card)).Take(limit).ToList(); public List GetMatchesByCardIdAndStatus(int id, EStatus status, int limit, int offset) => Matches.Where(match => match.Status == status && (match.PlayerACard.Id == id || match.PlayerBCard.Id == id)).Skip(offset).Take(limit).ToList(); public List GetMatchesByCardAndStatus(Card card, EStatus status, int limit, int offset) => Matches.Where(match => match.Status == status && (match.PlayerACard == card || match.PlayerBCard == card)).Skip(offset).Take(limit).ToList(); #endregion }