#!/home/hvgarnett/local/bin/python
import sys
from flup.server.fcgi_fork import WSGIServer
import cgi
import xml.dom
import xml.dom.minidom
import urllib
from random import randrange
import random

def choose(seq):
	if len(seq) == 19:
		return seq[random.randrange(10)+random.randrange(10)]
	else:
		return random.choice(seq)

invocation = [
"For the sake of all Creation,",
"If I wish to achieve my goals,",
"That Which Must Be has been revealed to me, and it is that"
]

group = [
	"the Bronze Faction",
	"House Mnemon",
	"the Temple of the Violet Lotus",
	"the Funereal Order of Righteous Morticians and Embalmers",
	"the Cult of the Illuminated",
	"the Silver Faction",
	"the Western Trade Alliance",
	"House Nellens",
	"Gens Karal",
	"the Order of Courtly Flame",
	"the Salinan Society",
	"the Heptagram",
	"the Terrestrial Exalted",
	"the Hushed Ones",
	"the Thousand Streams River",
	"House V'neef",
	"the Seneschals of the Sun-Kings",
	"the Brass Faction",
	"the Emissaries of Perfect Water",
	"the Winding Path",
	"the Immaculate Order",
	"House Ferem",
	"the Bureau of Destiny",
	"House Sesus",
	"the Lunar Exalted",
	"the Court of Seasons",
	"the Calendar Gods",
	"an Icewalker Tribe",
	"the Realm",
	"the Celestial Lions",
	"the Blind Mountain Society",
	"the Convention on Deathlords",
	"the Seventh Legion",
	"the Convention on Air",
	"House Cathak",
	"Gens Yushoto",
	"the Independents",
	"Gens Teresu",
	"the Initiates of the Unblinking Eye",
	"the Swords of Luna",
	"the Five Shade Association",
	"House Ragara",
	"the Bureau of Seasons",
	"House Tepet",
	"the Crossroads Society",
	"the Bureau of Humanity",
	"House Manosque",
	"the Cloister of Wisdom",
	"the Forest Witches",
	"the Mamorean Circle",
	"the Grass Spiders",
	"the Gold Faction",
	"the Seventeenth Legion",
	"the Wardens of Gaia",
	"House Ledaal",
	"the Silver Khatris",
	"the Angry Ghosts",
	"House Peleps",
	"the Egg Hunters",
	"the Mountain Folk",
	"the Solar Exalted",
	"the Guild",
	"the Convention on Water",
	"the Akuma",
	"the Three-Edged Sword",
	"the Brides of Ahlat",
	"Gens Amilar",
	"House Cynis",
	"Gens Varil",
	"the Confederation of Rivers",
	"the Thousand Scales",
	"the Haslanti League",
	"the Deathlords",
	"the Tya",
	"the Lintha",
	"Gens Maheka",
	"Mansef Jaghun",
	"the Shadow Puppeteers",
	"the Auspicious Committee for Exalted Health and Welfare",
	"Majmun di Kauterion",
	"House Iselsi",
	"the Ice Squadron",
	"the Linowa",
	"the White Veil Society",
	"the Sunset Empire",
	"the Bureau of Heaven",
	"Janara",
	"the All-Seeing Eye",
	"the Council of Thirteen",
	"Chayan Monks",
	"the Shayanti",
	"the Triad of Ice",
	"a Perfect Circle",
	"the Dragon Kings",
	"the Silver Pact",
	"the Greater Metropolitan Five District Planning Group",
	"the Bureau of Nature",
	"the Incarnae",
	"a Guild front",
	"the Ministry of Day and Night"
]

