This commit is contained in:
2025-10-01 23:59:31 +03:00
parent 2a29571dbf
commit b6c4b9b6bb
28 changed files with 689 additions and 383 deletions

View File

@@ -4,52 +4,98 @@ using LctMonolith.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using Serilog;
namespace LctMonolith.Services;
public class DialogueService : IDialogueService
namespace LctMonolith.Services
{
private readonly IUnitOfWork _uow;
public DialogueService(IUnitOfWork uow) => _uow = uow;
public async Task<Dialogue?> GetDialogueByMissionIdAsync(Guid missionId)
public class DialogueService : IDialogueService
{
try { return await _uow.Dialogues.Query(d => d.MissionId == missionId).FirstOrDefaultAsync(); }
catch (Exception ex) { Log.Error(ex, "GetDialogueByMissionIdAsync failed {MissionId}", missionId); throw; }
}
private readonly IUnitOfWork _uow;
public async Task<Dialogue> CreateDialogueAsync(Dialogue dialogue)
{
try
public DialogueService(IUnitOfWork uow)
{
dialogue.Id = Guid.NewGuid();
await _uow.Dialogues.AddAsync(dialogue);
await _uow.SaveChangesAsync();
return dialogue;
_uow = uow;
}
catch (Exception ex) { Log.Error(ex, "CreateDialogueAsync failed {MissionId}", dialogue.MissionId); throw; }
}
public async Task<DialogueMessage?> GetDialogueMessageByIdAsync(Guid messageId)
{
try { return await _uow.DialogueMessages.GetByIdAsync(messageId); }
catch (Exception ex) { Log.Error(ex, "GetDialogueMessageByIdAsync failed {MessageId}", messageId); throw; }
}
public async Task<IEnumerable<DialogueMessageResponseOption>> GetResponseOptionsAsync(Guid messageId)
{
try { return await _uow.DialogueMessageResponseOptions.Query(o => o.ParentDialogueMessageId == messageId).ToListAsync(); }
catch (Exception ex) { Log.Error(ex, "GetResponseOptionsAsync failed {MessageId}", messageId); throw; }
}
public async Task<DialogueMessage?> ProcessDialogueResponseAsync(Guid messageId, Guid responseOptionId, Guid playerId)
{
try
public async Task<Dialogue?> GetDialogueByMissionIdAsync(Guid missionId)
{
var option = await _uow.DialogueMessageResponseOptions.Query(o => o.Id == responseOptionId && o.ParentDialogueMessageId == messageId).FirstOrDefaultAsync();
if (option == null) return null;
if (option.DestinationDialogueMessageId == null) return null; // end branch
return await _uow.DialogueMessages.GetByIdAsync(option.DestinationDialogueMessageId);
try
{
return await _uow.Dialogues
.Query(d => d.MissionId == missionId)
.FirstOrDefaultAsync();
}
catch (Exception ex)
{
Log.Error(ex, "GetDialogueByMissionIdAsync failed {MissionId}", missionId);
throw;
}
}
public async Task<Dialogue> CreateDialogueAsync(Dialogue dialogue)
{
try
{
dialogue.Id = Guid.NewGuid();
await _uow.Dialogues.AddAsync(dialogue);
await _uow.SaveChangesAsync();
return dialogue;
}
catch (Exception ex)
{
Log.Error(ex, "CreateDialogueAsync failed {MissionId}", dialogue.MissionId);
throw;
}
}
public async Task<DialogueMessage?> GetDialogueMessageByIdAsync(Guid messageId)
{
try
{
return await _uow.DialogueMessages.GetByIdAsync(messageId);
}
catch (Exception ex)
{
Log.Error(ex, "GetDialogueMessageByIdAsync failed {MessageId}", messageId);
throw;
}
}
public async Task<IEnumerable<DialogueMessageResponseOption>> GetResponseOptionsAsync(Guid messageId)
{
try
{
return await _uow.DialogueMessageResponseOptions
.Query(o => o.ParentDialogueMessageId == messageId)
.ToListAsync();
}
catch (Exception ex)
{
Log.Error(ex, "GetResponseOptionsAsync failed {MessageId}", messageId);
throw;
}
}
public async Task<DialogueMessage?> ProcessDialogueResponseAsync(Guid messageId, Guid responseOptionId, Guid playerId)
{
try
{
var option = await _uow.DialogueMessageResponseOptions
.Query(o => o.Id == responseOptionId && o.ParentDialogueMessageId == messageId)
.FirstOrDefaultAsync();
if (option == null)
{
return null;
}
if (option.DestinationDialogueMessageId == null)
{
return null;
}
return await _uow.DialogueMessages.GetByIdAsync(option.DestinationDialogueMessageId);
}
catch (Exception ex)
{
Log.Error(ex, "ProcessDialogueResponseAsync failed {MessageId} {ResponseId}", messageId, responseOptionId);
throw;
}
}
catch (Exception ex) { Log.Error(ex, "ProcessDialogueResponseAsync failed {MessageId} {ResponseId}", messageId, responseOptionId); throw; }
}
}