Skip to content

Commit

Permalink
Implemented item inventories, additional node condition, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Nörtershäuser committed Mar 3, 2023
1 parent 2b1cde1 commit 84c6045
Show file tree
Hide file tree
Showing 132 changed files with 2,470 additions and 897 deletions.
5 changes: 5 additions & 0 deletions Config/MiscConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ public class MiscConfig
/// True if all fields can have script settings
/// </summary>
public bool? AllowScriptSettingsForAllFieldTypes { get; set; }

/// <summary>
/// True if item inventors must be disabled
/// </summary>
public bool? DisableItemInventory { get; set; }
}
}
53 changes: 52 additions & 1 deletion Controllers/Api/StyrApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
using GoNorth.Services.Export.ExportSnippets;
using GoNorth.Data.Evne;
using GoNorth.Data.StateMachines;
using Microsoft.Extensions.Options;
using GoNorth.Config;

namespace GoNorth.Controllers.Api
{
Expand Down Expand Up @@ -162,6 +164,11 @@ public class StyrApiController : FlexFieldBaseApiController<StyrItem>
/// </summary>
private readonly IKortistoNpcTemplateDbAccess _npcTemplateDbAccess;

/// <summary>
/// True if item inventory is disabled
/// </summary>
private readonly bool _disableItemInventory;

/// <summary>
/// Constructor
/// </summary>
Expand Down Expand Up @@ -195,11 +202,12 @@ public class StyrApiController : FlexFieldBaseApiController<StyrItem>
/// <param name="xssChecker">Xss Checker</param>
/// <param name="logger">Logger</param>
/// <param name="localizerFactory">Localizer Factory</param>
/// <param name="configuration">Config Data</param>
public StyrApiController(IStyrFolderDbAccess folderDbAccess, IStyrItemTemplateDbAccess templateDbAccess, IStyrItemDbAccess itemDbAccess, IStyrItemTagDbAccess tagDbAccess, IExportTemplateDbAccess exportTemplateDbAccess, IStyrImportFieldValuesLogDbAccess importFieldValuesLogDbAccess,
ILanguageKeyDbAccess languageKeyDbAccess, IExportFunctionIdDbAccess exportFunctionIdDbAccess, IObjectExportSnippetDbAccess objectExportSnippetDbAccess, IObjectExportSnippetSnapshotDbAccess objectExportSnippetSnapshotDbAccess, IExportSnippetRelatedObjectNameResolver exportSnippetRelatedObjectNameResolver,
IStateMachineDbAccess stateMachineDbAccess, IStyrItemImageAccess imageAccess, IStyrThumbnailService thumbnailService, IAikaQuestDbAccess aikaQuestDbAccess, IEvneSkillDbAccess skillDbAccess, ITaleDbAccess taleDbAccess, IKirjaPageDbAccess kirjaPageDbAccess, IKartaMapDbAccess kartaMapDbAccess,
IKortistoNpcDbAccess kortistoNpcDbAccess, IKortistoNpcTemplateDbAccess npcTemplateDbAccess, IUserProjectAccess userProjectAccess, ICsvGenerator csvGenerator, ICsvParser csvReader, UserManager<GoNorthUser> userManager, IImplementationStatusComparer implementationStatusComparer, ITimelineService timelineService,
IXssChecker xssChecker, ILogger<StyrApiController> logger, IStringLocalizerFactory localizerFactory)
IXssChecker xssChecker, ILogger<StyrApiController> logger, IStringLocalizerFactory localizerFactory, IOptions<ConfigurationData> configuration)
: base(folderDbAccess, templateDbAccess, itemDbAccess, tagDbAccess, exportTemplateDbAccess, importFieldValuesLogDbAccess, languageKeyDbAccess, exportFunctionIdDbAccess, objectExportSnippetDbAccess, objectExportSnippetSnapshotDbAccess, exportSnippetRelatedObjectNameResolver, stateMachineDbAccess, userProjectAccess, imageAccess,
thumbnailService, csvGenerator, csvReader, userManager, implementationStatusComparer, timelineService, xssChecker, logger, localizerFactory)
{
Expand All @@ -210,6 +218,7 @@ public StyrApiController(IStyrFolderDbAccess folderDbAccess, IStyrItemTemplateDb
_kartaMapDbAccess = kartaMapDbAccess;
_kortistoNpcDbAccess = kortistoNpcDbAccess;
_npcTemplateDbAccess = npcTemplateDbAccess;
_disableItemInventory = configuration.Value.Misc.DisableItemInventory.HasValue ? configuration.Value.Misc.DisableItemInventory.Value : false;
}

/// <summary>
Expand Down Expand Up @@ -283,6 +292,20 @@ public async Task<IActionResult> FlexFieldTemplateImageUpload(string id)
return await BaseFlexFieldTemplateImageUpload(id);
}

