Skip to content
Open
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
53 changes: 53 additions & 0 deletions StarWars5e.Api/Controllers/FightingStrategyController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos.Table;
using StarWars5e.Api.Storage;
using StarWars5e.Models;
using StarWars5e.Models.Enums;

namespace StarWars5e.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FightingStrategyController : ControllerBase
{
private readonly IAzureTableStorage _tableStorage;

public FightingStrategyController(IAzureTableStorage tableStorage)
{
_tableStorage = tableStorage;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<FightingStrategy>>> Get(Language language = Language.en)
{
List<FightingStrategy> FightingStrategy;
try
{
FightingStrategy = (await _tableStorage.GetAllAsync<FightingStrategy>($"fightingStrategies{language}")).ToList();
}
catch (StorageException e)
{
if (e.Message == "Not Found")
{
FightingStrategy = (await _tableStorage.GetAllAsync<FightingStrategy>($"fightingStrategies{Language.en}")).ToList();
return Ok(FightingStrategy);
}
throw;
}
return Ok(FightingStrategy);
}

//[HttpPost]
//public void Post([FromBody] Feat feat)
//{
//}

//[HttpDelete("{name}")]
//public void Delete(string name)
//{
//}
}
}
43 changes: 43 additions & 0 deletions StarWars5e.Api/Controllers/LanguagesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos.Table;
using StarWars5e.Api.Storage;
using StarWars5e.Models;
using StarWars5e.Models.Enums;

namespace StarWars5e.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class LanguagesController : ControllerBase
{
private readonly IAzureTableStorage _tableStorage;

public LanguagesController(IAzureTableStorage tableStorage)
{
_tableStorage = tableStorage;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<CommonLanguage>>> Get(Language language = Language.en)
{
List<CommonLanguage> languages;
try
{
languages = (await _tableStorage.GetAllAsync<CommonLanguage>($"languages{language}")).ToList();
}
catch (StorageException e)
{
if (e.Message == "Not Found")
{
languages = (await _tableStorage.GetAllAsync<CommonLanguage>($"languages{Language.en}")).ToList();
return Ok(languages);
}
throw;
}
return Ok(languages);
}
}
}
5 changes: 3 additions & 2 deletions StarWars5e.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ public void ConfigureServices(IServiceCollection services)
});
});

var searchEndpoint = Configuration["SearchEndpoint"] ?? "https://sw5esearch.search.windows.net";
var tableStorage = new AzureTableStorage(Configuration["StorageAccountConnectionString"]);
var cloudStorageAccount = CloudStorageAccount.Parse(Configuration["StorageAccountConnectionString"]);
var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
var cloudBlobClient = new BlobServiceClient(Configuration["StorageAccountConnectionString"]);
var searchIndexClient = new SearchIndexClient(new Uri("https://sw5esearch.search.windows.net"), new AzureKeyCredential(Configuration["SearchKey"]));
var searchClient = new SearchClient(new Uri("https://sw5esearch.search.windows.net"), "searchterms-index", new AzureKeyCredential(Configuration["SearchKey"]));
var searchIndexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(Configuration["SearchKey"]));
var searchClient = new SearchClient(new Uri(searchEndpoint), "searchterms-index", new AzureKeyCredential(Configuration["SearchKey"]));

services.AddSingleton<IAzureTableStorage>(tableStorage);

