Skip to content

Commit

Permalink
- Reldens - Items System
Browse files Browse the repository at this point in the history
- Merge pull request #54 from damian-pastorini/v0.19.2
  • Loading branch information
damian-pastorini authored Jun 15, 2023
2 parents b41be1c + 808762e commit 3e6c541
Show file tree
Hide file tree
Showing 10 changed files with 826 additions and 3,013 deletions.
14 changes: 7 additions & 7 deletions lib/client/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Receiver
if(false === sc.get(props, 'avoidDefaults', false)){
this.setDefaultActions();
}
this.recieveErrorsLog = [];
}

setDefaultActions()
Expand Down Expand Up @@ -69,8 +70,7 @@ class Receiver
return false;
}
if('function' !== typeof this[this.actions[action]]){
// for now, I'm leaving this a silent return (will probably end up making the error log configurable):
// Logger.error(['Items action is not a function', message.act]);
Logger.info('Items action is not a function.', message.act);
return false;
}
this[this.actions[action]](message);
Expand Down Expand Up @@ -108,35 +108,35 @@ class Receiver
let itemClass = this.getItemClass(itemsProps.key, itemsProps.type);
let tmpItem = new itemClass(itemsProps);
this.manager.addItem(tmpItem).catch((err) => {
Logger.error(['Error receiver onAddItem.', err]);
this.recieveErrorsLog.push({'Error receiver onAddItem.': err});
});
}

onRemoveItem(message)
{
this.manager.removeItem(message.item.idx).catch((err) => {
Logger.error(['Error receiver onRemoveItem.', err]);
this.recieveErrorsLog.push({'Error receiver onRemoveItem.': err});
});
}

onSetQty(message)
{
this.manager.setItemQty(message.item.idx, message.item.qty).catch((err) => {
Logger.error(['Error receiver onSetQty.', err]);
this.recieveErrorsLog.push({'Error receiver onSetQty.': err});
});
}

onEquipItem(message)
{
this.manager.items[message.item.idx].equip(false).catch((err) => {
Logger.error(['Error receiver onEquipItem.', err]);
this.recieveErrorsLog.push({'Error receiver onEquipItem.': err});
});
}

