feat: json format, et al.

This commit is contained in:
Subarashimo
2025-12-03 14:55:30 +01:00
parent 56349f30e6
commit 0f7fdfcef1
28 changed files with 5692 additions and 237 deletions
+63 -4
View File
@@ -31,10 +31,36 @@ export function updateInventoryItem(field, index, newName, location) {
return;
}
// Get current items for the field
// Get the OLD item name before updating (for skill link updates)
let oldItemName = null;
if (extensionSettings.inventoryV3) {
let structuredArray = null;
if (field === 'simplified' && extensionSettings.inventoryV3.simplified) {
structuredArray = extensionSettings.inventoryV3.simplified;
} else if (field === 'stored' && extensionSettings.inventoryV3.stored?.[location]) {
structuredArray = extensionSettings.inventoryV3.stored[location];
} else if (field === 'onPerson' && extensionSettings.inventoryV3.onPerson) {
structuredArray = extensionSettings.inventoryV3.onPerson;
} else if (field === 'assets' && extensionSettings.inventoryV3.assets) {
structuredArray = extensionSettings.inventoryV3.assets;
}
if (structuredArray && structuredArray[index]) {
const item = structuredArray[index];
oldItemName = typeof item === 'string' ? item : item.name;
// Update the structured item
if (typeof item === 'object') {
item.name = sanitizedName;
} else {
structuredArray[index] = sanitizedName;
}
}
}
// Get current items for the legacy format
let currentString;
if (field === 'simplified') {
// For simplified inventory, use items field or fall back to onPerson
currentString = inventory.items || inventory.onPerson || 'None';
} else if (field === 'stored') {
if (!location) {
@@ -55,15 +81,19 @@ export function updateInventoryItem(field, index, newName, location) {
return;
}
// Get old name from legacy format if not found in structured format
if (!oldItemName) {
oldItemName = items[index];
}
// Update the item at this index
items[index] = sanitizedName;
// Serialize back to string
const newItemString = serializeItems(items);
// Update the inventory
// Update the legacy inventory
if (field === 'simplified') {
// Update both items and onPerson for simplified mode
inventory.items = newItemString;
inventory.onPerson = newItemString;
} else if (field === 'stored') {
@@ -72,6 +102,11 @@ export function updateInventoryItem(field, index, newName, location) {
inventory[field] = newItemString;
}
// Update skill links if the item name changed
if (oldItemName && oldItemName !== sanitizedName && extensionSettings.skillAbilityLinks) {
updateSkillLinksForRenamedItem(oldItemName, sanitizedName);
}
// Update lastGeneratedData and committedTrackerData with new inventory
updateLastGeneratedDataInventory();
@@ -84,6 +119,30 @@ export function updateInventoryItem(field, index, newName, location) {
renderInventory();
}
/**
* Updates skill-ability links when an inventory item is renamed
* @param {string} oldName - The old item name
* @param {string} newName - The new item name
*/
function updateSkillLinksForRenamedItem(oldName, newName) {
if (!extensionSettings.skillAbilityLinks) return;
const oldNameLower = oldName.toLowerCase().trim();
let updated = false;
for (const [key, linkedItem] of Object.entries(extensionSettings.skillAbilityLinks)) {
// Case-insensitive comparison to match the linking logic
if (linkedItem && linkedItem.toLowerCase().trim() === oldNameLower) {
extensionSettings.skillAbilityLinks[key] = newName;
updated = true;
}
}
if (updated) {
console.log(`[RPG Companion] Updated skill links: "${oldName}" -> "${newName}"`);
}
}
/**
* Updates lastGeneratedData.userStats AND committedTrackerData.userStats to include
* current inventory in text format.