Skip to content

Commit

Permalink
Add "Download all claimed init items" button
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 6, 2024
1 parent 07d383c commit 4e65103
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
<a href="/bulkfix">Bulk fix</a>
}

<button class="button" @onclick="DownloadClaimedInitItemsAsync">Download all claimed init items</button>

<a href="/data/NationsConverterCLI.zip" download="NationsConverterCLI.zip">Download converter</a>
<a href="/data/items.zip" download="NC2Items_Init.zip">Download all init items</a>
<a href="/data/GameData_TMUF_NC2.zip" download="GameData_TMUF_NC2.zip">Download TMUF GameData</a>
Expand Down Expand Up @@ -211,14 +213,14 @@

currentDiscordUser = discordUsers.FirstOrDefault(x => x.Id == snowflake);

/*blocks = db.Blocks
/*blocks = db.Blocks
.Where(x => x.Name.Contains(searchValue))
.Where(x => x.EnvironmentId.Contains(Environment ?? ""))
.Include(x => x.AssignedTo)
.ThenInclude(x => x!.DiscordUser)
.AsNoTracking()
.Select(x => new BlockDto
{
.Select(x => new BlockDto
{
Id = x.Id,
Name = x.Name,
AssignedTo = x.AssignedTo,
Expand Down Expand Up @@ -289,6 +291,44 @@
await JS.InvokeVoidAsync("downloadFileFromStream", "UserData.zip", streamRef);
}

private async Task DownloadClaimedInitItemsAsync()
{
if (currentDiscordUser is null)
{
return;
}

await using var db = DbFactory.CreateDbContext();

var claimedBlocks = await db.Blocks
.Where(x => x.AssignedToId == currentDiscordUser.UserId && !x.IsDone)
.Include(x => x.Items)
.ToListAsync();

await using var ms = new MemoryStream();

using (var zip = new System.IO.Compression.ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
{
foreach (var block in claimedBlocks)
{
foreach (var item in block.Items)
{
var entry = zip.CreateEntry(Path.Combine("ClaimedInits", block.Name, item.FileName));
await using var entryStream = entry.Open();

var blockDirBasePath = Path.Combine(AppContext.BaseDirectory, "Data", "items", "NC2", block.CategoryId, block.SubCategoryId, "MM_Collision", block.EnvironmentId, block.PageName, block.Name);
await using var fs = new FileStream(Path.Combine(blockDirBasePath, item.FileName), FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true);
await fs.CopyToAsync(entryStream);
}
}
}

ms.Position = 0;

using var streamRef = new DotNetStreamReference(ms);
await JS.InvokeVoidAsync("downloadFileFromStream", "ClaimedInits.zip", streamRef);
}

private async Task DownloadValueReviewItemsAsync()
{
await using var db = DbFactory.CreateDbContext();
Expand Down

0 comments on commit 4e65103

Please sign in to comment.