/// <summary>
/// Strips an object based on the rights of a user
/// </summary>
/// <param name="flexFieldObject">Flex field object to strip</param>
/// <returns>Stripped object</returns>
protected override StyrItem StripObject(StyrItem flexFieldObject)
{
if(_disableItemInventory)
{
flexFieldObject.Inventory = new List<StyrInventoryItem>();
}

return flexFieldObject;
}

/// <summary>
/// Checks if a object is referenced before a delete
Expand Down Expand Up @@ -351,9 +374,32 @@ protected override async Task<string> CheckObjectReferences(string id)
return _localizer["CanNotDeleteItemUsedInSkill", referencedInSkillsString].Value;
}

if(!_disableItemInventory)
{
List<StyrItem> referencedInItems = await ((IStyrItemDbAccess)_objectDbAccess).GetItemsByItemInInventory(id);
if(referencedInItems.Count > 0)
{
string referencedInItemsString = string.Join(", ", referencedInItems.Select(n => n.Name));
return _localizer["CanNotDeleteItemUsedInItemInventory", referencedInItemsString].Value;
}
}

return string.Empty;
}

/// <summary>
/// Returns the items which have an item in their inventory with only the main values
/// </summary>
/// <param name="itemId">Item id</param>
/// <returns>Items</returns>
[ProducesResponseType(typeof(List<StyrItem>), StatusCodes.Status200OK)]
[HttpGet]
public async Task<IActionResult> GetItemsByItemInInventory(string itemId)
{
List<StyrItem> items = await ((IStyrItemDbAccess)_objectDbAccess).GetItemsByItemInInventory(itemId);
return Ok(items);
}

