Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # ------------------------------------------------------------------------------
- #
- # IOU Recruits Manager 0.1.3 (C) 2015 turidrum
- #
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301, USA.
- #
- # ------------------------------------------------------------------------------
- import sys
- import json
- program = {
- "name": "IOU Recruits Manager",
- "version": "0.1.3"
- }
- json_file = "config.json"
- title_file = "title.txt"
- header_file = "header.txt"
- footer_file = "footer.txt"
- def help():
- """
- Prints a little help
- """
- print "-" * 80
- print "%s %s" % (program["name"], program["version"])
- print "-" * 80
- print ""
- print "Description: Helper tool for managing Idle Online Universe's recruits threads"
- print ""
- print "Usage: %s [ -f FORUM ] [ -t | -v | -h ]" % __file__
- print "{:<20s} {:<60s}".format("-f --forum FORUM", "Output format as FORUM, you can use IOU or Kong as value, IOU default.")
- print "{:<20s} {:<60s}".format("-t --title", "Prints the thread's title.")
- print "{:<20s} {:<60s}".format("-v --version", "Prints program version.")
- print "{:<20s} {:<60s}".format("-h --help", "Show this help.")
- print ""
- print "Without arguments generates the full post."
- print "More info: https://iourpg.com/forum/showthread.php?tid=1350"
- class Building:
- buildName = "" # name of the building
- buildLevel = 0 # current build level
- upgradeStatusPercent = 0 # current percentage of building update
- percBarURL = "" # URL of the image that match the building upgrade status
- # ##########################################################################
- # NOTE:
- # update this from http://iourpg.wikia.com/wiki/Guild_Buildings
- # ##########################################################################
- buildLevels = { # stones needed for each build level
- 0: 0,
- 1: 5000,
- 2: 10000,
- 3: 20000,
- 4: 40000,
- 5: 80000,
- 6: 160000,
- 7: 290000,
- 8: 450000,
- 9: 650000,
- 10: 900000,
- 11: 1000000,
- 12: 2000000,
- 13: 3000000,
- 14: 4000000,
- 15: 5000000
- }
- bars = { # links to images corresponding to percentage
- 0: "https://upload.wikimedia.org/wikipedia/commons/c/cf/Gasr0percent.png",
- 5: "https://upload.wikimedia.org/wikipedia/commons/9/90/Gasr5percent.png",
- 10: "https://upload.wikimedia.org/wikipedia/commons/5/5b/Gasr10percent.png",
- 15: "https://upload.wikimedia.org/wikipedia/commons/c/c8/Gasr15percent.png",
- 20: "https://upload.wikimedia.org/wikipedia/commons/6/62/Gasr20percent.png",
- 25: "https://upload.wikimedia.org/wikipedia/commons/0/08/Gasr25percent.png",
- 30: "https://upload.wikimedia.org/wikipedia/commons/6/61/Gasr30percent.png",
- 35: "https://upload.wikimedia.org/wikipedia/commons/4/47/Gasr35percent.png",
- 40: "https://upload.wikimedia.org/wikipedia/commons/3/3e/Gasr40percent.png",
- 45: "https://upload.wikimedia.org/wikipedia/commons/0/0e/Gasr45percent.png",
- 50: "https://upload.wikimedia.org/wikipedia/commons/a/a8/Gasr50percent.png",
- 55: "https://upload.wikimedia.org/wikipedia/commons/2/27/Gasr55percent.png",
- 60: "https://upload.wikimedia.org/wikipedia/commons/e/e7/Gasr60percent.png",
- 65: "https://upload.wikimedia.org/wikipedia/commons/9/96/Gasr65percent.png",
- 70: "https://upload.wikimedia.org/wikipedia/commons/7/70/Gasr70percent.png",
- 75: "https://upload.wikimedia.org/wikipedia/commons/7/77/Gasr75percent.png",
- 80: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Gasr80percent.png",
- 85: "https://upload.wikimedia.org/wikipedia/commons/9/9a/Gasr85percent.png",
- 90: "https://upload.wikimedia.org/wikipedia/commons/b/bd/Gasr90percent.png",
- 95: "https://upload.wikimedia.org/wikipedia/commons/7/78/Gasr95percent.png",
- 100: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Gasr100percent.png"
- }
- def __init__(self, buildName, buildLevel, stonesNeeded):
- """
- constructor
- """
- self.buildName = buildName
- self.buildLevel = buildLevel
- if self.buildLevel > len(self.buildLevels)-1 or self.buildLevel < 0:
- while self.buildLevel > len(self.buildLevels)-1 or self.buildLevel < 0:
- print "Insert a build level between 0 and %d" % (len(self.buildLevels)-1,)
- self.buildLevel = int(raw_input("%s level: " % self.buildName))
- if stonesNeeded > self.buildLevels[self.buildLevel+1] or stonesNeeded < 0:
- while stonesNeeded > self.buildLevels[self.buildLevel+1] or stonesNeeded < 0:
- print "Insert the stones needed between 0 and %d" % (self.buildLevels[self.buildLevel+1],)
- stonesNeeded = int(raw_input("%s stones needed to next level: " % self.buildName))
- self.setUpgradePercent(stonesNeeded)
- self.setPercentURL()
- def round_to_5(self, n):
- """
- Rounds a number to nearest 5
- @param integer the number to round
- @return integer the rounded number
- """
- return int(round(n*2,-1)/2)
- def setUpgradePercent(self, stonesNeeded):
- """
- Sets the percentage upgraded status of a building
- @param integer the building's current level
- @param integer the stones needed to the next building level
- """
- nextBuildLevel = self.buildLevels[self.buildLevel+1]
- percent = (((nextBuildLevel-stonesNeeded)*100)/nextBuildLevel)
- self.upgradeStatusPercent = percent
- def setPercentURL(self):
- """
- Sets the image URL of building's percentage upgraded status
- """
- url = self.bars[self.round_to_5(self.upgradeStatusPercent)]
- self.percBarURL = url
- def getBuildingProperties(self):
- if self.buildName == "Guild Hall":
- return "Max Members: %d" % (self.buildLevel + 10)
- if self.buildName == "Fortress":
- return "Ascension Points: %d" % (self.buildLevel)
- if self.buildName == "Altar":
- return "Gold: %1.1f%%, Exp: %1.1f%%" % (self.buildLevel * 0.5, self.buildLevel * 0.5)
- if self.buildName == "Stable":
- return "Pet Dmg: %d%%, Pet Train: %d" % (self.buildLevel * 2, self.buildLevel / 5)
- if self.buildName == "Warehouse":
- return "Yelds: %d%%" % (self.buildLevel * 3)
- def printBuilding(self):
- """
- Prints a row for the guild's thread that contains building info
- """
- print "{:<40s} {:<40s} {:<110s}".format("[*][font=Courier New]%s" % self.buildName, ("Lv: %d (%s)") % (self.buildLevel, self.getBuildingProperties()),"Upgrade status: [[img=119x8]%s[/img]][/font]" % (self.percBarURL))
- class Guild:
- guildName = ""
- guildRank = 0
- members = []
- maxMembers = 0
- def __init__(self, guildName, guildRank):
- """
- constructor
- """
- self.guildName = guildName
- self.guildRank = guildRank
- def setMaxMembers(self, maxMembers):
- """
- Sets the max members number for the guild
- """
- self.maxMembers = maxMembers
- def addMember(self, name, status, level, IOUScore):
- """
- Adds a member to the guild
- """
- self.members.append([name, status, level, IOUScore])
- def getAverageLevel(self):
- """
- Calc the average level of guild's members
- @return float the average level of guild's members
- """
- sumlevels = 0.0
- for m in self.members:
- sumlevels += m[2]
- return float(sumlevels/len(self.members))
- def getGuildIOUScore(self):
- """
- Calc the guild's IOU score
- @return integer the guild's IOU score
- """
- sumIOU = 0
- for m in self.members:
- sumIOU += m[3]
- return sumIOU
- def getAverageIOUScore(self):
- """
- Calc the average IOU score of guild's members
- @return float the average IOU score of guild's members
- """
- return float((0.0+self.getGuildIOUScore())/len(self.members))
- def printGuildIOUScore(self):
- """
- Prints the guild's IOU Score
- """
- print "[b]%s[/b] IOU: [b]%d[/b]\n" % (self.guildName, self.getGuildIOUScore())
- def printAverageLevel(self):
- """
- Prints the average level of guild's members
- """
- print "[b]%s[/b] average level: [b]%.2f[/b]\n" % (self.guildName, float(self.getAverageLevel()))
- def printAverageIOUScore(self):
- """
- Prints the average IOU Score of guild's members
- """
- print "[b]%s[/b] average IOU: [b]%.2f[/b]\n" % (self.guildName, float(self.getAverageIOUScore()))
- def printMembers(self):
- """
- Prints the members list
- """
- membersCountString = "[color=#33ff33]%d[/color]/%d" % (len(self.members), self.maxMembers) if len(self.members) < self.maxMembers else "[color=#ff3333]%d[/color]/%d" % (len(self.members), self.maxMembers)
- print "[b]%s (%s)[/b]:" % (self.guildName, membersCountString)
- print "[list]"
- for m in self.members:
- print "{:<40s} {:<8s} {:<5s} {:<8s}".format("[*][font=Courier New]%s" % m[0], "[%s" % m[1], "| Lv %d" % m[2], "| IOU %d][/font]" % m[3])
- print "[/list]\n\n"
- class TemplateParser:
- def __init__(self, guild, buildings):
- """
- Constructor
- @param object an instance of guild class
- @param list a list that contains instances of building class
- """
- levels = []
- for b in buildings:
- levels.append(str(b.buildLevel))
- if (guild.maxMembers-len(guild.members)) > 0:
- dynamicSlots = str(guild.maxMembers-len(guild.members)) + " players"
- if (guild.maxMembers-len(guild.members)) == 1:
- dynamicSlots = str(guild.maxMembers-len(guild.members)) + " player"
- else:
- dynamicSlots = "Full"
- guildPosition = ""
- if guild.guildRank == 1:
- guildPosition = "1st"
- elif guild.guildRank == 2:
- guildPosition = "2nd"
- elif guild.guildRank == 3:
- guildPosition = "3rd"
- else:
- guildPosition = "%dth" % guild.guildRank
- self.words = {
- "{GUILDNAME}": guild.guildName,
- "{GUILDRANK}": str(guild.guildRank),
- "{GUILDPOSITION}": guildPosition,
- "{BUILDINGS}": "/".join(levels),
- "{MEMBERS}": str(len(guild.members)),
- "{MAXMEMBERS}": str(guild.maxMembers),
- "{EMPTYSLOTS}": str(guild.maxMembers-len(guild.members)),
- "{DYNAMICSLOTS}": dynamicSlots
- }
- def parseText(self, text):
- """
- Translate special words to datas
- @param string the template text
- @return string the translated text
- """
- for word in self.words:
- text = text.replace(word, self.words[word])
- return text
- def generateThread(guild, buildings):
- """
- Output guild's recruit thread
- @param object an instance of guild class
- @param list a list that contains instances of building class
- """
- # prints header text
- try:
- f = file(header_file, "r")
- except:
- print "Cannot read header file '%s'" % header_file
- else:
- parser = TemplateParser(guild, buildings)
- for l in f.readlines():
- print parser.parseText(l.rstrip())
- #prints guild's data
- guild.printGuildIOUScore()
- # prints members data
- guild.printAverageLevel()
- guild.printAverageIOUScore()
- guild.printMembers()
- # prints buildigs data
- print "[b]Buildings[/b]:"
- print "[list]"
- for b in buildings:
- b.printBuilding()
- print "[/list]\n\n"
- # prints footer text
- try:
- f = file(footer_file, "r")
- except:
- print "Cannot read header file '%s'" % footer_file
- else:
- for l in f.readlines():
- print l.rstrip()
- def generateTitle(guild, buildings):
- """
- Output guild's recruit thread title
- @param object instance of Guild class
- @param list contains instances of Building class
- """
- # read title template
- titleTemplate = ""
- try:
- f = file(title_file, "r")
- except:
- print "Cannot read title file '%s'" % header_file
- else:
- parser = TemplateParser(guild, buildings)
- print parser.parseText(f.readline().rstrip())
- # read guild data from configuration file
- json_data=open(json_file)
- config = json.load(json_data)
- json_data.close()
- # sets members data
- guild = Guild(config["guild_name"], config["guild_rank"])
- for m in config["members"]:
- guild.addMember(m[0], m[1], m[2], m[3])
- # sets buildings data
- buildings = []
- for b in config["buildings"]:
- buildings.append(Building(b[0], b[1], b[2]))
- if b[0] == "Guild Hall":
- guild.setMaxMembers(b[1] + 10)
- if len(sys.argv) > 1:
- if sys.argv[1] == "--title" or sys.argv[1] == "-t":
- generateTitle(guild, buildings)
- elif sys.argv[1] == "--version" or sys.argv[1] == "-v":
- print "%s %s" % (program["name"], program["version"])
- elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
- help()
- else:
- generateThread(guild, buildings)
Add Comment
Please, Sign In to add comment