From 3b9f8220a4e4604a4d05fdbca2ff98719769f1bb Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 11 Aug 2005 10:35:04 +0000 Subject: [PATCH] Explicit snack support --- sql-edition/servers/SnackConfig.py | 77 ++++++++++++++++++++++++++++++ sql-edition/servers/VendServer.py | 23 +++++++-- sql-edition/servers/snacks.conf | 14 ++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100755 sql-edition/servers/SnackConfig.py create mode 100644 sql-edition/servers/snacks.conf diff --git a/sql-edition/servers/SnackConfig.py b/sql-edition/servers/SnackConfig.py new file mode 100755 index 0000000..e608f81 --- /dev/null +++ b/sql-edition/servers/SnackConfig.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +class VendingException( Exception ): pass + +FILENAME="snacks.conf" + +def parse_line( l ): + toks = l.strip().split() + if not len( toks ): + return + + if toks[0][:1] == '#': + return + + if len( toks ) < 4: + raise VendingException( "Bad line '%s' in snack config file, too few items" % l ) + + # price + try: + price = int( toks[0] ) + except ValueError: + raise VendingException( "Bad line '%s' in snack config file, bad price" % l) + + # slots + slots = toks[1].split(",") + for s in slots: + if len(s) != 2: + raise VendingException( "Bad line %s' in snack config file, bad slot '%s'" % (l, s) ) + + # shortname for dispense + shortname = toks[2] + + # name + name = ' '.join( toks[3:] ) + + return ( price, slots, shortname, name ) + +def get_snacks( filename = FILENAME ): + + snacks = [] + try: + f = file( filename ) + snacks = filter( bool, map( parse_line, f ) ) + except IOError, e: + raise VendingException( e ) + + ret = {} + for price, slots, shortname, name in snacks: + for s in slots: + ret[s] = (price, shortname, name) + + return ret + +def get_snack( slot ): + + snacks = get_snacks() + if slot not in key: + raise VendingException( "Slot '%s' isn't in config file" % slot ) + + return snacks[slot] + +def get_price( slot ): + p, sn, n = get_snacks( slot ) + return p + +def get_name( slot ): + p, sn, n = get_snacks( slot ) + return n + +def get_short_name( slot ): + p, sn, n = get_snacks( slot ) + return sn + +if __name__ == '__main__': + snacks = get_snacks() + + print snacks diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index c93fda0..a253f28 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -105,8 +105,19 @@ def scroll_options(username, mk, welcome = False): (slot_num, price, slot_name) = c.split(' ', 2) if slot_name == 'dead': continue choices += '%s8-%s (%sc) '%(slot_num, slot_name, price) + +# we don't want to print snacks for now since it'll be too large +# and there's physical bits of paper in the machine anyway - matt +# try: +# snacks = get_snacks() +# except: +# snacks = {} +# +# for slot, ( name, price ) in snacks.items(): +# choices += '%s8-%s (%sc) ' % ( slot, name, price ) + choices += '55-DOOR ' - choices += 'OR A SNACK. ' + choices += 'OR ANOTHER SNACK. ' choices += '99 TO READ AGAIN. ' choices += 'CHOICE? ' msg.append((choices, False, None)) @@ -396,8 +407,14 @@ def make_selection(v, vstatus): else: v.display('GOT COKE!') else: - v.display(vstatus.cur_selection+' - $1.00') - if ((os.system('su - "%s" -c "dispense snack"'%(vstatus.username)) >> 8) == 0): + # first see if it's a named slot + try: + price, shortname, name = get_snack( vstatus.cur_selection ) + except: + price, shortname, name = get_snack( '--' ) + dollarprice = "$%.2f" % ( price / 100.0 ) + v.display(vstatus.cur_selection+' - %s'%dollarprice) + if ((os.system('su - "%s" -c "dispense %s"'%(vstatus.username, shortname)) >> 8) == 0): v.vend(vstatus.cur_selection) v.display('THANK YOU') else: diff --git a/sql-edition/servers/snacks.conf b/sql-edition/servers/snacks.conf new file mode 100644 index 0000000..4f036ed --- /dev/null +++ b/sql-edition/servers/snacks.conf @@ -0,0 +1,14 @@ +# format: +# all are whitespace seperated, name can have whitespace. +# price - in cents +# slots - comma seperated list of slots with this item. +# - don't put any spaces in this +# - the magically named '--' slot is the default for +# unadded slots +# name - the name, spaces are OK. + +# price slots dispensename name +# eg +# 550 88,00 5out $5 withdrawal + 550 88,00 5out $5 withdrawal + 110 -- snack tasty snack -- 2.20.1