Skip to content

Commit

Permalink
fix: Players can write using characters not added to specific chapter #…
Browse files Browse the repository at this point in the history
…16

fix: Character toggle in nav bar should change the character used for writing #20
  • Loading branch information
KrystianKempski committed Nov 3, 2024
1 parent 76e9d3c commit e131f9d
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 142 deletions.
16 changes: 12 additions & 4 deletions DA_Business/Repository/CharacterReps/CharacterRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public async Task<IEnumerable<CharacterDTO>> GetAll(int? id=null)
.Include(r => r.Race)
.Include(r => r.Profession)
.Include(r=>r.EquipmentSlots)
.Where(u=>u.NPCName != SD.NPCName_GameMaster));
.Where(u=>u.NPCName != SD.GameMaster_NPCName));
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters
.Include(r => r.Race)
.Include(r => r.Profession)
.Include(r => r.EquipmentSlots)
.Where(u=>u.Id == id && u.NPCName != SD.NPCName_GameMaster));
.Where(u=>u.Id == id && u.NPCName != SD.GameMaster_NPCName));
}

public async Task<CharacterDTO> GetById(int id)
Expand Down Expand Up @@ -447,6 +447,15 @@ public async Task<IEnumerable<CharacterDTO>> GetAllApproved(string? userName = n
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters.Include(r => r.Race).Include(r => r.Race).Include(r => r.Profession).Include(r => r.EquipmentSlots).Where(u =>u.IsApproved == true));
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters.Include(r => r.Race).Include(r => r.Race).Include(r => r.Profession).Include(r => r.EquipmentSlots).Where(u => u.UserName == userName && u.IsApproved == true));

}

public async Task<IEnumerable<CharacterDTO>> GetAllInfoApproved(string? userName = null)
{
using var contex = await _db.CreateDbContextAsync();
if (userName == null || userName.Length < 2)
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters.Where(u => u.IsApproved == true));
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters.Where(u => u.UserName == userName && u.IsApproved == true));

}
public async Task<string> GetPortraitUrl(int id)
{
Expand All @@ -464,8 +473,7 @@ public async Task<IEnumerable<CharacterDTO>> GetAllInfoForUser(string userName)
using var contex = await _db.CreateDbContextAsync();
if (userName == null || userName.Length < 2)
return new List<CharacterDTO>();
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters
.Where(u => u.UserName == userName));
return _mapper.Map<IEnumerable<Character>, IEnumerable<CharacterDTO>>(contex.Characters.Where(u => u.UserName == userName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface ICharacterRepository
public Task<IEnumerable<CharacterDTO>> GetAllForCampaign(int campaignId);

public Task<IEnumerable<CharacterDTO>> GetAllApproved(string? userName = null);
public Task<IEnumerable<CharacterDTO>> GetAllInfoApproved(string? userName = null);
public Task<string> GetPortraitUrl(int id);
}
}
2 changes: 1 addition & 1 deletion DA_Business/Repository/ChatRepos/CampaignRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task<CampaignDTO> Update(CampaignDTO objDTO)
if (!updatedCampaign.Characters.Any(c => c.Id == existingChild.Id))
{
//remove from campaign
var detachedCharacter = contex.Characters.Include(c => c.Campaigns).FirstOrDefault(c => c.Id == existingChild.Id && c.Id != default(int));
var detachedCharacter = contex.Characters.Include(c => c.Campaigns).Include(c => c.Chapters).FirstOrDefault(c => c.Id == existingChild.Id && c.Id != default(int));
if (detachedCharacter == null || detachedCharacter.Campaigns == null || !detachedCharacter.Campaigns.Contains(obj))
continue;
detachedCharacter.Campaigns.Remove(obj);
Expand Down
26 changes: 26 additions & 0 deletions DA_Business/Services/CallbackService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA_Business.Services
{
public class CallbackService
{
public int SelectedCharId { get; private set; }

public void SetSelectedChar(int Id)
{
if (SelectedCharId != Id)
{
SelectedCharId = Id;
NotifyStateChanged();
}
}

public event Action OnChange; // event raised when changed

private void NotifyStateChanged() => OnChange?.Invoke();
}
}
2 changes: 1 addition & 1 deletion DA_Business/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserService : IUserService
private readonly AuthenticationStateProvider _authState;
private readonly UserInfo _userInfo;
private readonly IDbContextFactory<ApplicationDbContext> _db;


public UserService(AuthenticationStateProvider authState, IOptions<UserInfo> userInfo, IDbContextFactory<ApplicationDbContext> db)
{
Expand Down Expand Up @@ -90,7 +91,6 @@ public async Task SetSelectedCharId(int charId)
_userInfo.SelectedCharacterId = charId;
}
}

}
}
}
3 changes: 2 additions & 1 deletion DA_Common/SD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static class SD
public const string TraitType_Profession = "Profession";
public const string TraitType_Unique = "Unique";

