Guest User

IOU Recruits Manager 0.1.3

a guest
Jan 12th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.80 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # ------------------------------------------------------------------------------
  5. #  
  6. #  IOU Recruits Manager 0.1.3 (C) 2015 turidrum
  7. #  
  8. #  
  9. #  This program is free software; you can redistribute it and/or modify
  10. #  it under the terms of the GNU General Public License as published by
  11. #  the Free Software Foundation; either version 2 of the License, or
  12. #  (at your option) any later version.
  13. #  
  14. #  This program is distributed in the hope that it will be useful,
  15. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #  GNU General Public License for more details.
  18. #  
  19. #  You should have received a copy of the GNU General Public License
  20. #  along with this program; if not, write to the Free Software
  21. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. #  MA 02110-1301, USA.
  23. #
  24. # ------------------------------------------------------------------------------
  25.  
  26.  
  27.  
  28.  
  29.  
  30. import sys
  31. import json
  32.  
  33.  
  34. program = {
  35.     "name": "IOU Recruits Manager",
  36.     "version": "0.1.3"
  37. }
  38.  
  39. json_file = "config.json"
  40. title_file = "title.txt"
  41. header_file = "header.txt"
  42. footer_file = "footer.txt"
  43.  
  44.  
  45. def help():
  46.     """
  47.     Prints a little help
  48.     """
  49.     print "-" * 80
  50.     print "%s %s" % (program["name"], program["version"])
  51.     print "-" * 80
  52.     print ""
  53.     print "Description: Helper tool for managing Idle Online Universe's recruits threads"
  54.     print ""
  55.     print "Usage: %s [ -f FORUM ] [ -t | -v | -h ]" % __file__
  56.     print "{:<20s} {:<60s}".format("-f --forum FORUM", "Output format as FORUM, you can use IOU or Kong as value, IOU default.")
  57.     print "{:<20s} {:<60s}".format("-t --title", "Prints the thread's title.")
  58.     print "{:<20s} {:<60s}".format("-v --version", "Prints program version.")
  59.     print "{:<20s} {:<60s}".format("-h --help", "Show this help.")
  60.     print ""
  61.     print "Without arguments generates the full post."
  62.     print "More info: https://iourpg.com/forum/showthread.php?tid=1350"
  63.  
  64.  
  65.    
  66.  
  67.  
  68. class Building:
  69.    
  70.     buildName = "" # name of the building
  71.     buildLevel = 0 # current build level
  72.     upgradeStatusPercent = 0 # current percentage of building update
  73.     percBarURL = "" # URL of the image that match the building upgrade status
  74.    
  75.     # ##########################################################################
  76.     # NOTE:
  77.     # update this from http://iourpg.wikia.com/wiki/Guild_Buildings
  78.     # ##########################################################################
  79.     buildLevels = { # stones needed for each build level
  80.         0: 0,
  81.         1: 5000,
  82.         2: 10000,
  83.         3: 20000,
  84.         4: 40000,
  85.         5: 80000,
  86.         6: 160000,
  87.         7: 290000,
  88.         8: 450000,
  89.         9: 650000,
  90.         10: 900000,
  91.         11: 1000000,
  92.         12: 2000000,
  93.         13: 3000000,
  94.         14: 4000000,
  95.         15: 5000000
  96.     }
  97.    
  98.     bars = { # links to images corresponding to percentage
  99.         0: "https://upload.wikimedia.org/wikipedia/commons/c/cf/Gasr0percent.png",
  100.         5: "https://upload.wikimedia.org/wikipedia/commons/9/90/Gasr5percent.png",
  101.         10: "https://upload.wikimedia.org/wikipedia/commons/5/5b/Gasr10percent.png",
  102.         15: "https://upload.wikimedia.org/wikipedia/commons/c/c8/Gasr15percent.png",
  103.         20: "https://upload.wikimedia.org/wikipedia/commons/6/62/Gasr20percent.png",
  104.         25: "https://upload.wikimedia.org/wikipedia/commons/0/08/Gasr25percent.png",
  105.         30: "https://upload.wikimedia.org/wikipedia/commons/6/61/Gasr30percent.png",
  106.         35: "https://upload.wikimedia.org/wikipedia/commons/4/47/Gasr35percent.png",
  107.         40: "https://upload.wikimedia.org/wikipedia/commons/3/3e/Gasr40percent.png",
  108.         45: "https://upload.wikimedia.org/wikipedia/commons/0/0e/Gasr45percent.png",
  109.         50: "https://upload.wikimedia.org/wikipedia/commons/a/a8/Gasr50percent.png",
  110.         55: "https://upload.wikimedia.org/wikipedia/commons/2/27/Gasr55percent.png",
  111.         60: "https://upload.wikimedia.org/wikipedia/commons/e/e7/Gasr60percent.png",
  112.         65: "https://upload.wikimedia.org/wikipedia/commons/9/96/Gasr65percent.png",
  113.         70: "https://upload.wikimedia.org/wikipedia/commons/7/70/Gasr70percent.png",
  114.         75: "https://upload.wikimedia.org/wikipedia/commons/7/77/Gasr75percent.png",
  115.         80: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Gasr80percent.png",
  116.         85: "https://upload.wikimedia.org/wikipedia/commons/9/9a/Gasr85percent.png",
  117.         90: "https://upload.wikimedia.org/wikipedia/commons/b/bd/Gasr90percent.png",
  118.         95: "https://upload.wikimedia.org/wikipedia/commons/7/78/Gasr95percent.png",
  119.         100: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Gasr100percent.png"
  120.     }
  121.    
  122.    
  123.     def __init__(self, buildName, buildLevel, stonesNeeded):
  124.         """
  125.         constructor
  126.         """
  127.        
  128.         self.buildName = buildName
  129.         self.buildLevel = buildLevel
  130.        
  131.         if self.buildLevel > len(self.buildLevels)-1 or self.buildLevel < 0:
  132.             while self.buildLevel > len(self.buildLevels)-1 or self.buildLevel < 0:
  133.                 print "Insert a build level between 0 and %d" % (len(self.buildLevels)-1,)
  134.                 self.buildLevel = int(raw_input("%s level: " % self.buildName))
  135.        
  136.         if stonesNeeded > self.buildLevels[self.buildLevel+1] or stonesNeeded < 0:
  137.             while stonesNeeded > self.buildLevels[self.buildLevel+1] or stonesNeeded < 0:
  138.                 print "Insert the stones needed between 0 and %d" % (self.buildLevels[self.buildLevel+1],)
  139.                 stonesNeeded = int(raw_input("%s stones needed to next level: " % self.buildName))
  140.        
  141.         self.setUpgradePercent(stonesNeeded)
  142.         self.setPercentURL()
  143.    
  144.    
  145.     def round_to_5(self, n):
  146.         """
  147.         Rounds a number to nearest 5
  148.        
  149.         @param integer              the number to round
  150.         @return integer             the rounded number
  151.         """
  152.        
  153.         return int(round(n*2,-1)/2)
  154.    
  155.    
  156.     def setUpgradePercent(self, stonesNeeded):
  157.         """
  158.         Sets the percentage upgraded status of a building
  159.        
  160.         @param integer              the building's current level
  161.         @param integer              the stones needed to the next building level
  162.         """
  163.        
  164.         nextBuildLevel = self.buildLevels[self.buildLevel+1]
  165.         percent = (((nextBuildLevel-stonesNeeded)*100)/nextBuildLevel)
  166.         self.upgradeStatusPercent = percent
  167.    
  168.    
  169.     def setPercentURL(self):
  170.         """
  171.         Sets the image URL of building's percentage upgraded status
  172.         """
  173.        
  174.         url = self.bars[self.round_to_5(self.upgradeStatusPercent)]
  175.         self.percBarURL = url
  176.    
  177.    
  178.     def getBuildingProperties(self):
  179.         if self.buildName == "Guild Hall":
  180.             return "Max Members: %d" % (self.buildLevel + 10)
  181.         if self.buildName == "Fortress":
  182.             return "Ascension Points: %d" % (self.buildLevel)
  183.         if self.buildName == "Altar":
  184.             return "Gold: %1.1f%%, Exp: %1.1f%%" % (self.buildLevel * 0.5, self.buildLevel * 0.5)
  185.         if self.buildName == "Stable":
  186.             return "Pet Dmg: %d%%, Pet Train: %d" % (self.buildLevel * 2, self.buildLevel / 5)
  187.         if self.buildName == "Warehouse":
  188.             return "Yelds: %d%%" % (self.buildLevel * 3)
  189.        
  190.    
  191.    
  192.     def printBuilding(self):
  193.         """
  194.         Prints a row for the guild's thread that contains building info
  195.         """
  196.        
  197.         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))
  198.  
  199.  
  200.  
  201.  
  202. class Guild:
  203.    
  204.     guildName = ""
  205.     guildRank = 0
  206.     members = []
  207.     maxMembers = 0
  208.    
  209.     def __init__(self, guildName, guildRank):
  210.         """
  211.         constructor
  212.         """
  213.        
  214.         self.guildName = guildName
  215.         self.guildRank = guildRank
  216.    
  217.     def setMaxMembers(self, maxMembers):
  218.         """
  219.         Sets the max members number for the guild
  220.         """
  221.        
  222.         self.maxMembers = maxMembers
  223.    
  224.     def addMember(self, name, status, level, IOUScore):
  225.         """
  226.         Adds a member to the guild
  227.         """
  228.        
  229.         self.members.append([name, status, level, IOUScore])
  230.    
  231.     def getAverageLevel(self):
  232.         """
  233.         Calc the average level of guild's members
  234.        
  235.         @return float               the average level of guild's members
  236.         """
  237.        
  238.         sumlevels = 0.0
  239.         for m in self.members:
  240.             sumlevels += m[2]
  241.         return float(sumlevels/len(self.members))
  242.    
  243.     def getGuildIOUScore(self):
  244.         """
  245.         Calc the guild's IOU score
  246.        
  247.         @return integer             the guild's IOU score
  248.         """
  249.        
  250.         sumIOU = 0
  251.         for m in self.members:
  252.             sumIOU += m[3]
  253.         return sumIOU
  254.    
  255.     def getAverageIOUScore(self):
  256.         """
  257.         Calc the average IOU score of guild's members
  258.        
  259.         @return float               the average IOU score of guild's members
  260.         """
  261.        
  262.         return float((0.0+self.getGuildIOUScore())/len(self.members))
  263.    
  264.     def printGuildIOUScore(self):
  265.         """
  266.         Prints the guild's IOU Score
  267.         """
  268.        
  269.         print "[b]%s[/b] IOU: [b]%d[/b]\n" % (self.guildName, self.getGuildIOUScore())
  270.    
  271.     def printAverageLevel(self):
  272.         """
  273.         Prints the average level of guild's members
  274.         """
  275.        
  276.         print "[b]%s[/b] average level: [b]%.2f[/b]\n" % (self.guildName, float(self.getAverageLevel()))
  277.    
  278.     def printAverageIOUScore(self):
  279.         """
  280.         Prints the average IOU Score of guild's members
  281.         """
  282.        
  283.         print "[b]%s[/b] average IOU: [b]%.2f[/b]\n" % (self.guildName, float(self.getAverageIOUScore()))
  284.    
  285.     def printMembers(self):
  286.         """
  287.         Prints the members list
  288.         """
  289.        
  290.         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)
  291.         print "[b]%s (%s)[/b]:" % (self.guildName, membersCountString)
  292.         print "[list]"
  293.         for m in self.members:
  294.             print "{:<40s} {:<8s} {:<5s} {:<8s}".format("[*][font=Courier New]%s" % m[0], "[%s" % m[1], "| Lv %d" % m[2], "| IOU %d][/font]" % m[3])
  295.         print "[/list]\n\n"
  296.        
  297.        
  298.  
  299. class TemplateParser:
  300.    
  301.     def __init__(self, guild, buildings):
  302.         """
  303.         Constructor
  304.        
  305.         @param object               an instance of guild class
  306.         @param list                 a list that contains instances of building class
  307.         """
  308.        
  309.         levels = []
  310.         for b in buildings:
  311.             levels.append(str(b.buildLevel))
  312.        
  313.         if (guild.maxMembers-len(guild.members)) > 0:
  314.             dynamicSlots = str(guild.maxMembers-len(guild.members)) + " players"
  315.             if (guild.maxMembers-len(guild.members)) == 1:
  316.                 dynamicSlots = str(guild.maxMembers-len(guild.members)) + " player"
  317.         else:
  318.             dynamicSlots = "Full"
  319.        
  320.         guildPosition = ""
  321.         if guild.guildRank == 1:
  322.             guildPosition = "1st"
  323.         elif guild.guildRank == 2:
  324.             guildPosition = "2nd"
  325.         elif guild.guildRank == 3:
  326.             guildPosition = "3rd"
  327.         else:
  328.             guildPosition = "%dth" % guild.guildRank
  329.        
  330.         self.words = {
  331.             "{GUILDNAME}": guild.guildName,
  332.             "{GUILDRANK}": str(guild.guildRank),
  333.             "{GUILDPOSITION}": guildPosition,
  334.             "{BUILDINGS}": "/".join(levels),
  335.             "{MEMBERS}": str(len(guild.members)),
  336.             "{MAXMEMBERS}": str(guild.maxMembers),
  337.             "{EMPTYSLOTS}": str(guild.maxMembers-len(guild.members)),
  338.             "{DYNAMICSLOTS}": dynamicSlots
  339.         }
  340.    
  341.     def parseText(self, text):
  342.         """
  343.         Translate special words to datas
  344.        
  345.         @param string               the template text
  346.         @return string              the translated text
  347.         """
  348.        
  349.         for word in self.words:
  350.             text = text.replace(word, self.words[word])
  351.        
  352.         return text
  353.  
  354.  
  355. def generateThread(guild, buildings):
  356.     """
  357.     Output guild's recruit thread
  358.    
  359.     @param object               an instance of guild class
  360.     @param list                 a list that contains instances of building class
  361.     """
  362.     # prints header text
  363.     try:
  364.         f = file(header_file, "r")
  365.     except:
  366.         print "Cannot read header file '%s'" % header_file
  367.     else:
  368.         parser = TemplateParser(guild, buildings)
  369.         for l in f.readlines():
  370.             print parser.parseText(l.rstrip())
  371.    
  372.     #prints guild's data
  373.     guild.printGuildIOUScore()
  374.  
  375.     # prints members data
  376.     guild.printAverageLevel()
  377.     guild.printAverageIOUScore()
  378.     guild.printMembers()
  379.    
  380.     # prints buildigs data
  381.     print "[b]Buildings[/b]:"
  382.     print "[list]"
  383.     for b in buildings:
  384.         b.printBuilding()
  385.     print "[/list]\n\n"
  386.    
  387.     # prints footer text
  388.     try:
  389.         f = file(footer_file, "r")
  390.     except:
  391.         print "Cannot read header file '%s'" % footer_file
  392.     else:
  393.         for l in f.readlines():
  394.             print l.rstrip()
  395.  
  396.  
  397. def generateTitle(guild, buildings):
  398.     """
  399.     Output guild's recruit thread title
  400.    
  401.     @param object               instance of Guild class
  402.     @param list                 contains instances of Building class
  403.     """
  404.     # read title template
  405.     titleTemplate = ""
  406.     try:
  407.         f = file(title_file, "r")
  408.     except:
  409.         print "Cannot read title file '%s'" % header_file
  410.     else:
  411.         parser = TemplateParser(guild, buildings)
  412.         print parser.parseText(f.readline().rstrip())
  413.  
  414.  
  415.  
  416.  
  417. # read guild data from configuration file
  418. json_data=open(json_file)
  419. config = json.load(json_data)
  420. json_data.close()
  421.  
  422.  
  423. # sets members data
  424. guild = Guild(config["guild_name"], config["guild_rank"])
  425. for m in config["members"]:
  426.     guild.addMember(m[0], m[1], m[2], m[3])
  427.  
  428.  
  429. # sets buildings data
  430. buildings = []
  431. for b in config["buildings"]:
  432.     buildings.append(Building(b[0], b[1], b[2]))
  433.    
  434.     if b[0] == "Guild Hall":
  435.         guild.setMaxMembers(b[1] + 10)
  436.  
  437. if len(sys.argv) > 1:
  438.     if sys.argv[1] == "--title" or sys.argv[1] == "-t":
  439.         generateTitle(guild, buildings)
  440.     elif sys.argv[1] == "--version" or sys.argv[1] == "-v":
  441.         print "%s %s" % (program["name"], program["version"])
  442.     elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
  443.         help()
  444. else:
  445.     generateThread(guild, buildings)
Add Comment
Please, Sign In to add comment