Permalink
Browse files

Use InventoryLists to manage cart and storage data

`%cart` and `%storage` are now removed - use `$char->cart` and `$char->storage`.

- `$cart{inventory}` is now `$char->cart->getItems`;
- all items in storage are now accessed as `$char->storage->getItems`;
- check if `$char` is defined before accessing cart or storage;
- there are now `$char->inventory->isReady`, `$char->cart->isReady` and `$char->storage->isReady` for things that need to wait before full inventory information is received. That replaces `$ai_v{inventory_time}`, `$ai_v{cart_time}` and `$ai_v{temp}{storage_opened}`.
  • Loading branch information...
1 parent c2c6b8e commit 17b82ad2f7e2ba7f6ea5d32ca7bf6a240a924dd3 @Henrybk Henrybk committed with farrainbow Dec 11, 2016
View
No changes.
@@ -229,27 +229,27 @@ sub checkPercent {
else {return cmpr($char->{$what}, $cond, $amount)}
}
elsif ($what eq 'cweight') {
- return 0 unless (defined $cart{weight} && defined $cart{weight_max});
- if ($amount =~ /^\s*(?:\d+|\d+\s*\.{2}\s*\d+)%$/ && $cart{weight_max}) {
+ return 0 unless (defined $char->cart->{weight} && defined $char->cart->{weight_max});
+ if ($amount =~ /^\s*(?:\d+|\d+\s*\.{2}\s*\d+)%$/ && $char->cart->{weight_max}) {
$amount =~ s/%$//;
- return cmpr(($cart{weight} / $cart{weight_max} * 100), $cond, $amount)
+ return cmpr(($char->cart->{weight} / $char->cart->{weight_max} * 100), $cond, $amount)
}
elsif ($amount =~ /^\s*\$/) {
my ($var, $percent) = $amount =~ /^\$([a-zA-Z][a-zA-Z\d]*)(%)?\s*/;
return 0 unless defined $var;
- if ((defined $percent || $percent eq "%") && defined $cart{weight_max}) {
+ if ((defined $percent || $percent eq "%") && defined $char->cart->{weight_max}) {
if (exists $varStack{$var}) {
$amount = $varStack{$var};
- return cmpr(($cart{weight} / $cart{weight_max} * 100), $cond, $amount)
+ return cmpr(($char->cart->{weight} / $char->cart->{weight_max} * 100), $cond, $amount)
}
} else {
if (exists $varStack{$var}) {
$amount = $varStack{$var};
- return cmpr($cart{weight}, $cond, $amount)
+ return cmpr($char->cart->{weight}, $cond, $amount)
}
}
}
- else {return cmpr($cart{weight}, $cond, $amount)}
+ else {return cmpr($char->cart->{weight}, $cond, $amount)}
}
return 0
}
@@ -290,12 +290,10 @@ sub checkItem {
else {return 0}
}
my $what;
- if ($where eq 'inv') {$what = getInventoryAmount($item)}
- if ($where eq 'cart') {$what = getCartAmount($item)}
- if ($where eq 'inv') {return 0 unless (time > $ai_v{'inventory_time'}); $what = getInventoryAmount($item);};
- if ($where eq 'cart') {return 0 unless (time > $ai_v{'cart_time'}); $what = getCartAmount($item)};
+ if ($where eq 'inv') {return 0 unless ($char->inventory->isReady()); $what = getInventoryAmount($item);};
+ if ($where eq 'cart') {return 0 unless ($char->cart->isReady()); $what = getCartAmount($item)};
if ($where eq 'shop') {return 0 unless $shopstarted; $what = getShopAmount($item)}
- if ($where eq 'stor') {return 0 unless $::storage{opened}; $what = getStorageAmount($item)}
+ if ($where eq 'stor') {return 0 unless ($char->storage->isReady()); $what = getStorageAmount($item)}
return cmpr($what, $cond, $amount)?1:0
}
@@ -302,8 +302,8 @@ sub parseCmd {
my $randomized = 0;
if ($kw eq 'npc') {$ret = getnpcID($arg)}
- elsif ($kw eq 'cart') {($ret) = getItemIDs($arg, $::cart{'inventory'})}
- elsif ($kw eq 'Cart') {$ret = join ',', getItemIDs($arg, $::cart{'inventory'})}
+ elsif ($kw eq 'cart') {($ret) = getItemIDs($arg, $char->cart->getItems)}
+ elsif ($kw eq 'Cart') {$ret = join ',', getItemIDs($arg, $char->cart->getItems)}
elsif ($kw eq 'inventory') {($ret) = getInventoryIDs($arg)}
elsif ($kw eq 'Inventory') {$ret = join ',', getInventoryIDs($arg)}
elsif ($kw eq 'store') {($ret) = getItemIDs($arg, \@::storeList)}
@@ -287,7 +287,7 @@ sub getVenderID {
# get inventory item ids
# checked and ok
sub getInventoryIDs {
- return unless $char->inventory->size();
+ return unless $char->inventory->isReady();
my $find = lc($_[0]);
my @ids;
foreach my $item (@{$char->inventory->getItems}) {
@@ -298,7 +298,6 @@ sub getInventoryIDs {
}
# get item array index
-# works for $cart{'inventory'}, @articles
sub getItemIDs {
my ($item, $pool) = (lc($_[0]), $_[1]);
my @ids;
@@ -323,11 +322,11 @@ sub getItemPrice {
# get storage array index
# returns -1 if no matching items in storage
sub getStorageIDs {
- my $item = lc($_[0]);
+ return unless $char->storage->wasOpenedThisSession();
+ my $find = lc($_[0]);
my @ids;
- for (my $id = 0; $id < @storageID; $id++) {
- next unless $storageID[$id];
- if (lc($storage{$storageID[$id]}{name}) eq $item) {push @ids, $id}
+ foreach my $item (@{$char->storage->getItems}) {
+ if (lc($item->name) eq $find) {push @ids, $item->{invIndex}}
}
unless (@ids) {push @ids, -1}
return @ids
@@ -347,6 +346,7 @@ sub getSoldOut {
# get amount of an item in inventory
sub getInventoryAmount {
my $arg = lc($_[0]);
+ return -1 unless ($char->inventory->isReady());
my $amount = 0;
foreach my $item (@{$char->inventory->getItems}) {
if (lc($item->name) eq $arg) {$amount += $item->{amount}}
@@ -357,11 +357,10 @@ sub getInventoryAmount {
# get amount of an item in cart
sub getCartAmount {
my $arg = lc($_[0]);
- return 0 unless $cart{'inventory'};
+ return -1 unless ($char->cart->isReady());
my $amount = 0;
- for (my $id = 0; $id < @{$cart{'inventory'}}; $id++) {
- next unless $cart{'inventory'}[$id];
- if (lc($cart{'inventory'}[$id]{'name'}) eq $arg) {$amount += $cart{'inventory'}[$id]{amount}}
+ foreach my $item (@{$char->cart->getItems}) {
+ if (lc($item->name) eq $arg) {$amount += $item->{amount}}
}
return $amount
}
@@ -381,11 +380,10 @@ sub getShopAmount {
# returns -1 if the storage is closed
sub getStorageAmount {
my $arg = lc($_[0]);
- return -1 unless $::storage{opened};
+ return -1 unless ($char->storage->wasOpenedThisSession());
my $amount = 0;
- for (my $id = 0; $id < @storageID; $id++) {
- next unless $storageID[$id];
- if (lc($storage{$storageID[$id]}{name}) eq $arg) {$amount += $storage{$storageID[$id]}{amount}}
+ foreach my $item (@{$char->storage->getItems}) {
+ if (lc($item->name) eq $arg) {$amount += $item->{amount}}
}
return $amount
}
@@ -22,22 +22,26 @@ =head1 AUTHOR
use sort 'stable';
use Plugins;
use Globals;
-use Log qw(message);
+use Log qw(message error);
Plugins::register('cartlog', 'writes cart inventory to file.', \&Unload);
my $hook = Commands::register(['cartlog', "print cart contents to file", \&cartLog]);
-
+
sub Unload {
Commands::unregister($hook)
}
sub cartLog {
my $logfile = $_[1]?$_[1]:"$Settings::logs_folder/cartlog.csv";
+ unless ($char) {
+ error "Character not ready\n";
+ return;
+ }
+
my $cartlog;
- foreach my $inv (@{$cart{inventory}}) {
- next unless defined $inv->{name};
+ foreach my $inv (@{$char->cart->getItems}) {
if (defined $cartlog->{$inv->{name}}) {
$cartlog->{$inv->{name}}++
} else {
@@ -339,18 +339,16 @@ sub request {
my (@cartItemName, @cartItemAmount, @cartItemJS, @cartItemID);
my $cartActive = 'disabled';
- if ($char->cartActive) {
+ if ($char->cart->isReady) {
$cartActive = undef;
- for (my $i = 0; $i < @{$cart{'inventory'}}; $i++) {
- next if (!$cart{'inventory'}[$i] || !%{$cart{'inventory'}[$i]});
-
- my $itemName = $cart{'inventory'}[$i]{'name'};
- $itemName .= T(" -- Not Identified") if !$cart{inventory}[$i]{identified};
+ for my $item (@{$char->cart->getItems}) {
+ my $itemName = $item->{name};
+ $itemName .= T(" -- Not Identified") unless $item->{identified};
push @cartItemName, $itemName;
- push @cartItemAmount, $cart{'inventory'}[$i]{'amount'};
- push @cartItemJS, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=cart+get+' . $i . '">' . T('Get') . '</a></td>';
- push @cartItemID, $cart{'inventory'}[$i]{'nameID'};
+ push @cartItemAmount, $item->{amount};
+ push @cartItemJS, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=cart+get+' . $item->{invIndex} . '">' . T('Get') . '</a></td>';
+ push @cartItemID, $item->{nameID};
}
}
@@ -359,25 +357,22 @@ sub request {
my (@storageUnusableAmount, @storageUsableAmount, @storageEquipmentAmount);
my (@storageUnusableID, @storageUsableID, @storageEquipmentID);
my (@storageUnusableGetButton, @storageUsableGetButton, @storageEquipmentGetButton);
- for (my $i = 0; $i < @storageID; $i++) {
- my $item = $storage{$storageID[$i]};
- next if (!$item);
-
+ for my $item (@{$char->storage->getItems}) {
if ($item->usable) {
push @storageUsableID, $item->{nameID};
push @storageUsable, '<a href="' . sprintf($config{webDBLink_item} || 'http://ratemyserver.net/index.php?page=item_db&item_id=%s', $item->{nameID}) . "\">$item->{name}</a>";
push @storageUsableAmount, $item->{amount};
- push @storageUsableGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $i . '">' . T('Get') . '</a></td><td></td>' if ($storage{opened});
+ push @storageUsableGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $item->{invIndex} . '">' . T('Get') . '</a></td><td></td>' if $char->storage->isReady;
} elsif ($item->equippable) {
push @storageEquipmentID, $item->{nameID};
push @storageEquipment, '<a href="' . sprintf($config{webDBLink_item} || 'http://ratemyserver.net/index.php?page=item_db&item_id=%s', $item->{nameID}) . "\">$item->{name}</a>";
push @storageEquipmentAmount, $item->{amount};
- push @storageEquipmentGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $i . '">' . T('Get') . '</a></td><td></td>' if ($storage{opened});
+ push @storageEquipmentGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $item->{invIndex} . '">' . T('Get') . '</a></td><td></td>' if $char->storage->isReady;
} else {
push @storageUnusableID, $item->{nameID};
push @storageUnusable, '<a href="' . sprintf($config{webDBLink_item} || 'http://ratemyserver.net/index.php?page=item_db&item_id=%s', $item->{nameID}) . "\">$item->{name}</a>";
push @storageUnusableAmount, $item->{amount};
- push @storageUnusableGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $i . '">' . T('Get') . '</a></td><td></td>' if ($storage{opened});
+ push @storageUnusableGetButton, '<td><a class="btn btn-mini btn-inverse" href="/handler?csrf=' . $csrf . '&command=storage+get+' . $item->{invIndex} . '">' . T('Get') . '</a></td><td></td>' if $char->storage->isReady;
}
}
@@ -538,7 +533,7 @@ sub request {
push @selfShopItemName, $item->{name};
}
$selfShopButton = '<a href="/handler?csrf='.$csrf.'&command=closeshop" class="btn btn-danger btn-mini pull-right"><i class="icon-shopping-cart icon-white"></i> closeshop</a>';
- } elsif (!$shopstarted && $char->{skills}{MC_VENDING}{lv} && $cart{exists} && $char->cartActive && $shop{title_line}) {
+ } elsif (!$shopstarted && $char->{skills}{MC_VENDING}{lv} && $char->cart->isReady && $shop{title_line}) {
$selfShopButton = '<a href="/handler?csrf='.$csrf.'&command=openshop" class="btn btn-success btn-mini pull-right"><i class="icon-shopping-cart icon-white"></i> openshop</a>';
} else {
$selfShopButton = " "
Oops, something went wrong.

0 comments on commit 17b82ad

Please sign in to comment.