/// <summary>
/// Deletes additional depencendies for a flex field object
/// </summary>
Expand All @@ -372,6 +418,11 @@ protected override Task DeleteAdditionalFlexFieldObjectDependencies(StyrItem fle
/// <returns>Updated flex field object</returns>
protected override Task<StyrItem> RunAdditionalUpdates(StyrItem flexFieldObject, StyrItem loadedFlexFieldObject)
{
if(!_disableItemInventory)
{
loadedFlexFieldObject.Inventory = flexFieldObject.Inventory;
}

return Task.FromResult(loadedFlexFieldObject);
}

Expand Down
21 changes: 20 additions & 1 deletion Controllers/ProjectConfigController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using GoNorth.Config;
using GoNorth.Models.ProjectConfigViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace GoNorth.Controllers
{
Expand All @@ -10,14 +13,30 @@ namespace GoNorth.Controllers
[ApiExplorerSettings(IgnoreApi = true)]
public class ProjectConfigController : Controller
{
/// <summary>
/// Misc config
/// </summary>
private readonly MiscConfig _config;

/// <summary>
/// Constructor
/// </summary>
/// <param name="configuration">Configuration</param>
public ProjectConfigController(IOptions<ConfigurationData> configuration)
{
_config = configuration.Value.Misc;
}

/// <summary>
/// Index view
/// </summary>
/// <returns>View</returns>
[HttpGet]
public IActionResult Index()
{
return View();
IndexViewModel viewModel = new IndexViewModel();
viewModel.DisableItemInventory = _config.DisableItemInventory.HasValue ? _config.DisableItemInventory.Value : false;
return View(viewModel);
}
}
}
4 changes: 3 additions & 1 deletion Controllers/StyrController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GoNorth.Config;
using GoNorth.Models.FlexFieldDatabaseModels;
using GoNorth.Models.StyrViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -44,9 +45,10 @@ public IActionResult Index()
[HttpGet]
public IActionResult Item()
{
DetailFormViewModel viewModel = new DetailFormViewModel();
ItemViewModel viewModel = new ItemViewModel();
viewModel.DisableAutoSaving = _config.DisableAutoSaving.HasValue ? _config.DisableAutoSaving.Value : false;
viewModel.AllowScriptSettingsForAllFieldTypes = _config.AllowScriptSettingsForAllFieldTypes.HasValue ? _config.AllowScriptSettingsForAllFieldTypes.Value : false;
viewModel.DisableItemInventory = _config.DisableItemInventory.HasValue ? _config.DisableItemInventory.Value : false;
return View(viewModel);
}

Expand Down
8 changes: 8 additions & 0 deletions Data/Styr/IStyrItemDbAccess.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GoNorth.Data.FlexFieldDatabase;

namespace GoNorth.Data.Styr
Expand All @@ -7,5 +9,11 @@ namespace GoNorth.Data.Styr
/// </summary>
public interface IStyrItemDbAccess : IFlexFieldObjectDbAccess<StyrItem>
{
/// <summary>
/// Returns the items which have an item in their inventory with only the main values
/// </summary>
/// <param name="itemId">Item id</param>
/// <returns>Npcs</returns>
Task<List<StyrItem>> GetItemsByItemInInventory(string itemId);
}
}
55 changes: 55 additions & 0 deletions Data/Styr/StyrInventoryItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Text.Json.Serialization;
using GoNorth.Services.ImplementationStatusCompare;

namespace GoNorth.Data.Styr
{
/// <summary>
/// Inventory Item
/// </summary>
public class StyrInventoryItem : IImplementationListComparable, ICloneable
{
/// <summary>
/// Item Id
/// </summary>
public string ItemId { get; set; }

/// <summary>
/// Quantity
/// </summary>
[ValueCompareAttribute]
public int Quantity { get; set; }

/// <summary>
/// Role of the item
/// </summary>
[ValueCompareAttribute]
public string Role { get; set; }


/// <summary>
/// Id which is used in a list compare to detect deleted or new objects
/// </summary>
[JsonIgnore]
public string ListComparableId { get { return ItemId; } }

/// <summary>
/// Value which is used in a list compare for display
/// </summary>
[JsonIgnore]
public CompareDifferenceValue ListComparableValue { get { return new CompareDifferenceValue(ItemId, CompareDifferenceValue.ValueResolveType.ResolveItemName); } }

/// <summary>
/// Clones the object
/// </summary>
/// <returns>Cloned object</returns>
public object Clone()
{
return new StyrInventoryItem {
ItemId = this.ItemId,
Quantity = this.Quantity,
Role = this.Role
};
}
}
}
14 changes: 13 additions & 1 deletion Data/Styr/StyrItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GoNorth.Data.FlexFieldDatabase;
using GoNorth.Services.Export.Placeholder;
using GoNorth.Services.ImplementationStatusCompare;

namespace GoNorth.Data.Styr
{
Expand All @@ -9,13 +12,22 @@ namespace GoNorth.Data.Styr
/// </summary>
public class StyrItem : FlexFieldObject, IExportSnippetExportable, ICloneable
{
/// <summary>
/// Inventory Items
/// </summary>
[ListCompareAttribute(LabelKey = "InventoryChanged")]
public List<StyrInventoryItem> Inventory { get; set; }

/// <summary>
/// Clones the item
/// </summary>
/// <returns>Cloned item</returns>
public object Clone()
{
return CloneObject<StyrItem>();
StyrItem clonedItem = CloneObject<StyrItem>();
clonedItem.Inventory = Inventory != null ? Inventory.Select(i => i.Clone()).Cast<StyrInventoryItem>().ToList() : null;

return clonedItem;
}
}
}
18 changes: 18 additions & 0 deletions Data/Styr/StyrItemMongoDbAccess.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GoNorth.Config;
using GoNorth.Data.FlexFieldDatabase;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace GoNorth.Data.Styr
{
Expand All @@ -26,5 +31,18 @@ public class StyrItemMongoDbAccess : FlexFieldObjectBaseMongoDbAccess<StyrItem>,
public StyrItemMongoDbAccess(IOptions<ConfigurationData> configuration) : base(StyrItemCollectionName, StyrItemRecyclingBinCollectionName, configuration)
{
}

/// <summary>
/// Returns the items which have an item in their inventory with only the main values
/// </summary>
/// <param name="itemId">Item id</param>
/// <returns>Npcs</returns>
public async Task<List<StyrItem>> GetItemsByItemInInventory(string itemId)
{
return await _ObjectCollection.AsQueryable().Where(n => n.Inventory != null && n.Inventory.Any(i => i.ItemId == itemId)).OrderBy(n => n.Name).Select(n => new StyrItem {
Id = n.Id,
Name = n.Name
}).ToListAsync();
}
}
}
13 changes: 13 additions & 0 deletions DefaultExportTemplates/Object/ObjectItem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@ function OnInit(this)
-- Values
this:add_localized_string_value("Name", "{{ langkey item.name }}")
{{ item.unused_fields | attribute_list }}
{{- if !inventory.empty? ~}}

-- Inventory
{{~ for cur_inventory_item in inventory ~}}
this:add_item("{{ cur_inventory_item.fields.ScriptName.value }}")
{{~ if cur_inventory_item.quantity > 1 ~}}
this:get_item({{ for.index }}):set_value("Quantity", {{ cur_inventory_item.quantity }})
{{~ end ~}}
{{~ if cur_inventory_item.role ~}}
this:get_item({{ for.index }}):set_role("{{ cur_inventory_item.role }}")
{{~ end ~}}
{{~ end ~}}
{{~ end ~}}
end
6 changes: 5 additions & 1 deletion DefaultExportTemplates/Tale/TaleConditionNpcInventory.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
BaseNpc_GetItemQuantityInInventory(this, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- if condition.is_equipped_check -}}
BaseNpc_GetEquippedItemQuantityInInventory(this, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- else -}}
BaseNpc_GetItemQuantityInInventory(this, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- end -}}
6 changes: 5 additions & 1 deletion DefaultExportTemplates/Tale/TaleConditionPlayerInventory.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
BaseNpc_GetItemQuantityInInventory(playerNpc, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- if condition.is_equipped_check -}}
BaseNpc_GetEquippedItemQuantityInInventory(playerNpc, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- else -}}
BaseNpc_GetItemQuantityInInventory(playerNpc, "{{ condition.selected_item.fields.ScriptName.value }}") {{ condition.operator }} {{ condition.quantity }}
{{- end -}}
2 changes: 1 addition & 1 deletion DefaultExportTemplates/Tale/TaleDialogFunction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ function {{ dialog_function.function_name }}(this)
{{~ end ~}}{{~ if (dialog_function.code | string.contains "dialogManager") || (dialog_function.code | string.contains "playerNpc") ~}}

{{~ end ~}}
{{ dialog_function.code | indent_multiline}}
{{ dialog_function.code | indent_multiline }}
end

12 changes: 6 additions & 6 deletions GoNorth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="CsvHelper" Version="30.0.0" />
<PackageReference Include="MongoDB.Bson" Version="2.18.0" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.18.0" />
<PackageReference Include="Scriban" Version="5.5.0" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="MongoDB.Bson" Version="2.19.0" />
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.19.0" />
<PackageReference Include="Scriban" Version="5.7.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion GoNorthVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class GoNorthVersion
/// <summary>
/// Current GoNorth Version
/// </summary>
public static readonly Version CurrentVersion = new Version(1, 9, 1, 0);
public static readonly Version CurrentVersion = new Version(1, 9, 1, 5);
};
}
Loading

0 comments on commit 84c6045

Please sign in to comment.