Began implementing inventory (adding/counting)

master
Mike Gerwitz 2011-08-22 21:27:06 -04:00
parent a320badb3a
commit 0bcd69d04e
7 changed files with 208 additions and 3 deletions

View File

@ -1,5 +1,16 @@
#!/bin/bash #!/bin/bash
# init the "game" and profile
export GSGP_ID=gsgptest
export GSGP_TITLE="Test Suite"
export GSGP_PROFILE=TEST
. "$( dirname $0 )/../util/common"
# annihilate profile to start fresh
rm -rf "$GSGP_PROFILE_PATH"
mkdir -p "$GSGP_PROFILE_PATH"
lastgiven='' lastgiven=''
lastexpected='' lastexpected=''
@ -24,10 +35,15 @@ assert-eq()
} }
fail() simplefail()
{ {
echo "FAILURE: $1" >&2 echo "FAILURE: $1" >&2
}
fail()
{
simplefail "$1"
echo "Expected '$lastexpected', but received '$lastgiven'" >&2 echo "Expected '$lastexpected', but received '$lastgiven'" >&2
echo >&2 echo >&2
} }

54
test/test-inv 100755
View File

@ -0,0 +1,54 @@
#!/bin/bash
mypath=$( dirname $0 )
. "$mypath/common"
add="$mypath/../util/inv-add"
count="$mypath/../util/inv-count"
# the item _foo does not exist
$add 1:_foo 2>/dev/null && {
simplefail "Should not be permitted to add unknown items to inventory"
}
# count should be 0 if we have none of the item in the inventory
assert-equal $( $count _foo ) 0 || {
fail "Should return 0 if item is not in inventory"
}
# let's create our dummy item
itemfile="$mypath/../items/_foo"
cat > "$itemfile" <<'EOF'
NAME Test Foobar
EOF
# now, let's give that another shot.
$add 1:_foo 2>/dev/null || {
simplefail "Should be able to add item to inventory if it exists"
}
# one should have been added
assert-equal $( $count _foo ) 1 || {
fail "Should add item to inventory"
}
# number should be required
$add _foo 2>/dev/null && {
simplefail "Item quantity should be required"
}
# let's try adding another and ensure it's appended
$add 1:_foo
assert-equal $( $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 || {
fail "Should be able to add multiple items to inventory"
}
# we no longer need our test item
rm "$itemfile"

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
if [ ! "$GSGP_ID" ]; then if [ ! "$GSGP_ID" ]; then
echo "ID unavailable" >&2 echo "ID unavailable" >&2
@ -9,3 +9,9 @@ if [ ! "$GSGP_PROFILE" ]; then
echo "Profile unavailable" >&2 echo "Profile unavailable" >&2
exit 2 exit 2
fi fi
# path to current profile
export GSGP_PROFILE_PATH="$HOME/.$GSGP_ID/profiles/$GSGP_PROFILE"
# create path in case it does not yet exist
mkdir -p "$GSGP_PROFILE_PATH" 2>/dev/null

View File

@ -1,4 +1,24 @@
#!/bin/bash #!/bin/bash
#
# Common inventory commands
#
# 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/common"
itemdir="$( dirname $0 )/../items" itemdir="$( dirname $0 )/../items"

6
util/getinvpath 100755
View File

@ -0,0 +1,6 @@
#!/bin/bash
. "$( dirname $0 )/common"
echo "$GSGP_PROFILE_PATH/_inv"

63
util/inv-add 100755
View File

@ -0,0 +1,63 @@
#!/bin/bash
#
# Adds an item to the inventory, given the item tag
#
# 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
# we should be provided with the item "tag", which identifies the item itself as
# well as additional metadata
tag="$1"
inv_path=$( $mypath/getinvpath )
item_path="$mypath/../items"
item_id=$( inv-item-id "$tag" )
item_count=$( inv-item-count "$tag" )
item_file="$item_path/$item_id"
inv_item_file="$inv_path/$item_id"
# use -gt rather than -lt to ensure we'll still fail if $item_count is not an
# integer
[ $item_count -gt 0 ] || {
echo "Item count required" >&2
exit 1
}
# does the item we're attempting to access actually exist?
[ -r "$item_file" ] || {
echo "Item does not exist" >&2
exit 1
}
# attempt to create the inventory in case it does not exist
mkdir -p "$inv_path" 2>/dev/null
# currently unused
attr=''
# append item(s) to inventory
for i in $( seq 1 $item_count ); do
echo "$sttr" >> "$inv_item_file"
done

40
util/inv-count 100755
View File

@ -0,0 +1,40 @@
#!/bin/bash
#
# Counts number of items in 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
# we should be provided with the id, not the tag
item_id="$1"
inv_item_file="$( $mypath/getinvpath )/$item_id"
# if the file does not exit, then we clearly have 0 of the item in our inventory
if [ ! -r "$inv_item_file" ]; then
echo 0
exit
fi
# count the number of items in the inventory
wc -l < "$inv_item_file" || echo 0