onUnequipItem(message)
{
this.manager.items[message.item.idx].unequip(false).catch((err) => {
Logger.error(['Error receiver onUnequipItem.', err]);
this.recieveErrorsLog.push({'Error receiver onUnequipItem.': err});
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
BEHAVIOR_BROADCAST: 'broadcast',
BEHAVIOR_BOTH: 'both',
TYPES: {
ITEM_BASE: 0,
ITEM_BASE: 10,
EQUIPMENT: 1,
USABLE: 2,
SINGLE: 3, // @NOTE: single instance items the quantity will be grouped in a single inventory object.
Expand Down
14 changes: 4 additions & 10 deletions lib/exchange/exchange-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ExchangePlatform
async pushForExchange(itemUid, qty, inventoryKey)
{
if(this.confirmations['A'] || this.confirmations['B']){
// @TODO - BETA - Make lock exchange configurable so confirmation will be auto-removed on exchange changes.
Logger.info('Push for exchange "'+itemUid+'" was blocked.'
+' Exchange for "'+inventoryKey+'" and owner "'+this.inventories[inventoryKey].owner_id+'"'
+' was already confirmed.');
Expand Down Expand Up @@ -194,25 +195,23 @@ class ExchangePlatform

canBePushed(itemUid, qty, inventoryKey)
{
// inventory has the item:
if(!sc.hasOwn(this.inventories[inventoryKey].items, itemUid)){
Logger.critical('Item Key does not exists on the inventory', itemUid, this.inventories[inventoryKey].items);
return false;
}
// item qty is available or -1 item qty is infinite:
let pushedItem = this.inventories[inventoryKey].items[itemUid];
let isValidQuantity = qty <= pushedItem.qty || -1 === pushedItem.qty;
this.lastErrorMessage = isValidQuantity
? ''
: 'Invalid item pushed quantity ('+qty+'), available: '+pushedItem.qty;
if(!isValidQuantity){
this.lastErrorMessage = 'Invalid item pushed quantity ('+qty+'), available: '+pushedItem.qty;
}
return isValidQuantity;
}

async executeExchangeFromTo(from, to)
{
if(from === to){
this.lastErrorMessage = 'Inventories "FROM" and "TO" are the same, exchange cancelled.';
Logger.error(this.lastErrorMessage);
this.cancelExchange();
return this.inventories;
}
Expand All @@ -223,7 +222,6 @@ class ExchangePlatform
let itemQtyFrom = this.exchangeBetween[from][itemUid];
if(0 === itemQtyFrom){
this.lastErrorMessage = 'Invalid item quantity 0.';
Logger.error(this.lastErrorMessage, itemUid, from, this.exchangeBetween[from]);
return false;
}
// first create the new item because the decrease quantity could remove the instance we need to clone:
Expand All @@ -239,7 +237,6 @@ class ExchangePlatform
this
);
if(false === rewardsResult){
Logger.error(this.lastErrorMessage, itemUid, from, this.exchangeBetween[from]);
return false;
}
// @NOTE: since we already validate the transaction requirements before execute it, here we only need to
Expand All @@ -254,7 +251,6 @@ class ExchangePlatform
this
);
if(false === requirementsResult){
Logger.error(this.lastErrorMessage, itemUid, from, this.exchangeBetween[from]);
return false;
}
let inventoryFromDecreaseItemQtyResult = true
Expand All @@ -268,7 +264,6 @@ class ExchangePlatform
this.lastErrorMessage = '' !== inventoryFrom.lastErrorMessage
? 'Exchange inventory error. '+inventoryFrom.lastErrorMessage
: 'Exchange decreaseItemQty item error.';
Logger.error(this.lastErrorMessage, itemUid, from, this.exchangeBetween[from]);
return false;
}
if(false === this.dropExchange[to]){
Expand All @@ -278,7 +273,6 @@ class ExchangePlatform
this.lastErrorMessage = '' !== inventoryTo.lastErrorMessage
? 'Exchange add result error. '+inventoryTo.lastErrorMessage
: 'Exchange add item error.';
Logger.error(this.lastErrorMessage, itemUid, from, this.exchangeBetween[from]);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/exchange/requirements-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ class RequirementsProcessor
exchange.lastErrorMessage = '' !== inventoryTo.lastErrorMessage
? 'Requirements process error. '+inventoryTo.lastErrorMessage
: 'Requirement add item error.';
Logger.error(exchange.lastErrorMessage);
return false;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/exchange/rewards-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ class RewardsProcessor
}
if('' === exchange.lastErrorMessage){
exchange.lastErrorMessage = 'Rewards error on add items: '+(Object.keys(addItems).join(','))+'.';
Logger.error(exchange.lastErrorMessage, addItems);
}
return false;
}
Expand Down
19 changes: 1 addition & 18 deletions lib/item/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const ItemsConst = require('../constants');
const ItemsEvents = require('../items-events');
const { EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
const { EventsManagerSingleton, sc } = require('@reldens/utils');

class Inventory
{
Expand Down Expand Up @@ -37,17 +37,14 @@ class Inventory
let result = true;
if(!item){
this.lastErrorMessage = 'Add item error, undefined item.';
Logger.error(this.lastErrorMessage, item);
result = false;
}
if(!sc.isObjectFunction(item, 'getInventoryId')){
this.lastErrorMessage = 'Add item error, undefined getInventoryId.';
Logger.error(this.lastErrorMessage, item);
result = false;
}
if(!item.key){
this.lastErrorMessage = 'Add item error, undefined item key.';
Logger.error(this.lastErrorMessage, item);
result = false;
}
await this.fireEvent(ItemsEvents.VALIDATE, this, item, result);
Expand All @@ -69,28 +66,23 @@ class Inventory
let isValid = await this.validate(item);
if(!isValid){
this.lastErrorMessage = 'Invalid item instance.';
Logger.error(this.lastErrorMessage, item);
return false;
}
let itemId = item.getInventoryId();
if(this.locked){
this.lastErrorMessage = 'Inventory locked, cannot add item: '+itemId;
Logger.error(this.lastErrorMessage, item);
return false;
}
if(this.itemsLimit === Object.keys(this.items).length && 0 <= this.itemsLimit){
this.lastErrorMessage = 'Cannot add item, max total reached.';
Logger.error(this.lastErrorMessage, item);
return false;
}
if(sc.hasOwn(this.items, itemId) && !item.singleInstance){
this.lastErrorMessage = 'Cannot add item, item already exists: '+itemId;
Logger.error(this.lastErrorMessage, item);
return false;
}
if(item.qty > this.limitPerItem && 0 <= this.limitPerItem){
this.lastErrorMessage = 'Cannot add item, item qty limit exceeded.';
Logger.error(this.lastErrorMessage, item);
return false;
}
if(sc.hasOwn(this.items, itemId) && item.singleInstance){
Expand All @@ -109,7 +101,6 @@ class Inventory
let addResult = await this.addItem(item);
if(!addResult){
this.lastErrorMessage = 'Cannot add item "'+item.getInventoryId()+'".';
Logger.error({'Cannot add item': item, 'Result': addResult});
return false;
}
}
Expand All @@ -120,7 +111,6 @@ class Inventory
{
if(this.locked){
this.lastErrorMessage = 'Inventory locked, cannot set item: '+item.getInventoryId();
Logger.error(this.lastErrorMessage, item);
return false;
}
this.items[item.getInventoryId()] = item;
Expand All @@ -130,12 +120,10 @@ class Inventory
{
if(this.locked){
this.lastErrorMessage = 'Inventory locked, cannot remove item: '+key;
Logger.error(this.lastErrorMessage);
return false;
}
if(!sc.hasOwn(this.items, key)){
this.lastErrorMessage = 'Cannot remove item, key not found: '+key;
Logger.error(this.lastErrorMessage);
return false;
}
await this.fireEvent(ItemsEvents.REMOVE_ITEM, this, key);
Expand All @@ -162,17 +150,14 @@ class Inventory
{
if(this.locked){
this.lastErrorMessage = 'Inventory locked, cannot modify item qty: '+key;
Logger.error(this.lastErrorMessage);
return false;
}
if(!sc.hasOwn(this.items, key)){
this.lastErrorMessage = 'Cannot '+op+' item qty, undefined item key: '+key;
Logger.error(this.lastErrorMessage);
return false;
}
if(isNaN(qty)){
this.lastErrorMessage = 'Cannot '+op+' item qty, qty is not a number: '+qty;
Logger.error(this.lastErrorMessage);
return false;
}
if(
Expand All @@ -181,7 +166,6 @@ class Inventory
&& (op === ItemsConst.SET || op === ItemsConst.INCREASE)
){
this.lastErrorMessage = 'Cannot '+op+' item qty, item qty limit exceeded: '+qty+' > '+this.limitPerItem;
Logger.error(this.lastErrorMessage);
return false;
}
if(op === ItemsConst.SET){
Expand Down Expand Up @@ -209,7 +193,6 @@ class Inventory
{
if(this.locked){
this.lastErrorMessage = 'Inventory locked, cannot set items.';
Logger.error(this.lastErrorMessage);
return false;
}
this.items = items;
Expand Down
6 changes: 3 additions & 3 deletions lib/item/type/equipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const ItemBase = require('./item-base');
const ItemsConst = require('../../constants');
const ItemsEvents = require('../../items-events');
const { Logger, sc } = require('@reldens/utils');
const { sc } = require('@reldens/utils');

class Equipment extends ItemBase
{
Expand Down Expand Up @@ -48,7 +48,7 @@ class Equipment extends ItemBase
async applyModifiers()
{
if(!this.equipped){
Logger.error('Item not equipped: '+this.getInventoryId());
this.manager.lastErrorMessage = 'Cannot apply modifiers the item is not equipped: '+this.getInventoryId();
return false;
}
return await super.applyModifiers();
Expand All @@ -57,7 +57,7 @@ class Equipment extends ItemBase
async revertModifiers()
{
if(this.equipped){
Logger.error('Item equipped: '+this.getInventoryId());
this.manager.lastErrorMessage = 'Cannot revert modifiers the item is not equipped: '+this.getInventoryId();
return false;
}
return await super.revertModifiers();
Expand Down
2 changes: 1 addition & 1 deletion lib/server/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Sender
let itemProps = {idx: item.getInventoryId()};
for(let prop of this.sendProperties[act][behavior]){
if(!sc.hasOwn(item, prop)){
Logger.error(['Undefined property:', prop, 'Item:', item.getInventoryId()]);
Logger.info('Undefined property: '+prop, item);
continue;
}
itemProps[prop] = item[prop];
Expand Down
Loading

0 comments on commit 3e6c541

Please sign in to comment.