Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import re
  2. from random import randrange
  3.  
  4. # CONFIG
  5. LOOT = """
  6.        <item id="2148" chance="30000" countmax="30"/><!-- 30 30% gold coin -->
  7.        <item id="2148" chance="40000" countmax="25"/><!-- 25 40% gold coin -->
  8.        <item id="2148" chance="40000" countmax="40"/><!-- 40 40% gold coin -->
  9.        <item id="2489" chance="7000"/><!-- 5% dark armor -->
  10.        <item id="2647" chance="1500"/><!-- 1.5% plate legs -->
  11.        <item id="2497" chance="500"/><!-- 0.5% crusader helmet -->
  12.        <item id="1987" chance="90000"><!-- 90% bag -->
  13.            <inside>
  14.                <item id="2044" chance="8000"/><!-- 8% lamp -->
  15.                <item id="2377" chance="5000"/><!-- 5% two handed sword -->
  16.                <item id="2209" chance="900"/><!-- 0.9% club ring -->
  17.                <item id="2425" chance="1200"/><!-- 1,2% obsidian lance -->
  18.                <item id="2509" chance="3000"/><!-- 3% steel shield -->
  19.                <item id="2149" chance="1000" countmax="2"/> <!-- 2 1% small emerald -->
  20. """.splitlines()
  21.  
  22.  
  23. REGEXP_RULES = {'chance': 'chance=\"(\d*)\"',
  24.                 'id': 'id=\"(\d*)\"',
  25.                 'countmax': 'countmax=\"(\d*)\"'}
  26.  
  27. VALUES = {
  28.     # itemid: price_per_item
  29.     2148: 1,
  30. }
  31.  
  32.  
  33. def collect_values(LOOT):
  34.     global POSSIBLE_ITEMS
  35.     for item in LOOT:
  36.         if item is not None and item != "":
  37.             try:
  38.                 item_id = int(re.search(REGEXP_RULES['id'], item).group(1))
  39.             except:
  40.                 continue
  41.             try:
  42.                 loot_countmax = int(re.search(REGEXP_RULES['countmax'], item).group(1))
  43.             except:
  44.                 loot_countmax = 1
  45.             POSSIBLE_ITEMS.append({'id': item_id,
  46.                                    'chance': int(re.search(REGEXP_RULES['chance'], item).group(1)) / 1000,
  47.                                    'countmax': loot_countmax})
  48.  
  49.  
  50. LOOTED = {}
  51. POSSIBLE_ITEMS = []
  52.  
  53.  
  54. def pretend_loot():
  55.     global LOOTED
  56.     global POSSIBLE_ITEMS
  57.     for item in POSSIBLE_ITEMS:
  58.         rand = randrange(101)  # from <0-100> included
  59.         if rand <= item['chance']:
  60.             count = 1
  61.             if int(item['countmax']) > 1:
  62.                 count = randrange(int(item['countmax']) + 1)
  63.             try:
  64.                 LOOTED[item['id']] = LOOTED[item['id']] + count
  65.             except:
  66.                 LOOTED[item['id']] = count
  67.  
  68.  
  69. def calculate_value():
  70.     global VALUES
  71.     global LOOTED
  72.     value = 0
  73.     for item in LOOTED:
  74.         if item in VALUES:
  75.             value += LOOTED[item] * VALUES[item]
  76.         else:
  77.             print("Couldn't find {} in VALUES".format(item))
  78.     print("End value is: {}".format(value))
  79.  
  80. TRIES = 1000
  81. collect_values(LOOT)
  82. for i in range(0, TRIES):
  83.     pretend_loot()
  84. print(LOOTED)
  85. calculate_value()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement