def l(self, uuid): mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) dloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) html = htmlheader html += """<br/> <br/> <br /> <div class="left inner"> <h2>Files available in loot %s </h2>""" % dloot.getname(uuid) html += htmlnav html += "<table>" html += "<tr><td>Filename</td><td>Fetch</td></tr>" for fileinindex in mindex.search("^((?!forban).)*$", uuid): filei = fileinindex.rsplit(",",1)[0] if re.search('/\.',filei): continue if re.search('^\+\.',filei): continue html += "<tr>" sourcev4 = dloot.getipv4(uuid) sourcev6 = dloot.getipv6(uuid) size = tools.convertbytes(mindex.getfilesize(filename=filei,uuid=uuid)) html += """<td>%s (%s)</td><td><a href="/s/?g=%s&f=b64e">get</a></td> """ % (filei,size,base64e.encode(filei)) html += "</tr>" html += "</table>" html += htmlfooter return html
def q(self, v=None, r=None): querystring = v mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) discoveredloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) searchresult = [] for name in discoveredloot.listall(): if (discoveredloot.exist(name) and discoveredloot.lastannounced(name)): fileavailable = mindex.search( uuid=name, query=querystring) for filefound in fileavailable: searchresult.append((filefound,name)) searchresult.sort() html = htmlheader html += "<title>search results of %s</title>" % (querystring) if r is not None: html += """<meta http-equiv="refresh" content="%s">""" % (r) html += "</head>" html += htmlnav html += """<br/> <br/> <div class="left inner">""" previousfile = None html += "<table><tr><th>Filename</th><th>Available on</th></tr>" for foundfiles in searchresult: if foundfiles[0] == previousfile: html += """<a href="%s">%s</a> """ % (forban_geturl(uuid=foundfiles[1],filename=filename),discoveredloot.getname(foundfiles[1])) elif previousfile == None: filename = foundfiles[0].rsplit(",",1)[0] html += """<td>%s</td> <td><a href="%s">%s</a> """ % (foundfiles[0].rsplit(",",1)[0],forban_geturl(uuid=foundfiles[1],filename=filename),discoveredloot.getname(foundfiles[1])) else: filename = foundfiles[0].rsplit(",",1)[0] html += """</td></tr><td>%s</td> <td><a href="%s">%s</a> """ % (foundfiles[0].rsplit(",",1)[0],forban_geturl(uuid=foundfiles[1],filename=filename),discoveredloot.getname(foundfiles[1])) previousfile=foundfiles[0] html += "</td></tr></table></div>" return html
def v(self, uuid): mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) dloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) missingfiles = mindex.howfar(uuid) html = htmlheader html += """<br/> <br/> <br/> <div class="left inner"> <h2>Missing files on your loot from %s </h2>""" % dloot.getname(uuid) html += htmlnav html += "<table>" if missingfiles is None: html += "You are not missing any files with %s " % dloot.getname(uuid) else: for filemissed in missingfiles: html += "<tr>" sourcev4 = dloot.getipv4(uuid) sourcev6 = dloot.getipv6(uuid) html += """<td>%s</td><td><a href="http://%s:12555/s/?g=%s&f=b64e">v4</a></td> """ % (filemissed,sourcev4,base64e.encode(filemissed)) if sourcev6 is not None: html += """<td><a href="http://[%s]:12555/s/?g=%s&f=b64e">v6</a></td>""" % (sourcev6, base64e.encode(filemissed)) html += "</tr>" html += "</table>" html += htmlfooter return html
def v(self, uuid): mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) dloot = loot.loot(dynpath=os.path.join(forbanpath, "var")) missingfiles = mindex.howfar(uuid) html = htmlheader html += """<br/> <br/> <br/> <div class="left inner"> <h2>Missing files on your loot from %s </h2>""" % dloot.getname( uuid) html += htmlnav html += "<table>" if missingfiles is None: html += "You are not missing any files with %s " % dloot.getname( uuid) else: for filemissed in missingfiles: html += "<tr>" sourcev4 = dloot.getipv4(uuid) sourcev6 = dloot.getipv6(uuid) html += """<td>%s</td><td><a href="http://%s:12555/s/?g=%s&f=b64e">v4</a></td> """ % ( filemissed, sourcev4, base64e.encode(filemissed)) if sourcev6 is not None: html += """<td><a href="http://[%s]:12555/s/?g=%s&f=b64e">v6</a></td>""" % ( sourcev6, base64e.encode(filemissed)) html += "</tr>" html += "</table>" html += htmlfooter return html
def l(self, uuid): mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) dloot = loot.loot(dynpath=os.path.join(forbanpath, "var")) html = htmlheader html += """<br/> <br/> <br /> <div class="left inner"> <h2>Files available in loot %s </h2>""" % dloot.getname( uuid) html += htmlnav html += "<table>" html += "<tr><td>Filename</td><td>Fetch</td></tr>" tempindex = mindex.search("^((?!forban).)*$", uuid) tempindex.sort(key=string.lower) for fileinindex in tempindex: filei = fileinindex.rsplit(",", 1)[0] if re.search('/\.', filei): continue if re.search('^\+\.', filei): continue html += "<tr>" sourcev4 = dloot.getipv4(uuid) sourcev6 = dloot.getipv6(uuid) size = tools.convertbytes( mindex.getfilesize(filename=filei, uuid=uuid)) html += """<td>%s (%s)</td><td><a href="/s/?g=%s&f=b64e">get</a></td> """ % ( filei, size, base64e.encode(filei)) html += "</tr>" html += "</table>" html += htmlfooter return html
def handle(self): data = self.request[0].strip() socket = self.request[1] if data[:6] == "forban": myloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) myloot.add(data, self.client_address[0]) else: print "debug : not a forban message"
def cache(self, uuid): cachepath = os.path.join(self.lootdir, uuid, "cache") if not os.path.exists(cachepath): os.makedirs(cachepath) lloot = loot.loot(self.dynpath) for url in lloot.getindexurl(uuid): hmacannounced = lloot.gethmac(uuid) hmaccalculated = self.calchmac(cachepath + "/forban/index") # only fetch the index if the HMAC is different if not hmacannounced == hmaccalculated: fetch.urlget(url, cachepath + "/forban/index")
def cache (self, uuid): cachepath = os.path.join (self.lootdir, uuid, "cache") if not os.path.exists(cachepath): os.makedirs(cachepath) lloot = loot.loot(self.dynpath) for url in lloot.getindexurl(uuid): hmacannounced = lloot.gethmac(uuid) hmaccalculated = self.calchmac(cachepath+"/forban/index") # only fetch the index if the HMAC is different if not hmacannounced == hmaccalculated: fetch.urlget(url, cachepath+"/forban/index")
def q(self, v=None, r=None): querystring = v mindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) discoveredloot = loot.loot(dynpath=os.path.join(forbanpath, "var")) searchresult = [] for name in discoveredloot.listall(): if (discoveredloot.exist(name) and discoveredloot.lastannounced(name)): fileavailable = mindex.search(uuid=name, query=querystring) for filefound in fileavailable: searchresult.append((filefound, name)) searchresult.sort() html = htmlheader html += "<title>search results of %s</title>" % (querystring) if r is not None: html += """<meta http-equiv="refresh" content="%s">""" % (r) html += "</head>" html += htmlnav html += """<br/> <br/> <div class="left inner">""" previousfile = None html += "<table><tr><th>Filename</th><th>Available on</th></tr>" for foundfiles in searchresult: if foundfiles[0] == previousfile: html += """<a href="%s">%s</a> """ % (forban_geturl( uuid=foundfiles[1], filename=filename), discoveredloot.getname(foundfiles[1])) elif previousfile == None: filename = foundfiles[0].rsplit(",", 1)[0] html += """<td>%s</td> <td><a href="%s">%s</a> """ % ( foundfiles[0].rsplit(",", 1)[0], forban_geturl(uuid=foundfiles[1], filename=filename), discoveredloot.getname(foundfiles[1])) else: filename = foundfiles[0].rsplit(",", 1)[0] html += """</td></tr><td>%s</td> <td><a href="%s">%s</a> """ % ( foundfiles[0].rsplit(",", 1)[0], forban_geturl(uuid=foundfiles[1], filename=filename), discoveredloot.getname(foundfiles[1])) previousfile = foundfiles[0] html += "</td></tr></table></div>" return html
def forban_geturl(uuid=None, filename=None, protocol="v4"): if uuid is None or filename is None: return False discoveredloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) if not discoveredloot.exist(uuid): return False if protocol == "v4": ip = discoveredloot.getipv4(uuid) else: ip = discoveredloot.getipv6(uuid) if protocol == "v4": url = "http://%s:12555/s/?g=%s&f=b64e" % (ip,base64e.encode(filename)) else: url = "http://[%s]:12555/s/?g=%s&f=b64e" % (ip,base64e.encode(filename)) return url
def forban_geturl(uuid=None, filename=None, protocol="v4"): if uuid is None or filename is None: return False discoveredloot = loot.loot(dynpath=os.path.join(forbanpath, "var")) if not discoveredloot.exist(uuid): return False if protocol == "v4": ip = discoveredloot.getipv4(uuid) else: ip = discoveredloot.getipv6(uuid) if protocol == "v4": url = "http://%s:12555/s/?g=%s&f=b64e" % (ip, base64e.encode(filename)) else: url = "http://[%s]:12555/s/?g=%s&f=b64e" % (ip, base64e.encode(filename)) return url
efilter = config.get('opportunistic','efilter') except ConfigParser.Error: efilter = None refilter = re.compile(ofilter, re.I) if efilter is not None: exfilter = re.compile(efilter, re.I) else: exfilter = re.compile("", re.I) try: maxsize = config.get('opportunistic','maxsize') except ConfigParser.Error: maxsize = 0 discoveredloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) allindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) allindex.build() flogger.info("forban_opportunistic starting...") flogger.info("applied including regexp filter: %s" % ofilter) flogger.info("applied excluding regexp filter: %s" % efilter) while(1): for uuid in discoveredloot.listall(): # check if my loot is not exceeding the maxsize mysize = allindex.totalfilesize(discoveredloot.whoami()) if float(maxsize) != 0: if mysize is False: continue
def index(self, directory=forbanshareroot): html = htmlheader html += htmlnav html += """<br/> <br/> <br/> <div class="right inner">""" html += """ <h2>Search the loot...</h2> """ html += """ <form method=get action="q/"><input type="text" name="v" value=""> <input type="submit" value="search"></form> """ html += """</div> <div class="left inner">""" html += """ <h2>Discovered link-local Forban available with their loot in the last 3 minutes</h2> """ html += "<table>" html += "<th><td>Access</td><td>Name</td><td>Last seen</td><td>Size</td><td>How many files are missing from yourself?</td><td></td></th>" discoveredloot = loot.loot(dynpath=os.path.join(forbanpath,"var")) mysourcev4 = discoveredloot.getipv4(discoveredloot.whoami()) allindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) for name in discoveredloot.listall(): if (discoveredloot.exist(name) and discoveredloot.lastannounced(name)): allindex.cache(name) if discoveredloot.lastannounced(name): html += "<tr>" rname = discoveredloot.getname(name) sourcev4 = discoveredloot.getipv4(name) if sourcev4 is not None: html += """<td><a href="http://%s:12555/">v4</a></td> """ % sourcev4 else: html += """<td></td>""" sourcev6 = discoveredloot.getipv6(name) if sourcev6 is not None: html += """<td><a href="http://[%s]:12555/">v6</a></td> """ % sourcev6 else: html += """<td></td>""" html += "<td>"+rname+"</td>" lastseen = discoveredloot.lastannounced(name) if lastseen is not None: html += """<td>%s secs ago</td>""" % lastseen else: html += "<td>never seen</td>" missingfiles = allindex.howfar(name) totalsize = allindex.totalfilesize(name) html +="<td>%s</td>" % str(totalsize) if type(missingfiles) is bool: html += "<td><b>Missing index</b> from this loot" elif missingfiles is not None: html += "<td>Missing %s files from this loot" % len(missingfiles) else: html += "<td>Missing no files from this loot" if name != discoveredloot.whoami(): html += """ <a href="http://%s:12555/v/%s">[missing?]</a> """ % (mysourcev4,name) html += """ <a href="http://%s:12555/l/%s">[browse]</a> """ % (mysourcev4,name) if name == discoveredloot.whoami(): html += """ <a href="/l/%s">[browse]</a> """ % (name) html += "<td><i>yourself</i></td>" else: html += "<td></td>" html += "</tr>" html += "</table></div>" html += htmlfooter return html
def debug(self, directory=forbanshareroot): html = htmlheader html += htmlnav html += """<br/> <br/> <br/> <div class="right inner">""" html += """ <h2>Search the loot...</h2> """ html += """ <form method=get action="q/"><input type="text" name="v" value=""> <input type="submit" value="search"></form> """ html += """</div> <div class="left inner">""" html += """ <h2>Discovered link-local Forban available with their loot in the last 3 minutes</h2> """ html += "<table>" html += "<th><td>Access</td><td>Name</td><td>Last seen</td><td>Size</td><td>How many files are missing from yourself?</td><td></td></th>" discoveredloot = loot.loot(dynpath=os.path.join(forbanpath, "var")) mysourcev4 = discoveredloot.getipv4(discoveredloot.whoami()) allindex = index.manage(sharedir=forbanshareroot, forbanglobal=forbanpath) for name in discoveredloot.listall(): if (discoveredloot.exist(name) and discoveredloot.lastannounced(name)): allindex.cache(name) if discoveredloot.lastannounced(name): html += "<tr>" rname = discoveredloot.getname(name) sourcev4 = discoveredloot.getipv4(name) if sourcev4 is not None: html += """<td><a href="http://%s:12555/">v4</a></td> """ % sourcev4 else: html += """<td></td>""" sourcev6 = discoveredloot.getipv6(name) if sourcev6 is not None: html += """<td><a href="http://[%s]:12555/">v6</a></td> """ % sourcev6 else: html += """<td></td>""" html += "<td>" + rname + "</td>" lastseen = discoveredloot.lastannounced(name) if lastseen is not None: html += """<td>%s secs ago</td>""" % lastseen else: html += "<td>never seen</td>" missingfiles = allindex.howfar(name) totalsize = allindex.totalfilesize(name) html += "<td>%s</td>" % str(totalsize) if type(missingfiles) is bool: html += "<td><b>Missing index</b> from this loot" elif missingfiles is not None: html += "<td>Missing %s files from this loot" % len( missingfiles) else: html += "<td>Missing no files from this loot" if name != discoveredloot.whoami(): html += """ <a href="http://%s:12555/v/%s">[missing?]</a> """ % ( mysourcev4, name) html += """ <a href="http://%s:12555/l/%s">[browse]</a> """ % ( mysourcev4, name) if name == discoveredloot.whoami(): html += """ <a href="/l/%s">[browse]</a> """ % (name) html += "<td><i>yourself</i></td>" else: html += "<td></td>" html += "</tr>" html += "</table></div>" html += htmlfooter return html
def monster_l1(inventory, stats): #Pulls your Statistics from stats hp = stats[0] base_damage = stats[1] #Here we prime the out put of our function being a dictionary. The fist key is out inventory, the second our stats, #The third, the type of monster, and the fourth the boolean false value. This is used for when we run. The parent #function will check this. If true, if will generate a random room to go to instead of the usual ones. output = { "inventory": inventory, "stats": stats, "enemy": "", "run": False } #Sets monster hp ehp = 100.0 #This next statement checks to see what weapon you have and sets your damage multiplier if inventory[1] == 1: weapon_damage_multiplier = 1.5 else: weapon_damage_multiplier = 1.0 #This next statment checks to seet what shield you have sets the armor damage reduction if inventory[3] == 1: armor_damage_reduction = .66 else: armor_damage_reduction = 1.0 #Creating the dictionary for get_response combat_options = { "Help": [ "\nYour options are 1: Attack", "\t\t 2: Defend", "\t\t 3: Run Away", "\t\t 4: Use Item" ], "Valid_Inputs": ["attack", "defend", "run", "item"] } item_options = { "Help": ["\nYour options are 1: Use Potion", "\t\t 2: Use Magic Scroll"], "Valid_Inputs": ["potion", "scroll", "magic"] } item_prompt = "Which item would you like to use? \n" #determines which monster you face enemy = randint(1, 2) #enemy=2 if enemy == 1: monster = "Pack of goblins" output["enemy"] = monster prompt = "What's your next move against the %s?.\n" % monster print "You have a encountered %s. Time for battle!!\n" % monster #setting a counter. The allows the while loop to alernate between your move and the monters counter = 0 #These lines allow attacks to deal more or less damage. Defending/running away will changing these hero_damage_multiplier = 1.0 monster_damage_multiplier = 1.0 #Initializes the monsters moves enraged = False decision = 1 #While loop in which the battle actually takes place while hp > 0 and ehp > 0: #If counter is Even, the hero moves if counter % 2 == 0: move = get_str_response(prompt, combat_options) if move == "attack": attack_damage = base_damage * weapon_damage_multiplier * hero_damage_multiplier ehp = ehp - attack_damage counter += 1 print "You heroically attack the %s for %f damage." % ( monster, attack_damage) #Resets the monster damage multiplier to make sure that defend only lasts one round monster_damage_multiplier = 1.0 elif move == "defend": print "You ready yourself against the %s's next attack." % monster monster_damage_multiplier = .2 counter += 1 #Uses your potion elif move == "item": item = get_str_response(item_prompt, item_options) if item == "potion": if inventory[4] > 0: print "You use a potion and restore hp. You feel restored." hp = 90 counter += 1 inventory[4] -= 1 monster_damage_multiplier = 1.0 else: print "You have no potions left" #Uses you magic scroll elif item == "magic" or item == "scroll": if inventory[5] > 0: print "You read the words off the magic scroll and suddenly a mighty lightning bolt", print " strikes your enemy for 50 damage." print "As you admire the damage the scroll dissolves in your hand." ehp = ehp - 50 counter += 1 inventory[5] -= 1 monster_damage_multiplier = 1.0 else: print "You have no magic scrolls left." elif move == "run": print "You successfully escape from the %s" % monster output["run"] = True output["stats"][0] = hp return output #If counter is odd the Monster moves elif counter % 2 == 1: #Monsters have set attack patterns, well mostly set. The key is figuring out what they are and #what the best strategy is given their pattern. #Weak attack if decision % 3 > 0 and not enraged: print "The %s charges at you " % monster, #Calculates how much damage the attack does. damage = 10 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "for %f damage." % damage if (ehp / 100.0) <= .2: #Enrages the monster if hp is less than 20% enraged = True print "The %s has become enraged." % monster #hero_damage_multiplier=1.0 Monsters don't defend for now counter += 1 decision += 1 #Strong attack elif decision % 3 == 0 and not enraged: print "The %s swings at you with their swords" % monster, damage = 30 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print " for %f damage." % damage counter += 1 decision += 1 if (ehp / 100.0) <= .2: #Enrages the monster if hp is less than 20% enraged = True print "The %s has become enraged." % monster #This is your enraged attack. elif enraged: print "The %s are enraged and attack you with suicidle ferver." % monster damage = 50 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "The %s deals a massive %f damage." % (monster, damage) counter += 1 #Displays/computs the following if the monster kills you. if hp <= 0: print "The %s has defeated you. You have failed in your" % monster, print "quest for the Master Sword. Better luck next time." print " Game Over " exit(0) #Displays/computs the following if you kill the monster elif ehp <= 0: print "Congratuations: You have felled the %s. As a reward you have received:" % monster #Next block of code determines your loot. output["inventory"] = loot(output["inventory"], 1) output["stats"][0] = hp return output if enemy == 2: monster = "Swarm of Fire Bats" output["enemy"] = monster prompt = "What's your next move against the %s?.\n" % monster print "You have a encountered %s. Time for battle!!\n" % monster #setting a counter. The allows the while loop to alernate between your move and the monters counter = 0 #These lines allow attacks to deal more or less damage. Defending/running away will changing these hero_damage_multiplier = 1.0 monster_damage_multiplier = 1.0 #Initializes the monsters moves enraged = False decision = 2 #While loop in which the battle actually takes place while hp > 0 and ehp > 0: #If counter is Even, the hero moves if counter % 2 == 0: move = get_str_response(prompt, combat_options) if move == "attack": attack_damage = base_damage * weapon_damage_multiplier * hero_damage_multiplier ehp = ehp - attack_damage counter += 1 print "You heroically attack the %s for %f damage." % ( monster, attack_damage) #Resets the monster damage multiplier to make sure that defend only lasts one round monster_damage_multiplier = 1.0 elif move == "defend": print "You ready yourself against the %s's next attack." % monster monster_damage_multiplier = .2 counter += 1 #Uses your potion elif move == "item": item = get_str_response(item_prompt, item_options) if item == "potion": if inventory[4] > 0: print "You use a potion and restore hp. You feel restored." hp = 90 counter += 1 inventory[4] -= 1 monster_damage_multiplier = 1.0 else: print "You have no potions left" #Uses you magic scroll elif item == "magic" or item == "scroll": if inventory[5] > 0: print "You read the words off the magic scroll and suddenly a mighty lightning bolt", print " strikes your enemy for 50 damage." print "As you admire the damage the scroll dissolves in your hand." ehp = ehp - 50 counter += 1 inventory[5] -= 1 monster_damage_multiplier = 1.0 else: print "You have no magic scrolls left." elif move == "run": print "You successfully escape from the %s" % monster output["run"] = True output["stats"][0] = hp return output #If counter is odd the Monster moves elif counter % 2 == 1: #Monsters have set attack patterns, well mostly set. The key is figuring out what they are and #what the best strategy is given their pattern. #Fly attack if decision % 4 == 2 and not enraged: print "The %s fly up into the air" % monster #Fly up into the air to Swoop down for the next attack. Take not damage on the next attack. hero_damage_multiplier = 0.0 counter += 1 decision += 1 #Fly part 2 elif decision % 4 == 3 and not enraged: damage = 40 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "The %s swoops down for %f damage." % (monster, damage) counter += 1 decision += 1 hero_damage_multiplier = 1.0 if (ehp / 100.0) <= .2: enraged = True print "The %s has become enraged." % monster #Screech attack elif decision % 4 < 2 and not enraged: print "The %s crys at you with a powerful screech" % monster, damage = 10 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print " for %f damage." % damage counter += 1 decision += 1 if (ehp / 100.0) <= .2: #Enrages the monster if hp is less than 20% enraged = True print "The %s has become enraged." % monster #This is your enraged attack. elif enraged: print "The %s are enraged and attack with a wave of immolating fire." % monster damage = 50 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "The %s deals a massive %f damage." % (monster, damage) counter += 1 #Displays/computs the following if the monster kills you. if hp <= 0: print "The %s has defeated you. You have failed in your" % monster, print "quest for the Master Sword. Better luck next time." print " Game Over " exit(0) #Displays/computs the following if you kill the monster elif ehp <= 0: print "Congratuations: You have felled the %s. As a reward you have received:" % monster #Next block of code determines your loot. output["inventory"] = loot(output["inventory"], 1) output["stats"][0] = hp return output if enemy == 3: monster = "Contingent of Skeletons" output["enemy"] = monster prompt = "What's your next move against the %s?.\n" % monster print "You have a encountered %s. Time for battle!!\n" % monster #setting a counter. The allows the while loop to alernate between your move and the monters counter = 0 #These lines allow attacks to deal more or less damage. Defending/running away will changing these hero_damage_multiplier = 1.0 monster_damage_multiplier = 1.0 #Initializes the monsters moves enraged = False decision = 0 #While loop in which the battle actually takes place while hp > 0 and ehp > 0: #If counter is Even, the hero moves if counter % 2 == 0: move = get_str_response(prompt, combat_options) if move == "attack": attack_damage = base_damage * weapon_damage_multiplier * hero_damage_multiplier ehp = ehp - attack_damage counter += 1 print "You heroically attack the %s for %f damage." % ( monster, attack_damage) #Resets the monster damage multiplier to make sure that defend only lasts one round monster_damage_multiplier = 1.0 elif move == "defend": print "You ready yourself against the %s's next attack." % monster monster_damage_multiplier = .2 counter += 1 #Uses your potion elif move == "item": item = get_str_response(item_prompt, item_options) if item == "potion": if inventory[4] > 0: print "You use a potion and restore hp. You feel restored." hp = 90 counter += 1 inventory[4] -= 1 monster_damage_multiplier = 1.0 else: print "You have no potions left" #Uses you magic scroll elif item == "magic" or item == "scroll": if inventory[5] > 0: print "You read the words off the magic scroll and suddenly a mighty lightning bolt", print " strikes your enemy for 50 damage." print "As you admire the damage the scroll dissolves in your hand." ehp = ehp - 50 counter += 1 inventory[5] -= 1 monster_damage_multiplier = 1.0 else: print "You have no magic scrolls left." elif move == "run": print "You successfully escape from the %s" % monster output["run"] = True output["stats"][0] = hp return output #If counter is odd the Monster moves elif counter % 2 == 1: #Monsters have set attack patterns, well mostly set. The key is figuring out what they are and #what the best strategy is given their pattern. #Weak attack if decision % 3 > 0 and not enraged: print "The horde of goblins charges at you ", #Calculates how much damage the attack does. damage = 10 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "for %f damage." % damage if (ehp / 100.0) <= .2: #Enrages the monster if hp is less than 20% enraged = True #hero_damage_multiplier=1.0 Monsters don't defend for now counter += 1 decision += 1 #Strong attack elif decision % 3 == 0 and not enraged: print "The %s swings at you with their swords" % monster, damage = 30 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print " for %f damage." % damage counter += 1 decision += 1 if (ehp / 100.0) <= .2: #Enrages the monster if hp is less than 20% enraged = True print "enraged" #This is your enraged attack. elif enraged: print "The %s are enraged and attack you with suicidle ferver." % monster damage = 50 * monster_damage_multiplier * armor_damage_reduction hp = hp - damage print "The %s deals a massive %f damage." % (monster, damage) counter += 1 #Displays/computs the following if the monster kills you. if hp <= 0: print "The %s has defeated you. You have failed in your" % monster, print "quest for the Master Sword. Better luck next time." print " Game Over " exit(0) #Displays/computs the following if you kill the monster elif ehp <= 0: print "Congratuations: You have felled the %s. As a reward you have received:" % monster #Next block of code determines your loot. output["inventory"] = loot(output["inventory"], 1) output["stats"][0] = hp return output