poodirect.py
poo/poodirect.py · 2.9 KB
From the archive poo.tar.gz
Download the archive « Back to the archive contents
#----------------------------------------------------------------------
# poodirect.py JJS 8/15/97
#
# This module serves as a main program for poo.py,
# and connects the engine directly to the terminal.
#----------------------------------------------------------------------
#----------------------------------------------------------------------
import poo
import sys
import string
term = sys.stdout
def AskYesNo(prompt, default='Y'):
while 1:
ans = raw_input(prompt + ' [' + default + '] ')
if len(ans) > 1: ans = ans[0]
if ans == '': ans = default
if ans == 'y' or ans == 'Y': return 1
if ans == 'n' or ans == 'N': return 0
print ("Please enter Y or N.")
def startup():
# check for poo.dat
try:
file = open('poo.dat', 'r')
file.close()
good = 1
except:
good = 0
if good:
print "Loading database..."
poo.initialize() # load file
else:
print "Cannot open data file 'poo.dat'."
if not AskYesNo("Create new database from scratch?"):
return 0
poo.initialize(1)
return 1
def login(name):
matches = filter(lambda x,a=name: x.__isuser and x.name==a, \
poo.gObjlist.values())
if not matches:
print "User", name, "not found."
return 0
user = matches[0]
user.Login(term)
return user
def play():
# show login message
try:
f = open('connect.txt','r')
for line in f.readlines(): print line[:-1]
except: pass
name = raw_input("Name: ")
print "Password: (not needed in direct mode)"
user = login(name)
if not user: return
done = 0
while not done:
sys.stdout = term
cmd = raw_input('\n')
print
if cmd == "quit" or cmd == "@quit": done = 1
elif cmd == "savedb": poo.gSave()
else:
user.handleMsg(cmd)
for i in range(0,10):
poo.gUpdate()
try: user.Logout()
except: print "Problem calling user.Logout()"
sys.stdout = term
if AskYesNo("Save database?"): poo.gSave()
def readFile():
name = raw_input("Login Name: ")
user = login(name)
if not user: return
fname = raw_input("Filename: ")
file = open(fname, 'r')
lines = file.readlines()
for i in range(0,len(lines)):
print fname + ">" + lines[i]
user.handleMsg(string.rstrip(lines[i]))
for i in range(0,5):
poo.gUpdate()
try: user.Logout()
except: print "Problem calling user.Logout()"
sys.stdout = term
if AskYesNo("Save database?"): poo.gSave()
def editHelp():
import poohelp
poohelp.hLoad()
poohelp.popDB()
if AskYesNo("Save help database?"): poohelp.hSave()
def setTreasury():
instr = raw_input("Enter Treasury amount [" \
+ str(poo.gTreasury) + "]: ")
if instr:
poo.gTreasury = string.atoi(instr)
if AskYesNo("Save database?"): poo.gSave()
def menu():
done = 0
while not done:
print
print "1. Enter POO"
print "2. Edit help database"
print "3. Read commands from a file"
print "4. Set the treasury amount"
print "0. Quit"
print
choice = input(">")
if choice == 1: play()
elif choice == 2: editHelp()
elif choice == 3: readFile()
elif choice == 4: setTreasury()
elif choice == 0: done=1
if startup():
menu()