Refactor inventory lock logic to use item names
Updated inventory lock management and rendering to match items by name instead of index, improving reliability and consistency. Also adjusted quest rendering and parsing to handle locked quest objects with a value property.
This commit is contained in:
@@ -98,16 +98,19 @@ function applyUserStatsLocks(data, lockedItems) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock inventory items - handle bracket notation paths like "inventory.onPerson[0]"
|
// Lock inventory items - match by item name instead of index
|
||||||
if (data.inventory && lockedItems.inventory) {
|
if (data.inventory && lockedItems.inventory) {
|
||||||
// Helper function to parse bracket notation and apply lock
|
// Helper function to apply locks based on item name
|
||||||
const applyInventoryLocks = (items, category) => {
|
const applyInventoryLocks = (items, category) => {
|
||||||
if (!Array.isArray(items)) return items;
|
if (!Array.isArray(items)) return items;
|
||||||
|
if (!lockedItems.inventory[category]) return items;
|
||||||
|
|
||||||
return items.map((item, index) => {
|
return items.map((item) => {
|
||||||
// Check if this specific item is locked using bracket notation with inventory prefix
|
// Get item name (handle both string and object formats)
|
||||||
const bracketPath = `${category}[${index}]`;
|
const itemName = typeof item === 'string' ? item : (item.item || item.name || '');
|
||||||
if (lockedItems.inventory[bracketPath]) {
|
|
||||||
|
// Check if this specific item name is locked
|
||||||
|
if (lockedItems.inventory[category][itemName]) {
|
||||||
return typeof item === 'string'
|
return typeof item === 'string'
|
||||||
? { item, locked: true }
|
? { item, locked: true }
|
||||||
: { ...item, locked: true };
|
: { ...item, locked: true };
|
||||||
@@ -131,13 +134,13 @@ function applyUserStatsLocks(data, lockedItems) {
|
|||||||
data.inventory.assets = applyInventoryLocks(data.inventory.assets, 'assets');
|
data.inventory.assets = applyInventoryLocks(data.inventory.assets, 'assets');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply locks to stored items (nested structure with inventory.stored.location[index])
|
// Apply locks to stored items - match by item name
|
||||||
if (data.inventory.stored && lockedItems.inventory.stored) {
|
if (data.inventory.stored && lockedItems.inventory.stored) {
|
||||||
for (const location in data.inventory.stored) {
|
for (const location in data.inventory.stored) {
|
||||||
if (Array.isArray(data.inventory.stored[location])) {
|
if (Array.isArray(data.inventory.stored[location]) && lockedItems.inventory.stored[location]) {
|
||||||
data.inventory.stored[location] = data.inventory.stored[location].map((item, index) => {
|
data.inventory.stored[location] = data.inventory.stored[location].map((item) => {
|
||||||
const bracketPath = `${location}[${index}]`;
|
const itemName = typeof item === 'string' ? item : (item.item || item.name || '');
|
||||||
if (lockedItems.inventory.stored[bracketPath]) {
|
if (lockedItems.inventory.stored[location][itemName]) {
|
||||||
return typeof item === 'string'
|
return typeof item === 'string'
|
||||||
? { item, locked: true }
|
? { item, locked: true }
|
||||||
: { ...item, locked: true };
|
: { ...item, locked: true };
|
||||||
|
|||||||
@@ -617,6 +617,8 @@ export function parseUserStats(statsText) {
|
|||||||
if (!quest) return '';
|
if (!quest) return '';
|
||||||
if (typeof quest === 'string') return quest;
|
if (typeof quest === 'string') return quest;
|
||||||
if (typeof quest === 'object') {
|
if (typeof quest === 'object') {
|
||||||
|
// Check for locked format: {value, locked}
|
||||||
|
if (quest.value !== undefined) return String(quest.value);
|
||||||
// v3 format: {title, description, status}
|
// v3 format: {title, description, status}
|
||||||
return quest.title || quest.description || JSON.stringify(quest);
|
return quest.title || quest.description || JSON.stringify(quest);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ export function renderOnPersonView(onPersonItems, viewMode = 'list') {
|
|||||||
if (viewMode === 'grid') {
|
if (viewMode === 'grid') {
|
||||||
// Grid view: card-style items
|
// Grid view: card-style items
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.onPerson[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.onPerson.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-card" data-field="onPerson" data-index="${index}">
|
<div class="rpg-item-card" data-field="onPerson" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -94,7 +94,7 @@ export function renderOnPersonView(onPersonItems, viewMode = 'list') {
|
|||||||
} else {
|
} else {
|
||||||
// List view: full-width rows
|
// List view: full-width rows
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.onPerson[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.onPerson.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-row" data-field="onPerson" data-index="${index}">
|
<div class="rpg-item-row" data-field="onPerson" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -163,7 +163,7 @@ export function renderClothingView(clothingItems, viewMode = 'list') {
|
|||||||
if (viewMode === 'grid') {
|
if (viewMode === 'grid') {
|
||||||
// Grid view: card-style items
|
// Grid view: card-style items
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.clothing[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.clothing.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-card" data-field="clothing" data-index="${index}">
|
<div class="rpg-item-card" data-field="clothing" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -176,7 +176,7 @@ export function renderClothingView(clothingItems, viewMode = 'list') {
|
|||||||
} else {
|
} else {
|
||||||
// List view: full-width rows
|
// List view: full-width rows
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.clothing[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.clothing.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-row" data-field="clothing" data-index="${index}">
|
<div class="rpg-item-row" data-field="clothing" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -291,7 +291,7 @@ export function renderStoredView(stored, collapsedLocations = [], viewMode = 'li
|
|||||||
if (viewMode === 'grid') {
|
if (viewMode === 'grid') {
|
||||||
// Grid view: card-style items
|
// Grid view: card-style items
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.stored.${location}[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.stored.${location}.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-card" data-field="stored" data-location="${escapeHtml(location)}" data-index="${index}">
|
<div class="rpg-item-card" data-field="stored" data-location="${escapeHtml(location)}" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -304,7 +304,7 @@ export function renderStoredView(stored, collapsedLocations = [], viewMode = 'li
|
|||||||
} else {
|
} else {
|
||||||
// List view: full-width rows
|
// List view: full-width rows
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.stored.${location}[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.stored.${location}.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-row" data-field="stored" data-location="${escapeHtml(location)}" data-index="${index}">
|
<div class="rpg-item-row" data-field="stored" data-location="${escapeHtml(location)}" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -393,7 +393,7 @@ export function renderAssetsView(assets, viewMode = 'list') {
|
|||||||
if (viewMode === 'grid') {
|
if (viewMode === 'grid') {
|
||||||
// Grid view: card-style items
|
// Grid view: card-style items
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.assets[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.assets.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-card" data-field="assets" data-index="${index}">
|
<div class="rpg-item-card" data-field="assets" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
@@ -406,7 +406,7 @@ export function renderAssetsView(assets, viewMode = 'list') {
|
|||||||
} else {
|
} else {
|
||||||
// List view: full-width rows
|
// List view: full-width rows
|
||||||
itemsHtml = items.map((item, index) => {
|
itemsHtml = items.map((item, index) => {
|
||||||
const lockIconHtml = getLockIconHtml('userStats', `inventory.assets[${index}]`);
|
const lockIconHtml = getLockIconHtml('userStats', `inventory.assets.${item}`);
|
||||||
return `
|
return `
|
||||||
<div class="rpg-item-row" data-field="assets" data-index="${index}">
|
<div class="rpg-item-row" data-field="assets" data-index="${index}">
|
||||||
${lockIconHtml}
|
${lockIconHtml}
|
||||||
|
|||||||
@@ -212,8 +212,11 @@ export function renderQuests() {
|
|||||||
// Get current sub-tab from container or default to 'main'
|
// Get current sub-tab from container or default to 'main'
|
||||||
const activeSubTab = $questsContainer.data('active-subtab') || 'main';
|
const activeSubTab = $questsContainer.data('active-subtab') || 'main';
|
||||||
|
|
||||||
// Get quests data
|
// Get quests data - extract value if it's a locked object
|
||||||
const mainQuest = extensionSettings.quests.main || 'None';
|
let mainQuest = extensionSettings.quests.main || 'None';
|
||||||
|
if (typeof mainQuest === 'object' && mainQuest.value !== undefined) {
|
||||||
|
mainQuest = mainQuest.value;
|
||||||
|
}
|
||||||
const optionalQuests = extensionSettings.quests.optional || [];
|
const optionalQuests = extensionSettings.quests.optional || [];
|
||||||
|
|
||||||
// Build HTML
|
// Build HTML
|
||||||
|
|||||||
Reference in New Issue
Block a user