#!/usr/bin/env python import cgi import cgitb import os import sys import random import game def getToken( form ): keys = form.keys() if 2 != len( keys ): return [] if 'token' in form.keys(): return form['token'].value return None def getCommand( form ): keys = form.keys() if 2 != len( keys ): return None if 'token' == keys[0]: return form[keys[1]].name else: return form[keys[0]].name def emitInteraction( i ): return '' % (i.title(), i.command()) def drawMap( map ): ## ## assume -8 to 8 in x and y ## # print '', # for line in map: # for token in line: # print '' % token.ascii( None ), # print '' # print '
%s
' print '
'
  count = 1
  for line in map:
    for token in line:
      if count == 145:
        print '@',
      else:
        print token.ascii( None ),
      count = count + 1
    print

def emitMessages():
  pass

def getCardinal( l, c ):
  for x in l:
    if c == x.title():
      return emitInteraction( x )
  return '' % (c,c)

def createForm( g ):
  print '
' print '' % g.dump() interactions = g.player().listInteractions( None ) moves=[] for interaction in interactions: if 'move' in interaction.hint(): moves.append( interaction ) for move in moves: interactions.remove( move ) w = sys.stdout.write w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '' ) w( '
 ' ) w( getCardinal( moves, "Up" ) ) w( ' 
' ) w( getCardinal( moves, "Northwest" ) ) w( '' ) w( getCardinal( moves, 'North' ) ) w( '' ) w( getCardinal( moves, 'Northeast' ) ) w( '
' ) w( getCardinal( moves, 'West' ) ) w( '' ) w( ' ' ) # w( str( g.player().location() ) ) w( '' ) w( getCardinal( moves, 'East' ) ) w( '
' ) w( getCardinal( moves, 'Southwest' ) ) w( '' ) w( getCardinal( moves, 'South' ) ) w( '' ) w( getCardinal( moves, 'Southeast' ) ) w( '
 ' ) w( getCardinal( moves, "Down" ) ) w( ' 
' ) print for interaction in interactions: print emitInteraction( interaction ) print '
' cgitb.enable() print 'Content-Type: text/html\n' form = cgi.FieldStorage() token = getToken( form ) command = getCommand( form ) g = game.gameClass() if not token: ## start a new game token = g.newGame() g.load( token ) if command: g.player().interact( command ) loc = g.player().location() zone = g.map().getFill( loc[0], loc[1], loc[2] ).zone( None ) title = g.map().getFill( loc[0], loc[1], loc[2] ).title( None ) if zone and title: print '%s: %s' % (zone,title) print '

%s: %s

' % (zone,title) elif zone: print '%s' % zone print '

%s

' % zone elif title: print '%s' % title print '

%s

' % title drawMap( g.player().getMap( g.map() ) ) #print '
' #print 'Messages
' #emitMessages() print '
' print 'Commands
' createForm( g )