action = [
	"discover",
	"kill",
	"overthrow",
	"love",
	"lead",
	"betray",
	"dance with",
	"marry",
	"join",
	"sympathise with",
	"submit to",
	"murder",
	"hate",
	"fear",
	"ally with",
	"gain",
	"lose",
	"declare war on",
	"find",
	"repair",
	"have a child with",
	"understand",
	"heal",
	"fight",
	"misunderstand",
	"fail",
	"succeed",
	"talk",
	"eat with",
	"eat",
	"rival",
	"hunt",
	"flee",
	"admire",
	"pity",
	"destroy",
	"torture",
	"desire",
	"believe in",
	"envy",
	"shun",
	"clean",
	"cover up",
	"befriend",
	"dirty",
	"portray",
	"teach",
	"change",
	"denounce",
	"convert to",
	"dominate",
	"confess",
	"interrogate",
	"rebel against",
	"beg",
	"relieve",
	"impersonate",
	"dominate",
	"reject",
	"equip",
	"hire",
	"rescue",
	"challenge",
	"champion",
	"conceal",
	"oppose",
	"awaken",
	"kidnap",
	"mutilate",
	"avenge",
	"steal from",
	"steal",
	"justify",
	"enchant",
	"patronise",
	"adopt",
	"defend",
	"mock",
	"praise",
	"censure",
	"immortalise",
	"burn",
	"follow",
	"break",
	"possess",
	"study",
	"consider",
	"free",
	"hide from",
	"emulate",
	"seduce",
	"train",
	"deny",
	"pursue",
	"inspire",
	"reveal",
	"terrify",
	"spread rumours about",
	"investigate",
	"blow up"
]

item = [
	"a Crown of Thunder",
	"coral snake venom",
	"a pattern spider",
	"a Daiklave of Conquest",
	"a hellforged artifact",
	"a windblade",
	"a Fire-aspect hearthstone",
	"an Infernal manse",
	"trousers",
	"the blood of a Yozi",
	"a crystallized Fae",
	"the Godspear",
	"Chiaroscuro glass",
	"blackmail material",
	"Dominica's Mantle",
	"a velocipede",
	"Gem",
	"a ten-thousand pronged iron spear",
	"firmin resin",
	"the Daystar",
	"a teacup",
	"yellow jade",
	"a yasal crystal",
	"an ironwood desk",
	"an Earth-aspect hearthstone",
	"a tiger",
	"an acid-rime blade",
	"one ton of firedust",
	"the Lotus Root",
	"an auhzian",
	"celestial wine",
	"dragon-smothering elixir",
	"the Mantle of Brigid",
	"incriminating evidence",
	"the Skull Diary",
	"a little black dress",
	"Manacles of Night",
	"a tall dark stranger",
	"Yozi venom",
	"the Eye of Autochthon",
	"celestial cocaine",
	"the Perfected Lotus Blossom",
	"an orichalcum crown",
	"Daric's Laws of Magic",
	"a Metal-aspected manse",
	"diamonds",
	"a shocking insight",
	"the Broken-Winged Crane",
	"a soulsteel artifact",
	"a steam cannon",
	"a piece of the Neverborn",
	"a trident",
	"Death at the Root",
	"the Book of Bone & Ebony",
	"tickets to that thing you love",
	"the Silver-Flash Grotto",
	"a pie",
	"a Sijanese embalming hook",
	"a Forgotten Blade",
	"a Shogunate-era sundial",
	"an Infinite Fetish Ensemble",
	"a creation of Alveua",
	"a Water-aspect hearthstone",
	"a Protoshinmaic Vortex",
	"the Infinitely Unfolding Manse",
	"Eye of the Fire Dragon",
	"the Sceptre of Peace & Order",
	"the Book of Shadows",
	"the Girdle of the Cthonic Baara",
	"the Iron Wood Winery",
	"a Sky-Mantis Tower",
	"a war boomerang",
	"the Five-Metal Shrike",
	"the biography of Saibok Gauto",
	"a royal warstrider",
	"the Book of Three Circles",
	"an apple",
	"a Thousand-Forged Dragon",
	"Salina's Girdle",
	"a Living Jade Staff",
	"the Golden Asp",
	"a candlestick",
	"poisoned fruit",
	"Oadenol's Codex",
	"peaches of immortality",
	"a Soulbreaker Orb",
	"the Soul Mirror",
	"raw fallen starmetal",
	"a Wood-aspect hearthstone",
	"the Orb of Peace & Order",
	"an intelligent animal",
	"deceitful cake",
	"a Wood Dragon's Tongue",
	"the Soul Mirror",
	"a severed head",
	"gunzosha armour",
	"the Brass Leviathan",
	"a starmetal fish",
	"the Black Treatise",
	"the White Treatise"
]

