Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a simple ability to export match records #291

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
snixtho marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using EvoSC.Commands.Attributes;
using EvoSC.Commands.Interfaces;
using EvoSC.Common.Controllers;
using EvoSC.Common.Controllers.Attributes;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces;

namespace EvoSC.Modules.Official.MatchTrackerModule.Controllers;

[Controller]
public class MatchTrackerCommandsController(IMatchTrackerExportService exportService) : EvoScController<ICommandInteractionContext>
{
[ChatCommand("matchtrackerexport", "Export all match tracker data to a csv file.")]
[CommandAlias("/mtexport", true)]
public Task ExportCsvAsync(string file) => exportService.ExportCsv(file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ namespace EvoSC.Modules.Official.MatchTrackerModule.Interfaces;
public interface IMatchRecordRepository
{
public Task<DbMatchRecord> InsertStateAsync(IMatchState state);
public Task<DbMatchRecord[]> GetRecordsAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EvoSC.Modules.Official.MatchTrackerModule.Interfaces;

public interface IMatchTrackerExportService
{
public Task ExportCsv(string file);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces.Models;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces.Stores;

namespace EvoSC.Modules.Official.MatchTrackerModule.Interfaces;

public interface ITrackerStoreService
{
public Task SaveTimelineAsync(IMatchTimeline timeline);
public Task SaveState(IMatchState state);
public void AddStore(IMatchTrackerStore store);
}
6 changes: 6 additions & 0 deletions src/Modules/MatchTrackerModule/MatchTrackerModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using EvoSC.Modules.Attributes;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces.Stores;

namespace EvoSC.Modules.Official.MatchTrackerModule;

[Module(IsInternal = true)]
public class MatchTrackerModule : EvoScModule
{
public MatchTrackerModule(IDatabaseMatchTrackerStore dbStore, ITrackerStoreService stores)
{
stores.AddStore(dbStore);
}
}
2 changes: 1 addition & 1 deletion src/Modules/MatchTrackerModule/Models/MatchTimeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace EvoSC.Modules.Official.MatchTrackerModule.Models;

public class MatchTimeline : IMatchTimeline
{
public Guid TimelineId { get; } = Guid.NewGuid();
public Guid TimelineId { get; init; } = Guid.NewGuid();
public List<IMatchState> States { get; init; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EvoSC.Common.Services.Models;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces.Models;
using EvoSC.Modules.Official.MatchTrackerModule.Models;
using EvoSC.Modules.Official.MatchTrackerModule.Models.Database;
using EvoSC.Modules.Official.MatchTrackerModule.Util;
using LinqToDB;
Expand Down Expand Up @@ -40,4 +41,6 @@ public async Task<DbMatchRecord> InsertStateAsync(IMatchState state)
throw;
}
}

public Task<DbMatchRecord[]> GetRecordsAsync() => Table<DbMatchRecord>().ToArrayAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Text;
using EvoSC.Common.Services.Attributes;
using EvoSC.Modules.Official.MatchTrackerModule.Interfaces;

namespace EvoSC.Modules.Official.MatchTrackerModule.Services;

[Service]
public class MatchTrackerExportService(IMatchRecordRepository repository) : IMatchTrackerExportService
{
public async Task ExportCsv(string file)
{
var records = await repository.GetRecordsAsync();
var sb = new StringBuilder();

sb.AppendLine("Id,Timestamp,TimelineId,Status,Report");

foreach (var record in records)
{
sb.Append(record.Id);
sb.Append(",");
sb.Append(record.Timestamp);
sb.Append(",");
sb.Append(record.TimelineId);
sb.Append(",");
sb.Append(record.Status);
sb.Append(",\"");
sb.Append(EscapeCsvValue(record.Report));
sb.AppendLine("\"");
}

await File.WriteAllTextAsync(file, sb.ToString());
}

private static string EscapeCsvValue(string value)
{
return value.Replace("\"", "\"\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@

namespace EvoSC.Modules.Official.MatchTrackerModule.Services;

[Service(LifeStyle = ServiceLifeStyle.Transient)]
[Service(LifeStyle = ServiceLifeStyle.Singleton)]
public class TrackerStoreService : ITrackerStoreService
{
private readonly List<IMatchTrackerStore> _stores = new();

public TrackerStoreService(IDatabaseMatchTrackerStore dbStore)
{
_stores.Add(dbStore);
}

public async Task SaveTimelineAsync(IMatchTimeline timeline)
{
Expand All @@ -31,4 +26,6 @@ public async Task SaveState(IMatchState state)
await store.SaveStateAsync(state);
}
}

public void AddStore(IMatchTrackerStore store) => _stores.Add(store);
}
Loading