Skip to content

Commit

Permalink
More items and NPC types
Browse files Browse the repository at this point in the history
  • Loading branch information
qkmaxware committed Apr 29, 2021
1 parent 4c301bd commit cba268b
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 24 deletions.
6 changes: 3 additions & 3 deletions TrekSharp.AdventureTools/Pages/CreateCustomData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
Create custom resources for use within your own campaigns where the species, items, or npc types provided in the official rulebooks do not provide what you require.
</div>
<NavLink class="w3-button w3-block" href="new/custom/species" Match="NavLinkMatch.All">
Custom Species
Create Custom Species
</NavLink>
<NavLink class="w3-button w3-block" href="new/custom/npc" Match="NavLinkMatch.All">
Custom NPC Type
Create Custom NPC Type
</NavLink>
<NavLink class="w3-button w3-block" href="new/custom/item" Match="NavLinkMatch.All">
Custom Item
Create Custom Item
</NavLink>
</LCARS>
2 changes: 1 addition & 1 deletion TrekSharp.AdventureTools/Pages/CreateNpcType.razor
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
private IEnumerable<Species> AllSpeciesOptions => books.AllRulebooks.SelectMany(book => book.Value.Species);

protected override void OnInitialized() {
books.AddRulebook("User Created", new UserCreatedSpeciesRulebook(Data));
books.AddRulebook("Homebrew", new UserCreatedSpeciesRulebook(Data));
}

private NpcCharacter character = new NpcCharacter() {
Expand Down
22 changes: 19 additions & 3 deletions TrekSharp.AdventureTools/Pages/ManageEncounter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,41 @@

@code {
protected override void OnInitialized() {
books.AddRulebook("User Created", new UserCreatedSpeciesRulebook(Data));
books.AddRulebook("Homebrew", new UserCreatedSpeciesRulebook(Data));
// Load last loaded guid automatically
if (!string.IsNullOrEmpty(lastGuid)) {
var guid_encounter = Data.Encounters.Where(enc => enc.GUID == lastGuid).FirstOrDefault();
if (guid_encounter != null) {
this.encounter = guid_encounter;
StateHasChanged();
}
}
}

private bool ShowShips = false;
private static string lastGuid = null;
private Encounter encounter = null;
private void load(Encounter encounter) {
this.encounter = encounter ?? new Encounter();
lastGuid = this.encounter.GUID;
}
private void delete(Encounter encounter) {
Data.Encounters.Remove(encounter);
// if the deleted encounter is the selected one, deselect it
if (encounter == this.encounter) {
this.encounter = null;
}
}
private void create() {
encounter = new Encounter();
encounter.Title = "New Encounter";
save();
}
private void save() {
delete(this.encounter);
Data.Encounters.Add(this.encounter);
if (!Data.Encounters.Contains(this.encounter)) {
Data.Encounters.Add(this.encounter);
}
lastGuid = this.encounter?.GUID;
}
private float AlliedSpaceStrength => Data.Ships.Select(ship => ship.Scale).Sum() + encounter.AlliedSpaceStrength;
private float AlliedInfantryStrength => Data.Party.Count() + encounter.AlliedInfantryStrength;
Expand Down
31 changes: 26 additions & 5 deletions TrekSharp.AdventureTools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using TrekSharp.AdventureTools;

namespace TrekSharp.AdventureTools
{
public class Program
{
public static async Task Main(string[] args)
{
public class Program {

private static AppData data;
private static IJSRuntime runtime;

public static async Task Main(string[] args) {
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services.AddSingleton<AppData>();
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
var host = builder.Build();
var provider = host.Services;
data = provider.GetService<AppData>();
runtime = provider.GetService<IJSRuntime>();

await host.RunAsync();
}

[JSInvokable]
public static async Task DownloadAppData(AppData data = null) {
data = data ?? Program.data;
if (data == null || runtime == null)
return;

var filename = "adventure";
var json = System.Text.Json.JsonSerializer.Serialize(data);
byte[] file = System.Text.Encoding.UTF8.GetBytes(json);
await runtime.InvokeVoidAsync("BlazorDownloadFile", $"{filename}.json", "text/json", file);
}
}
}
2 changes: 1 addition & 1 deletion TrekSharp.AdventureTools/Shared/EquipmentDrawer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

protected override void OnInitialized() {
RulebookContainer books = new RulebookContainer();
books.AddRulebook("User Created", new UserCreatedSpeciesRulebook(Data));
books.AddRulebook("Homebrew", new UserCreatedSpeciesRulebook(Data));
items = books.AllRulebooks.SelectMany(book => book.Value.Items).OrderBy((item) => item.Name).ToList();
}

Expand Down
7 changes: 4 additions & 3 deletions TrekSharp.AdventureTools/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
Ship
</NavLink>
<NavLink class="w3-button w3-block" href="new/custom" Match="NavLinkMatch.All">
Custom Data
Homebrew Data
</NavLink>
<NavLink class="w3-button w3-block" href="new/grid" Match="NavLinkMatch.All">
Battle Grid
Expand Down Expand Up @@ -92,7 +92,7 @@
Ships
</NavLink>
<NavLink class="w3-button w3-block" href="manage/custom" Match="NavLinkMatch.All">
Custom Data
Homebrew Data
</NavLink>
<NavLink class="w3-button w3-block" href="manage/task/extended" Match="NavLinkMatch.All">
Extended Tasks
Expand Down Expand Up @@ -206,7 +206,8 @@
}

private async Task downloadDataJson() {
await Program.DownloadAppData(this.Data);
// Generate a text file
await this.DownloadJson("adventure", Data);
//await this.DownloadJson("adventure", Data);
}
}
2 changes: 1 addition & 1 deletion TrekSharp.AdventureTools/Shared/RulebookPicker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

protected override void OnInitialized() {
// Add the user created data to the rulebook picker
Rulebooks.AddRulebook("User Created", new UserCreatedSpeciesRulebook(data));
Rulebooks.AddRulebook("Homebrew", new UserCreatedSpeciesRulebook(data));
}

private void Toggle(string name) {
Expand Down
16 changes: 15 additions & 1 deletion TrekSharp.AdventureTools/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>TrekSharp Tools</title>
<title>TrekSharp</title>
<base href="/" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link rel="apple-touch-icon" sizes="120x120" href="apple-touch-icon.png">
Expand Down Expand Up @@ -32,13 +32,27 @@
<script src="_framework/blazor.webassembly.js"></script>
<script src="js/helpers.js"></script>
<script>
// Allow components to scroll to top
function resetScroll() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
// Prevent accidental leaving
window.onbeforeunload = function() {
return "Unsaved app data will be lost if you leave the page, are you sure?";
};
// Overload the save function
document.addEventListener("keydown", function(e) {
if ((window.navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) && e.keyCode == 83) {
e.preventDefault();
// Invoke the function to download the app's data
DotNet.invokeMethodAsync (
"TrekSharp.AdventureTools",
"DownloadAppData",
null
);
}
}, false);
</script>
</body>

Expand Down
10 changes: 7 additions & 3 deletions TrekSharp.AdventureTools/wwwroot/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ async function BlazorReadFileAsString(file) {

async function BlazorHandleFileSelect(input) {
var files = input.files;
var file = files[0];

return await BlazorReadFileAsString(file);
if (files.length > 0) {
var file = files[0];

return await BlazorReadFileAsString(file);
} else {
return "";
}
}
Loading

0 comments on commit cba268b

Please sign in to comment.