character = [
	"Bright Crimson Sunset",
	"the Perfect",
	"Tepet Ejava",
	"Raksi",
	"the Maiden of Darkness in Bloodstained Robes",
	"Peleps Deled",
	"Harmonious Arbiter",
	"an unwed mother",
	"Ragara Bhagwei",
	"Flashing Peak",
	"Anja Silverclaws",
	"Thrice-Radiant Misho",
	"Cevis Ghandarva",
	"Eye & Seven Despairs",
	"Sikunare",
	"a Tyrant Lizard",
	"Mother Transgression",
	"Andilar Caresgil",
	"Luxana",
	"Admiral Sand",
	"Halkomelem",
	"Elegant Nova of Progression",
	"Gri-Fel",
	"Queen Merela",
	"Want of Nothing",
	"Cathek Meladus",
	"Swims in Shadows",
	"Ragoly Aglde",
	"V'Neef Risa",
	"Kura Murasu",
	"Demetheus",
	"the Scarlet Empress",
	"Madame Vert",
	"Han-Tha",
	"Sonorous Aria",
	"Echinna",
	"a rice farmer",
	"Hran-Tzu",
	"Regent Fokuf",
	"the Righteous Devil",
	"Thousand-Faceted Nelumbo",
	"Tachi-Kun",
	"Jagalza",
	"Ledaal Kes",
	"Peleps Aramida",
	"Maru",
	"a lizard",
	"Sulumor",
	"White Bone Sinner",
	"an elemental",
	"Shining Barrator",
	"Jiunan Nightwarden",
	"Karal Linwei",
	"Strength-of-Many",
	"Cacophonic Conductor of the Steel Chorus",
	"Mnemon",
	"Admiral Tirak",
	"Nasri",
	"Harmonious Jade",
	"Falling Tears Poet",
	"the Laughing Boy",
	"Faka Kun",
	"Unwanted Whisper",
	"Sesus Rafara",
	"Ligier",
	"Elias Tremalion",
	"Qaf",
	"Mask of Winters",
	"Ma-Ha-Suchi",
	"Ghataru",
	"Lytek",
	"Autochthon",
	"a small god",
	"Wong Bongerok",
	"Tepet Arada",
	"Berengiere",
	"Teresu Gido",
	"Samea",
	"Seven Devils Clever",
	"Mardukth, Who Holds In Thrall",
	"Amera",
	"the Lady of Darkness in Bloodstained Robes",
	"Kajeha Lef",
	"Jasara",
	"Behemoth",
	"the Unconquered Sun",
	"Ragara Myrrun",
	"Blue Crest Merchant",
	"a tax collector",
	"Typhon",
	"Baxishun",
	"twins",
	"Captain Buruku",
	"Weeping Raiton Cast Aside",
	"Cynis Avaku",
	"Yaogin",
	"Oliphem",
	"an eight-tailed mole hound",
	"Golden-Eyed Jorst",
	"the Ebon Dragon"
]

place = [
	"the Auberge",
	"the Imperial City",
	"Icehome",
	"Kirighast",
	"Gethamane",
	"Pangu",
	"the City of the Steel Lotus",
	"Rajtul",
	"Mount Metagalapa",
	"Melevil",
	"Inara",
	"Onyx",
	"Abalone",
	"Fort Bear",
	"the Serene Hall of Divided Contemplation",
	"Kunlun-Shan",
	"Lord's Crossing",
	"Lodros",
	"Inara",
	"Whitewall",
	"the Lap",
	"Hrotsvitha",
	"the North",
	"Chanos",
	"Ravanna",
	"Sijan",
	"the Neck",
	"Rakshastan",
	"Greyfalls",
	"Bluehaven",
	"Nexus",
	"Sdoia",
	"the Labyrinth",
	"Rathess",
	"the Delzahn Empire",
	"Mahalanka",
	"Larjyn",
	"Basten",
	"Deren's Ford",
	"Fair Isle",
	"Varsi",
	"Sunken Luthe",
	"Hundred Kingdoms",
	"Celeren",
	"Shadowholt",
	"the Coral Archipelago",
	"Halta",
	"the East",
	"Cold House",
	"the desert of Cecelyne",
	"Chiaroscuro",
	"Farhold",
	"Varangia",
	"Harborhead",
	"Port Rawar",
	"the Blessed Isle",
	"Lodros",
	"Paragon",
	"Cherak",
	"Gem",
	"the Hall of Celestial Stability",
	"An-Teng",
	"Windcreche",
	"Lookshy",
	"the South",
	"Ironfall",
	"the Skullstone Archipelago",
	"Tusktad",
	"Thorns",
	"Azure",
	"Port Calin",
	"the Wavecrest Archipelago",
	"Marita",
	"Stygia",
	"the West",
	"Sperimin",
	"the Fortress of Crimson Ice",
	"Kajeth",
	"the Imperial Mountain",
	"Chanta",
	"Juche",
	"Fella",
	"Mother Bog",
	"Crystal",
	"the Realm",
	"Yane",
	"Great Forks",
	"Yu-Shan",
	"Graf-Vindak",
	"the Thousand",
	"the Circus Radiant",
	"Diamond Hearth",
	"Malfeas",
	"Rubylak",
	"Yane",
	"the slopes of Qaf",
	"the Underworld",
	"Arjuf",
	"the City of the Shining Reefs",
	"Denandsor"
]