public const string NPCName_GameMaster = "Game Master";
public const string GameMaster_NPCName = "Game Master";
public const string GameMaster_Portrait = "../images/gm_avatar.webp";


public static string Portrait = "portraits";
Expand Down
5 changes: 0 additions & 5 deletions DA_DataAccess/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ public UserInfo() { }
public bool ResetUser { get; set; } = false;
public int SelectedCharacterId { get; set; } = 0;
public string? UserName { get; set; }

public string? UserId { get; set; }

public bool? IsAdminOrMG { get; set; }

public bool? IsAuthenticated { get; set; }

public string? Role { get; set; }

}
}
10 changes: 2 additions & 8 deletions DagoniteEmpire/Pages/CharacterPages/HealthPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,17 @@
[Parameter]
public int Id { get; set; }
public AllParamsModel AllParams { get; set; } = new();
MudDataGrid<WoundDTO> WoundsGrid { get; set; }

private MudDataGrid<WoundDTO> WoundsGrid { get; set; }
private Nutrition NutritionSelect {get;set;}
private IDisposable? registration;
private bool IsLoading { get; set; } = true;
private bool IsLeaveAllowed { get; set; } = true;
private string TargetLocation { get; set; } = "";

UserInfo? UserInfo { get; set; }
private UserInfo? UserInfo { get; set; }
private ICollection<WoundDTO> WoundsList { get; set; } = new List<WoundDTO>();
private ICollection<WoundDTO> DeletedWounds { get; set; } = new List<WoundDTO>();
private IDictionary<string, WoundDTO> Conditions { get; set; } = new Dictionary<string, WoundDTO>();
private WoundDTO NewWound { get; set; } = new();

public string[] Initial = Wounds.Location.All;
public Dictionary<EquippedItems, EquipItemComponent> EquipItemComp = new Dictionary<EquippedItems, EquipItemComponent>();

Expand All @@ -272,7 +269,6 @@
try
{
IsLoading = true;

registration = _navigationManager.RegisterLocationChangingHandler(OnLocationChanging);
await LoadCharacter();
WoundsList = AllParams.Health.GetAll();
Expand Down Expand Up @@ -315,7 +311,6 @@
{
AllParams.Health.CalculateHealTime(wound);
}

}

private async Task SaveChanges()
Expand Down Expand Up @@ -430,7 +425,6 @@
StateHasChanged();
}


