From db7354201b32ae16f578767f29471188058e7908 Mon Sep 17 00:00:00 2001 From: Shaun Date: Sat, 6 May 2023 10:28:52 +0200 Subject: [PATCH] bug #1052 --- .../tooltipItem/buildTooltip/lineBuilders.js | 2 +- src/client/ui/templates/trade/trade.js | 2 +- src/server/clientComponents/inventory.js | 15 ++-- src/server/clientComponents/reputation.js | 4 ++ src/server/components/inventory.js | 70 +++++-------------- .../components/inventory/simplifyItem.js | 37 ++++++++++ src/server/components/mob.js | 2 +- src/server/components/reputation.js | 40 +++-------- src/server/config/factions/helpers.js | 30 ++++++++ 9 files changed, 108 insertions(+), 94 deletions(-) create mode 100644 src/server/components/inventory/simplifyItem.js create mode 100644 src/server/config/factions/helpers.js diff --git a/src/client/ui/templates/tooltipItem/buildTooltip/lineBuilders.js b/src/client/ui/templates/tooltipItem/buildTooltip/lineBuilders.js index b9fd1286..0f9b2e29 100644 --- a/src/client/ui/templates/tooltipItem/buildTooltip/lineBuilders.js +++ b/src/client/ui/templates/tooltipItem/buildTooltip/lineBuilders.js @@ -322,7 +322,7 @@ define([ item.factions.forEach((f, i) => { let htmlF = f.name + ': ' + f.tierName; - if (f.noEquip) + if (f.tier > window.player.reputation.getTier(f.id)) htmlF = '' + htmlF + ''; htmlFactions += htmlF; diff --git a/src/client/ui/templates/trade/trade.js b/src/client/ui/templates/trade/trade.js index 9660c09a..b06558a2 100644 --- a/src/client/ui/templates/trade/trade.js +++ b/src/client/ui/templates/trade/trade.js @@ -93,7 +93,7 @@ define([ noAfford = (~~(item.worth * this.itemList.markup) > window.player.trade.gold); if (!noAfford && item.factions) - noAfford = item.factions.some(f => f.noEquip); + noAfford = item.factions.some(f => f.tier > window.player.reputation.getTier(f.id)); if (noAfford) $('
').appendTo(itemEl); diff --git a/src/server/clientComponents/inventory.js b/src/server/clientComponents/inventory.js index f4fa78d6..9fac5742 100644 --- a/src/server/clientComponents/inventory.js +++ b/src/server/clientComponents/inventory.js @@ -59,19 +59,18 @@ define([ }, equipItemErrors: function (item) { - let errors = []; - let stats = this.obj.stats.values; + const { obj: { reputation, stats: { values: statValues } } } = this; - if (item.level > stats.level) + const errors = []; + + if (item.level > statValues.level) errors.push('level'); - if (item.requires && item.requires[0] && stats[item.requires[0].stat] < item.requires[0].value) + if (item.requires && item.requires[0] && statValues[item.requires[0].stat] < item.requires[0].value) errors.push('stats'); - if (item.factions) { - if (item.factions.some(f => f.noEquip)) - errors.push('faction'); - } + if (item.factions?.some(f => reputation.getTier(f.id) < f.tier)) + errors.push('faction'); return errors; }, diff --git a/src/server/clientComponents/reputation.js b/src/server/clientComponents/reputation.js index 6f9f20c2..caac29ac 100644 --- a/src/server/clientComponents/reputation.js +++ b/src/server/clientComponents/reputation.js @@ -29,6 +29,10 @@ define([ events.emit('onGetReputations', this.list); } + }, + + getTier: function (factionId) { + return this.factions.find(f => f.id === factionId)?.tier ?? 3; } }; }); diff --git a/src/server/components/inventory.js b/src/server/components/inventory.js index 5a0b6e33..8b281ab6 100644 --- a/src/server/components/inventory.js +++ b/src/server/components/inventory.js @@ -1,16 +1,21 @@ +//System +const events = require('../misc/events'); + +//External Helpers let generator = require('../items/generator'); let salvager = require('../items/salvager'); let classes = require('../config/spirits'); let factions = require('../config/factions'); let itemEffects = require('../items/itemEffects'); -const events = require('../misc/events'); - -const { isItemStackable } = require('./inventory/helpers'); +//Helpers +const simplifyItem = require('./inventory/simplifyItem'); const getItem = require('./inventory/getItem'); const dropBag = require('./inventory/dropBag'); const useItem = require('./inventory/useItem'); +const { isItemStackable } = require('./inventory/helpers'); +//Component module.exports = { type: 'inventory', @@ -91,44 +96,7 @@ module.exports = { }, simplifyItem: function (item) { - let result = extend({}, item); - - if (result.effects) { - result.effects = result.effects.map(e => ({ - factionId: e.factionId || null, - text: e.text || null, - properties: e.properties || null, - type: e.type || null, - rolls: e.rolls || null - })); - } - - let reputation = this.obj.reputation; - if (result.factions) { - result.factions = result.factions.map(function (f) { - let res = { - id: f.id, - tier: f.tier, - tierName: ['Hated', 'Hostile', 'Unfriendly', 'Neutral', 'Friendly', 'Honored', 'Revered', 'Exalted'][f.tier] - }; - - if (reputation) { - let faction = reputation.getBlueprint(f.id); - let factionTier = reputation.getTier(f.id); - - let noEquip = null; - if (factionTier < f.tier) - noEquip = true; - - res.name = faction.name; - res.noEquip = noEquip; - } - - return res; - }, this); - } - - return result; + return simplifyItem(this, item); }, update: function () { @@ -722,25 +690,21 @@ module.exports = { }, equipItemErrors: function (item) { - let errors = []; + const { obj: { player, stats: { values: statValues }, reputation } } = this; - if (!this.obj.player) - return []; + const errors = []; - let stats = this.obj.stats.values; + if (!player) + return errors; - if (item.level > stats.level) + if (item.level > statValues.level) errors.push('level'); - if ((item.requires) && (stats[item.requires[0].stat] < item.requires[0].value)) + if (item.requires && statValues[item.requires[0].stat] < item.requires[0].value) errors.push(item.requires[0].stat); - if (item.factions) { - if (item.factions.some(function (f) { - return f.noEquip; - })) - errors.push('faction'); - } + if (item.factions?.some(f => reputation.getTier(f.id) < f.tier)) + errors.push('faction'); return errors; }, diff --git a/src/server/components/inventory/simplifyItem.js b/src/server/components/inventory/simplifyItem.js new file mode 100644 index 00000000..4c61ed34 --- /dev/null +++ b/src/server/components/inventory/simplifyItem.js @@ -0,0 +1,37 @@ +//Helpers +const { getFactionBlueprint } = require('../../config/factions/helpers'); + +//Internals +const tierNames = ['Hated', 'Hostile', 'Unfriendly', 'Neutral', 'Friendly', 'Honored', 'Revered', 'Exalted']; + +//Method +const simplifyItem = (cpnInventory, item) => { + const result = extend({}, item); + + if (result.effects) { + result.effects = result.effects.map(e => ({ + factionId: e.factionId ?? null, + text: e.text ?? null, + properties: e.properties ?? null, + type: e.type ?? null, + rolls: e.rolls ?? null + })); + } + + if (result.factions) { + result.factions = result.factions.map(f => { + const res = { + id: f.id, + tier: f.tier, + tierName: tierNames[f.tier], + name: getFactionBlueprint(f.id).name + }; + + return res; + }); + } + + return result; +}; + +module.exports = simplifyItem; diff --git a/src/server/components/mob.js b/src/server/components/mob.js index 0b39a546..d8e5a5b0 100644 --- a/src/server/components/mob.js +++ b/src/server/components/mob.js @@ -144,7 +144,7 @@ module.exports = { //Are we too far from home? let distanceFromHome = Math.max(abs(this.originX - obj.x), abs(this.originY - obj.y)); - if (distanceFromHome > this.maxChaseDistance || (distanceFromHome > this.walkDistance && !target)) + if (distanceFromHome > this.maxChaseDistance || (distanceFromHome > this.walkDistance && !target && !this.patrol)) this.goHome = true; else if (target && target !== obj && (!obj.follower || obj.follower.master !== target)) { //If we just started attacking, patrols need to know where home is diff --git a/src/server/components/reputation.js b/src/server/components/reputation.js index 1d6369c4..dae24453 100644 --- a/src/server/components/reputation.js +++ b/src/server/components/reputation.js @@ -1,6 +1,7 @@ -let factionBase = require('../config/factionBase'); -let factions = require('../config/factions'); +//Helpers +const { getFactionBlueprint } = require('../config/factions/helpers'); +//Component module.exports = { type: 'reputation', @@ -13,7 +14,7 @@ module.exports = { delete blueprint.list; list.forEach(function (l) { - let bpt = this.getBlueprint(l.id); + let bpt = getFactionBlueprint(l.id); if (!bpt) return; @@ -27,25 +28,6 @@ module.exports = { }, this); }, - getBlueprint: function (factionId) { - if (this.factions[factionId]) - return this.factions[factionId]; - - let factionBlueprint = null; - try { - factionBlueprint = factions.getFaction(factionId); - } catch (e) {} - - if (!factionBlueprint) - return; - - factionBlueprint = extend({}, factionBase, factionBlueprint); - - this.factions[factionBlueprint.id] = factionBlueprint; - - return factionBlueprint; - }, - getTier: function (factionId) { let faction = this.list.find(l => l.id === factionId); if (!faction) { @@ -53,9 +35,7 @@ module.exports = { faction = this.list.find(l => l.id === factionId); } - return (faction || { - tier: 3 - }).tier; + return faction?.tier ?? 3; }, canEquipItem: function (item) { @@ -71,7 +51,7 @@ module.exports = { }, calculateTier: function (factionId) { - let blueprint = this.getBlueprint(factionId); + let blueprint = getFactionBlueprint(factionId); let faction = this.list.find(l => l.id === factionId); let rep = faction.rep; @@ -99,7 +79,7 @@ module.exports = { getReputation: function (factionId, gain) { let fullSync = false; - let blueprint = this.getBlueprint(factionId); + let blueprint = getFactionBlueprint(factionId); let faction = this.list.find(l => l.id === factionId); if (!faction) { @@ -147,7 +127,7 @@ module.exports = { if (this.list.some(l => l.id === factionId)) return; - let blueprint = this.getBlueprint(factionId); + let blueprint = getFactionBlueprint(factionId); if (!blueprint) return; @@ -185,7 +165,7 @@ module.exports = { let sendList = this.list .map(function (l) { let result = {}; - let blueprint = this.getBlueprint(l.id); + let blueprint = getFactionBlueprint(l.id); extend(result, l, blueprint); return result; @@ -206,7 +186,7 @@ module.exports = { }; if (full) { - let blueprint = this.getBlueprint(factionId); + let blueprint = getFactionBlueprint(factionId); extend(faction, l, blueprint); } diff --git a/src/server/config/factions/helpers.js b/src/server/config/factions/helpers.js new file mode 100644 index 00000000..0262e631 --- /dev/null +++ b/src/server/config/factions/helpers.js @@ -0,0 +1,30 @@ +//Imports +let factionBase = require('../factionBase'); +let factions = require('../factions'); + +//Internals +const cache = {}; + +//Method +const getFactionBlueprint = factionId => { + if (cache[factionId]) + return cache[factionId]; + + let res = null; + try { + res = factions.getFaction(factionId); + } catch (e) {} + + if (!res) + return; + + res = extend({}, factionBase, res); + + cache[factionId] = res; + + return res; +}; + +module.exports = { + getFactionBlueprint +};