date = [
	"Calibration",
	"daytime",
	"Descending Water",
	"Ascending Fire",
	"Resplendent Earth",
	"Ascending Air",
	"Descending Air",
	"Ascending Water",
	"Descending Wood",
	"Ascending Earth",
	"Resplendent Fire",
	"Resplendent Wood",
	"Descending Earth",
	"Resplendent Air",
	"Ascending Wood",
	"Descending Fire",
	"Resplendent Water",
	"nighttime",
	"the Solar Eclipse"
]

def base_builder(subject,verb,object):
	return "%s must %s %s",[choose(q) for q in (subject,verb,object)]

def cond_builder(preposition,object):
	inner_stmt, inner_args = run_template()
	if preposition not in inner_args:
		inner_args += [preposition, choose(object)]
		inner_stmt += " %s %s"
	return inner_stmt, inner_args

def reviser(articles,replacements):
	inner_stmt, inner_args = run_template()
	for i in range(len(articles)):
		inner_args[articles[i]] = replacements[i]
	return inner_stmt, inner_args

template = [
(base_builder,(group,action,item)),
(base_builder,(place,action,group)),
(base_builder,(character,action,place)),
(base_builder,(place,action,item)),
(base_builder,(character,action,character)),
(base_builder,(group,action,group)),
(base_builder,(place,action,place)),
(base_builder,(character,action,item)),
(base_builder,(group,action,character)),
(base_builder,(place,action,character)),
(base_builder,(group,action,place)),
(base_builder,(character,action,group)),
(cond_builder,("in",place)),
(cond_builder,("outside",place)),
(cond_builder,("before",date)),
(cond_builder,("during",date)),
(cond_builder,("with",item)),
(reviser,([0],["I"])),
(reviser,([1,2],["blow up","Gem"]))
]

def run_template():
	inner_stmt, inner_args = choose(template)
	return inner_stmt(*inner_args)

def test(environ,responder):
 #if environ.has_key("QUERY_STRING") and environ["QUERY_STRING"][:9]=="resource=":
 # 
 # responder('200 OK',[('Content-Type',)])
 #else:
  pattern, nodes = run_template()
  responder('200 OK',[('Content-Type','text/html')])
  yield """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Hubristic Destiny</title>
<BODY>
<h1><center>
	<font color="#e4c548">&#9791;</font>
	<font color="#303d9a">&#9792;</font>
	<font color="#d2211f">&#9794;</font>
	<font color="#247941">&#9795;</font>
	<font color="#552256">&#9796;</font>
</center></h1>

<center><big><big><big><i>%s <b>%s</b>.</i></big></big></big></center>

<h1><center>
	<font color="#e4c548">&#9791;</font>
	<font color="#303d9a">&#9792;</font>
	<font color="#d2211f">&#9794;</font>
	<font color="#247941">&#9795;</font>
	<font color="#552256">&#9796;</font>
</center></h1>

</BODY>
</HTML>
"""%(choose(invocation),pattern%tuple(nodes))

WSGIServer(test).run()

