2020-12-05 21:58:45 +01:00
|
|
|
|
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;
|
2021-01-04 00:58:31 +01:00
|
|
|
|
using SessionCompanion.ViewModels.UniversalModels;
|
|
|
|
|
using SessionCompanion.Services.Helpers;
|
2021-01-12 22:22:56 +01:00
|
|
|
|
using SessionCompanion.ViewModels.RaceViewModels;
|
|
|
|
|
using SessionCompanion.ViewModels.ClassViewModels;
|
2020-12-05 21:58:45 +01:00
|
|
|
|
|
|
|
|
|
namespace SessionCompanion.Services.Services
|
|
|
|
|
{
|
|
|
|
|
public class CharacterService : ServiceBase<CharacterViewModel, Character>, ICharacterService
|
|
|
|
|
{
|
2021-01-12 22:22:56 +01:00
|
|
|
|
public CharacterService(IMapper mapper, IRepository<Character> repository, IRepository<Class> classRepository) : base(mapper, repository)
|
2020-12-05 21:58:45 +01:00
|
|
|
|
{ }
|
2020-12-18 11:18:48 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zwraca listę postaci przypisanych do podanego użytkownika
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">identyfikator użytkownika</param>
|
|
|
|
|
/// <returns>Lista postaci dosępnych dla podanego użytkownika</returns>
|
|
|
|
|
public async Task<IEnumerable<CharacterForLoginViewModel>> 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<IEnumerable<CharacterForLoginViewModel>>(characters);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 15:45:29 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zwraca podstawowy widok postaci na podstawie ich id
|
|
|
|
|
/// </summary>
|
2020-12-21 13:30:59 +01:00
|
|
|
|
/// <param name="charactersId">Lista identyfikatorów postaci</param>
|
|
|
|
|
/// <returns>Podstawowy widok podanych postaci</returns>
|
|
|
|
|
public async Task<IEnumerable<CharacterBasicStatsViewModel>> GetBasicCharactersData(List<int> charactersId)
|
2020-12-20 15:45:29 +01:00
|
|
|
|
{
|
2020-12-21 13:30:59 +01:00
|
|
|
|
var characters = await Repository.Get(c => charactersId.Contains(c.Id))
|
|
|
|
|
.Include(x => x.Biography)
|
2020-12-27 21:17:00 +01:00
|
|
|
|
.ThenInclude(x => x.Class)
|
2020-12-21 13:30:59 +01:00
|
|
|
|
.Include(x => x.Statistics).ToListAsync();
|
|
|
|
|
var result = Mapper.Map<IEnumerable<CharacterBasicStatsViewModel>>(characters);
|
2020-12-20 15:45:29 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-01-03 19:49:37 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zwraca wszystkie statystyki danej postaci
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="characterId">indentyfikator postaci</param>
|
|
|
|
|
/// <returns>ViewModel z statystykami postaci</returns>
|
|
|
|
|
public async Task<CharacterEveryStatViewModel> 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<CharacterEveryStatViewModel>(character);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-01-04 00:58:31 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zwraca listę, zawierającą statystyki danej postaci
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="characterId"></param>
|
|
|
|
|
/// <returns>lista zawierającą statystyki danej postaci</returns>
|
|
|
|
|
public async Task<List<UniversalStatisticViewModel>> GetCharacterStatistics(int characterId)
|
|
|
|
|
{
|
|
|
|
|
List<UniversalStatisticViewModel> statistics = new List<UniversalStatisticViewModel>();
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-01-07 17:39:01 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zwraca podstawowy widok postaci na podstawie ich id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="characterId">Lista identyfikatorów postaci</param>
|
|
|
|
|
/// <returns>Podstawowy widok podanych postaci</returns>
|
|
|
|
|
public async Task<CharacterBasicInfoViewModel> 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<CharacterBasicInfoViewModel>(character);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-01-12 22:22:56 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function returns basic stats from Characters in templates
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<IEnumerable<CharacterFromTemplatesSimpleViewModel>> GetCharactersFromTemplate(List<RaceViewModel> raceViewModels, List<ClassViewModel> classViewModels)
|
|
|
|
|
{
|
|
|
|
|
const string file = "../SessionCompanion.Database/JsonData/characters_templates.json";
|
|
|
|
|
List<CharacterFromTemplatesSimpleViewModel> basicStats = new List<CharacterFromTemplatesSimpleViewModel>();
|
|
|
|
|
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;
|
|
|
|
|
}
|
2020-12-05 21:58:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|