Expand Down
10 changes: 10 additions & 0 deletions StarWars5e.Models/CommonLanguage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace StarWars5e.Models
{
public class CommonLanguage : BaseEntity
{
public string Name { get; set; }
public string Text { get; set; }
public string Metadata { get; set; }
}

}
3 changes: 2 additions & 1 deletion StarWars5e.Models/Enums/GlobalSearchTermType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public enum GlobalSearchTermType
SplashclassImprovement,
WeaponFocus,
WeaponSupremacy,
Maneuver
Maneuver,
FightingStrategy
}
}
2 changes: 2 additions & 0 deletions StarWars5e.Models/Feat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace StarWars5e.Models
{
public class Feat : BaseEntity
{
public Feat() { }
public string Name { get; set; }
public string Prerequisite { get; set; }
public string Text { get; set; }
public string Metadata { get; set; }
public List<string> AttributesIncreased { get; set; }
public string AttributesIncreasedJson
{
Expand Down
9 changes: 9 additions & 0 deletions StarWars5e.Models/FightingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace StarWars5e.Models
{
public class FightingStrategy : BaseEntity
{
public string Name { get; set; }
public string Text { get; set; }
public string Metadata { get; set; }
}
}
19 changes: 19 additions & 0 deletions StarWars5e.Models/Utils/StringExtentions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text;
using System.Text.RegularExpressions;

namespace StarWars5e.Models.Utils
{
public static class StringExtentions
{
public static string MinifyJson(this string json)
{
var options =
new JsonWriterOptions
{
Indented = false,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
using var document = JsonDocument.Parse(json);
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, options);
document.WriteTo(writer);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
public static bool HasLeadingHtmlWhitespace(this string input)
{
return input.Trim().StartsWith("&emsp;") || input.Trim().StartsWith("&nbsp;");
Expand Down
37 changes: 37 additions & 0 deletions StarWars5e.Parser/Managers/PlayerHandbookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,20 @@ await _tableStorage.AddBatchAsync<ArmorProperty>($"armorProperties{_localization
Console.WriteLine("Failed to upload PHB armor properties.");
}

try
{
var languages = await new PlayerHandbookLanguagesProcessor()
.Process(_phbFilesNames.Where(p => p.Equals("PHB.phb_04.txt")).ToList(), _localization);

await _tableStorage.AddBatchAsync<CommonLanguage>($"languages{_localization.Language}", languages,
new BatchOperationOptions { BatchInsertMethod = BatchInsertMethod.InsertOrReplace });
}
catch (StorageException)
{
Console.WriteLine("Failed to upload Common Languages.");
}


try
{
var maneuvers =
Expand All @@ -536,6 +549,30 @@ await _tableStorage.AddBatchAsync<Maneuver>($"maneuvers{_localization.Language}"
Console.WriteLine("Failed to upload PHB maneuvers.");
}


try
{
var fightingStrategies =
await new ExpandedContentFightingStrategiesProcessor(_localization).Process(_phbFilesNames.Where(p => p.Equals("PHB.phb_03.txt")).ToList(), _localization, ContentType.Core);

foreach (var strategy in fightingStrategies)
{
strategy.ContentSourceEnum = ContentSource.PHB;

var strategySearchTerm = _globalSearchTermRepository.CreateSearchTerm(strategy.Name,
GlobalSearchTermType.FightingStrategy, ContentType.Core,
$"/classes/fighter");
_globalSearchTermRepository.SearchTerms.Add(strategySearchTerm);
}

await _tableStorage.AddBatchAsync<FightingStrategy>($"fightingStrategies{_localization.Language}", fightingStrategies,
new BatchOperationOptions { BatchInsertMethod = BatchInsertMethod.InsertOrReplace });
}
catch (StorageException e)
{
Console.WriteLine("Failed to upload PHB fighting strategies.");
}

foreach (var referenceName in ReferenceNames)
{
var referenceSearchTerm = _globalSearchTermRepository.CreateSearchTerm(referenceName.name, referenceName.globalSearchTermType, ContentType.Core,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using StarWars5e.Models;
using StarWars5e.Models.Enums;
using StarWars5e.Models.Utils;
using StarWars5e.Parser.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarWars5e.Parser.Processors
{
public class ExpandedContentFightingStrategiesProcessor : BaseProcessor<FightingStrategy>
{
public ExpandedContentFightingStrategiesProcessor(ILocalization localization)
{
Localization = localization;
}

public override Task<List<FightingStrategy>> FindBlocks(List<string> lines, ContentType contentType)
{
var strategies = new List<FightingStrategy>();
lines = lines.CleanListOfStrings().ToList();
var strategyStartLines = lines.Where(f => f.StartsWith($"#### ") && f.EndsWith($"Strategist"));

foreach (var strategyStartLine in strategyStartLines)
{
var strategyStartIndex = lines.IndexOf(strategyStartLine);

var strategyEndIndex = lines.FindIndex(strategyStartIndex + 1, f => f.StartsWith("#"));
var strategyLines = lines.Skip(strategyStartIndex);

if (strategyEndIndex != -1)
{
strategyLines = lines.Skip(strategyStartIndex).Take(strategyEndIndex - strategyStartIndex);
}

strategies.Add(ParseStrategy(strategyLines.CleanListOfStrings().ToList(), contentType));
}

return Task.FromResult(strategies);
}

public FightingStrategy ParseStrategy(List<string> strategyLines, ContentType contentType)
{
var name = strategyLines[0].Split("####")[1].Trim();

try
{
var strategy = new FightingStrategy
{
RowKey = name.FormatKey(),
Name = name,
PartitionKey = contentType.ToString(),
ContentTypeEnum = contentType
};

strategy.Metadata = MetadataProcessor.ProcessMetadata(strategy);
strategy.Text = string.Join(Environment.NewLine, strategyLines.Skip(1));

return strategy;
}
catch (Exception e)
{
throw new Exception($"Failed while parsing fighting strategy {name}", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public Species ParseSpecies(List<string> speciesLines, ContentType contentType)

foreach (var speciesTrait in species.Traits)
{
//var metadata = ReadMetadata($"features.{Enum.GetName(FeatureSource.Species)}.{name.Replace(" ", "")}.{speciesTrait.Name.Replace(" ", "")}");
var feature = new Feature
{
Name = speciesTrait.Name,
Expand All @@ -122,6 +123,7 @@ public Species ParseSpecies(List<string> speciesLines, ContentType contentType)
SourceEnum = FeatureSource.Species,
PartitionKey = contentType.ToString()
};
feature.Metadata = MetadataProcessor.ProcessMetadata(feature);
species.Features.Add(feature);
}

Expand Down
Loading