### ### Rune Word Guru ### ### Version 20041118 ### ### by Nick Vargish ### ### See LICENSE.TXT for copyright and license details. ### ### ### rwg.py ### ### Web application code for Rune Word Guru ### from mod_python import apache, Cookie, util from sets import Set import os import runes import runewords datadir = os.path.dirname(__file__) + '/' head = ''' Rune Word Guru
''' foot = '''
''' formbuttons = '''
 
 
 
 If I had rune
Show me
Sort by
 
''' runedict = runes.RuneDict(datadir + 'runes.xml') rwdict = runewords.RunewordDict(datadir + 'runewords.xml', runedict) def handler(req): """mod_python entry point for the web application.""" req.content_type = 'text/html' content = makepage(req) req.write(content) return apache.OK def allruneids(): """Returns a list of all valid rune ids ('1' ... '33').""" return ([ '%d' % (x) for x in range(1, 34) ]) def makepage(req): """Return an HTML document that will be presented to the user.""" c = [] req.cgidata = util.FieldStorage(req, keep_blank_values=True) req.mycmd = req.cgidata.get('submit', '') req.myrunes = req.cgidata.get('runes', []) req.myalmost = req.cgidata.get('almost', '') req.myfilter = req.cgidata.get('filter', '') req.myorder = req.cgidata.get('order', '') req.mynode = req.cgidata.get('node', '') if req.mycmd == 'Save my runes': saverunes(req) elif req.mycmd == 'Load my runes': loadrunes(req) elif req.mycmd == 'Set all': setall(req) elif req.mycmd == 'Clear all': clearall(req) elif req.mycmd != 'Find Rune Words' and not req.mynode: req.mynode = 'home' req.mycmd = 'Find Rune Words' c.append(head) c.append(makeform(req)) c.append('
') if req.mynode: c.append(makenode(req.mynode)) else: c.append(findrunewords(req)) c.append('
') c.append(foot) return ''.join(c) def runecheck(runenum, haverune): """Returns a table element for a rune checkbox.""" if haverune: checked = 'checked="1"' else: checked = '' return ' %s' \ % (runenum, checked, runedict[str(runenum)].name) def makeform(req): """Builds the form interface for the application.""" c = [] c.append('
') c.append('
') c.append('') # start of form table c.append('') # end of rune table c.append('') # end of other form elements c.append('
') # start of rune table for row in range(0, 11): c.append('') for col in range(0, 3): runenum = (row + (col * 11)) + 1 runeid = str(runenum) haverune = False if runeid in req.myrunes: haverune = True c.append('' \ % (runecheck(runenum, haverune))) c.append('') c.append('
%s 
') # start of other form elements fv = {} fv['almost'] = '' if req.myalmost: fv['almost'] = 'checked' fv['all'] = fv['armor'] = fv['helm'] = fv['shield'] = fv['weapon'] = '' fv[req.myfilter] = 'selected' fv['name'] = fv['type'] = fv['level'] = '' fv[req.myorder] = 'selected' c.append(formbuttons % fv) c.append('
') # end of form table c.append('
') c.append('
') return '\n'.join(c) def makenode(node): """Returns the content for a text node.""" c = [] fname = '%s/node_%s.html' % (datadir, node) if node == "runes" and not os.path.exists(fname): f = file(fname, 'w') f.write(runedict.makehtmltable()) f.close() try: f = file(fname, 'r') c.append(f.read()) f.close() except: pass return '\n'.join(c) def runewordtablerow(r, rowspec=0): """Returns a table row describing a runeword.""" c = [] c.append('' % (rowspec)) c.append('') c.append('%s
' % (r.name)) rword = r.word for mw in r.missing: mname = runedict[mw].name rword = rword.replace(mname, \ '%s' % (mname)) c.append('%s
' % (rword)) c.append('(%d sockets)' \ % (len(r.sockets))) c.append('') c.append('') c.append('%s
' \ % (', '.join([ t.capitalize() for t in r.itype ]))) if r.items: c.append('%s
' % (r.items)) c.append('
') c.append('Level %s
' % (r.level)) if r.restrictions: rl = [ '%s' % (x) \ for x in r.restrictions ] c.append('
'.join(rl)) c.append('') c.append('') bl = [ '%s' % (x) \ for x in r.bonuses ] c.append('
'.join(bl)) c.append('') c.append('') return '\n'.join(c) def orderwordlist(rwl, orderby): """Returns a sorted rune word list.""" if orderby == 'level': tmpl = [ (int(x.level), x) for x in rwl ] elif orderby == 'type': tmpl = [ (x.itype, x) for x in rwl ] else: tmpl = [ (x.name, x) for x in rwl ] tmpl.sort() rwl = [ x for (key, x) in tmpl ] return rwl def makeablerunewords(req): """Generates a rune word list of makeable rune words.""" almost = 0 if req.myalmost: almost = 1 rwl = rwdict.makeablewith(req.myrunes, filter=req.myfilter, almost=almost) return rwl def findrunewords(req): """Returns an HTML table of rune words makeable by data stored in req.""" c = [] rwl = makeablerunewords(req) if not rwl: return '

No rune words can be made from those runes, sorry!

' rwl = orderwordlist(rwl, req.myorder) c.append('') pl = 's' if len(rwl) == 1: pl = '' c.append('' \ % (len(rwl), pl)) count = 0 for r in rwl: c.append(runewordtablerow(r, count)) count = (count + 1) % 2 c.append('
%d Rune Word%s
') return '\n'.join(c) def saverunes(req): """Set a cookie on the client that stores a list of runes.""" uc = Cookie.Cookie('runes', ','.join(req.myrunes)) Cookie.add_cookie(req, uc) def loadrunes(req): """Retrieve the runes stored in a cookie on the client browser.""" cookies = Cookie.get_cookies(req) if cookies.has_key('runes'): req.myrunes = cookies['runes'].value.split(',') else: req.myrunes = [] def setall(req): """Selects all runes.""" req.myrunes = allruneids() def clearall(req): """Deselects all runes.""" req.myrunes = []