Added ability to remove items from inventory

- This leaves us with very primitive inventory support
master
Mike Gerwitz 2011-08-22 23:48:30 -04:00
parent 0bcd69d04e
commit 1fe3e68f88
5 changed files with 134 additions and 7 deletions

View File

@ -35,6 +35,13 @@ assert-eq()
}
assert-no-exist()
{
last "$1 exists" "$1 to not exist"
[ ! -e "$1" ]
}
simplefail()
{
echo "FAILURE: $1" >&2

View File

@ -5,6 +5,8 @@ mypath=$( dirname $0 )
add="$mypath/../util/inv-add"
count="$mypath/../util/inv-count"
invrm="$mypath/../util/inv-rm"
getpath="$mypath/../util/getinvpath"
# the item _foo does not exist
$add 1:_foo 2>/dev/null && {
@ -16,11 +18,15 @@ assert-equal $( $count _foo ) 0 || {
fail "Should return 0 if item is not in inventory"
}
# let's create our dummy item
# let's create our dummy items
itemfile="$mypath/../items/_foo"
itemfile2="$mypath/../items/_foo2"
cat > "$itemfile" <<'EOF'
NAME Test Foobar
EOF
cat > "$itemfile2" <<'EOF'
NAME Test foobar 2
EOF
# now, let's give that another shot.
$add 1:_foo 2>/dev/null || {
@ -28,10 +34,20 @@ $add 1:_foo 2>/dev/null || {
}
# one should have been added
assert-equal $( $count _foo ) 1 || {
assert-eq $( $count _foo ) 1 || {
fail "Should add item to inventory"
}
# should be able to get location of file
assert-eq $( cat $( $getpath _foo ) | wc -l ) 1 || {
fail "Should be able to manually access file"
}
# and inventory path
assert-equal $( dirname "$( $getpath _foo )" ) $( $getpath ) || {
fail "Should return full inventory path if no item is provided"
}
# number should be required
$add _foo 2>/dev/null && {
simplefail "Item quantity should be required"
@ -39,16 +55,44 @@ $add _foo 2>/dev/null && {
# let's try adding another and ensure it's appended
$add 1:_foo
assert-equal $( $count _foo ) 2 || {
assert-eq $( $count _foo ) 2 || {
fail "Items should be appended to inventory"
}
# now let's try to add multiple
$add 2:_foo
assert-equal $( $count _foo ) 4 || {
assert-eq $( $count _foo ) 4 || {
fail "Should be able to add multiple items to inventory"
}
# we no longer need our test item
rm "$itemfile"
# add a different item
$add 2:_foo2
assert-eq $( $count _foo2 ) 2 || {
fail "Distinct items have separate counts"
}
# remove one item
$invrm _foo2
assert-eq $( $count _foo2 ) 1 || {
fail "Should be able to remove a single item from the inventory"
}
# remove the other
$invrm _foo2
assert-eq $( $count _foo2 ) 0 || {
fail "Should be able to remove all of one item from inventory"
}
# the file should be removed if there are no more of the item in the inventory
assert-no-exist "$( $getpath _foo2 )" || {
fail "Item inventory file should not exist when item count is 0"
}
# can we remove an item that's not in the inventory?
$invrm _foo2 2>/dev/null && {
fail "Should not be able to remove an item that isn't in the inventory"
}
# we no longer need our test item
rm "$itemfile" "$itemfile2"

View File

@ -1,6 +1,33 @@
#!/bin/bash
#
# Retrieve path to inventory or specific item inventory
#
# Copyright (C) 2011 Mike Gerwitz
#
# This file is part of gsgp. This program is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
. "$( dirname $0 )/common"
echo "$GSGP_PROFILE_PATH/_inv"
# output inventory path
echo -n "$GSGP_PROFILE_PATH/_inv"
# if an item was requested, add that onto the end
if [ "$1" ]; then
echo -n "/$1"
fi
# add the newline
echo

View File

@ -16,6 +16,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
mypath=$( dirname $0 )

48
util/inv-rm 100755
View File

@ -0,0 +1,48 @@
#!/bin/bash
#
# Removes an item from the inventory
#
# Copyright (C) 2011 Mike Gerwitz
#
# This file is part of gsgp. This program is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
mypath=$( dirname $0 )
. "$mypath/common-inv"
if [ $# -lt 1 ]; then
echo "Usage: $0 ITEM"
fi
item_id="$1"
inv_item_file="$( $mypath/getinvpath )/$item_id"
count=$( "$mypath/inv-count" $item_id )
# can't remove an item that's not in the inventory
if [ $count -eq 0 ]; then
echo "$item_id: not in inventory" >&2
exit 1
fi
# remove an item from the item inventory
sed -i '1d' "$inv_item_file"
# if all the items have been removed, then remove the file to speed up inventory
# scanning and to keep the filesystem clean
(( count-- ))
if [ $count -eq 0 ]; then
rm "$inv_item_file"
fi