public async Task ConfirmLeave_Click(bool isConfirmed)
{
IsLoading = true;
Expand Down
2 changes: 0 additions & 2 deletions DagoniteEmpire/Pages/CharacterPages/ProfessionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@
position: relative;
}
.e-numeric.e-style .e-control.e-numerictextbox {
padding: 2px;
Expand Down
97 changes: 55 additions & 42 deletions DagoniteEmpire/Pages/Chat/CampaignList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@inject IChatManager _chatManager
@inject ICampaignRepository _campaignRepository
@inject IUserService _userService

@inject CallbackService callbackService
@implements IDisposable

@using Microsoft.AspNetCore.SignalR.Client;

Expand All @@ -23,7 +24,7 @@
}else{
<MudBreadcrumbs Items="_items"></MudBreadcrumbs>

@if (Characters.Count() > 0)
@* @if (Characters.Count() > 0)
{
<div class="d-flex flex-row">
<MudText Typo="Typo.h4" Class="py-3"><img src="@MyIcon.Bookmark" width="40px" height="40px" class="pr-2" />Avalible campaigns for:</MudText>
Expand All @@ -36,9 +37,9 @@
</div>
}
else if(Characters.Count() == 1)
{
<MudText Typo="Typo.h4" Class="py-3"><img src="@MyIcon.Bookmark" width="40px" height="40px" class="pr-2" />Avalible campaigns for: @Characters.First().NPCName</MudText>
}
{ *@
<MudText Typo="Typo.h4" Class="py-3"><img src="@MyIcon.Bookmark" width="40px" height="40px" class="pr-2" />Avalible campaigns for: @SelectedCharacter.NPCName</MudText>


@if (UserInfo?.IsAdminOrMG == true)
{
Expand Down Expand Up @@ -100,8 +101,6 @@
</MudStack>
}
<MudPaper Width="100%" Square="true" Class="ma-2">


</MudPaper>
</MudContainer>

Expand All @@ -119,6 +118,8 @@

public UserInfo? UserInfo { get; set; }
private List<BreadcrumbItem> _items = new List<BreadcrumbItem>();
private System.Action handler;


protected override async Task OnInitializedAsync()
{
Expand All @@ -128,51 +129,55 @@
var uri = new Uri(_navigationManager.Uri);
_navigationManager.NavigateTo($"/identity/account/login?returnUrl={uri.LocalPath}", forceLoad: true);
}

handler = async () => await LoadSelectedCharacter();
callbackService.OnChange += handler;
await LoadCharactersAndCampaigns();
}

protected override async Task OnAfterRenderAsync(bool firstRender)
public void Dispose()
{
if (firstRender)
{
// await LoadCharactersAndCampaigns();
}
callbackService.OnChange -= handler;
}

private async Task SelectCharacter(CharacterDTO character)
{
IsLoading = true;
StateHasChanged();
SelectedCharacter = character;
Campaigns = (await _campaignRepository.GetAll(character.Id)).ToList();
IsLoading = false;
StateHasChanged();
}

private async Task LoadCharactersAndCampaigns()
private async Task LoadSelectedCharacter()
{
if (UserInfo is null)
return;
IsLoading = true;
StateHasChanged();
_items = new List<BreadcrumbItem>
{
new BreadcrumbItem("Campaign", href: $"/campaignlist/"),
};


if (UserInfo?.IsAdminOrMG == true)
{
Title = "All avalible campaingns";
Characters = await _characterRepository.GetAll();
Campaigns = (await _campaignRepository.GetAll()).ToList();
SelectedCharacter = await _characterRepository.GetByName(SD.NPCName_GameMaster);
Characters = await _characterRepository.GetAllInfoApproved();
if(Characters is not null)
SelectedCharacter = Characters.FirstOrDefault(c => c.Id == UserInfo.SelectedCharacterId);
if (SelectedCharacter is not null && SelectedCharacter.NPCName.Equals(SD.GameMaster_NPCName))
{

Title = "All avalible campaingns";
SelectedCharacter = await _characterRepository.GetByName(SD.GameMaster_NPCName);
Campaigns = (await _campaignRepository.GetAll()).ToList();
}
else
{
Title = "Characters of " + UserInfo?.UserName;
Characters = await _characterRepository.GetAllInfoApproved(UserInfo.UserName);
if (Characters.Any())
{
SelectedCharacter = Characters.FirstOrDefault(c => c.Id == UserInfo.SelectedCharacterId);
if (SelectedCharacter is not null)
Campaigns = (await _campaignRepository.GetAll(SelectedCharacter.Id)).ToList();
}
}
}
else
{
Title = "Characters of " + UserInfo?.UserName;
Characters = await _characterRepository.GetAllForUser(UserInfo?.UserName);
Characters = await _characterRepository.GetAllInfoApproved(UserInfo.UserName);
if (Characters.Any())
{
SelectedCharacter = Characters.First();
SelectedCharacter = Characters.FirstOrDefault(c => c.Id == UserInfo.SelectedCharacterId);
Campaigns = (await _campaignRepository.GetAll(SelectedCharacter.Id)).ToList();
}

Expand All @@ -182,6 +187,20 @@
StateHasChanged();
}

private async Task LoadCharactersAndCampaigns()
{
IsLoading = true;
StateHasChanged();
_items = new List<BreadcrumbItem>
{
new BreadcrumbItem("Campaign", href: $"/campaignlist/"),
};

await LoadSelectedCharacter();
IsLoading = false;
StateHasChanged();
}

private async Task CreateCampaign()
{

Expand Down Expand Up @@ -258,15 +277,9 @@

private async Task ChooseCampaign(CampaignDTO campaign)
{
_navigationManager.NavigateTo($"/campaign/{campaign.Id}/{SelectedCharacter.Id}");
if (UserInfo is null) return;
_navigationManager.NavigateTo($"/campaign/{campaign.Id}/{UserInfo.SelectedCharacterId}");
}









}
Loading

0 comments on commit e131f9d

Please sign in to comment.