Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Fix Various Weight Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 6, 2023
1 parent 6d21f39 commit 8499342
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Athena.systems.inventory.convert.toBaseItem
Athena.systems.inventory.convert.toItem
Athena.systems.inventory.convert.toStoredItem
Fix Various Weight Related Bugs
--------------------------------------
--- Everything Below is Before April 2
--------------------------------------
Expand Down
27 changes: 4 additions & 23 deletions src/core/plugins/core-inventory/server/src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,35 +318,16 @@ const Internal = {

// Check Storage Capacity
const config = Athena.systems.inventory.config.get();
const maxWeight = openStoragesWeight[player.id];
let itemsToCheck: Array<StoredItem>;

if (info.startType === 'custom' && config.weight.enabled) {
itemsToCheck = complexSwap.from;

if ((Athena.systems.inventory.weight.isWeightExceeded([complexSwap.to]), config.weight.player)) {
// console.log('weight exceeded at 1');
if (config.weight.enabled) {
const storageWeight = openStoragesWeight[player.id];
const maxWeight = info.endType === 'custom' ? storageWeight : config.weight.player;
if (Athena.systems.inventory.weight.isWeightExceeded([complexSwap.to], maxWeight)) {
InventoryView.storage.resync(player);
return;
}
}

if (info.endType === 'custom' && config.weight.enabled) {
itemsToCheck = complexSwap.to;
if ((Athena.systems.inventory.weight.isWeightExceeded([complexSwap.from]), maxWeight)) {
// console.log('weight exceeded at 2');
InventoryView.storage.resync(player);
return;
}
}

const isWeightExceeded = Athena.systems.inventory.weight.isWeightExceeded([itemsToCheck], maxWeight);
if (isWeightExceeded) {
// console.log('weight exceeded at 3');
InventoryView.storage.resync(player);
return;
}

// Assign Data
if (info.startType === 'custom') {
openStorages[player.id] = complexSwap.from;
Expand Down
26 changes: 18 additions & 8 deletions src/core/plugins/core-inventory/webview/components/Grid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
</div>
<Context :contextTitle="context.title" :x="context.x" :y="context.y" v-if="context">
<div v-if="context.hasUseEffect" @click="contextAction('use')">Use</div>
<template v-for="(customAction) in context.customEvents">
<div @click="contextAction('use',customAction.eventToCall)">{{customAction.name}}</div>
<template v-for="customAction in context.customEvents">
<div @click="contextAction('use', customAction.eventToCall)">{{ customAction.name }}</div>
</template>
<div @click="contextAction('split')">Split</div>
<div @click="contextAction('drop')">Drop</div>
Expand Down Expand Up @@ -152,7 +152,16 @@ export default defineComponent({
toolbar: 5,
custom: 35,
},
context: undefined as { x: number; y: number; title: string; slot: number; hasUseEffect: boolean, customEvents: Array<CustomContextAction>} | undefined,
context: undefined as
| {
x: number;
y: number;
title: string;
slot: number;
hasUseEffect: boolean;
customEvents: Array<CustomContextAction>;
}
| undefined,
itemSingleClick: undefined as { type: InventoryType; index: number },
itemName: '',
itemDescription: '',
Expand Down Expand Up @@ -256,7 +265,7 @@ export default defineComponent({
hasItem: false,
name: undefined,
quantity: 0,
weight: 0,
totalWeight: 0,
highlight: false,
slot,
};
Expand All @@ -281,7 +290,7 @@ export default defineComponent({
defaultTemplate.highlight = item.isEquipped;
defaultTemplate.name = item.name;
defaultTemplate.quantity = item.quantity;
defaultTemplate.weight = typeof item.weight === 'number' ? item.weight : 0;
defaultTemplate.totalWeight = typeof item.totalWeight === 'number' ? item.totalWeight : 0;
return defaultTemplate;
},
/**
Expand Down Expand Up @@ -347,10 +356,11 @@ export default defineComponent({
}
const item = this.getItem('inventory', slot);
// Let's check, if this item can be used. This information is used to show/hide the "Use" context menu action.
const hasUseEffect: boolean = (typeof item.consumableEventToCall !== 'undefined' ||
(item.behavior && (item.behavior.isEquippable || item.behavior.isWeapon || item.behavior.isClothing)));
const hasUseEffect: boolean =
typeof item.consumableEventToCall !== 'undefined' ||
(item.behavior && (item.behavior.isEquippable || item.behavior.isWeapon || item.behavior.isClothing));
this.context = {
title: item.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<slot name="index"></slot>
</div>
<div class="quantity" v-if="info.quantity !== 0">{{ info.quantity }}x</div>
<div class="weight" v-if="info.weight !== 0">{{ info.weight.toFixed(2) }}{{ units }}</div>
<div class="weight" v-if="info.totalWeight !== 0">{{ info.totalWeight.toFixed(2) }}{{ units }}</div>
<div class="name" v-if="info.name !== '' && showName">
{{ info.name }}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export interface SlotInfo {
hasItem: boolean;
quantity: number;
name: string;
weight: number;
totalWeight: number;
highlight?: boolean;
}
4 changes: 2 additions & 2 deletions src/core/server/systems/inventory/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ export function fromStoredItem<CustomData = {}, CustomBehavior = DefaultItemBeha

const combinedItem = Object.assign(baseItem, item) as Item<CustomBehavior & DefaultItemBehavior, CustomData>;

if (typeof combinedItem.weight === 'number') {
combinedItem.totalWeight = combinedItem.weight * combinedItem.quantity;
if (typeof baseItem.weight === 'number') {
combinedItem.totalWeight = baseItem.weight * combinedItem.quantity;
}

return combinedItem;
Expand Down
43 changes: 19 additions & 24 deletions src/core/server/systems/inventory/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function add<CustomData = {}>(
}

if (item.quantity === 0) {
return data;
return Athena.systems.inventory.weight.update<CustomData>(data);
}

// Lookup the base item based on the dbName of the item.
Expand Down Expand Up @@ -377,12 +377,10 @@ export function add<CustomData = {}>(
item.quantity -= 1;
}

// Re-calculate item weight
itemClone = calculateItemWeight(baseItem, itemClone);
copyOfData.push(itemClone);

if (item.quantity === 0) {
return copyOfData;
return Athena.systems.inventory.weight.update<CustomData>(copyOfData);
}

return add(item, copyOfData, size);
Expand Down Expand Up @@ -463,9 +461,7 @@ export function sub<CustomData = {}>(

// If the quantity of the found item is greater than; subtract necessary amount.
copyOfData[existingItemIndex].quantity -= item.quantity;
copyOfData[existingItemIndex] = calculateItemWeight(baseItem, copyOfData[existingItemIndex]);

return copyOfData;
return Athena.systems.inventory.weight.update(copyOfData);
}

/**
Expand Down Expand Up @@ -528,10 +524,9 @@ export function splitAt<CustomData = {}>(

// Remove quantity from existing item based on split count.
copyOfData[index].quantity -= splitCount;
copyOfData[index] = calculateItemWeight<CustomData>(baseItem, copyOfData[index]);
copyOfData.push(itemClone);

return copyOfData;
return Athena.systems.inventory.weight.update<CustomData>(copyOfData);
}

/**
Expand Down Expand Up @@ -584,19 +579,15 @@ export function combineAt<CustomData = {}>(

if (copyOfData[fromIndex].quantity + copyOfData[toIndex].quantity <= baseItem.maxStack) {
copyOfData[toIndex].quantity += copyOfData[fromIndex].quantity;
copyOfData[toIndex] = calculateItemWeight(baseItem, copyOfData[toIndex]);
copyOfData.splice(fromIndex, 1);
return copyOfData;
return Athena.systems.inventory.weight.update<CustomData>(copyOfData);
}

const spaceAvailable = baseItem.maxStack - copyOfData[toIndex].quantity;
copyOfData[fromIndex].quantity -= spaceAvailable;
copyOfData[toIndex].quantity += spaceAvailable;

copyOfData[fromIndex] = calculateItemWeight(baseItem, copyOfData[fromIndex]);
copyOfData[toIndex] = calculateItemWeight(baseItem, copyOfData[toIndex]);

return copyOfData;
return Athena.systems.inventory.weight.update<CustomData>(copyOfData);
}

export function combineAtComplex(from: ComplexSwap, to: ComplexSwap): ComplexSwapReturn | undefined {
Expand Down Expand Up @@ -645,18 +636,19 @@ export function combineAtComplex(from: ComplexSwap, to: ComplexSwap): ComplexSwa
// Simply move quantities; and return results.
if (spaceAvailable > fromData[fromIndex].quantity) {
toData[toIndex].quantity += fromData[fromIndex].quantity;
toData[toIndex] = calculateItemWeight(baseItem, toData[toIndex]);
fromData.splice(fromIndex, 1);
return { from: fromData, to: toData };
return {
from: Athena.systems.inventory.weight.update(fromData),
to: Athena.systems.inventory.weight.update(toData),
};
}

fromData[fromIndex].quantity -= spaceAvailable;
toData[toIndex].quantity += spaceAvailable;

fromData[fromIndex] = calculateItemWeight(baseItem, fromData[fromIndex]);
toData[toIndex] = calculateItemWeight(baseItem, toData[toIndex]);

return { from: fromData, to: toData };
return {
from: Athena.systems.inventory.weight.update(fromData),
to: Athena.systems.inventory.weight.update(toData),
};
}

/**
Expand Down Expand Up @@ -706,7 +698,7 @@ export function swap(
copyOfData[endIndex].slot = fromSlot;
}

return copyOfData;
return Athena.systems.inventory.weight.update(copyOfData);
}

/**
Expand Down Expand Up @@ -778,7 +770,10 @@ export function swapBetween(from: ComplexSwap, to: ComplexSwap): ComplexSwapRetu

// Move the 'from' item to the other data set.
toData.push(fromItem);
return { from: fromData, to: toData };
return {
from: Athena.systems.inventory.weight.update(fromData),
to: Athena.systems.inventory.weight.update(toData),
};
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/core/server/systems/inventory/weight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Item, StoredItem } from '@AthenaShared/interfaces/item';
import * as Athena from '@AthenaServer/api';
import * as config from './config';

/**
Expand Down Expand Up @@ -60,6 +61,27 @@ export function isWeightExceeded(dataSets: Array<Array<StoredItem | Item>>, amou
return false;
}

/**
* Update weight for a given data set, and all items.
*
* @export
* @template T
* @param {(Array<StoredItem<T> | Item>)} dataSet
* @return {(Array<StoredItem<T> | Item>)}
*/
export function update<T = {}>(dataSet: Array<StoredItem<T>>): Array<StoredItem<T>> {
for (let i = 0; i < dataSet.length; i++) {
const baseItem = Athena.systems.inventory.factory.getBaseItem(dataSet[i].dbName);
if (typeof baseItem === 'undefined') {
continue;
}

dataSet[i].totalWeight = baseItem.weight * dataSet[i].quantity;
}

return dataSet;
}

interface WeightFuncs {
getDataWeight: typeof getDataWeight;
getTotalWeight: typeof getTotalWeight;
Expand Down

0 comments on commit 8499342

Please sign in to comment.