using AutoMapper; using SessionCompanion.Database.Repositories.Base; using SessionCompanion.Database.Tables; using SessionCompanion.Services.Base; using SessionCompanion.Services.Interfaces; using SessionCompanion.ViewModels.CharacterViewModels; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Linq; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore; using AutoMapper.QueryableExtensions; using System.IO; using Newtonsoft.Json.Linq; using SessionCompanion.ViewModels.UniversalModels; using SessionCompanion.Services.Helpers; using SessionCompanion.ViewModels.RaceViewModels; using SessionCompanion.ViewModels.ClassViewModels; namespace SessionCompanion.Services.Services { public class CharacterService : ServiceBase, ICharacterService { public CharacterService(IMapper mapper, IRepository repository, IRepository classRepository) : base(mapper, repository) { } /// /// Funkcja zwraca listę postaci przypisanych do podanego użytkownika /// /// identyfikator użytkownika /// Lista postaci dosępnych dla podanego użytkownika public async Task> GetUserLoginCharacters(int userId) { var characters = await Repository.Get(c => c.UserId.Equals(userId)) .Include(x => x.Biography) .Include(x => x.Statistics) .Include(x => x.Biography).ThenInclude(b => b.Class).ToListAsync(); var result = Mapper.Map>(characters); return result; } /// /// Funkcja zwraca podstawowy widok postaci na podstawie ich id /// /// Lista identyfikatorów postaci /// Podstawowy widok podanych postaci public async Task> GetBasicCharactersData(List charactersId) { var characters = await Repository.Get(c => charactersId.Contains(c.Id)) .Include(x => x.Biography) .ThenInclude(x => x.Class) .Include(x => x.Statistics).ToListAsync(); var result = Mapper.Map>(characters); return result; } /// /// Funkcja zwraca wszystkie statystyki danej postaci /// /// indentyfikator postaci /// ViewModel z statystykami postaci public async Task GetCharacterEveryStat(int characterId) { var character = await Repository.Get(c => c.Id.Equals(characterId)) .Include(x => x.Intelligence) .Include(x => x.Strength) .Include(x => x.Wisdom) .Include(x => x.Charisma) .Include(x => x.Constitution) .Include(x => x.Dexterity).SingleAsync(); var result = Mapper.Map(character); return result; } /// /// Funkcja zwraca listę, zawierającą statystyki danej postaci /// /// /// lista zawierającą statystyki danej postaci public async Task> GetCharacterStatistics(int characterId) { List statistics = new List(); var character = await Repository.Get(c => c.Id.Equals(characterId)) .Include(x => x.Intelligence) .Include(x => x.Strength) .Include(x => x.Wisdom) .Include(x => x.Charisma) .Include(x => x.Constitution) .Include(x => x.Dexterity) .SingleAsync(); statistics.Add(CustomMappings.MapCharisma(character.Charisma)); statistics.Add(CustomMappings.MapDexterity(character.Dexterity)); statistics.Add(CustomMappings.MapConstitution(character.Constitution)); statistics.Add(CustomMappings.MapIntelligence(character.Intelligence)); statistics.Add(CustomMappings.MapStrength(character.Strength)); statistics.Add(CustomMappings.MapWisdom(character.Wisdom)); return statistics; } /// /// Funkcja zwraca podstawowy widok postaci na podstawie ich id /// /// Lista identyfikatorów postaci /// Podstawowy widok podanych postaci public async Task GetBasicCharacterbasicInfo(int characterId) { var character = await Repository.Get(c => c.Id.Equals(characterId)) .Include(x => x.Biography).ThenInclude(x => x.Class) .Include(x => x.Biography).ThenInclude(x => x.Race) .Include(x => x.Statistics).SingleAsync(); var result = Mapper.Map(character); return result; } /// /// Function returns basic stats from Characters in templates /// /// public async Task> GetCharactersFromTemplate(List raceViewModels, List classViewModels) { const string file = "../SessionCompanion.Database/JsonData/characters_templates.json"; List basicStats = new List(); using (StreamReader reader = new StreamReader(file)) { var json = await reader.ReadToEndAsync(); foreach (var item in JArray.Parse(json)) { CharacterFromTemplatesSimpleViewModel characterFromTemplatesSimpleViewModel = new CharacterFromTemplatesSimpleViewModel() { Id = (int)item["id"], Class = classViewModels.Where(c => c.Id.Equals((int)item["biography"]["ClassId"])).Select(c => c.Name).Single(), Race = raceViewModels.Where(r => r.Id.Equals((int)item["biography"]["RaceId"])).Select(r => r.Name).Single(), Name = (string)item["biography"]["Name"] }; basicStats.Add(characterFromTemplatesSimpleViewModel); } } return basicStats; } } }